Debian Codegraph @

agent/mibgroup/ucd-snmp/lmSensors.c

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