summaryrefslogtreecommitdiff
blob: 29ba1ea8e01b7a57c06acf163abe5d0031b5f3b1 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
diff -Naur net-snmp-5.1.1.old/agent/mibgroup/host/hr_sensor.c net-snmp-5.1.1/agent/mibgroup/host/hr_sensor.c
--- net-snmp-5.1.1.old/agent/mibgroup/host/hr_sensor.c	1970-01-01 02:00:00.000000000 +0200
+++ net-snmp-5.1.1/agent/mibgroup/host/hr_sensor.c	2003-10-21 12:49:57.000000000 +0300
@@ -0,0 +1,322 @@
+/* This file was generated by mib2c and is intended for use as a mib module
+   for the ucd-snmp snmpd agent. */
+
+#include <net-snmp/net-snmp-config.h>
+
+#include <stdio.h>
+#if HAVE_STDLIB_H
+#include <stdlib.h>
+#endif
+#if HAVE_STRING_H
+#include <string.h>
+#else
+#include <strings.h>
+#endif
+
+#include <getopt.h>
+#include <errno.h>
+#include <dlfcn.h>
+
+#include "sensors/sensors.h" 
+#include "sensors/error.h"
+
+#include "host_res.h"
+#include "hr_sensor.h"
+
+
+/* 
+ * hr_sensor_variables_oid:
+ *   this is the top level oid that we want to register under.  This
+ *   is essentially a prefix, with the suffix appearing in the
+ *   variable below.
+ */
+
+
+oid hr_sensor_variables_oid[] = { 1,3,6,1,2,1,25,8 };
+
+
+/* 
+ * variable2 hr_sensor_variables:
+ *   this variable defines function callbacks and type return information 
+ *   for the hr_sensor mib section 
+ */
+
+
+struct variable2 hr_sensor_variables[] = {
+  /*  magic number        , variable type , ro/rw , callback fn  , L, oidsuffix */
+#define   HRSENSORMAPPING  2
+  { HRSENSORMAPPING, ASN_INTEGER   , RONLY , var_hr_sensor, 2, { 1,1 } },
+#define   HRSENSORNUMBER  3
+  { HRSENSORNUMBER, ASN_INTEGER   , RONLY , var_hr_sensor, 2, { 1,2 } },
+#define   HRSENSORNAME    4
+  { HRSENSORNAME  , ASN_OCTET_STR , RONLY , var_hr_sensor, 2, { 1,3 } },
+#define   HRSENSORLABEL   5
+  { HRSENSORLABEL , ASN_OCTET_STR , RONLY , var_hr_sensor, 2, { 1,4 } },
+#define   HRSENSORVALUE   6
+  { HRSENSORVALUE , ASN_OCTET_STR , RONLY , var_hr_sensor, 2, { 1,5 } },
+
+};
+/*    (L = length of the oidsuffix) */
+
+
+#define DEFAULT_CONFIG_FILE_NAME "sensors.conf"
+
+static char *config_file_name;
+FILE *config_file;
+static const char *config_file_path[] = 
+{ "/etc", "/usr/lib/sensors", "/usr/local/lib/sensors", "/usr/lib",
+  "/usr/local/lib", ".", 0 };
+
+static void open_config_file(void);
+static int open_this_config_file(char *filename);
+
+
+/* 
+ * functions that are dynamically loaded if the lm_sensors library is 
+ *   installed. 
+ */
+static int (*sensors_get_label_dl)(sensors_chip_name name, int feature,
+				char **result);
+static int (*sensors_init_dl)(FILE *input);
+static int (*sensors_get_feature_dl)(sensors_chip_name name, int feature,
+				  double *result);
+static const sensors_feature_data *(*sensors_get_all_features_dl)
+     (sensors_chip_name name, int *nr1,int *nr2);
+static const sensors_chip_name *(*sensors_get_detected_chips_dl)(int *nr);
+
+#define CHIPS_MAX 20
+static sensors_chip_name chips[CHIPS_MAX];
+static int chips_count=0;
+static int do_sets;
+
+/* This examines global var config_file, and leaves the name there too. 
+   It also opens config_file. */
+static void open_config_file(void)
+{
+#define MAX_FILENAME_LEN 1024
+  char *filename;
+  char buffer[MAX_FILENAME_LEN];
+  int res,i;
+
+  if (config_file_name && !strcmp(config_file_name,"-")) {
+    config_file = stdin;
+    return;
+  } else if (config_file_name && index(config_file_name,'/')) {
+    if ((res = open_this_config_file(config_file_name))) {
+      fprintf(stderr,"Could not locate or open config file\n");
+      fprintf(stderr,"%s: %s\n",config_file_name,strerror(res));
+      exit(1);
+    }
+  }
+  else {
+    if (config_file_name)
+      filename = config_file_name;
+    else
+      filename = strdup(DEFAULT_CONFIG_FILE_NAME);
+    for (i = 0; config_file_path[i]; i++) {
+      if ((snprintf(buffer,MAX_FILENAME_LEN,
+		    "%s/%s",config_file_path[i],filename)) < 1) {
+        fprintf(stderr,
+                "open_config_file: ridiculous long config file name!\n");
+        exit(1);
+      }
+      if (!open_this_config_file(buffer)) {
+        free(config_file_name);
+        config_file_name = strdup(buffer);
+        return;
+      }
+    }
+    fprintf(stderr,"Could not locate or open config file!\n");
+    exit(1);
+  }
+}
+    
+static int open_this_config_file(char *filename)
+{
+  config_file = fopen(filename,"r");
+  if (! config_file)
+    return -errno;
+  return 0;
+}
+
+static long *sensor_tbl = 0;
+sensors_chip_name *chip_tbl = 0;
+static int sensor_maxnum = 0;
+
+static void do_register(sensors_chip_name name)
+{
+  const char *algo,*adap;
+
+  {
+    const sensors_feature_data *sensor;
+    const char *map=0;
+    int index0 = 0, index1 = 0;
+    
+    printf("%s\n", name);
+
+    while ((sensor = sensors_get_all_features_dl (name, &index0, &index1))
+	    != NULL) {
+      if(sensor_tbl = realloc(sensor_tbl, sizeof(long) * (sensor_maxnum+2)))
+	sensor_tbl[sensor_maxnum] = sensor->number;
+      if (chip_tbl = realloc(chip_tbl, sizeof(sensors_chip_name) * (sensor_maxnum+2)))
+	chip_tbl[sensor_maxnum] = name;
+      sensor_maxnum++;
+    }
+  }
+}
+
+/*
+ * init_hr_sensor():
+ *   Initialization routine.  This is called when the agent starts up.
+ *   At a minimum, registration of your variables should take place here.
+ */
+void init_hr_sensor(void) {
+
+  const sensors_chip_name *chip;
+  int chip_nr,i;
+  int c,res;
+  void *sensors_library;
+  
+  /* This could probably due with a bit more error reporting. */
+  sensors_library=dlopen("libsensors.so",RTLD_LAZY);
+  if(sensors_library==NULL)
+    return; // return before anything is registered
+
+  sensors_get_label_dl=dlsym(sensors_library,"sensors_get_label");
+  sensors_init_dl=dlsym(sensors_library,"sensors_init");
+  sensors_get_feature_dl=dlsym(sensors_library,"sensors_get_feature");
+  sensors_get_all_features_dl=dlsym(sensors_library,
+				    "sensors_get_all_features");
+  sensors_get_detected_chips_dl=dlsym(sensors_library,
+				      "sensors_get_detected_chips");
+  if(sensors_get_label_dl==NULL || sensors_init_dl==NULL || 
+     sensors_get_feature_dl==NULL || sensors_get_all_features_dl==NULL ||
+     sensors_get_detected_chips_dl==NULL)
+    return; // return before anything is registered
+
+  /* register ourselves with the agent to handle our mib tree */
+  REGISTER_MIB("hr_sensor", hr_sensor_variables, variable2,
+               hr_sensor_variables_oid);
+
+
+  /* place any other initialization junk you need here */
+
+
+  chips[0].prefix = SENSORS_CHIP_NAME_PREFIX_ANY;
+  chips[0].bus = SENSORS_CHIP_NAME_BUS_ANY;
+  chips[0].addr = SENSORS_CHIP_NAME_ADDR_ANY;
+  chips_count = 1;
+
+  open_config_file();
+
+  if ((res = sensors_init_dl(config_file))) {
+    return;
+  }
+
+  for (chip_nr = 0; (chip = sensors_get_detected_chips_dl(&chip_nr));)
+    do_register (*chip);
+}
+
+
+/*
+ * var_hr_sensor():
+ *   This function is called every time the agent gets a request for
+ *   a scalar variable that might be found within your mib section
+ *   registered above.  It is up to you to do the right thing and
+ *   return the correct value.
+ *     You should also correct the value of "var_len" if necessary.
+ *
+ *   Please see the documentation for more information about writing
+ *   module extensions, and check out the examples in the examples
+ *   and mibII directories.
+ */
+unsigned char *
+var_hr_sensor(struct variable *vp, 
+		   oid     *name, 
+		   size_t  *length, 
+		   int     exact, 
+		   size_t  *var_len, 
+		   WriteMethod **write_method)
+{
+  /* variables we may use later */
+  static long long_ret;
+  static char *string = 0;
+  static oid objid[MAX_OID_LEN];
+  static struct counter64 c64;
+
+  const sensors_feature_data *sensor;
+  const char *map=0;
+  int index0 = 0, index1 = 0;
+  int sensor_idx = 0, sensor_num = 0;
+  sensors_chip_name chip_name;
+  double value;
+
+
+  if (header_simple_table(vp,name,length,exact,var_len,write_method,sensor_maxnum)
+      == MATCH_FAILED )
+    return NULL;
+
+   sensor_idx = name[*length - 1] - 1;
+   sensor_num = sensor_tbl[sensor_idx];
+   chip_name = chip_tbl[sensor_idx];
+
+  while ((sensor = sensors_get_all_features_dl (chip_name, &index0, &index1)) 
+	  != NULL) {
+    if(sensor_num == sensor->number) {      
+      /* 
+       * this is where we do the value assignments for the mib results.
+       */
+      switch(vp->magic) {
+	
+	
+      case HRSENSORMAPPING:
+        
+        long_ret = sensor->mapping;
+        return (unsigned char *) &long_ret;
+	
+      case HRSENSORNUMBER:
+        
+        long_ret = sensor_num;
+        return (unsigned char *) &long_ret;
+	
+      case HRSENSORNAME:
+        
+	string = malloc(SPRINT_MAX_LEN);
+        strncpy(string, sensor->name, SPRINT_MAX_LEN);
+        *var_len = strlen(string);
+        return (unsigned char *) string;
+	
+      case HRSENSORLABEL:
+        
+	string = malloc(SPRINT_MAX_LEN);
+
+	if (sensors_get_label_dl (chip_name, sensor->number, &string)) {
+	  return 0;
+	} 
+	
+	*var_len = strlen(string);
+	return (unsigned char *) string;
+	
+      case HRSENSORVALUE:
+	
+	if (!(sensor->mode & SENSORS_MODE_R)) {
+	  return 0;
+	} else if (!(sensor->mode & SENSORS_MODE_R)) {
+	  return 0;
+	} else if (sensors_get_feature_dl (chip_name, sensor->number, &value)){
+	  return 0;
+	}      
+	string = malloc(SPRINT_MAX_LEN);
+	snprintf(string, SPRINT_MAX_LEN, "%f", value);
+	*var_len = strlen(string);
+	return (unsigned char *) string;
+	
+      default:
+	ERROR_MSG("");
+	return NULL;
+      }
+    }
+  }
+  return NULL;
+}
+
diff -Naur net-snmp-5.1.1.old/agent/mibgroup/host/hr_sensor.h net-snmp-5.1.1/agent/mibgroup/host/hr_sensor.h
--- net-snmp-5.1.1.old/agent/mibgroup/host/hr_sensor.h	1970-01-01 02:00:00.000000000 +0200
+++ net-snmp-5.1.1/agent/mibgroup/host/hr_sensor.h	2003-10-21 12:47:04.000000000 +0300
@@ -0,0 +1,24 @@
+/* This file was generated by mib2c and is intended for use as a mib module
+  for the ucd-snmp snmpd agent. */
+
+
+#ifndef _MIBGROUP_HRSENSOR_H
+#define _MIBGROUP_HRSENSOR_H
+
+
+/* we may use header_generic and header_simple_table from the util_funcs module */
+
+
+config_require(util_funcs)
+
+
+/* function prototypes */
+
+
+extern void   init_hr_sensor(void);
+extern FindVarMethod var_hr_sensor;
+
+
+
+
+#endif /* _MIBGROUP_HRSENSOR_H */
diff -Naur net-snmp-5.1.1.old/agent/mibgroup/host.h net-snmp-5.1.1/agent/mibgroup/host.h
--- net-snmp-5.1.1.old/agent/mibgroup/host.h	2002-04-20 10:30:00.000000000 +0300
+++ net-snmp-5.1.1/agent/mibgroup/host.h	2003-10-21 12:47:04.000000000 +0300
@@ -15,6 +15,7 @@
 	config_require(host/hr_filesys)
 	config_require(host/hr_swrun)
 	config_require(host/hr_swinst)
+	config_require(host/hr_sensor)
 
 /* add the host resources mib to the default mibs to load */
 config_add_mib(HOST-RESOURCES-MIB)
diff -Naur net-snmp-5.1.1.old/mibs/HOST-RESOURCES-MIB.txt net-snmp-5.1.1/mibs/HOST-RESOURCES-MIB.txt
--- net-snmp-5.1.1.old/mibs/HOST-RESOURCES-MIB.txt	2002-04-20 10:30:15.000000000 +0300
+++ net-snmp-5.1.1/mibs/HOST-RESOURCES-MIB.txt	2003-10-21 12:47:04.000000000 +0300
@@ -106,6 +106,7 @@
 hrSWRunPerf     OBJECT IDENTIFIER ::= { host 5 }
 hrSWInstalled   OBJECT IDENTIFIER ::= { host 6 }
 hrMIBAdminInfo  OBJECT IDENTIFIER ::= { host 7 }
+hrSensor        OBJECT IDENTIFIER ::= { host 8 }
 
 -- textual conventions
 
@@ -1390,6 +1391,66 @@
         (hex)'00 00 01 01 00 00 00 00'."
     ::= { hrSWInstalledEntry 5 }
 
+
+hrSensorEntry OBJECT-TYPE
+    SYNTAX     hrSensorEntry
+    MAX-ACCESS not-accessible
+    STATUS     current
+    DESCRIPTION
+        "The CPU temperature sensor settings."
+    INDEX { hrSensor }
+    ::= { hrSensor 1 }
+
+hrSensorEntry ::= SEQUENCE {
+        hrSensorNumber     Integer32,
+        hrSensorMapping    Integer32,
+        hrSensorName       DisplayString,
+        hrSensorLabel      DisplayString,
+        hrSensorValue      DisplayString
+    }
+
+
+
+hrSensorMapping OBJECT-TYPE
+    SYNTAX     Integer32 (1..2147483647)
+    MAX-ACCESS read-only
+    STATUS     current
+    DESCRIPTION
+        "The mapping"
+    ::= { hrSensorEntry 1 }
+
+hrSensorNumber OBJECT-TYPE
+    SYNTAX     Integer32 (1..2147483647)
+    MAX-ACCESS read-only
+    STATUS     current
+    DESCRIPTION
+        "The Number"
+    ::= { hrSensorEntry 2 }
+
+hrSensorName OBJECT-TYPE
+    SYNTAX     DisplayString
+    MAX-ACCESS read-only
+    STATUS     current
+    DESCRIPTION
+        "The name of the sensor."
+    ::= { hrSensorEntry 3 }
+
+hrSensorLabel OBJECT-TYPE
+    SYNTAX     DisplayString
+    MAX-ACCESS read-only
+    STATUS     current
+    DESCRIPTION
+        "The label of the sensor."
+    ::= { hrSensorEntry 4 }
+
+hrSensorValue OBJECT-TYPE
+    SYNTAX     DisplayString
+    MAX-ACCESS read-only
+    STATUS     current
+    DESCRIPTION
+        "The value of the sensor"
+    ::= { hrSensorEntry 5 }
+
 -- Conformance information
 
 hrMIBCompliances OBJECT IDENTIFIER ::= { hrMIBAdminInfo 2 }
@@ -1456,6 +1517,13 @@
               "The Installed Software Group.
               Implementation of this group is at the discretion
               of the implementor."
+
+      GROUP hrSensorGroup
+          DESCRIPTION
+              "The Sensors Group.
+              Implementation of this group is at the discretion
+              of the implementor."
+
     ::= { hrMIBCompliances 1 }
 
     hrSystemGroup OBJECT-GROUP
@@ -1537,4 +1605,17 @@
             "The Host Resources Installed Software Group."
         ::= { hrMIBGroups 6 }
 
+    hrSensorGroup OBJECT-GROUP
+         OBJECTS {
+		 hrSensorEntry,
+		 hrSensorNumber,
+		 hrSensorMapping,
+		 hrSensorName,
+		 hrSensorLabel,
+	         hrSensorValue
+	 }
+	 STATUS current 
+	 DESCRIPTION
+		"The Sensor Resources Group."	
+         ::= { hrMIBGroups 7 }		
 END