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

SNMP编程

开发平台:

Unix_Linux

  1. /*
  2.  *  Systemstats MIB architecture support
  3.  *
  4.  * $Id: systemstats_common.c,v 1.2.2.1 2005/02/08 21:58:11 nba Exp $
  5.  */
  6. #include <net-snmp/net-snmp-config.h>
  7. #include <net-snmp/net-snmp-includes.h>
  8. #include "ip-mib/ipSystemStatsTable/ipSystemStatsTable_constants.h"
  9. #include <net-snmp/agent/net-snmp-agent-includes.h>
  10. #include <net-snmp/data_access/ipstats.h>
  11. #include <net-snmp/data_access/systemstats.h>
  12. /**---------------------------------------------------------------------*/
  13. /*
  14.  * local static vars
  15.  */
  16. static int need_wrap_check = -1;
  17. /*
  18.  * local static prototypes
  19.  */
  20. static void _entry_release(netsnmp_systemstats_entry * entry, void *unused);
  21. /**---------------------------------------------------------------------*/
  22. /*
  23.  * external per-architecture functions prototypes
  24.  *
  25.  * These shouldn't be called by the general public, so they aren't in
  26.  * the header file.
  27.  */
  28. extern int
  29. netsnmp_access_systemstats_container_arch_load(netsnmp_container* container,
  30.                                              u_int load_flags);
  31. extern void
  32. netsnmp_access_systemstats_arch_init(void);
  33. /**---------------------------------------------------------------------*/
  34. /*
  35.  * initialization
  36.  */
  37. void
  38. netsnmp_access_systemstats_init(void)
  39. {
  40.     netsnmp_container * ifcontainer;
  41.     netsnmp_access_systemstats_arch_init();
  42.     /*
  43.      * load once to set up ifIndexes
  44.      */
  45.     ifcontainer = netsnmp_access_systemstats_container_load(NULL, 0);
  46.     if(NULL != ifcontainer)
  47.         netsnmp_access_systemstats_container_free(ifcontainer, 0);
  48. }
  49. /**---------------------------------------------------------------------*/
  50. /*
  51.  * container functions
  52.  */
  53. /**
  54.  * initialize systemstats container
  55.  */
  56. netsnmp_container *
  57. netsnmp_access_systemstats_container_init(u_int flags)
  58. {
  59.     netsnmp_container *container;
  60.     DEBUGMSGTL(("access:systemstats:container", "initn"));
  61.     /*
  62.      * create the containers. one indexed by ifIndex, the other
  63.      * indexed by ifName.
  64.      */
  65.     container = netsnmp_container_find("access_systemstats:table_container");
  66.     if (NULL == container)
  67.         return NULL;
  68.     return container;
  69. }
  70. /**
  71.  * load systemstats information in specified container
  72.  *
  73.  * @param container empty container, or NULL to have one created for you
  74.  * @param load_flags flags to modify behaviour.
  75.  *
  76.  * @retval NULL  error
  77.  * @retval !NULL pointer to container
  78.  */
  79. netsnmp_container*
  80. netsnmp_access_systemstats_container_load(netsnmp_container* container, u_int load_flags)
  81. {
  82.     int rc;
  83.     DEBUGMSGTL(("access:systemstats:container", "loadn"));
  84.     if (NULL == container)
  85.         container = netsnmp_access_systemstats_container_init(load_flags);
  86.     if (NULL == container) {
  87.         snmp_log(LOG_ERR, "no container specified/found for access_systemstatsn");
  88.         return NULL;
  89.     }
  90.     rc =  netsnmp_access_systemstats_container_arch_load(container, load_flags);
  91.     if (0 != rc) {
  92.         netsnmp_access_systemstats_container_free(container,
  93.                                                 NETSNMP_ACCESS_SYSTEMSTATS_FREE_NOFLAGS);
  94.         container = NULL;
  95.     }
  96.     return container;
  97. }
  98. void
  99. netsnmp_access_systemstats_container_free(netsnmp_container *container, u_int free_flags)
  100. {
  101.     DEBUGMSGTL(("access:systemstats:container", "freen"));
  102.     if (NULL == container) {
  103.         snmp_log(LOG_ERR, "invalid container for netsnmp_access_systemstats_freen");
  104.         return;
  105.     }
  106.     if(! (free_flags & NETSNMP_ACCESS_SYSTEMSTATS_FREE_DONT_CLEAR)) {
  107.         /*
  108.          * free all items.
  109.          */
  110.         CONTAINER_CLEAR(container,
  111.                         (netsnmp_container_obj_func*)_entry_release,
  112.                         NULL);
  113.     }
  114.     CONTAINER_FREE(container);
  115. }
  116. /**---------------------------------------------------------------------*/
  117. /*
  118.  * entry functions
  119.  */
  120. /**
  121.  */
  122. netsnmp_systemstats_entry *
  123. netsnmp_access_systemstats_entry_get_by_index(netsnmp_container *container, oid index)
  124. {
  125.     netsnmp_index   tmp;
  126.     DEBUGMSGTL(("access:systemstats:entry", "by_indexn"));
  127.     if (NULL == container) {
  128.         snmp_log(LOG_ERR,
  129.                  "invalid container for netsnmp_access_systemstats_entry_get_by_indexn");
  130.         return NULL;
  131.     }
  132.     tmp.len = 1;
  133.     tmp.oids = &index;
  134.     return (netsnmp_systemstats_entry *) CONTAINER_FIND(container, &tmp);
  135. }
  136. /**
  137.  */
  138. netsnmp_systemstats_entry *
  139. netsnmp_access_systemstats_entry_create(int version)
  140. {
  141.     netsnmp_systemstats_entry *entry =
  142.         SNMP_MALLOC_TYPEDEF(netsnmp_systemstats_entry);
  143.     DEBUGMSGTL(("access:systemstats:entry", "createn"));
  144.     if(NULL == entry)
  145.         return NULL;
  146.     entry->ns_ip_version = version;
  147.     entry->oid_index.len = 1;
  148.     entry->oid_index.oids = (oid *) & entry->ns_ip_version;
  149.     return entry;
  150. }
  151. /**
  152.  */
  153. void
  154. netsnmp_access_systemstats_entry_free(netsnmp_systemstats_entry * entry)
  155. {
  156.     DEBUGMSGTL(("access:systemstats:entry", "freen"));
  157.     if (NULL == entry)
  158.         return;
  159.     /*
  160.      * SNMP_FREE not needed, for any of these, 
  161.      * since the whole entry is about to be freed
  162.      */
  163.     if (NULL != entry->old_stats)
  164.         free(entry->old_stats);
  165.     free(entry);
  166. }
  167. /**---------------------------------------------------------------------*/
  168. /*
  169.  * Utility routines
  170.  */
  171. /**
  172.  * internal
  173.  */
  174. static void
  175. _entry_release(netsnmp_systemstats_entry * entry, void *context)
  176. {
  177.     netsnmp_access_systemstats_entry_free(entry);
  178. }
  179. /**
  180.  * update entry stats (checking for counter wrap)
  181.  *
  182.  * @retval  0 : success
  183.  * @retval <0 : error
  184.  */
  185. int
  186. netsnmp_access_systemstats_entry_update_stats(netsnmp_systemstats_entry * prev_vals,
  187.                                               netsnmp_systemstats_entry * new_vals)
  188. {
  189.     DEBUGMSGTL(("access:systemstats", "check_wrapn"));
  190.     
  191.     /*
  192.      * sanity checks
  193.      */
  194.     if ((NULL == prev_vals) || (NULL == new_vals) ||
  195.         (prev_vals->ns_ip_version != new_vals->ns_ip_version))
  196.         return -1;
  197.     /*
  198.      * if we've determined that we have 64 bit counters, just copy them.
  199.      */
  200.     if (0 == need_wrap_check) {
  201.         memcpy(&prev_vals->stats, &new_vals->stats, sizeof(new_vals->stats));
  202.         return 0;
  203.     }
  204.     if (NULL == prev_vals->old_stats) {
  205.         /*
  206.          * if we don't have old stats, they can't have wrapped, so just copy
  207.          */
  208.         prev_vals->old_stats = SNMP_MALLOC_TYPEDEF(netsnmp_ipstats);
  209.         if (NULL == prev_vals->old_stats) {
  210.             return -2;
  211.         }
  212.     }
  213.     else {
  214.         /*
  215.          * update straight 32 bit counters
  216.          */
  217.         prev_vals->stats.InHdrErrors = new_vals->stats.InHdrErrors;
  218.         prev_vals->stats.InNoRoutes = new_vals->stats.InNoRoutes;
  219.         prev_vals->stats.InAddrErrors = new_vals->stats.InAddrErrors;
  220.         prev_vals->stats.InUnknownProtos = new_vals->stats.InUnknownProtos;
  221.         prev_vals->stats.InTruncatedPkts = new_vals->stats.InTruncatedPkts;
  222.         prev_vals->stats.ReasmReqds = new_vals->stats.InTruncatedPkts;
  223.         prev_vals->stats.ReasmOKs = new_vals->stats.ReasmOKs;
  224.         prev_vals->stats.ReasmFails = new_vals->stats.ReasmFails;
  225.         prev_vals->stats.InDiscards = new_vals->stats.InDiscards;
  226.         prev_vals->stats.OutNoRoutes = new_vals->stats.OutNoRoutes;
  227.         prev_vals->stats.OutDiscards = new_vals->stats.OutDiscards;
  228.         prev_vals->stats.OutFragReqds = new_vals->stats.OutFragReqds;
  229.         prev_vals->stats.OutFragOKs = new_vals->stats.OutFragOKs;
  230.         prev_vals->stats.OutFragFails = new_vals->stats.OutFragFails;
  231.         prev_vals->stats.OutFragCreates = new_vals->stats.OutFragCreates;
  232.         /*
  233.          * update 64bit counters
  234.          */
  235.         netsnmp_c64_check32_and_update(&prev_vals->stats.HCInReceives,
  236.                                        &new_vals->stats.HCInReceives,
  237.                                        &prev_vals->old_stats->HCInReceives,
  238.                                        &need_wrap_check);
  239.         netsnmp_c64_check32_and_update(&prev_vals->stats.HCInOctets,
  240.                                        &new_vals->stats.HCInOctets,
  241.                                        &prev_vals->old_stats->HCInOctets,
  242.                                        &need_wrap_check);
  243.         netsnmp_c64_check32_and_update(&prev_vals->stats.HCInForwDatagrams,
  244.                                        &new_vals->stats.HCInForwDatagrams,
  245.                                        &prev_vals->old_stats->HCInForwDatagrams,
  246.                                        &need_wrap_check);
  247.         netsnmp_c64_check32_and_update(&prev_vals->stats.HCInDelivers,
  248.                                        &new_vals->stats.HCInDelivers,
  249.                                        &prev_vals->old_stats->HCInDelivers,
  250.                                        &need_wrap_check);
  251.         netsnmp_c64_check32_and_update(&prev_vals->stats.HCOutRequests,
  252.                                        &new_vals->stats.HCOutRequests,
  253.                                        &prev_vals->old_stats->HCOutRequests,
  254.                                        &need_wrap_check);
  255.         netsnmp_c64_check32_and_update(&prev_vals->stats.HCOutForwDatagrams,
  256.                                        &new_vals->stats.HCOutForwDatagrams,
  257.                                        &prev_vals->old_stats->HCOutForwDatagrams,
  258.                                        &need_wrap_check);
  259.         netsnmp_c64_check32_and_update(&prev_vals->stats.HCOutTransmits,
  260.                                        &new_vals->stats.HCOutTransmits,
  261.                                        &prev_vals->old_stats->HCOutTransmits,
  262.                                        &need_wrap_check);
  263.         netsnmp_c64_check32_and_update(&prev_vals->stats.HCOutOctets,
  264.                                        &new_vals->stats.HCOutOctets,
  265.                                        &prev_vals->old_stats->HCOutOctets,
  266.                                        &need_wrap_check);
  267.         netsnmp_c64_check32_and_update(&prev_vals->stats.HCInMcastPkts,
  268.                                        &new_vals->stats.HCInMcastPkts,
  269.                                        &prev_vals->old_stats->HCInMcastPkts,
  270.                                        &need_wrap_check);
  271.         netsnmp_c64_check32_and_update(&prev_vals->stats.HCInMcastOctets,
  272.                                        &new_vals->stats.HCInMcastOctets,
  273.                                        &prev_vals->old_stats->HCInMcastOctets,
  274.                                        &need_wrap_check);
  275.         netsnmp_c64_check32_and_update(&prev_vals->stats.HCOutMcastPkts,
  276.                                        &new_vals->stats.HCOutMcastPkts,
  277.                                        &prev_vals->old_stats->HCOutMcastPkts,
  278.                                        &need_wrap_check);
  279.         netsnmp_c64_check32_and_update(&prev_vals->stats.HCOutMcastOctets,
  280.                                        &new_vals->stats.HCOutMcastOctets,
  281.                                        &prev_vals->old_stats->HCOutMcastOctets,
  282.                                        &need_wrap_check);
  283.         netsnmp_c64_check32_and_update(&prev_vals->stats.HCInBcastPkts,
  284.                                        &new_vals->stats.HCInBcastPkts,
  285.                                        &prev_vals->old_stats->HCInBcastPkts,
  286.                                        &need_wrap_check);
  287.         netsnmp_c64_check32_and_update(&prev_vals->stats.HCOutBcastPkts,
  288.                                        &new_vals->stats.HCOutBcastPkts,
  289.                                        &prev_vals->old_stats->HCOutBcastPkts,
  290.                                        &need_wrap_check);
  291.     }
  292.     
  293.     /*
  294.      * if we've decided we no longer need to check wraps, free old stats
  295.      */
  296.     if (0 == need_wrap_check) {
  297.         SNMP_FREE(prev_vals->old_stats);
  298.     }
  299.     
  300.     /*
  301.      * update old stats from new stats.
  302.      * careful - old_stats is a pointer to stats...
  303.      */
  304.     memcpy(prev_vals->old_stats, &new_vals->stats, sizeof(new_vals->stats));
  305.     
  306.     return 0;
  307. }
  308. /**
  309.  * update systemstats entry data (checking for counter wraps)
  310.  *
  311.  * Given an existing entry, update it with the new values from another
  312.  * entry.
  313.  *
  314.  * @retval -2 : malloc failed
  315.  * @retval -1 : systemstatss not the same
  316.  * @retval  0 : no error
  317.  */
  318. int
  319. netsnmp_access_systemstats_entry_update(netsnmp_systemstats_entry * lhs,
  320.                                         netsnmp_systemstats_entry * rhs)
  321. {
  322.     DEBUGMSGTL(("access:systemstats", "copyn"));
  323.     
  324.     if ((NULL == lhs) || (NULL == rhs) ||
  325.         (lhs->ns_ip_version != rhs->ns_ip_version))
  326.         return -1;
  327.     /*
  328.      * update stats
  329.      */
  330.     netsnmp_access_systemstats_entry_update_stats(lhs, rhs);
  331.     /*
  332.      * update other data
  333.      */
  334.     lhs->flags = rhs->flags;
  335.     
  336.     return 0;
  337. }
  338. /**---------------------------------------------------------------------*/
  339. /*
  340.  *
  341.  */