kernel_linux.c
上传用户:wxp200602
上传日期:2007-10-30
资源大小:4028k
文件大小:7k
源码类别:

SNMP编程

开发平台:

Unix_Linux

  1. /*
  2.  *  Linux kernel interface
  3.  *
  4.  */
  5. #include <net-snmp/net-snmp-config.h>
  6. #include <net-snmp/net-snmp-includes.h>
  7. #include <net-snmp/agent/net-snmp-agent-includes.h>
  8. #include "util_funcs.h"
  9. #if HAVE_STRING_H
  10. #include <string.h>
  11. #endif
  12. #include <sys/types.h>
  13. #if HAVE_SYS_PARAM_H
  14. #include <sys/param.h>
  15. #endif
  16. #include "kernel_linux.h"
  17. struct ip_mib   cached_ip_mib;
  18. struct icmp_mib cached_icmp_mib;
  19. struct tcp_mib  cached_tcp_mib;
  20. struct udp_mib  cached_udp_mib;
  21. #define IP_STATS_LINE "Ip: %lu %lu %lu %lu %lu %lu %lu %lu %lu %lu %lu %lu %lu %lu %lu %lu %lu %lu %lu"
  22. #define ICMP_STATS_LINE "Icmp: %lu %lu %lu %lu %lu %lu %lu %lu %lu %lu %lu %lu %lu %lu %lu %lu %lu %lu %lu %lu %lu %lu %lu %lu %lu %lu"
  23. #define TCP_STATS_LINE "Tcp: %lu %lu %lu %lu %lu %lu %lu %lu %lu %lu %lu %lu %lu %lu"
  24. #define UDP_STATS_LINE "Udp: %lu %lu %lu %lu"
  25. #define IP_STATS_PREFIX_LEN 4
  26. #define ICMP_STATS_PREFIX_LEN 6
  27. #define TCP_STATS_PREFIX_LEN 5
  28. #define UDP_STATS_PREFIX_LEN 5
  29. int
  30. linux_read_mibII_stats(void)
  31. {
  32.     FILE           *in = fopen("/proc/net/snmp", "r");
  33.     char            line[1024];
  34.     if (!in) {
  35.         return -1;
  36.     }
  37.     while (line == fgets(line, sizeof(line), in)) {
  38.         if (!strncmp(line, IP_STATS_LINE, IP_STATS_PREFIX_LEN)) {
  39.             sscanf(line, IP_STATS_LINE,
  40.                    &cached_ip_mib.ipForwarding,
  41.                    &cached_ip_mib.ipDefaultTTL,
  42.                    &cached_ip_mib.ipInReceives,
  43.                    &cached_ip_mib.ipInHdrErrors,
  44.                    &cached_ip_mib.ipInAddrErrors,
  45.                    &cached_ip_mib.ipForwDatagrams,
  46.                    &cached_ip_mib.ipInUnknownProtos,
  47.                    &cached_ip_mib.ipInDiscards,
  48.                    &cached_ip_mib.ipInDelivers,
  49.                    &cached_ip_mib.ipOutRequests,
  50.                    &cached_ip_mib.ipOutDiscards,
  51.                    &cached_ip_mib.ipOutNoRoutes,
  52.                    &cached_ip_mib.ipReasmTimeout,
  53.                    &cached_ip_mib.ipReasmReqds,
  54.                    &cached_ip_mib.ipReasmOKs,
  55.                    &cached_ip_mib.ipReasmFails,
  56.                    &cached_ip_mib.ipFragOKs,
  57.                    &cached_ip_mib.ipFragFails,
  58.                    &cached_ip_mib.ipFragCreates);
  59.             cached_ip_mib.ipRoutingDiscards = 0;        /* XXX */
  60.         } else if (!strncmp(line, ICMP_STATS_LINE, ICMP_STATS_PREFIX_LEN)) {
  61.             sscanf(line, ICMP_STATS_LINE,
  62.                    &cached_icmp_mib.icmpInMsgs,
  63.                    &cached_icmp_mib.icmpInErrors,
  64.                    &cached_icmp_mib.icmpInDestUnreachs,
  65.                    &cached_icmp_mib.icmpInTimeExcds,
  66.                    &cached_icmp_mib.icmpInParmProbs,
  67.                    &cached_icmp_mib.icmpInSrcQuenchs,
  68.                    &cached_icmp_mib.icmpInRedirects,
  69.                    &cached_icmp_mib.icmpInEchos,
  70.                    &cached_icmp_mib.icmpInEchoReps,
  71.                    &cached_icmp_mib.icmpInTimestamps,
  72.                    &cached_icmp_mib.icmpInTimestampReps,
  73.                    &cached_icmp_mib.icmpInAddrMasks,
  74.                    &cached_icmp_mib.icmpInAddrMaskReps,
  75.                    &cached_icmp_mib.icmpOutMsgs,
  76.                    &cached_icmp_mib.icmpOutErrors,
  77.                    &cached_icmp_mib.icmpOutDestUnreachs,
  78.                    &cached_icmp_mib.icmpOutTimeExcds,
  79.                    &cached_icmp_mib.icmpOutParmProbs,
  80.                    &cached_icmp_mib.icmpOutSrcQuenchs,
  81.                    &cached_icmp_mib.icmpOutRedirects,
  82.                    &cached_icmp_mib.icmpOutEchos,
  83.                    &cached_icmp_mib.icmpOutEchoReps,
  84.                    &cached_icmp_mib.icmpOutTimestamps,
  85.                    &cached_icmp_mib.icmpOutTimestampReps,
  86.                    &cached_icmp_mib.icmpOutAddrMasks,
  87.                    &cached_icmp_mib.icmpOutAddrMaskReps);
  88.         } else if (!strncmp(line, TCP_STATS_LINE, TCP_STATS_PREFIX_LEN)) {
  89.             int             ret = sscanf(line, TCP_STATS_LINE,
  90.                                          &cached_tcp_mib.tcpRtoAlgorithm,
  91.                                          &cached_tcp_mib.tcpRtoMin,
  92.                                          &cached_tcp_mib.tcpRtoMax,
  93.                                          &cached_tcp_mib.tcpMaxConn,
  94.                                          &cached_tcp_mib.tcpActiveOpens,
  95.                                          &cached_tcp_mib.tcpPassiveOpens,
  96.                                          &cached_tcp_mib.tcpAttemptFails,
  97.                                          &cached_tcp_mib.tcpEstabResets,
  98.                                          &cached_tcp_mib.tcpCurrEstab,
  99.                                          &cached_tcp_mib.tcpInSegs,
  100.                                          &cached_tcp_mib.tcpOutSegs,
  101.                                          &cached_tcp_mib.tcpRetransSegs,
  102.                                          &cached_tcp_mib.tcpInErrs,
  103.                                          &cached_tcp_mib.tcpOutRsts);
  104.             cached_tcp_mib.tcpInErrsValid = (ret > 12) ? 1 : 0;
  105.             cached_tcp_mib.tcpOutRstsValid = (ret > 13) ? 1 : 0;
  106.         } else if (!strncmp(line, UDP_STATS_LINE, UDP_STATS_PREFIX_LEN)) {
  107.             sscanf(line, UDP_STATS_LINE,
  108.                    &cached_udp_mib.udpInDatagrams,
  109.                    &cached_udp_mib.udpNoPorts,
  110.                    &cached_udp_mib.udpInErrors,
  111.                    &cached_udp_mib.udpOutDatagrams);
  112.         }
  113.     }
  114.     fclose(in);
  115.     /*
  116.      * Tweak illegal values:
  117.      *
  118.      * valid values for ipForwarding are 1 == yup, 2 == nope
  119.      * a 0 is forbidden, so patch:
  120.      */
  121.     if (!cached_ip_mib.ipForwarding)
  122.         cached_ip_mib.ipForwarding = 2;
  123.     /*
  124.      * 0 is illegal for tcpRtoAlgorithm
  125.      * so assume `other' algorithm:
  126.      */
  127.     if (!cached_tcp_mib.tcpRtoAlgorithm)
  128.         cached_tcp_mib.tcpRtoAlgorithm = 1;
  129.     return 0;
  130. }
  131. int
  132. linux_read_ip_stat(struct ip_mib *ipstat)
  133. {
  134.     memset((char *) ipstat, (0), sizeof(*ipstat));
  135.     if (linux_read_mibII_stats() == -1)
  136.         return -1;
  137.     memcpy((char *) ipstat, (char *) &cached_ip_mib, sizeof(*ipstat));
  138.     return 0;
  139. }
  140. int
  141. linux_read_icmp_stat(struct icmp_mib *icmpstat)
  142. {
  143.     memset((char *) icmpstat, (0), sizeof(*icmpstat));
  144.     if (linux_read_mibII_stats() == -1)
  145.         return -1;
  146.     memcpy((char *) icmpstat, (char *) &cached_icmp_mib,
  147.            sizeof(*icmpstat));
  148.     return 0;
  149. }
  150. int
  151. linux_read_tcp_stat(struct tcp_mib *tcpstat)
  152. {
  153.     memset((char *) tcpstat, (0), sizeof(*tcpstat));
  154.     if (linux_read_mibII_stats() == -1)
  155.         return -1;
  156.     memcpy((char *) tcpstat, (char *) &cached_tcp_mib, sizeof(*tcpstat));
  157.     return 0;
  158. }
  159. int
  160. linux_read_udp_stat(struct udp_mib *udpstat)
  161. {
  162.     memset((char *) udpstat, (0), sizeof(*udpstat));
  163.     if (linux_read_mibII_stats() == -1)
  164.         return -1;
  165.     memcpy((char *) udpstat, (char *) &cached_udp_mib, sizeof(*udpstat));
  166.     return 0;
  167. }