# BRCM_VERSION=3
[bcm963xx.git] / userapps / opensource / net-snmp / agent / mibgroup / ucd-snmp / lmSensors.c
1 /*
2  * This file was generated by mib2c and is intended for use as a mib module
3  * for the ucd-snmp snmpd agent. 
4  */
5
6
7 /*
8  * This should always be included first before anything else 
9  */
10 #include <net-snmp/net-snmp-config.h>
11
12 #include <net-snmp/net-snmp-includes.h>
13 #include <net-snmp/agent/net-snmp-agent-includes.h>
14
15 /*
16  * minimal include directives 
17  */
18 #include "util_funcs.h"
19
20
21 #include <time.h>
22 #include <sensors/sensors.h>
23
24 #include "lmSensors.h"
25
26 #define N_TYPES      (4)
27 #define MAX_NAME     (64)
28 #define MAX_SENSORS  (32)
29
30 /*
31  * lmSensors_variables_oid:
32  *   this is the top level oid that we want to register under.  This
33  *   is essentially a prefix, with the suffix appearing in the
34  *   variable below.
35  */
36
37
38 oid             lmSensors_variables_oid[] =
39     { 1, 3, 6, 1, 4, 1, 2021, 13, 16 };
40
41 /*
42  * variable4 lmSensors_variables:
43  *   this variable defines function callbacks and type return information 
44  *   for the lmSensors mib section 
45  */
46
47 struct variable4 lmSensors_variables[] = {
48     /*
49      * magic number        , variable type , ro/rw , callback fn  , L, oidsuffix 
50      */
51 #define   LMTEMPSENSORSINDEX    3
52     {LMTEMPSENSORSINDEX, ASN_INTEGER, RONLY, var_lmSensorsTable, 3,
53      {2, 1, 1}},
54 #define   LMTEMPSENSORSDEVICE   4
55     {LMTEMPSENSORSDEVICE, ASN_OCTET_STR, RONLY, var_lmSensorsTable, 3,
56      {2, 1, 2}},
57 #define   LMTEMPSENSORSVALUE    5
58     {LMTEMPSENSORSVALUE, ASN_GAUGE, RONLY, var_lmSensorsTable, 3,
59      {2, 1, 3}},
60 #define   LMFANSENSORSINDEX     8
61     {LMFANSENSORSINDEX, ASN_INTEGER, RONLY, var_lmSensorsTable, 3,
62      {3, 1, 1}},
63 #define   LMFANSENSORSDEVICE    9
64     {LMFANSENSORSDEVICE, ASN_OCTET_STR, RONLY, var_lmSensorsTable, 3,
65      {3, 1, 2}},
66 #define   LMFANSENSORSVALUE     10
67     {LMFANSENSORSVALUE, ASN_GAUGE, RONLY, var_lmSensorsTable, 3,
68      {3, 1, 3}},
69 #define   LMVOLTSENSORSINDEX    13
70     {LMVOLTSENSORSINDEX, ASN_INTEGER, RONLY, var_lmSensorsTable, 3,
71      {4, 1, 1}},
72 #define   LMVOLTSENSORSDEVICE   14
73     {LMVOLTSENSORSDEVICE, ASN_OCTET_STR, RONLY, var_lmSensorsTable, 3,
74      {4, 1, 2}},
75 #define   LMVOLTSENSORSVALUE    15
76     {LMVOLTSENSORSVALUE, ASN_GAUGE, RONLY, var_lmSensorsTable, 3,
77      {4, 1, 3}},
78 #define   LMMISCSENSORSINDEX    18
79     {LMMISCSENSORSINDEX, ASN_INTEGER, RONLY, var_lmSensorsTable, 3,
80      {5, 1, 1}},
81 #define   LMMISCSENSORSDEVICE   19
82     {LMMISCSENSORSDEVICE, ASN_OCTET_STR, RONLY, var_lmSensorsTable, 3,
83      {5, 1, 2}},
84 #define   LMMISCSENSORSVALUE    20
85     {LMMISCSENSORSVALUE, ASN_GAUGE, RONLY, var_lmSensorsTable, 3,
86      {5, 1, 3}},
87 };
88
89 typedef struct {
90     char            name[MAX_NAME];
91     int             value;
92 } _sensor;
93
94 typedef struct {
95     int             n;
96     _sensor         sensor[MAX_SENSORS];
97 } _sensor_array;
98
99 static _sensor_array sensor_array[N_TYPES];
100 static clock_t  timestamp;
101
102
103 static int      sensor_init(void);
104 static void     sensor_load(void);
105 static void     _sensor_load(clock_t t);
106
107 #define CONFIG_FILE_NAME "/etc/sensors.conf"
108
109 /*
110  * init_lmSensors():
111  *   Initialization routine.  This is called when the agent starts up.
112  *   At a minimum, registration of your variables should take place here.
113  */
114 void
115 init_lmSensors(void)
116 {
117     sensor_init();
118
119     /*
120      * register ourselves with the agent to handle our mib tree 
121      */
122     REGISTER_MIB("lmSensors", lmSensors_variables, variable4,
123                  lmSensors_variables_oid);
124 }
125
126 /*
127  * var_lmSensorsTable():
128  *   Handle this table separately from the scalar value case.
129  *   The workings of this are basically the same as for var_lmSensors above.
130  */
131 unsigned char  *
132 var_lmSensorsTable(struct variable *vp,
133                    oid * name,
134                    size_t * length,
135                    int exact,
136                    size_t * var_len, WriteMethod ** write_method)
137 {
138     static long     long_ret;
139     static unsigned char string[SPRINT_MAX_LEN];
140     int             i;
141
142     int             s_index = name[*length - 1] - 1;
143     int             s_type = -1;
144     int             n_sensors;
145
146     _sensor         s;
147
148     sensor_load();
149
150     switch (vp->magic) {
151     case LMTEMPSENSORSINDEX:
152     case LMTEMPSENSORSDEVICE:
153     case LMTEMPSENSORSVALUE:
154         s_type = 0;
155         n_sensors = sensor_array[0].n;
156         break;
157
158     case LMFANSENSORSINDEX:
159     case LMFANSENSORSDEVICE:
160     case LMFANSENSORSVALUE:
161         s_type = 1;
162         n_sensors = sensor_array[1].n;
163         break;
164
165     case LMVOLTSENSORSINDEX:
166     case LMVOLTSENSORSDEVICE:
167     case LMVOLTSENSORSVALUE:
168         s_type = 2;
169         n_sensors = sensor_array[2].n;
170         break;
171
172     case LMMISCSENSORSINDEX:
173     case LMMISCSENSORSDEVICE:
174     case LMMISCSENSORSVALUE:
175         s_type = 3;
176         n_sensors = sensor_array[3].n;
177         break;
178
179     default:
180         s_type = -1;
181         n_sensors = 0;
182     }
183
184     if (header_simple_table(vp, name, length, exact,
185                             var_len, write_method,
186                             n_sensors) == MATCH_FAILED)
187         return NULL;
188
189     if (s_type < 0)
190         return NULL;
191
192     s = sensor_array[s_type].sensor[s_index];
193
194     switch (vp->magic) {
195     case LMTEMPSENSORSINDEX:
196     case LMFANSENSORSINDEX:
197     case LMVOLTSENSORSINDEX:
198     case LMMISCSENSORSINDEX:
199         long_ret = s_index;
200         return (unsigned char *) &long_ret;
201
202     case LMTEMPSENSORSDEVICE:
203     case LMFANSENSORSDEVICE:
204     case LMVOLTSENSORSDEVICE:
205     case LMMISCSENSORSDEVICE:
206         strncpy(string, s.name, SPRINT_MAX_LEN - 1);
207         *var_len = strlen(string);
208         return (unsigned char *) string;
209
210     case LMTEMPSENSORSVALUE:
211     case LMFANSENSORSVALUE:
212     case LMVOLTSENSORSVALUE:
213     case LMMISCSENSORSVALUE:
214         long_ret = s.value;
215         return (unsigned char *) &long_ret;
216
217     default:
218         ERROR_MSG("Unable to handle table request");
219     }
220
221     return NULL;
222 }
223
224 static int
225 sensor_init(void)
226 {
227     int             res;
228     char            filename[] = CONFIG_FILE_NAME;
229     clock_t         t = clock();
230
231     FILE           *fp = fopen(filename, "r");
232     if (!fp)
233         return 1;
234
235     if (res = sensors_init(fp))
236         return 2;
237
238     _sensor_load(t);
239     return 0;
240 }
241
242 static void
243 sensor_load(void)
244 {
245     clock_t         t = clock();
246
247     if (t > timestamp + 60)
248         _sensor_load(t);
249
250     return;
251 }
252
253 static void
254 _sensor_load(clock_t t)
255 {
256     const sensors_chip_name *chip;
257     const sensors_feature_data *data;
258     int             chip_nr = 0;
259     int             a = 0;
260     int             b = 0;
261
262     int             i;
263     for (i = 0; i < N_TYPES; i++)
264         sensor_array[i].n = 0;
265
266     while (chip = sensors_get_detected_chips(&chip_nr)) {
267         while (data = sensors_get_all_features(*chip, &a, &b)) {
268             char           *label;
269             double          val;
270
271             if ((data->mode & SENSORS_MODE_R) &&
272                 (data->mapping == SENSORS_NO_MAPPING) &&
273                 !sensors_get_label(*chip, data->number, &label) &&
274                 !sensors_get_feature(*chip, data->number, &val)) {
275                 int             type;
276                 float           mul;
277                 _sensor_array  *array;
278
279                 if (strstr(label, "temp")) {
280                     type = 0;
281                     mul = 1000.0;
282                 } else if (strstr(label, "fan")) {
283                     type = 1;
284                     mul = 1.0;
285                 } else if (strstr(label, "V")) {
286                     type = 2;
287                     mul = 1000.0;
288                 } else {
289                     type = 3;
290                     mul = 1000.0;
291                 }
292
293                 array = &sensor_array[type];
294
295                 strncpy(array->sensor[array->n].name, label, MAX_NAME);
296                 array->sensor[array->n].value = (int) (val * mul);
297                 array->n++;
298             }
299         }
300     }
301
302     timestamp = t;
303 }