Debian Codegraph @

agent/mibgroup/ucd-snmp/lmSensors.c

raw ◀ ▶
1 Debian patch touch this file
1 /* lmSensors.c
2 *
3 * Sections of this code were derived from the published API's of
4 * some Sun products. Hence, portions of the code may be copyright
5 * Sun Microsystems.
6 *
7 * Additional code provided by Mike Fisher and Thomas E. Lackley
8 *
9 * This component allows net-snmp to report sensor information.
10 *
11 * In order to use it, the ./configure invocation must include...
12 *
13 * --with-mib-modules="ucd-snmp/lmSensors"
14 *
15 * It uses one of three different methodologies. Some platforms make
16 * use of an lm_sensors driver to access the information on the
17 * health monitoring hardware, such as the LM75 and LM78 chips.
18 *
19 * For further information see http://secure.netroedge.com/~lm78/
20 *
21 * The Solaris platform uses the other two methodologies. Earlier
22 * platforms such as the Enterprise 450 use kstat to report sensor
23 * information. Later platforms, such as the V880 use the picld
24 * daemon to control system resources and report sensor information.
25 * Picld is supported only on Solaris 2.8 and later.
26 *
27 * Both these methodologies are implemented in a "read only" manner.
28 * You cannot use this code to change anything eg. fan speeds.
29 *
30 * The lmSensors component delivers the information documented in the
31 * LM-SENSORS-MIB. The information is divided up as follows:
32 *
33 * -temperatures (in thousandsths of a Celsius degree)
34 * -fans (rpm's)
35 * -voltages (in milliVolts)
36 * -other (switches, LEDs and i2c's (things that use the i2c bus))
37 * NOTE: This version does not support gpio's. Still on the learning curve.
38 *
39 * Because the MIB only allows output of the datatype Gauge32 this
40 * limits the amount of meaningful information that can be delivered
41 * from "other" sensors. Hence, the code does a certain amount of
42 * translating. See the source for individual sensor types.
43 *
44 * If an "other" sensor delivers a value 99, it means that it
45 * is delivering a "status" that the code does not account for.
46 * If you discover one of these, please pass it on and I'll
47 * put it in.
48 *
49 * It was recently discovered that the sensors code had not be following
50 * the MIB for some sensors. The MIB required reporting some items
51 * in mV and mC. These changes have been noted in the source.
52 *
53 * To see debugging messages, run the daemon as follows:
54 *
55 * /usr/local/sbin/snmpd -f -L -Ducd-snmp/lmSensors
56 * (change path to wherever you installed it)
57 *
58 * or using gdb:
59 *
60 * gdb snmpd
61 * run -f -L -Ducd-snmp/lmSensors
62 *
63 * The component can record up to 256 instances of each type.
64 *
65 * The following should always be included first before anything else
66 */
67
68 #include <net-snmp/net-snmp-config.h>
69 #include <net-snmp/net-snmp-features.h>
70 #include <net-snmp/net-snmp-includes.h>
71 #include <net-snmp/agent/net-snmp-agent-includes.h>
72
73 /*
74 * minimal include directives
75 */
76
77 #include "util_funcs/header_simple_table.h"
78 #include <time.h>
79
80 netsnmp_feature_require(table_container);
81
82
83 /*
84 * Load required drivers and libraries.
85 */
86
87 #ifdef solaris2
88 #include <kstat.h>
89 #ifdef HAVE_PICL_H
90 #include <picl.h> /* accesses the picld daemon */
91 #else
92 /* the following should be sufficient for any Sun-based sensors */
93 #include </usr/platform/sun4u/include/sys/envctrl.h>
94 #endif
95 #else
96 #include <sensors/sensors.h>
97 #endif
98
99 #include "lmSensors.h"
100
101 #define TEMP_TYPE (0)
102 #define FAN_TYPE (1)
103 #define VOLT_TYPE (2)
104 #define MISC_TYPE (3)
105 #define N_TYPES (4)
106
107 #ifdef solaris2
108 #define MAX_NAME (256)
109 #define MAX_SENSORS (256) /* there's a lot of sensors on a v880 */
110 #else
111 #define MAX_NAME (64)
112 #define DEFAULT_SENSORS (256)
113 #endif
114
115
116 /*
117 * lmSensors_variables_oid:
118 * this is the top level oid that we want to register under. This
119 * is essentially a prefix, with the suffix appearing in the
120 * variable below.
121 */
122
123
124 oid lmSensors_variables_oid[] =
125 { 1, 3, 6, 1, 4, 1, 2021, 13, 16 };
126
127 /*
128 * variable4 lmSensors_variables:
129 * this variable defines function callbacks and type return information
130 * for the lmSensors mib section
131 */
132
133 struct variable4 lmSensors_variables[] = {
134 /*
135 * magic number , variable type , ro/rw , callback fn , L, oidsuffix
136 */
137 #define LMTEMPSENSORSINDEX 3
138 {LMTEMPSENSORSINDEX, ASN_INTEGER, NETSNMP_OLDAPI_RONLY,
139 var_lmSensorsTable, 3, {2, 1, 1}},
140 #define LMTEMPSENSORSDEVICE 4
141 {LMTEMPSENSORSDEVICE, ASN_OCTET_STR, NETSNMP_OLDAPI_RONLY,
142 var_lmSensorsTable, 3, {2, 1, 2}},
143 #define LMTEMPSENSORSVALUE 5
144 {LMTEMPSENSORSVALUE, ASN_GAUGE, NETSNMP_OLDAPI_RONLY,
145 var_lmSensorsTable, 3, {2, 1, 3}},
146 #define LMFANSENSORSINDEX 8
147 {LMFANSENSORSINDEX, ASN_INTEGER, NETSNMP_OLDAPI_RONLY,
148 var_lmSensorsTable, 3, {3, 1, 1}},
149 #define LMFANSENSORSDEVICE 9
150 {LMFANSENSORSDEVICE, ASN_OCTET_STR, NETSNMP_OLDAPI_RONLY,
151 var_lmSensorsTable, 3, {3, 1, 2}},
152 #define LMFANSENSORSVALUE 10
153 {LMFANSENSORSVALUE, ASN_GAUGE, NETSNMP_OLDAPI_RONLY,
154 var_lmSensorsTable, 3, {3, 1, 3}},
155 #define LMVOLTSENSORSINDEX 13
156 {LMVOLTSENSORSINDEX, ASN_INTEGER, NETSNMP_OLDAPI_RONLY,
157 var_lmSensorsTable, 3, {4, 1, 1}},
158 #define LMVOLTSENSORSDEVICE 14
159 {LMVOLTSENSORSDEVICE, ASN_OCTET_STR, NETSNMP_OLDAPI_RONLY,
160 var_lmSensorsTable, 3, {4, 1, 2}},
161 #define LMVOLTSENSORSVALUE 15
162 {LMVOLTSENSORSVALUE, ASN_GAUGE, NETSNMP_OLDAPI_RONLY,
163 var_lmSensorsTable, 3, {4, 1, 3}},
164 #define LMMISCSENSORSINDEX 18
165 {LMMISCSENSORSINDEX, ASN_INTEGER, NETSNMP_OLDAPI_RONLY,
166 var_lmSensorsTable, 3, {5, 1, 1}},
167 #define LMMISCSENSORSDEVICE 19
168 {LMMISCSENSORSDEVICE, ASN_OCTET_STR, NETSNMP_OLDAPI_RONLY,
169 var_lmSensorsTable, 3, {5, 1, 2}},
170 #define LMMISCSENSORSVALUE 20
171 {LMMISCSENSORSVALUE, ASN_GAUGE, NETSNMP_OLDAPI_RONLY,
172 var_lmSensorsTable, 3, {5, 1, 3}},
173 };
174
175 typedef struct {
176 #ifdef solaris2
177 #ifdef HAVE_PICL_H
178 char name[PICL_PROPNAMELEN_MAX]; /*required for picld*/
179 int value;
180 #else
181 char name[MAX_NAME];
182 int value;
183 #endif
184 #else
185 char name[MAX_NAME];
186 int value;
187 #endif
188 } _sensor;
189
190 typedef struct {
191 int n;
192 #ifdef solaris2
193 _sensor sensor[MAX_SENSORS];
194 #else
195 _sensor* sensor;
196 size_t current_len;
197 #endif
198 } _sensor_array;
199
200 static _sensor_array sensor_array[N_TYPES];
201 static time_t timestamp;
202
203 static int sensor_init(void);
204 static int sensor_load(void);
205 static int _sensor_load(time_t t);
206 #ifndef solaris2
207 static void free_sensor_arrays(void);
208 #endif
209
210 /*
211 * init_lmSensors():
212 * Initialization routine. This is called when the agent starts up.
213 * At a minimum, registration of your variables should take place here.
214 */
215 void
216 init_lmSensors(void)
217 {
218 sensor_init();
219
220 /*
221 * register ourselves with the agent to handle our mib tree
222 */
223 REGISTER_MIB("lmSensors", lmSensors_variables, variable4,
224 lmSensors_variables_oid);
225 }
226
227 /*
228 * shutdown_lmSensors():
229 * A shutdown/cleanup routine. This is called when the agent shutsdown.
230 */
231 void
232 shutdown_lmSensors(void)
233 {
234 #ifndef solaris2
235 DEBUGMSG(("ucd-snmp/lmSensors", "=> shutdown_lmSensors\n"));
236 free_sensor_arrays();
237 DEBUGMSG(("ucd-snmp/lmSensors", "<= shutdown_lmSensors\n"));
238 #endif
239 } /* shutdown_lmSensors */
240
241 /*
242 * var_lmSensorsTable():
243 * Handle this table separately from the scalar value case.
244 * The workings of this are basically the same as for var_lmSensors above.
245 */
246 unsigned char *
247 var_lmSensorsTable(struct variable *vp,
248 oid * name,
249 size_t * length,
250 int exact,
251 size_t * var_len, WriteMethod ** write_method)
252 {
253 static long long_ret;
254 static char string[SPRINT_MAX_LEN];
255
256 int s_index;
257 int s_type = -1;
258 int n_sensors;
259 unsigned char* ret = NULL;
260
261 _sensor s;
262
263 if (sensor_load())
264 {
265 ret = NULL;
266 goto leaving;
267 }
268
269 switch (vp->magic) {
270 case LMTEMPSENSORSINDEX:
271 case LMTEMPSENSORSDEVICE:
272 case LMTEMPSENSORSVALUE:
273 s_type = TEMP_TYPE;
274 n_sensors = sensor_array[s_type].n;
275 break;
276
277 case LMFANSENSORSINDEX:
278 case LMFANSENSORSDEVICE:
279 case LMFANSENSORSVALUE:
280 s_type = FAN_TYPE;
281 n_sensors = sensor_array[s_type].n;
282 break;
283
284 case LMVOLTSENSORSINDEX:
285 case LMVOLTSENSORSDEVICE:
286 case LMVOLTSENSORSVALUE:
287 s_type = VOLT_TYPE;
288 n_sensors = sensor_array[s_type].n;
289 break;
290
291 case LMMISCSENSORSINDEX:
292 case LMMISCSENSORSDEVICE:
293 case LMMISCSENSORSVALUE:
294 s_type = MISC_TYPE;
295 n_sensors = sensor_array[s_type].n;
296 break;
297
298 default:
299 s_type = -1;
300 n_sensors = 0;
301 }
302
303 if (header_simple_table(vp, name, length, exact,
304 var_len, write_method,
305 n_sensors) == MATCH_FAILED)
306 {
307 ret = NULL;
308 goto leaving;
309 }
310
311 if (s_type < 0)
312 {
313 ret = NULL;
314 goto leaving;
315 }
316
317 s_index = name[*length - 1] - 1;
318 s = sensor_array[s_type].sensor[s_index];
319
320 switch (vp->magic) {
321 case LMTEMPSENSORSINDEX:
322 case LMFANSENSORSINDEX:
323 case LMVOLTSENSORSINDEX:
324 case LMMISCSENSORSINDEX:
325 long_ret = s_index;
326 ret = (unsigned char *) &long_ret;
327 goto leaving;
328
329 case LMTEMPSENSORSDEVICE:
330 case LMFANSENSORSDEVICE:
331 case LMVOLTSENSORSDEVICE:
332 case LMMISCSENSORSDEVICE:
333 strlcpy(string, s.name, sizeof(string));
334 *var_len = strlen(string);
335 ret = (unsigned char *) string;
336 goto leaving;
337
338 case LMTEMPSENSORSVALUE:
339 case LMFANSENSORSVALUE:
340 case LMVOLTSENSORSVALUE:
341 case LMMISCSENSORSVALUE:
342 long_ret = s.value;
343 ret = (unsigned char *) &long_ret;
344 goto leaving;
345
346 default:
347 ERROR_MSG("Unable to handle table request");
348 }
349
350 leaving:
351 return ret;
352 }
353
354 static int
355 sensor_init(void)
356 {
357 int res;
358 #ifndef solaris2
359 time_t t = time(NULL);
360 int i = 0;
361
362 DEBUGMSG(("ucd-snmp/lmSensors", "=> sensor_init\n"));
363
364 for (i = 0; i < N_TYPES; i++)
365 {
366 sensor_array[i].n = 0;
367 sensor_array[i].current_len = 0;
368 sensor_array[i].sensor = NULL;
369 }
370
•371 if (sensors_init(NULL))
372 {
373 res = 2;
374 goto leaving;
375 }
376
377 _sensor_load(t); /* I'll let the linux people decide whether they want to load right away */
378 leaving:
379 #endif /* not solaris2 */
380
381 DEBUGMSG(("ucd-snmp/lmSensors", "<= sensor_init\n"));
382 return res;
383 }
384
385 static int
386 sensor_load(void)
387 {
388 int rc = 0;
•389 time_t t = time(NULL);
390
391 if (t > timestamp + 7) /* this may require some tuning - currently 7 seconds*/
392 {
393 #ifndef solaris2
394 free_sensor_arrays();
395 #endif
396 rc = _sensor_load(t);
397 }
398
399 return rc;
400 }
401
402 /* This next code block includes all kstat and picld code for the Solaris platform.
403 * If you're not compiling on a Solaris that supports picld, it won't be included.
404 */
405
406 #ifdef solaris2
407 /* ******* picld sensor procedures * */
408 #ifdef HAVE_PICL_H
409
410 /* the following are generic modules for reading sensor information
411 the scale variable handles miniVolts */
412
413 static int
414 read_num_sensor(picl_nodehdl_t childh, const char *prop, int scale, int *value)
415 {
416 picl_nodehdl_t sensorh;
417 picl_propinfo_t sensor_info;
418 picl_errno_t error_code;
419 int valid = 1;
420
421 union valu {
422 char buf[PICL_PROPSIZE_MAX];
423 uint32_t us4;
424 uint16_t us2;
425 int32_t is4;
426 int16_t is2;
427 float f;
428 } val;
429
430 error_code = (picl_get_propinfo_by_name(childh, prop,
431 &sensor_info, &sensorh));
432
433 if (error_code != PICL_SUCCESS) {
434 DEBUGMSGTL(("ucd-snmp/lmSensors",
435 "sensor info lookup failed in read_num_sensor - error code->%d\n", error_code));
436 return(error_code);
437 }
438
439 error_code = picl_get_propval(sensorh, &val.buf, sensor_info.size);
440
441 if (error_code != PICL_SUCCESS) {
442 DEBUGMSGTL(("ucd-snmp/lmSensors",
443 "sensor value lookup failed in read_num_sensor - error code->%d\n", error_code));
444 return(error_code);
445 }
446
447 /* Can't make assumptions about the type or size of the value we get... */
448
449 if (sensor_info.type == PICL_PTYPE_FLOAT) {
450 *value = (int)(val.f*scale);
451 } else if (sensor_info.type == PICL_PTYPE_UNSIGNED_INT) {
452 if (sensor_info.size == 2) {
453 *value = (int)(val.us2 * scale);
454 } else if (sensor_info.size == 4) {
455 *value = (int)(val.us4 * scale);
456 } else
457 valid = 0;
458 } else if (sensor_info.type == PICL_PTYPE_INT) {
459 if (sensor_info.size == 2) {
460 *value = (int)(val.is2 * scale);
461 } else if (sensor_info.size == 4) {
462 *value = (int)(val.is4 * scale);
463 } else
464 valid = 0;
465 } else
466 valid = 0;
467
468 if (valid == 0) {
469 DEBUGMSGTL(("ucd-snmp/lmSensors",
470 "Don't know how to handle data type %d with length %d\n",
471 sensor_info.type, sensor_info.size));
472 error_code = PICL_FAILURE;
473 } else
474 DEBUGMSGTL(("ucd-snmp/lmSensors", "read_num_sensor value is %d\n", *value));
475
476 return(error_code);
477 } /* end of read_num_sensor() */
478
479 static int
480 read_enum_sensor(picl_nodehdl_t childh, const char **options, u_int *value)
481 {
482 picl_nodehdl_t sensorh;
483 picl_propinfo_t sensor_info;
484 picl_errno_t error_code;
485 char state[PICL_PROPSIZE_MAX];
486 int i;
487
488 error_code = (picl_get_propinfo_by_name(childh, "State",
489 &sensor_info, &sensorh));
490
491 if (error_code != PICL_SUCCESS) {
492 DEBUGMSGTL(("ucd-snmp/lmSensors",
493 "sensor info lookup failed in read_enum_sensor - error code->%d\n", error_code));
494 return(error_code);
495 }
496
497 error_code = picl_get_propval(sensorh, state, sensor_info.size);
498
499 if (error_code != PICL_SUCCESS) {
500 DEBUGMSGTL(("ucd-snmp/lmSensors",
501 "sensor value lookup failed in read_enum_sensor - error code->%d\n", error_code));
502 return(error_code);
503 }
504
505 /* Start with error value, then try to fill in something better.
506 Use case-insensitive match to find the right value since platforms
507 may return either case.
508 */
509
510 *value = 99;
511
512 for (i = 0; options[i] != NULL; i++){
513 if (strncasecmp(state, options[i], strlen(options[i])) == 0){
514 *value = i;
515 break;
516 }
517 }
518
519 DEBUGMSGTL(("ucd-snmp/lmSensors", "read_enum_sensor value is %d\n", *value));
520 return(error_code);
521 } /* end of read_enum_sensor() */
522
523 /* scale variable handles miniVolts*/
524
525 static void
526 process_num_sensor(picl_nodehdl_t childh,
527 const char propname[PICL_PROPNAMELEN_MAX],
528 const char propval[PICL_PROPNAMELEN_MAX], int typ, int scale)
529 {
530 int value = 0;
531 picl_errno_t error_code;
532
533 if (sensor_array[typ].n >= MAX_SENSORS){
534 DEBUGMSGTL(("ucd-snmp/lmSensors",
535 "There are too many sensors of type %d\n",typ));
536 } else{
537 error_code = read_num_sensor(childh, propval, scale, &value);
538
539 if (error_code == PICL_SUCCESS) {
540 sensor_array[typ].sensor[sensor_array[typ].n].value = value;
541 snprintf(sensor_array[typ].sensor[sensor_array[typ].n].name,
542 (PICL_PROPNAMELEN_MAX - 1),"%s",propname);
543 sensor_array[typ].sensor[sensor_array[typ].n].
544 name[PICL_PROPNAMELEN_MAX - 1] = '\0';
545 sensor_array[typ].n++;
546 } else
547 DEBUGMSGTL(("ucd-snmp/lmSensors",
548 "read of %s in process_num_sensor returned error code %d\n", propname, error_code));
549 }
550 } /* end process_num_sensor() */
551
552 static void
553 process_enum_sensor(picl_nodehdl_t childh,
554 const char propname[PICL_PROPNAMELEN_MAX],
555 int typ, const char **options)
556 {
557 int value = 0;
558 picl_errno_t error_code;
559
560 if (sensor_array[typ].n >= MAX_SENSORS){
561 DEBUGMSGTL(("ucd-snmp/lmSensors",
562 "There are too many sensors of type %d\n",typ));
563 } else{
564 error_code = read_enum_sensor(childh, options, &value);
565
566 if (error_code == PICL_SUCCESS) {
567 sensor_array[typ].sensor[sensor_array[typ].n].value = value;
568 snprintf(sensor_array[typ].sensor[sensor_array[typ].n].name,
569 (PICL_PROPNAMELEN_MAX - 1),"%s",propname);
570 sensor_array[typ].sensor[sensor_array[typ].n].
571 name[PICL_PROPNAMELEN_MAX - 1] = '\0';
572 sensor_array[typ].n++;
573 } else
574 DEBUGMSGTL(("ucd-snmp/lmSensors",
575 "read of %s in process_enum_sensor returned error code %d\n", propname, error_code));
576 }
577 } /* end process_enum_sensor() */
578
579 /* The following are modules for dealing with individual sensors types.
580 They call the generic modules above. */
581
582 static void
583 process_individual_fan(picl_nodehdl_t childh,
584 const char propname[PICL_PROPNAMELEN_MAX])
585 {
586 process_num_sensor(childh, propname, "AtoDSensorValue", FAN_TYPE, 1);
587 }
588
589
590 static void
591 process_newtype_fan(picl_nodehdl_t childh,
592 const char propname[PICL_PROPNAMELEN_MAX])
593 {
594 process_num_sensor(childh, propname, "Speed", FAN_TYPE, 1);
595 }
596
597
598 static void
599 process_temperature_sensor(picl_nodehdl_t childh,
600 const char propname[PICL_PROPNAMELEN_MAX])
601 {
602 process_num_sensor(childh, propname, "Temperature", TEMP_TYPE, 1000);
603 } /* MIB asks for mC */
604
605 static void
606 process_voltage_sensor(picl_nodehdl_t childh,
607 const char propname[PICL_PROPNAMELEN_MAX])
608 {
609 process_num_sensor(childh, propname, "Voltage", VOLT_TYPE, 1000);
610 } /* MIB asks for mV */
611
612 static void
613 process_digital_sensor(picl_nodehdl_t childh,
614 const char propname[PICL_PROPNAMELEN_MAX])
615 {
616 process_num_sensor(childh, propname, "AtoDSensorValue", VOLT_TYPE, 1);
617 }
618
619
620 static void
621 process_switch(picl_nodehdl_t childh,
622 const char propname[PICL_PROPNAMELEN_MAX])
623 {
624
625 const char *settings[]={"OFF","ON","NORMAL","LOCKED","UNKNOWN",
626 "DIAG","SECURE",NULL};
627
628 process_enum_sensor(childh, propname, MISC_TYPE, settings);
629 }
630
631 static void
632 process_led(picl_nodehdl_t childh,
633 const char propname[PICL_PROPNAMELEN_MAX])
634 {
635
636 const char *settings[]={"OFF","ON","BLINK",NULL};
637 process_enum_sensor(childh, propname, MISC_TYPE, settings);
638 }
639
640 static void
641 process_i2c(picl_nodehdl_t childh,
642 const char propname[PICL_PROPNAMELEN_MAX])
643 {
644 const char *settings[]={"OK",NULL};
645 process_enum_sensor(childh, propname, MISC_TYPE, settings);
646 }
647
648 /* walks its way recusively through the tree of sensors */
649
650 static int
651 process_sensors(int level, picl_nodehdl_t nodeh)
652 {
653 picl_nodehdl_t childh;
654 picl_nodehdl_t nexth;
655
656 char propname[PICL_PROPNAMELEN_MAX];
657 char propclass[PICL_CLASSNAMELEN_MAX];
658 picl_errno_t error_code;
659
660 level++;
661
662 DEBUGMSGTL(("ucd-snmp/lmSensors","in process_sensors() level %d\n",level));
663
664 /* look up first child node */
665 error_code = picl_get_propval_by_name(nodeh, PICL_PROP_CHILD, &childh,
666 sizeof (picl_nodehdl_t));
667 if (error_code != PICL_SUCCESS) {
668 DEBUGMSGTL(("ucd-snmp/lmSensors",
669 "picl_get_propval_by_name(%s) %d\n",
670 PICL_PROP_CHILD, error_code));
671 return (error_code);
672 }
673
674 /* step through child nodes, get the name first */
675 while (error_code == PICL_SUCCESS) {
676
677 error_code = picl_get_propval_by_name(childh, PICL_PROP_NAME,
678 propname, (PICL_PROPNAMELEN_MAX - 1));
679 if (error_code != PICL_SUCCESS) { /*we found a node with no name. Impossible.! */
680 DEBUGMSGTL(("ucd-snmp/lmSensors",
681 "picl_get_propval_by_name(%s) = %d\n",
682 PICL_PROP_NAME, error_code));
683 return (error_code);
684 }
685
686 error_code = picl_get_propval_by_name(childh, PICL_PROP_CLASSNAME,
687 propclass, sizeof (propclass));
688 if (error_code != PICL_SUCCESS) { /*we found a node with no class. Impossible.! */
689 DEBUGMSGTL(("ucd-snmp/lmSensors",
690 "picl_get_propval_by_name(%s) = %d\n",
691 PICL_PROP_CLASSNAME, error_code));
692 return (error_code);
693 }
694
695 DEBUGMSGTL(("ucd-snmp/lmSensors","found %s of class %s\n",propname,propclass));
696
697 if (strstr(propclass,"fan-tachometer"))
698 process_individual_fan(childh,propname);
699 else if (strstr(propclass,"fan"))
700 process_newtype_fan(childh,propname);
701 else if (strstr(propclass,"temperature-sensor"))
702 process_temperature_sensor(childh,propname);
703 else if (strstr(propclass,"voltage-sensor"))
704 process_voltage_sensor(childh,propname);
705 else if (strstr(propclass,"digital-sensor"))
706 process_digital_sensor(childh,propname);
707 else if (strstr(propclass,"switch"))
708 process_switch(childh,propname);
709 else if (strstr(propclass,"led"))
710 process_led(childh,propname);
711 else if (strstr(propclass,"i2c"))
712 process_i2c(childh,propname);
713 /*
714 else if (strstr(propclass,"gpio"))
715 process_gpio(childh,propname);
716 */
717
718
719 /* look for children of children (note, this is recursive) */
720 if (!(strstr(propclass,"picl") &&
721 (strstr(propname,"frutree") || strstr(propname,"obp")))) {
722 error_code = process_sensors(level,childh);
723 DEBUGMSGTL(("ucd-snmp/lmSensors",
724 "process_sensors(%s) returned %d\n",
725 propname, error_code));
726 }
727
728 /* get next child node at this level*/
729 error_code = picl_get_propval_by_name(childh, PICL_PROP_PEER,
730 &nexth, sizeof (picl_nodehdl_t));
731 if (error_code != PICL_SUCCESS) {/* no more children - buh bye*/
732 DEBUGMSGTL(("ucd-snmp/lmSensors","Process sensors is out of children! Returning...\n"));
733 return (error_code);
734 }
735
736 childh = nexth;
737
738 } /* while */
739 return (error_code);
740 } /* process sensors */
741
742 #endif
743 /* ******** end of picld sensor procedures * */
744
745 #endif /* solaris2 */
746 static int
747 _sensor_load(time_t t)
748 {
749 #ifdef solaris2
750 int i,j;
751 #ifdef HAVE_PICL_H
752 int er_code;
753 picl_errno_t error_code;
754 int level=0;
755 picl_nodehdl_t rooth;
756 #else
757 int typ;
758 int temp=0; /* do not reset this later, more than one typ has temperatures*/
759 int other=0;
760 const char *fantypes[]={"CPU","PWR","AFB"};
761 kstat_ctl_t *kc;
762 kstat_t *kp;
763 envctrl_fan_t *fan_info;
764 envctrl_ps_t *power_info;
765 envctrl_encl_t *enc_info;
766 #endif
767
768 /* DEBUGMSG(("ucd-snmp/lmSensors", "Reading the sensors\n")); */
769
770 /* initialize the array */
771 for (i = 0; i < N_TYPES; i++){
772 sensor_array[i].n = 0;
773 for (j=0; j < MAX_SENSORS; j++){
774 sensor_array[i].sensor[j].name[0] = '\0';
775 sensor_array[i].sensor[j].value = 0;
776 }
777 } /*end for i*/
778
779 /* try picld (if supported), if that doesn't work, try kstat */
780 #ifdef HAVE_PICL_H
781
782 er_code = picl_initialize();
783
784 if (er_code == PICL_SUCCESS) {
785
786 error_code = picl_get_root(&rooth);
787
788 if (error_code != PICL_SUCCESS) {
789 DEBUGMSG(("ucd-snmp/lmSensors", "picld couldn't get root error code->%d\n",error_code));
790 }
791 else{
792 DEBUGMSGTL(("ucd-snmp/lmSensors", "found root\n"));
793 error_code = process_sensors(level,rooth);
794 if (error_code != 255)
795 if (error_code != 7)
796 DEBUGMSG(("ucd-snmp/lmSensors", "picld had an internal problem error code->%d\n",error_code));
797 } /* end else */
798
799 picl_shutdown();
800
801 } /* end if err_code for picl_initialize */
802
803 else {
804 DEBUGMSG(("ucd-snmp/lmSensors", "picld couldn't initialize picld because error code->%d\n",er_code));
805
806 } /*end else picl_initialize */
807
808 #else /* end of picld section */
809 /* initialize kstat */
810
811 kc = kstat_open();
812 if (kc == 0) {
813 DEBUGMSG(("ucd-snmp/lmSensors", "couldn't open kstat"));
814 } /* endif kc */
815 else{
816 temp = 0;
817 kp = kstat_lookup(kc, ENVCTRL_MODULE_NAME, 0, ENVCTRL_KSTAT_FANSTAT);
818 if (kp == 0) {
819 DEBUGMSGTL(("ucd-snmp/lmSensors", "couldn't lookup fan kstat\n"));
820 } /* endif lookup fans */
821 else{
822 if (kstat_read(kc, kp, 0) == -1) {
823 DEBUGMSGTL(("ucd-snmp/lmSensors", "couldn't read fan kstat"));
824 } /* endif kstatread fan */
825 else{
826 typ = 1;
827 fan_info = (envctrl_fan_t *) kp->ks_data;
828 sensor_array[typ].n = kp->ks_ndata;
829 for (i=0; i < kp->ks_ndata; i++){
830 DEBUGMSG(("ucd-snmp/lmSensors", "found instance %d fan type %d speed %d OK %d bustedfan %d\n",
831 fan_info->instance, fan_info->type,fan_info->fanspeed,fan_info->fans_ok,fan_info->fanflt_num));
832 sensor_array[typ].sensor[i].value = fan_info->fanspeed;
833 snprintf(sensor_array[typ].sensor[i].name,(MAX_NAME - 1),
834 "fan type %s number %d",fantypes[fan_info->type],fan_info->instance);
835 sensor_array[typ].sensor[i].name[MAX_NAME - 1] = '\0';
836 fan_info++;
837 } /* end for fan_info */
838 } /* end else kstatread fan */
839 } /* end else lookup fans*/
840
841
842 kp = kstat_lookup(kc, ENVCTRL_MODULE_NAME, 0, ENVCTRL_KSTAT_PSNAME);
843 if (kp == 0) {
844 DEBUGMSGTL(("ucd-snmp/lmSensors", "couldn't lookup power supply kstat\n"));
845 } /* endif lookup power supply */
846 else{
847 if (kstat_read(kc, kp, 0) == -1) {
848 DEBUGMSGTL(("ucd-snmp/lmSensors", "couldn't read power supply kstat\n"));
849 } /* endif kstatread fan */
850 else{
851 typ = 0; /* this is a power supply temperature, not a voltage*/
852 power_info = (envctrl_ps_t *) kp->ks_data;
853 sensor_array[typ].n = kp->ks_ndata;
854 for (i=0; i < kp->ks_ndata; i++){
855 DEBUGMSG(("ucd-snmp/lmSensors", "found instance %d psupply temp mC %d %dW OK %d share %d limit %d\n",
856 power_info->instance, power_info->ps_tempr*1000,power_info->ps_rating,
857 power_info->ps_ok,power_info->curr_share_ok,power_info->limit_ok));
858 sensor_array[typ].sensor[temp].value = power_info->ps_tempr*1000;
859 snprintf(sensor_array[typ].sensor[temp].name,(MAX_NAME-1),
860 "power supply %d",power_info->instance);
861 sensor_array[typ].sensor[temp].name[MAX_NAME - 1] = '\0';
862 power_info++; /* increment the data structure */
863 temp++; /* increment the temperature sensor array element */
864 } /* end for power_info */
865 } /* end else kstatread power supply */
866 } /* end else lookup power supplies*/
867
868 kp = kstat_lookup(kc, ENVCTRL_MODULE_NAME, 0, ENVCTRL_KSTAT_ENCL);
869 if (kp == 0) {
870 DEBUGMSGTL(("ucd-snmp/lmSensors", "couldn't lookup enclosure kstat\n"));
871 } /* endif lookup enclosure */
872 else{
873 if (kstat_read(kc, kp, 0) == -1) {
874 DEBUGMSGTL(("ucd-snmp/lmSensors", "couldn't read enclosure kstat\n"));
875 } /* endif kstatread enclosure */
876 else{
877 enc_info = (envctrl_encl_t *) kp->ks_data;
878 other = 0;
879 for (i=0; i < kp->ks_ndata; i++){
880 switch (enc_info->type){
881 case ENVCTRL_ENCL_FSP:
882 DEBUGMSG(("ucd-snmp/lmSensors", "front panel value %d\n",enc_info->value));
883 typ = 3; /* misc */
884 sensor_array[typ].sensor[other].value = enc_info->value;
885 strlcpy(sensor_array[typ].sensor[other].name, "FSP",
886 MAX_NAME);
887 other++;
888 break;
889 case ENVCTRL_ENCL_AMBTEMPR:
890 DEBUGMSG(("ucd-snmp/lmSensors", "ambient temp mC %d\n",enc_info->value*1000));
891 typ = 0; /* temperature sensor */
892 sensor_array[typ].sensor[temp].value = enc_info->value*1000;
893 strlcpy(sensor_array[typ].sensor[temp].name, "Ambient",
894 MAX_NAME);
895 temp++;
896 break;
897 case ENVCTRL_ENCL_BACKPLANE4:
898 DEBUGMSG(("ucd-snmp/lmSensors", "There is a backplane4\n"));
899 typ = 3; /* misc */
900 sensor_array[typ].sensor[other].value = enc_info->value;
901 strlcpy(sensor_array[typ].sensor[other].name, "Backplane4",
902 MAX_NAME);
903 other++;
904 break;
905 case ENVCTRL_ENCL_BACKPLANE8:
906 DEBUGMSG(("ucd-snmp/lmSensors", "There is a backplane8\n"));
907 typ = 3; /* misc */
908 sensor_array[typ].sensor[other].value = enc_info->value;
909 strlcpy(sensor_array[typ].sensor[other].name, "Backplane8",
910 MAX_NAME);
911 other++;
912 break;
913 case ENVCTRL_ENCL_CPUTEMPR:
914 DEBUGMSG(("ucd-snmp/lmSensors", "CPU%d temperature mC %d\n",enc_info->instance,enc_info->value*1000));
915 typ = 0; /* temperature sensor */
916 sensor_array[typ].sensor[temp].value = enc_info->value*1000;
917 snprintf(sensor_array[typ].sensor[temp].name,MAX_NAME,"CPU%d",enc_info->instance);
918 sensor_array[typ].sensor[temp].name[MAX_NAME-1]='\0'; /* null terminate */
919 temp++;
920 break;
921 default:
922 DEBUGMSG(("ucd-snmp/lmSensors", "unknown element instance %d type %d value %d\n",
923 enc_info->instance, enc_info->type, enc_info->value));
924 break;
925 } /* end switch */
926 enc_info++;
927 } /* end for enc_info */
928 sensor_array[3].n = other;
929 sensor_array[0].n = temp;
930 } /* end else kstatread enclosure */
931 } /* end else lookup enclosure*/
932
933 kstat_close(kc);
934
935 } /* end else kstat */
936 #endif
937
938 #else /* end solaris2 only ie. ifdef everything else */
939
940 const sensors_chip_name *chip;
•941 const sensors_feature *feature;
942 const sensors_subfeature *subfeature;
943 int chip_nr = 0;
944 unsigned int i = 0;
945
946 DEBUGMSG(("ucd-snmp/lmSensors", "=> sensor_load\n"));
947
948 for (i = 0; i < N_TYPES; i++)
949 {
950 sensor_array[i].n = 0;
951 sensor_array[i].current_len = 0;
952
953 /* Malloc the default number of sensors. */
954 sensor_array[i].sensor = (_sensor*)malloc(sizeof(_sensor) * DEFAULT_SENSORS);
955 if (sensor_array[i].sensor == NULL)
956 {
957 /* Continuing would be unsafe */
958 snmp_log(LOG_ERR, "Cannot malloc sensor array!");
959 return 1;
960 } /* end if */
961 sensor_array[i].current_len = DEFAULT_SENSORS;
962 } /* end for */
963
•964 while ((chip = sensors_get_detected_chips(NULL, &chip_nr))) {
965 int a = 0;
966 while ((feature = sensors_get_features(chip, &a))) {
967 int b = 0;
968 while ((subfeature = sensors_get_all_subfeatures(chip,
969 feature, &b))) {
970 char *label = NULL;
971 double val;
972
973 if ((subfeature->flags & SENSORS_MODE_R) &&
974 (label = sensors_get_label(chip, feature)) &&
975 !sensors_get_value(chip, subfeature->number, &val)) {
976 int type = -1;
977 float mul;
978 _sensor_array *array;
979
980 /* The label, as determined for a given chip in
981 * sensors.conf, is used to place each sensor in the
982 * appropriate bucket. Volt, Fan, Temp, and Misc.
983 * If the text being looked for below is not in the
984 * label of a given sensor (e.g., the temp1 sensor
985 * has been labeled 'CPU' and not 'CPU temp') it
986 * will end up being lumped in the MISC bucket. */
987
988 if (strstr(label, "V")) {
989 type = VOLT_TYPE;
990 mul = 1000.0;
991 }
992 if (strstr(label, "fan") || strstr(label, "Fan")) {
993 type = FAN_TYPE;
994 mul = 1.0;
995 }
996 if (strstr(label, "temp") || strstr(label, "Temp")) {
997 type = TEMP_TYPE;
998 mul = 1000.0;
999 }
1000 if (type == -1) {
1001 type = MISC_TYPE;
1002 mul = 1000.0;
1003 }
1004
1005 array = &sensor_array[type];
1006 if ( array->current_len <= array->n) {
1007 _sensor* old_buffer = array->sensor;
1008 size_t new_size = (sizeof(_sensor) * array->current_len) + (sizeof(_sensor) * DEFAULT_SENSORS);
1009 array->sensor = (_sensor*)realloc(array->sensor, new_size);
1010 if (array->sensor == NULL)
1011 {
1012 /* Continuing would be unsafe */
1013 snmp_log(LOG_ERR, "too many sensors to fit, and failed to alloc more, failing on %s\n", label);
1014 free(old_buffer);
1015 old_buffer = NULL;
1016 if (label) {
1017 free(label);
1018 label = NULL;
1019 } /* end if label */
1020 return (rc=1);
1021 } /* end if array->sensor */
1022 array->current_len = new_size / sizeof(_sensor);
1023 DEBUGMSG(("ucd-snmp/lmSensors", "type #%d increased to %d elements\n", type, array->current_len));
1024 } /* end if array->current */
1025 strncpy(array->sensor[array->n].name, label, MAX_NAME);
1026 array->sensor[array->n].value = (int) (val * mul);
1027 DEBUGMSGTL(("sensors","sensor %d, value %d\n",
1028 array->sensor[array->n].name,
1029 array->sensor[array->n].value));
1030 array->n++;
1031 } /* end if data-mode */
1032 if (label) {
1033 free(label);
1034 label = NULL;
1035 } /* end if label */
1036 } /* end while subfeature */
1037 } /* end while feature */
1038 } /* end while chip */
1039 DEBUGMSG(("ucd-snmp/lmSensors", "<= sensor_load\n"));
1040 #endif /* end else ie. ifdef everything else */
1041 /* Update the timestamp after a load. */
1042 timestamp = t;
1043 return 0;
1044 }
1045
1046 #ifndef solaris2
1047 /* Free all the sensor arrays. */
1048 static void
1049 free_sensor_arrays()
1050 {
1051 unsigned int i = 0;
1052 DEBUGMSG(("ucd-snmp/lmSensors", "=> free_sensor_arrays\n"));
1053 for (i = 0; i < N_TYPES; i++){
1054 if (sensor_array[i].sensor != NULL)
1055 {
1056 free(sensor_array[i].sensor);
1057 sensor_array[i].sensor = NULL;
1058 }
1059 /* For good measure, reset the other values. */
1060 sensor_array[i].n = 0;
1061 sensor_array[i].current_len = 0;
1062 }
1063 DEBUGMSG(("ucd-snmp/lmSensors", "<= free_sensor_arrays\n"));
1064 }
1065 #endif