Gentoo Websites Logo
Go to: Gentoo Home Documentation Forums Lists Bugs Planet Store Wiki Get Gentoo!
View | Details | Raw Unified | Return to bug 57450
Collapse All | Expand All

(-)net-snmp-5.1.1.old/agent/mibgroup/host/hr_sensor.c (+322 lines)
Line 0 Link Here
1
/* This file was generated by mib2c and is intended for use as a mib module
2
   for the ucd-snmp snmpd agent. */
3
4
#include <net-snmp/net-snmp-config.h>
5
6
#include <stdio.h>
7
#if HAVE_STDLIB_H
8
#include <stdlib.h>
9
#endif
10
#if HAVE_STRING_H
11
#include <string.h>
12
#else
13
#include <strings.h>
14
#endif
15
16
#include <getopt.h>
17
#include <errno.h>
18
#include <dlfcn.h>
19
20
#include "sensors/sensors.h" 
21
#include "sensors/error.h"
22
23
#include "host_res.h"
24
#include "hr_sensor.h"
25
26
27
/* 
28
 * hr_sensor_variables_oid:
29
 *   this is the top level oid that we want to register under.  This
30
 *   is essentially a prefix, with the suffix appearing in the
31
 *   variable below.
32
 */
33
34
35
oid hr_sensor_variables_oid[] = { 1,3,6,1,2,1,25,8 };
36
37
38
/* 
39
 * variable2 hr_sensor_variables:
40
 *   this variable defines function callbacks and type return information 
41
 *   for the hr_sensor mib section 
42
 */
43
44
45
struct variable2 hr_sensor_variables[] = {
46
  /*  magic number        , variable type , ro/rw , callback fn  , L, oidsuffix */
47
#define   HRSENSORMAPPING  2
48
  { HRSENSORMAPPING, ASN_INTEGER   , RONLY , var_hr_sensor, 2, { 1,1 } },
49
#define   HRSENSORNUMBER  3
50
  { HRSENSORNUMBER, ASN_INTEGER   , RONLY , var_hr_sensor, 2, { 1,2 } },
51
#define   HRSENSORNAME    4
52
  { HRSENSORNAME  , ASN_OCTET_STR , RONLY , var_hr_sensor, 2, { 1,3 } },
53
#define   HRSENSORLABEL   5
54
  { HRSENSORLABEL , ASN_OCTET_STR , RONLY , var_hr_sensor, 2, { 1,4 } },
55
#define   HRSENSORVALUE   6
56
  { HRSENSORVALUE , ASN_OCTET_STR , RONLY , var_hr_sensor, 2, { 1,5 } },
57
58
};
59
/*    (L = length of the oidsuffix) */
60
61
62
#define DEFAULT_CONFIG_FILE_NAME "sensors.conf"
63
64
static char *config_file_name;
65
FILE *config_file;
66
static const char *config_file_path[] = 
67
{ "/etc", "/usr/lib/sensors", "/usr/local/lib/sensors", "/usr/lib",
68
  "/usr/local/lib", ".", 0 };
69
70
static void open_config_file(void);
71
static int open_this_config_file(char *filename);
72
73
74
/* 
75
 * functions that are dynamically loaded if the lm_sensors library is 
76
 *   installed. 
77
 */
78
static int (*sensors_get_label_dl)(sensors_chip_name name, int feature,
79
				char **result);
80
static int (*sensors_init_dl)(FILE *input);
81
static int (*sensors_get_feature_dl)(sensors_chip_name name, int feature,
82
				  double *result);
83
static const sensors_feature_data *(*sensors_get_all_features_dl)
84
     (sensors_chip_name name, int *nr1,int *nr2);
85
static const sensors_chip_name *(*sensors_get_detected_chips_dl)(int *nr);
86
87
#define CHIPS_MAX 20
88
static sensors_chip_name chips[CHIPS_MAX];
89
static int chips_count=0;
90
static int do_sets;
91
92
/* This examines global var config_file, and leaves the name there too. 
93
   It also opens config_file. */
94
static void open_config_file(void)
95
{
96
#define MAX_FILENAME_LEN 1024
97
  char *filename;
98
  char buffer[MAX_FILENAME_LEN];
99
  int res,i;
100
101
  if (config_file_name && !strcmp(config_file_name,"-")) {
102
    config_file = stdin;
103
    return;
104
  } else if (config_file_name && index(config_file_name,'/')) {
105
    if ((res = open_this_config_file(config_file_name))) {
106
      fprintf(stderr,"Could not locate or open config file\n");
107
      fprintf(stderr,"%s: %s\n",config_file_name,strerror(res));
108
      exit(1);
109
    }
110
  }
111
  else {
112
    if (config_file_name)
113
      filename = config_file_name;
114
    else
115
      filename = strdup(DEFAULT_CONFIG_FILE_NAME);
116
    for (i = 0; config_file_path[i]; i++) {
117
      if ((snprintf(buffer,MAX_FILENAME_LEN,
118
		    "%s/%s",config_file_path[i],filename)) < 1) {
119
        fprintf(stderr,
120
                "open_config_file: ridiculous long config file name!\n");
121
        exit(1);
122
      }
123
      if (!open_this_config_file(buffer)) {
124
        free(config_file_name);
125
        config_file_name = strdup(buffer);
126
        return;
127
      }
128
    }
129
    fprintf(stderr,"Could not locate or open config file!\n");
130
    exit(1);
131
  }
132
}
133
    
134
static int open_this_config_file(char *filename)
135
{
136
  config_file = fopen(filename,"r");
137
  if (! config_file)
138
    return -errno;
139
  return 0;
140
}
141
142
static long *sensor_tbl = 0;
143
sensors_chip_name *chip_tbl = 0;
144
static int sensor_maxnum = 0;
145
146
static void do_register(sensors_chip_name name)
147
{
148
  const char *algo,*adap;
149
150
  {
151
    const sensors_feature_data *sensor;
152
    const char *map=0;
153
    int index0 = 0, index1 = 0;
154
    
155
    printf("%s\n", name);
156
157
    while ((sensor = sensors_get_all_features_dl (name, &index0, &index1))
158
	    != NULL) {
159
      if(sensor_tbl = realloc(sensor_tbl, sizeof(long) * (sensor_maxnum+2)))
160
	sensor_tbl[sensor_maxnum] = sensor->number;
161
      if (chip_tbl = realloc(chip_tbl, sizeof(sensors_chip_name) * (sensor_maxnum+2)))
162
	chip_tbl[sensor_maxnum] = name;
163
      sensor_maxnum++;
164
    }
165
  }
166
}
167
168
/*
169
 * init_hr_sensor():
170
 *   Initialization routine.  This is called when the agent starts up.
171
 *   At a minimum, registration of your variables should take place here.
172
 */
173
void init_hr_sensor(void) {
174
175
  const sensors_chip_name *chip;
176
  int chip_nr,i;
177
  int c,res;
178
  void *sensors_library;
179
  
180
  /* This could probably due with a bit more error reporting. */
181
  sensors_library=dlopen("libsensors.so.2",RTLD_LAZY);
182
  if(sensors_library==NULL)
183
    return; // return before anything is registered
184
185
  sensors_get_label_dl=dlsym(sensors_library,"sensors_get_label");
186
  sensors_init_dl=dlsym(sensors_library,"sensors_init");
187
  sensors_get_feature_dl=dlsym(sensors_library,"sensors_get_feature");
188
  sensors_get_all_features_dl=dlsym(sensors_library,
189
				    "sensors_get_all_features");
190
  sensors_get_detected_chips_dl=dlsym(sensors_library,
191
				      "sensors_get_detected_chips");
192
  if(sensors_get_label_dl==NULL || sensors_init_dl==NULL || 
193
     sensors_get_feature_dl==NULL || sensors_get_all_features_dl==NULL ||
194
     sensors_get_detected_chips_dl==NULL)
195
    return; // return before anything is registered
196
197
  /* register ourselves with the agent to handle our mib tree */
198
  REGISTER_MIB("hr_sensor", hr_sensor_variables, variable2,
199
               hr_sensor_variables_oid);
200
201
202
  /* place any other initialization junk you need here */
203
204
205
  chips[0].prefix = SENSORS_CHIP_NAME_PREFIX_ANY;
206
  chips[0].bus = SENSORS_CHIP_NAME_BUS_ANY;
207
  chips[0].addr = SENSORS_CHIP_NAME_ADDR_ANY;
208
  chips_count = 1;
209
210
  open_config_file();
211
212
  if ((res = sensors_init_dl(config_file))) {
213
    return;
214
  }
215
216
  for (chip_nr = 0; (chip = sensors_get_detected_chips_dl(&chip_nr));)
217
    do_register (*chip);
218
}
219
220
221
/*
222
 * var_hr_sensor():
223
 *   This function is called every time the agent gets a request for
224
 *   a scalar variable that might be found within your mib section
225
 *   registered above.  It is up to you to do the right thing and
226
 *   return the correct value.
227
 *     You should also correct the value of "var_len" if necessary.
228
 *
229
 *   Please see the documentation for more information about writing
230
 *   module extensions, and check out the examples in the examples
231
 *   and mibII directories.
232
 */
233
unsigned char *
234
var_hr_sensor(struct variable *vp, 
235
		   oid     *name, 
236
		   size_t  *length, 
237
		   int     exact, 
238
		   size_t  *var_len, 
239
		   WriteMethod **write_method)
240
{
241
  /* variables we may use later */
242
  static long long_ret;
243
  static char *string = 0;
244
  static oid objid[MAX_OID_LEN];
245
  static struct counter64 c64;
246
247
  const sensors_feature_data *sensor;
248
  const char *map=0;
249
  int index0 = 0, index1 = 0;
250
  int sensor_idx = 0, sensor_num = 0;
251
  sensors_chip_name chip_name;
252
  double value;
253
254
255
  if (header_simple_table(vp,name,length,exact,var_len,write_method,sensor_maxnum)
256
      == MATCH_FAILED )
257
    return NULL;
258
259
   sensor_idx = name[*length - 1] - 1;
260
   sensor_num = sensor_tbl[sensor_idx];
261
   chip_name = chip_tbl[sensor_idx];
262
263
  while ((sensor = sensors_get_all_features_dl (chip_name, &index0, &index1)) 
264
	  != NULL) {
265
    if(sensor_num == sensor->number) {      
266
      /* 
267
       * this is where we do the value assignments for the mib results.
268
       */
269
      switch(vp->magic) {
270
	
271
	
272
      case HRSENSORMAPPING:
273
        
274
        long_ret = sensor->mapping;
275
        return (unsigned char *) &long_ret;
276
	
277
      case HRSENSORNUMBER:
278
        
279
        long_ret = sensor_num;
280
        return (unsigned char *) &long_ret;
281
	
282
      case HRSENSORNAME:
283
        
284
	string = malloc(SPRINT_MAX_LEN);
285
        strncpy(string, sensor->name, SPRINT_MAX_LEN);
286
        *var_len = strlen(string);
287
        return (unsigned char *) string;
288
	
289
      case HRSENSORLABEL:
290
        
291
	string = malloc(SPRINT_MAX_LEN);
292
293
	if (sensors_get_label_dl (chip_name, sensor->number, &string)) {
294
	  return 0;
295
	} 
296
	
297
	*var_len = strlen(string);
298
	return (unsigned char *) string;
299
	
300
      case HRSENSORVALUE:
301
	
302
	if (!(sensor->mode & SENSORS_MODE_R)) {
303
	  return 0;
304
	} else if (!(sensor->mode & SENSORS_MODE_R)) {
305
	  return 0;
306
	} else if (sensors_get_feature_dl (chip_name, sensor->number, &value)){
307
	  return 0;
308
	}      
309
	string = malloc(SPRINT_MAX_LEN);
310
	snprintf(string, SPRINT_MAX_LEN, "%f", value);
311
	*var_len = strlen(string);
312
	return (unsigned char *) string;
313
	
314
      default:
315
	ERROR_MSG("");
316
	return NULL;
317
      }
318
    }
319
  }
320
  return NULL;
321
}
322
(-)net-snmp-5.1.1.old/agent/mibgroup/host/hr_sensor.h (+24 lines)
Line 0 Link Here
1
/* This file was generated by mib2c and is intended for use as a mib module
2
  for the ucd-snmp snmpd agent. */
3
4
5
#ifndef _MIBGROUP_HRSENSOR_H
6
#define _MIBGROUP_HRSENSOR_H
7
8
9
/* we may use header_generic and header_simple_table from the util_funcs module */
10
11
12
config_require(util_funcs)
13
14
15
/* function prototypes */
16
17
18
extern void   init_hr_sensor(void);
19
extern FindVarMethod var_hr_sensor;
20
21
22
23
24
#endif /* _MIBGROUP_HRSENSOR_H */
(-)net-snmp-5.1.1.old/agent/mibgroup/host.h (+1 lines)
Lines 15-20 Link Here
15
	config_require(host/hr_filesys)
15
	config_require(host/hr_filesys)
16
	config_require(host/hr_swrun)
16
	config_require(host/hr_swrun)
17
	config_require(host/hr_swinst)
17
	config_require(host/hr_swinst)
18
	config_require(host/hr_sensor)
18
19
19
/* add the host resources mib to the default mibs to load */
20
/* add the host resources mib to the default mibs to load */
20
config_add_mib(HOST-RESOURCES-MIB)
21
config_add_mib(HOST-RESOURCES-MIB)
(-)net-snmp-5.1.1.old/mibs/HOST-RESOURCES-MIB.txt (+81 lines)
Lines 106-111 Link Here
106
hrSWRunPerf     OBJECT IDENTIFIER ::= { host 5 }
106
hrSWRunPerf     OBJECT IDENTIFIER ::= { host 5 }
107
hrSWInstalled   OBJECT IDENTIFIER ::= { host 6 }
107
hrSWInstalled   OBJECT IDENTIFIER ::= { host 6 }
108
hrMIBAdminInfo  OBJECT IDENTIFIER ::= { host 7 }
108
hrMIBAdminInfo  OBJECT IDENTIFIER ::= { host 7 }
109
hrSensor        OBJECT IDENTIFIER ::= { host 8 }
109
110
110
-- textual conventions
111
-- textual conventions
111
112
Lines 1390-1395 Link Here
1390
        (hex)'00 00 01 01 00 00 00 00'."
1391
        (hex)'00 00 01 01 00 00 00 00'."
1391
    ::= { hrSWInstalledEntry 5 }
1392
    ::= { hrSWInstalledEntry 5 }
1392
1393
1394
1395
hrSensorEntry OBJECT-TYPE
1396
    SYNTAX     hrSensorEntry
1397
    MAX-ACCESS not-accessible
1398
    STATUS     current
1399
    DESCRIPTION
1400
        "The CPU temperature sensor settings."
1401
    INDEX { hrSensor }
1402
    ::= { hrSensor 1 }
1403
1404
hrSensorEntry ::= SEQUENCE {
1405
        hrSensorNumber     Integer32,
1406
        hrSensorMapping    Integer32,
1407
        hrSensorName       DisplayString,
1408
        hrSensorLabel      DisplayString,
1409
        hrSensorValue      DisplayString
1410
    }
1411
1412
1413
1414
hrSensorMapping OBJECT-TYPE
1415
    SYNTAX     Integer32 (1..2147483647)
1416
    MAX-ACCESS read-only
1417
    STATUS     current
1418
    DESCRIPTION
1419
        "The mapping"
1420
    ::= { hrSensorEntry 1 }
1421
1422
hrSensorNumber OBJECT-TYPE
1423
    SYNTAX     Integer32 (1..2147483647)
1424
    MAX-ACCESS read-only
1425
    STATUS     current
1426
    DESCRIPTION
1427
        "The Number"
1428
    ::= { hrSensorEntry 2 }
1429
1430
hrSensorName OBJECT-TYPE
1431
    SYNTAX     DisplayString
1432
    MAX-ACCESS read-only
1433
    STATUS     current
1434
    DESCRIPTION
1435
        "The name of the sensor."
1436
    ::= { hrSensorEntry 3 }
1437
1438
hrSensorLabel OBJECT-TYPE
1439
    SYNTAX     DisplayString
1440
    MAX-ACCESS read-only
1441
    STATUS     current
1442
    DESCRIPTION
1443
        "The label of the sensor."
1444
    ::= { hrSensorEntry 4 }
1445
1446
hrSensorValue OBJECT-TYPE
1447
    SYNTAX     DisplayString
1448
    MAX-ACCESS read-only
1449
    STATUS     current
1450
    DESCRIPTION
1451
        "The value of the sensor"
1452
    ::= { hrSensorEntry 5 }
1453
1393
-- Conformance information
1454
-- Conformance information
1394
1455
1395
hrMIBCompliances OBJECT IDENTIFIER ::= { hrMIBAdminInfo 2 }
1456
hrMIBCompliances OBJECT IDENTIFIER ::= { hrMIBAdminInfo 2 }
Lines 1456-1461 Link Here
1456
              "The Installed Software Group.
1517
              "The Installed Software Group.
1457
              Implementation of this group is at the discretion
1518
              Implementation of this group is at the discretion
1458
              of the implementor."
1519
              of the implementor."
1520
1521
      GROUP hrSensorGroup
1522
          DESCRIPTION
1523
              "The Sensors Group.
1524
              Implementation of this group is at the discretion
1525
              of the implementor."
1526
1459
    ::= { hrMIBCompliances 1 }
1527
    ::= { hrMIBCompliances 1 }
1460
1528
1461
    hrSystemGroup OBJECT-GROUP
1529
    hrSystemGroup OBJECT-GROUP
Lines 1537-1540 Link Here
1537
            "The Host Resources Installed Software Group."
1605
            "The Host Resources Installed Software Group."
1538
        ::= { hrMIBGroups 6 }
1606
        ::= { hrMIBGroups 6 }
1539
1607
1608
    hrSensorGroup OBJECT-GROUP
1609
         OBJECTS {
1610
		 hrSensorEntry,
1611
		 hrSensorNumber,
1612
		 hrSensorMapping,
1613
		 hrSensorName,
1614
		 hrSensorLabel,
1615
	         hrSensorValue
1616
	 }
1617
	 STATUS current 
1618
	 DESCRIPTION
1619
		"The Sensor Resources Group."	
1620
         ::= { hrMIBGroups 7 }		
1540
END
1621
END

Return to bug 57450