Debian Codegraph @

agent/mibgroup/ucd-snmp/lmsensorsMib.c

raw
1 #ifdef HAVE_INTTYPES_H
2 #include <inttypes.h>
3 #endif
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 <net-snmp/agent/hardware/sensors.h>
9 #include "hardware/sensors/hw_sensors.h"
10 #include "ucd-snmp/lmsensorsMib.h"
11
12 struct reg_and_table {
13 netsnmp_handler_registration *reg;
14 netsnmp_table_registration_info *table;
15 };
16
17 static netsnmp_container *sensorContainer;
18 static struct reg_and_table temp_reg;
19 static struct reg_and_table fan_reg;
20 static struct reg_and_table volt_reg;
21 static struct reg_and_table misc_reg;
22
23 static const oid lmTempSensorsTable_oid[] = {1,3,6,1,4,1,2021,13,16,2};
24 static const oid lmFanSensorsTable_oid[] = {1,3,6,1,4,1,2021,13,16,3};
25 static const oid lmVoltSensorsTable_oid[] = {1,3,6,1,4,1,2021,13,16,4};
26 static const oid lmMiscSensorsTable_oid[] = {1,3,6,1,4,1,2021,13,16,5};
27 /* All the tables have the same length root OID */
28 const size_t lmSensorsTables_oid_len = OID_LENGTH(lmMiscSensorsTable_oid);
29
30
31 /*
32 * Common initialisation code, used for setting up all four tables
33 */
34 static struct reg_and_table
35 initialize_lmSensorsTable(const char *tableName, const oid *tableOID,
36 netsnmp_container_op *filter, int mult )
37 {
38 netsnmp_handler_registration *reg;
39 netsnmp_table_registration_info *table_info;
40 netsnmp_cache *cache;
41 netsnmp_container *container;
42 struct reg_and_table res;
43
44 memset(&res, 0, sizeof(res));
45
46 /*
47 * Ensure the HAL sensors module has been initialised,
48 * and retrieve the main sensors container.
49 * This table will then be registered using a filter on this container.
50 */
51 sensorContainer = get_sensor_container();
52 if ( !sensorContainer ) {
53 init_hw_sensors( );
54 sensorContainer = get_sensor_container();
55 }
56 container = netsnmp_container_find("sensorTable:table_container");
57 container->insert_filter = filter;
58 netsnmp_container_add_index( sensorContainer, container );
59
60
61 /*
62 * Create a basic registration structure for the table
63 */
64 reg = netsnmp_create_handler_registration(
65 tableName, lmSensorsTables_handler,
66 tableOID, lmSensorsTables_oid_len, HANDLER_CAN_RONLY
67 );
68
69 /*
70 * Register the table using the filtered container
71 * Include an indicator of any scaling to be applied to the sensor value
72 */
73 reg->my_reg_void = (void *)(uintptr_t)mult;
74 table_info = SNMP_MALLOC_TYPEDEF( netsnmp_table_registration_info );
75 netsnmp_table_helper_add_indexes(table_info, ASN_INTEGER, 0);
76 table_info->min_column = COLUMN_LMSENSORS_INDEX;
77 table_info->max_column = COLUMN_LMSENSORS_SIGNED;
78 if (netsnmp_container_table_register(reg, table_info, container, 0) !=
79 SNMPERR_SUCCESS) {
80 snmp_log(LOG_ERR, "Failed to register the sensors container table\n");
81 return res;
82 }
83
84 /*
85 * If the HAL sensors module was configured as an on-demand caching
86 * module (rather than being automatically loaded regularly),
87 * then ensure this table makes use of that cache.
88 */
89 cache = get_sensor_cache();
90 if ( cache ) {
91 netsnmp_inject_handler_before( reg, netsnmp_cache_handler_get( cache ),
92 "table_container");
93 }
94
95 res.reg = reg;
96 res.table = table_info;
97 return res;
98 }
99
100
101 /*
102 * Container filters for the four tables
103 *
104 * Used to ensure that sensor entries appear in the appropriate table.
105 */
106 static int _sensor_filter_temp( netsnmp_container *c, const void *v )
107 {
108 const netsnmp_sensor_info *sp = (const netsnmp_sensor_info *)v;
109 /* Only matches temperature sensors */
110 return (( sp->type == NETSNMP_SENSOR_TYPE_TEMPERATURE ) ? 0 : 1 );
111 }
112
113 static int _sensor_filter_fan( netsnmp_container *c, const void *v )
114 {
115 const netsnmp_sensor_info *sp = (const netsnmp_sensor_info *)v;
116 /* Only matches fan sensors */
117 return (( sp->type == NETSNMP_SENSOR_TYPE_RPM ) ? 0 : 1 );
118 }
119
120 static int _sensor_filter_volt( netsnmp_container *c, const void *v )
121 {
122 const netsnmp_sensor_info *sp = (const netsnmp_sensor_info *)v;
123 /* Only matches voltage sensors (AC or DC) */
124 return ((( sp->type == NETSNMP_SENSOR_TYPE_VOLTAGE_DC ) ||
125 ( sp->type == NETSNMP_SENSOR_TYPE_VOLTAGE_AC )) ? 0 : 1 );
126 }
127
128 static int _sensor_filter_misc( netsnmp_container *c, const void *v )
129 {
130 const netsnmp_sensor_info *sp = (const netsnmp_sensor_info *)v;
131 /* Matches everything except temperature, fan or voltage sensors */
132 return ((( sp->type == NETSNMP_SENSOR_TYPE_TEMPERATURE ) ||
133 ( sp->type == NETSNMP_SENSOR_TYPE_RPM ) ||
134 ( sp->type == NETSNMP_SENSOR_TYPE_VOLTAGE_DC ) ||
135 ( sp->type == NETSNMP_SENSOR_TYPE_VOLTAGE_AC )) ? 1 : 0 );
136 }
137
138 /* Initialize the LM Sensors MIB module */
139 void
140 init_lmsensorsMib(void)
141 {
142 DEBUGMSGTL(("ucd-snmp/lmsensorsMib","Initializing LM-SENSORS-MIB tables\n"));
143
144 /*
145 * Initialize the four LM-SENSORS-MIB tables
146 *
147 * They are almost identical, so we can use the same registration code.
148 */
149 temp_reg = initialize_lmSensorsTable("lmTempSensorsTable",
150 lmTempSensorsTable_oid, _sensor_filter_temp,
151 1000); /* MIB asks for mC */
152 fan_reg = initialize_lmSensorsTable("lmFanSensorsTable",
153 lmFanSensorsTable_oid, _sensor_filter_fan, 1);
154 volt_reg = initialize_lmSensorsTable("lmVoltSensorsTable",
155 lmVoltSensorsTable_oid, _sensor_filter_volt,
156 1000); /* MIB asks for mV */
157 misc_reg = initialize_lmSensorsTable("lmMiscSensorsTable",
158 lmMiscSensorsTable_oid, _sensor_filter_misc, 1);
159 }
160
161 void
162 shutdown_lmsensorsMib(void)
163 {
164 netsnmp_table_registration_info_free(misc_reg.table);
165 netsnmp_unregister_table(misc_reg.reg);
166 netsnmp_table_registration_info_free(volt_reg.table);
167 netsnmp_unregister_table(volt_reg.reg);
168 netsnmp_table_registration_info_free(fan_reg.table);
169 netsnmp_unregister_table(fan_reg.reg);
170 netsnmp_table_registration_info_free(temp_reg.table);
171 netsnmp_unregister_table(temp_reg.reg);
172 }
173
174 /*
175 * Handle requests for any of the four lmXxxxSensorsTables
176 *
177 * This is possible because all the table share the
178 * same structure and behaviour.
179 */
180 int
181 lmSensorsTables_handler(
182 netsnmp_mib_handler *handler,
183 netsnmp_handler_registration *reginfo,
184 netsnmp_agent_request_info *reqinfo,
185 netsnmp_request_info *requests) {
186
187 netsnmp_request_info *request;
188 netsnmp_table_request_info *table_info;
189 netsnmp_sensor_info *sensor_info;
190 int mult = (uintptr_t)reginfo->my_reg_void;
191
192 DEBUGMSGTL(( "ucd-snmp/lmsensorsMib","lmSensorsTables_handler - root: "));
193 DEBUGMSGOID(("ucd-snmp/lmsensorsMib", reginfo->rootoid, reginfo->rootoid_len));
194 DEBUGMSG(( "ucd-snmp/lmsensorsMib",", mode %d\n", reqinfo->mode ));
195 /*
196 * This is a read-only table, so we only need to handle GET requests.
197 * (The container helper converts GETNEXT->GET requests automatically).
198 */
199 switch (reqinfo->mode) {
200 case MODE_GET:
201 for (request=requests; request; request=request->next) {
202 sensor_info = (netsnmp_sensor_info *)
203 netsnmp_container_table_extract_context(request);
204 if ( !sensor_info ) {
205 netsnmp_set_request_error(reqinfo, request, SNMP_NOSUCHINSTANCE);
206 continue;
207 }
208
209 table_info = netsnmp_extract_table_info(request);
210 switch (table_info->colnum) {
211 case COLUMN_LMSENSORS_INDEX:
212 snmp_set_var_typed_integer( request->requestvb, ASN_INTEGER,
213 sensor_info->idx.oids[0]);
214 break;
215 case COLUMN_LMSENSORS_DEVICE:
216 if ( sensor_info->descr[0] != '\0' ) {
217 snmp_set_var_typed_value( request->requestvb, ASN_OCTET_STR,
218 sensor_info->descr, strlen(sensor_info->descr));
219 } else {
220 snmp_set_var_typed_value( request->requestvb, ASN_OCTET_STR,
221 sensor_info->name, strlen(sensor_info->name));
222 }
223 break;
224 case COLUMN_LMSENSORS_VALUE:
225 /* Multiply the value by the appropriate scaling factor for this table */
226 snmp_set_var_typed_integer( request->requestvb, ASN_GAUGE,
227 (int)(mult*sensor_info->value));
228 break;
229 case COLUMN_LMSENSORS_SIGNED:
230 /* Only avalable for temperature table */
231 if (reginfo->rootoid[9] != 2)
232 netsnmp_set_request_error(reqinfo, request, SNMP_NOSUCHOBJECT);
233 else
234 /* Multiply the value by the appropriate scaling factor for this table */
235 snmp_set_var_typed_integer( request->requestvb, ASN_INTEGER,
236 (int)(mult*sensor_info->value));
237 break;
238 default:
239 netsnmp_set_request_error(reqinfo, request,
240 SNMP_NOSUCHOBJECT);
241 break;
242 }
243 }
244 break;
245
246 }
247 return SNMP_ERR_NOERROR;
248 }