|
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 |
|