| 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 |
#include <time.h> |
| 8 |
#include <sensors/sensors.h> |
| 9 |
|
| 10 |
|
| 11 |
void netsnmp_sensor_arch_init( void ) { |
| 12 |
FILE *fp = fopen("/etc/sensors.conf", "r"); |
| 13 |
DEBUGMSGTL(("sensors:arch", "Initialise v3 LM Sensors module\n")); |
| 14 |
sensors_init( fp ); |
| 15 |
} |
| 16 |
|
| 17 |
int |
| 18 |
netsnmp_sensor_arch_load(netsnmp_cache *cache, void *vp) { |
| 19 |
netsnmp_sensor_info *sp; |
| 20 |
const sensors_chip_name *chip; |
| 21 |
const sensors_feature *data; |
| 22 |
const sensors_subfeature *data2; |
| 23 |
int chip_nr = 0; |
| 24 |
|
| 25 |
DEBUGMSGTL(("sensors:arch", "Reload v3 LM Sensors module\n")); |
| 26 |
while ((chip = sensors_get_detected_chips( NULL, &chip_nr))) { |
| 27 |
int a = 0; |
| 28 |
|
| 29 |
while ((data = sensors_get_features( chip, &a))) { |
| 30 |
int b = 0; |
| 31 |
|
| 32 |
DEBUGMSGTL(("sensors:arch:detail", "get_features (%s, %d)\n", data->name, data->number)); |
| 33 |
|
| 34 |
while ((data2 = sensors_get_all_subfeatures( chip, data, &b))) { |
| 35 |
char *label = NULL; |
| 36 |
double val = 0; |
| 37 |
int type = NETSNMP_SENSOR_TYPE_OTHER; |
| 38 |
|
| 39 |
DEBUGMSGTL(("sensors:arch:detail", " get_subfeatures (%s, %d)\n", data2->name, data2->number)); |
| 40 |
/* |
| 41 |
* Check the type of this subfeature, |
| 42 |
* concentrating on the main "input" measurements. |
| 43 |
*/ |
| 44 |
switch ( data2->type ) { |
| 45 |
case SENSORS_SUBFEATURE_IN_INPUT: |
| 46 |
type = NETSNMP_SENSOR_TYPE_VOLTAGE_DC; |
| 47 |
break; |
| 48 |
case SENSORS_SUBFEATURE_FAN_INPUT: |
| 49 |
type = NETSNMP_SENSOR_TYPE_RPM; |
| 50 |
break; |
| 51 |
case SENSORS_SUBFEATURE_TEMP_INPUT: |
| 52 |
type = NETSNMP_SENSOR_TYPE_TEMPERATURE; |
| 53 |
break; |
| 54 |
case SENSORS_SUBFEATURE_VID: |
| 55 |
type = NETSNMP_SENSOR_TYPE_VOLTAGE_DC; |
| 56 |
break; |
| 57 |
default: |
| 58 |
/* Skip everything other than these basic sensor features - ??? */ |
| 59 |
DEBUGMSGTL(("sensors:arch:detail", " Skip type %x\n", data2->type)); |
| 60 |
continue; |
| 61 |
} |
| 62 |
|
| 63 |
/* |
| 64 |
* Get the name and value of this subfeature |
| 65 |
*/ |
| 66 |
/* |
| 67 |
if (!(label = sensors_get_label(chip, data))) { |
| 68 |
DEBUGMSGTL(("sensors:arch:detail", " Can't get name (%s)\n", label)); |
| 69 |
continue; |
| 70 |
} |
| 71 |
if (sensors_get_value(chip, data2->number, &val) < 0) { |
| 72 |
DEBUGMSGTL(("sensors:arch:detail", " Can't get value (%f)\n", val)); |
| 73 |
continue; |
| 74 |
} |
| 75 |
*/ |
| 76 |
if (!(label = sensors_get_label(chip, data)) || |
| 77 |
(sensors_get_value(chip, data2->number, &val) < 0)) { |
| 78 |
DEBUGMSGTL(("sensors:arch:detail", " Can't get name/value (%s, %f)\n", label, val)); |
| 79 |
free(label); |
| 80 |
label = NULL; |
| 81 |
continue; |
| 82 |
} |
| 83 |
DEBUGMSGTL(("sensors:arch:detail", "%s = %f\n", label, val)); |
| 84 |
|
| 85 |
/* |
| 86 |
* Use this type to create a new sensor entry |
| 87 |
* (inserting it in the appropriate sub-containers) |
| 88 |
*/ |
| 89 |
sp = sensor_by_name( label, type ); |
| 90 |
if ( sp && sp->flags & NETSNMP_SENSOR_FLAG_ACTIVE) { |
| 91 |
/* |
| 92 |
* Some HW does not have unique sensors labels. |
| 93 |
* We already have a sensor with this label, thus |
| 94 |
* try to create unique label by adding chip-name prefix |
| 95 |
* and try again. |
| 96 |
*/ |
| 97 |
char chip_name[64]; |
| 98 |
char new_label[128]; |
| 99 |
int ret; |
| 100 |
DEBUGMSGTL(("sensors:arch:detail", "Already know label %s, adding prefix\n", label)); |
| 101 |
ret = sensors_snprintf_chip_name(chip_name, sizeof(chip_name), chip); |
| 102 |
if (ret < 0) { |
| 103 |
DEBUGMSGTL(("sensors:arch:detail", "Can't get chip name for label %s\n", label)); |
| 104 |
free(label); |
| 105 |
continue; |
| 106 |
} |
| 107 |
snprintf(new_label, sizeof(new_label), "%s:%s", chip_name, label); |
| 108 |
DEBUGMSGTL(("sensors:arch:detail", "New label: %s\n", new_label)); |
| 109 |
sp = sensor_by_name( new_label, type ); |
| 110 |
} |
| 111 |
if (sp) { |
| 112 |
sp->value = val; |
| 113 |
sp->flags|= NETSNMP_SENSOR_FLAG_ACTIVE; |
| 114 |
} |
| 115 |
if (label) { |
| 116 |
free(label); |
| 117 |
label = NULL; |
| 118 |
} |
| 119 |
} /* end while data2 */ |
| 120 |
} /* end while data */ |
| 121 |
} /* end while chip */ |
| 122 |
|
| 123 |
return 0; |
| 124 |
} |