Debian Codegraph @

agent/mibgroup/hardware/sensors/hw_sensors.c

raw
1 #include <net-snmp/net-snmp-config.h>
2 #include <net-snmp/net-snmp-includes.h>
3 #include <net-snmp/agent/net-snmp-agent-includes.h>
4 #include <net-snmp/agent/hardware/sensors.h>
5 #include "hw_sensors_private.h"
6
7
8 static int _sensor_load( void );
9 static void _sensor_free( void );
10
11 static int _sensorAutoUpdate = 0; /* 0 means on-demand caching */
12 static void _sensor_update_stats( unsigned int, void* );
13
14 netsnmp_cache *_sensor_cache = NULL;
15 netsnmp_container *_sensor_container = NULL;
16 static int _sensor_idx = 0;
17
18 void init_hw_sensors( void ) {
19
20 if ( _sensor_container )
21 return; /* Already initialised */
22
23 DEBUGMSGTL(("sensors", "Initialise Hardware Sensors module\n"));
24
25 /*
26 * Define a container to hold the basic list of sensors
27 * The four LM-SENSOR-MIB containers will be created in
28 * the relevant initialisation routine(s)
29 */
30 _sensor_container = netsnmp_container_find("sensorTable:table_container");
31 if ( NULL == _sensor_container ) {
32 snmp_log( LOG_ERR, "failed to create container for sensorTable");
33 return;
34 }
35 netsnmp_sensor_arch_init( );
36
37 /*
38 * If we're sampling the sensor information automatically,
39 * then arrange for this to be triggered regularly.
40 *
41 * If we're not sampling these values regularly,
42 * create a suitable cache handler instead.
43 */
44 if ( _sensorAutoUpdate ) {
45 DEBUGMSGTL(("sensors", "Reloading Hardware Sensors automatically (%d)\n",
46 _sensorAutoUpdate));
47 snmp_alarm_register( _sensorAutoUpdate, SA_REPEAT,
48 _sensor_update_stats, NULL );
49 }
50 else {
51 _sensor_cache = netsnmp_cache_create( 5, netsnmp_sensor_load,
52 netsnmp_sensor_free, NULL, 0 );
53 DEBUGMSGTL(("sensors", "Reloading Hardware Sensors on-demand (%p)\n",
54 _sensor_cache));
55 }
56 }
57
58 void shutdown_hw_sensors( void ) {
59 _sensor_free();
60 }
61
62 /*
63 * Return the main sensor container
64 */
65 netsnmp_container *get_sensor_container( void ) { return _sensor_container; }
66
67 /*
68 * Return the main sensor cache control structure (if defined)
69 */
70 netsnmp_cache *get_sensor_cache( void ) { return _sensor_cache; }
71
72
73 /*
74 * Wrapper routine for automatically updating sensor statistics
75 */
76 void
77 _sensor_update_stats( unsigned int clientreg, void *data )
78 {
79 _sensor_free();
80 _sensor_load();
81 }
82
83 /*
84 * Wrapper routine for re-loading sensor statistics on demand
85 */
86 int
87 netsnmp_sensor_load( netsnmp_cache *cache, void *data )
88 {
89 return _sensor_load();
90 }
91
92 /*
93 * Wrapper routine for releasing expired sensor statistics
94 */
95 void
96 netsnmp_sensor_free( netsnmp_cache *cache, void *data )
97 {
98 _sensor_free();
99 }
100
101
102 /*
103 * Architecture-independent processing of loading sensor statistics
104 */
105 static int
106 _sensor_load( void )
107 {
108 netsnmp_sensor_arch_load( NULL, NULL );
109 return 0;
110 }
111
112 /*
113 * Architecture-independent release of sensor statistics
114 */
115 static void
116 _sensor_free( void )
117 {
118 netsnmp_sensor_info *sp;
119
120 for (sp = CONTAINER_FIRST( _sensor_container );
121 sp;
122 sp = CONTAINER_NEXT( _sensor_container, sp )) {
123
124 sp->flags &= ~ NETSNMP_SENSOR_FLAG_ACTIVE;
125 }
126 }
127
128
129 /*
130 * Retrieve a sensor entry by name,
131 * or (optionally) insert a new one into the container
132 */
133 netsnmp_sensor_info *
134 sensor_by_name( const char *name, int create_type )
135 {
136 netsnmp_sensor_info *sp;
137
138 DEBUGMSGTL(("sensors:name", "Get sensor entry (%s)\n", name));
139
140 /*
141 * Look through the list for a matching entry
142 */
143 /* .. or use a secondary index container ?? */
144 for (sp = CONTAINER_FIRST( _sensor_container );
145 sp;
146 sp = CONTAINER_NEXT( _sensor_container, sp )) {
147
148 if ( !strcmp( name, sp->name ))
149 return sp;
150 }
151
152 /*
153 * Not found...
154 */
155 if ( create_type == NETSNMP_SENSOR_FIND_EXIST ) {
156 DEBUGMSGTL(("sensors:name", "No such sensor entry\n"));
157 return NULL;
158 }
159
160 /*
161 * ... so let's create a new one, using the type supplied
162 */
163 sp = SNMP_MALLOC_TYPEDEF( netsnmp_sensor_info );
164 if ( sp ) {
165 if (strlen(name) >= sizeof(sp->name)) {
166 snmp_log(LOG_ERR, "Sensor name is too large: %s\n", name);
167 free(sp);
168 return NULL;
169 }
170 strlcpy(sp->name, name, sizeof(sp->name));
171 sp->type = create_type;
172 /*
173 * Set up the index value.
174 *
175 * All this trouble, just for a simple integer.
176 * Surely there must be a better way?
177 */
178 sp->idx.len = 1;
179 sp->idx.oids = SNMP_MALLOC_TYPEDEF( oid );
180 sp->idx.oids[0] = ++_sensor_idx;
181 }
182
183 DEBUGMSGTL(("sensors:name", "Create sensor entry (type = %d, index = %d\n",
184 create_type, _sensor_idx));
185 CONTAINER_INSERT( _sensor_container, sp );
186 return sp;
187 }
188