void print_chip_raw(const sensors_chip_name *name) { int a, b, err; const sensors_feature *feature; const sensors_subfeature *sub; char *label; double val; a = 0; while ((feature = sensors_get_features(name, &a))) { if (!(label = sensors_get_label(name, feature))) { fprintf(stderr, "ERROR: Can't get label of feature " "%s!\n", feature->name); continue; } printf("%s:\n", label); free(label); b = 0; while ((sub = sensors_get_all_subfeatures(name, feature, &b))) { if (sub->flags & SENSORS_MODE_R) { if ((err = sensors_get_value(name, sub->number, &val))) fprintf(stderr, "ERROR: Can't get " "value of subfeature %s: %s\n", sub->name, sensors_strerror(err)); else printf(" %s: %.3f\n", sub->name, val); } else printf("(%s)\n", label); } } }
static PyObject* get_all_subfeatures(ChipName *self, PyObject *args, PyObject *kwargs) { char *kwlist[] = {"feature", NULL}; Feature *feature = NULL; if (!PyArg_ParseTupleAndKeywords(args, kwargs, "O!", kwlist, &FeatureType, &feature)) { return NULL; } PyObject *list = PyList_New(0); int n = 0; while (1) { const sensors_subfeature *subfeature = sensors_get_all_subfeatures( &self->chip_name, &feature->feature, &n); if (subfeature == NULL) { break; } else { PyObject *subfeature_args = Py_BuildValue( "siiiI", subfeature->name, subfeature->number, subfeature->type, subfeature->mapping, subfeature->flags); if (subfeature_args == NULL) { goto error; } PyObject *py_subfeature = PyObject_CallObject( (PyObject*)&SubfeatureType, subfeature_args); Py_DECREF(subfeature_args); if (py_subfeature == NULL) { goto error; } PyList_Append(list, py_subfeature); Py_DECREF(py_subfeature); } } return list; error: Py_DECREF(list); return NULL; }
unsigned int get_sensor_data(unsigned int _id, struct sensor_data *_data) { int a, b, c, num; const sensors_chip_name * chip; const sensors_feature * features; const sensors_subfeature * subfeatures; a = num = 0; _data->kind = -1; while ((chip = sensors_get_detected_chips(NULL, &a))) { b = 0; while ((features = sensors_get_features(chip, &b))) { c = 0; while ((subfeatures = sensors_get_all_subfeatures(chip, features, &c))) { if (subfeatures->type == SENSORS_SUBFEATURE_FAN_INPUT) { if (_id == num) { _data->id = _id; _data->chip = chip->addr; _data->sensor = features->number; char *label = sensors_get_label(chip, features); memcpy(_data->label, label, strlen(label)+1); free(label); _data->kind = SENSOR_FAN; sensors_get_value(chip, subfeatures->number, &_data->data); return 1; } num++; } if (subfeatures->type == SENSORS_SUBFEATURE_TEMP_INPUT) { if (_id == num) { _data->id = _id; _data->chip = chip->addr; _data->sensor = features->number; char *label = sensors_get_label(chip, features); memcpy(_data->label, label, strlen(label)+1); free(label); _data->kind = SENSOR_TEMP; sensors_get_value(chip, subfeatures->number, &_data->data); return 1; } num++; } } } } return 0; }
static int l_readAll(lua_State *L) { sensors_init(NULL); const sensors_chip_name *scn; const sensors_feature *sf; const sensors_subfeature *ss; int n, n1, n2, err; double r; char scns[80]; lua_newtable(L); //Push new table on to stack. Index -1 for(n = 0; (scn = sensors_get_detected_chips(NULL, &n)) != NULL; ) { // Iterate over detected chips for(n1 = 0; (sf = sensors_get_features(scn, &n1)) != NULL; ) // Iterate over features of chip for(n2 = 0; (ss = sensors_get_all_subfeatures(scn, sf, &n2)) != NULL; ) { // Iterate over subfeatures of features from chip sprintf(scns, "%s-%x-%x__%s", scn->prefix, scn->bus.nr, scn->addr, ss->name); // Set scns to (chip.prefix - chip.bus.nr - chip.addr __ subfeature.name ) err = sensors_get_value(scn, ss->number, &r); // Get value from chip using subfeature.number, store result in r. If err != 0 then an error occured. if (err == 0) { // Sensor data. Addres in char* scns, name in ss->name, result in r lua_pushnumber(L, r); //Push table value on stack. Table is now at -2 lua_setfield(L, -2, scns); //Set 3rd argument as key in table at stack index -2 to stack index -1 } else { return(luaL_error(L, "Can't read sensors!")); } } } sensors_cleanup(); return(1); }
int init_sensor_data() { const sensors_chip_name *chip_name; const sensors_feature *feature; const sensors_subfeature *subfeature; char *label; int fan_enabled=FALSE, temperature_enabled=FALSE; int a,b,c; a=0; while ( (chip_name = sensors_get_detected_chips(NULL, &a)) ) { b=0; while ( (feature = sensors_get_features(chip_name, &b)) ) { c=0; while ( (subfeature = sensors_get_all_subfeatures(chip_name, feature, &c)) ) { label = sensors_get_label(chip_name, feature); if ( strcmp(CPU_TEMP, label)==0 ) { printf ("Found sensor: %s (cpu temperature)\n", CPU_TEMP); sd.chip_name_temp=chip_name; sd.number_temp=subfeature->number; temperature_enabled=TRUE; break; } if (subfeature->type == SENSORS_SUBFEATURE_FAN_INPUT) { printf ("Found FAN sensor\n"); sd.chip_name_fan=chip_name; sd.number_fan=subfeature->number; fan_enabled=TRUE; break; } } } } if (fan_enabled==TRUE && temperature_enabled==TRUE) return TRUE; return FALSE; }
unsigned int get_sensor_num(void) { int a, b, c, num; const sensors_chip_name * chip; const sensors_feature * features; const sensors_subfeature * subfeatures; a = num = 0; sensors_init(NULL); while ((chip = sensors_get_detected_chips(NULL, &a))) { b = 0; while ((features = sensors_get_features(chip, &b))) { c = 0; while ((subfeatures = sensors_get_all_subfeatures(chip, features, &c))) { if (subfeatures->type == SENSORS_SUBFEATURE_FAN_INPUT) { // chip = chip->addr // sensor = features->number num++; break; } if (subfeatures->type != SENSORS_SUBFEATURE_TEMP_INPUT) { num++; break; } } } } return num; }
int netsnmp_sensor_arch_load(netsnmp_cache *cache, void *vp) { netsnmp_sensor_info *sp; const sensors_chip_name *chip; const sensors_feature *data; const sensors_subfeature *data2; int chip_nr = 0; DEBUGMSGTL(("sensors:arch", "Reload v3 LM Sensors module\n")); while ((chip = sensors_get_detected_chips( NULL, &chip_nr))) { int a = 0; while ((data = sensors_get_features( chip, &a))) { DEBUGMSGTL(("sensors:arch:detail", "get_features (%s, %d)\n", data->name, data->number)); int b = 0; while ((data2 = sensors_get_all_subfeatures( chip, data, &b))) { char *label = NULL; double val; int type = NETSNMP_SENSOR_TYPE_OTHER; DEBUGMSGTL(("sensors:arch:detail", " get_subfeatures (%s, %d)\n", data2->name, data2->number)); /* * Check the type of this subfeature, * concentrating on the main "input" measurements. */ switch ( data2->type ) { case SENSORS_SUBFEATURE_IN_INPUT: type = NETSNMP_SENSOR_TYPE_VOLTAGE_DC; break; case SENSORS_SUBFEATURE_FAN_INPUT: type = NETSNMP_SENSOR_TYPE_RPM; break; case SENSORS_SUBFEATURE_TEMP_INPUT: type = NETSNMP_SENSOR_TYPE_TEMPERATURE; break; case SENSORS_SUBFEATURE_VID: type = NETSNMP_SENSOR_TYPE_VOLTAGE_DC; break; default: /* Skip everything other than these basic sensor features - ??? */ DEBUGMSGTL(("sensors:arch:detail", " Skip type %x\n", data2->type)); continue; } /* * Get the name and value of this subfeature */ /* if (!(label = sensors_get_label(chip, data))) { DEBUGMSGTL(("sensors:arch:detail", " Can't get name (%s)\n", label)); continue; } if (sensors_get_value(chip, data2->number, &val) < 0) { DEBUGMSGTL(("sensors:arch:detail", " Can't get value (%f)\n", val)); continue; } */ if (!(label = sensors_get_label(chip, data)) || (sensors_get_value(chip, data2->number, &val) < 0)) { DEBUGMSGTL(("sensors:arch:detail", " Can't get name/value (%s, %f)\n", label, val)); free(label); label = NULL; continue; } DEBUGMSGTL(("sensors:arch:detail", "%s = %f\n", label, val)); /* * Use this type to create a new sensor entry * (inserting it in the appropriate sub-containers) */ sp = sensor_by_name( label, type ); if ( sp ) { sp->value = val; sp->flags|= NETSNMP_SENSOR_FLAG_ACTIVE; } if (label) { free(label); label = NULL; } } /* end while data2 */ } /* end while data */ } /* end while chip */ return 0; }
static int sensors_load_conf (void) { static int call_once = 0; FILE *fh = NULL; featurelist_t *last_feature = NULL; const sensors_chip_name *chip; int chip_num; int status; if (call_once) return 0; call_once = 1; if (conffile != NULL) { fh = fopen (conffile, "r"); if (fh == NULL) { char errbuf[1024]; ERROR ("sensors plugin: fopen(%s) failed: %s", conffile, sstrerror (errno, errbuf, sizeof (errbuf))); return (-1); } } status = sensors_init (fh); if (fh) fclose (fh); if (status != 0) { ERROR ("sensors plugin: Cannot initialize sensors. " "Data will not be collected."); return (-1); } #if SENSORS_API_VERSION < 0x400 chip_num = 0; while ((chip = sensors_get_detected_chips (&chip_num)) != NULL) { int feature_num0 = 0; int feature_num1 = 0; while (42) { const sensors_feature_data *feature; int feature_type; featurelist_t *fl; feature = sensors_get_all_features (*chip, &feature_num0, &feature_num1); /* Check if all features have been read. */ if (feature == NULL) break; /* "master features" only */ if (feature->mapping != SENSORS_NO_MAPPING) { DEBUG ("sensors plugin: sensors_load_conf: " "Ignoring subfeature `%s', " "because (feature->mapping " "!= SENSORS_NO_MAPPING).", feature->name); continue; } /* skip ignored in sensors.conf */ if (sensors_get_ignored (*chip, feature->number) == 0) { DEBUG ("sensors plugin: sensors_load_conf: " "Ignoring subfeature `%s', " "because " "`sensors_get_ignored' told " "me so.", feature->name); continue; } feature_type = sensors_feature_name_to_type ( feature->name); if (feature_type == SENSOR_TYPE_UNKNOWN) { DEBUG ("sensors plugin: sensors_load_conf: " "Ignoring subfeature `%s', " "because its type is " "unknown.", feature->name); continue; } fl = calloc (1, sizeof (*fl)); if (fl == NULL) { ERROR ("sensors plugin: calloc failed."); continue; } fl->chip = chip; fl->data = feature; fl->type = feature_type; if (first_feature == NULL) first_feature = fl; else last_feature->next = fl; last_feature = fl; } /* while sensors_get_all_features */ } /* while sensors_get_detected_chips */ /* #endif SENSORS_API_VERSION < 0x400 */ #elif (SENSORS_API_VERSION >= 0x400) && (SENSORS_API_VERSION < 0x500) chip_num = 0; while ((chip = sensors_get_detected_chips (NULL, &chip_num)) != NULL) { const sensors_feature *feature; int feature_num = 0; while ((feature = sensors_get_features (chip, &feature_num)) != NULL) { const sensors_subfeature *subfeature; int subfeature_num = 0; /* Only handle voltage, fanspeeds and temperatures */ if ((feature->type != SENSORS_FEATURE_IN) && (feature->type != SENSORS_FEATURE_FAN) && (feature->type != SENSORS_FEATURE_TEMP) && (feature->type != SENSORS_FEATURE_POWER)) { DEBUG ("sensors plugin: sensors_load_conf: " "Ignoring feature `%s', " "because its type is not " "supported.", feature->name); continue; } while ((subfeature = sensors_get_all_subfeatures (chip, feature, &subfeature_num)) != NULL) { featurelist_t *fl; if ((subfeature->type != SENSORS_SUBFEATURE_IN_INPUT) && (subfeature->type != SENSORS_SUBFEATURE_FAN_INPUT) && (subfeature->type != SENSORS_SUBFEATURE_TEMP_INPUT) && (subfeature->type != SENSORS_SUBFEATURE_POWER_INPUT)) continue; fl = calloc (1, sizeof (*fl)); if (fl == NULL) { ERROR ("sensors plugin: calloc failed."); continue; } fl->chip = chip; fl->feature = feature; fl->subfeature = subfeature; if (first_feature == NULL) first_feature = fl; else last_feature->next = fl; last_feature = fl; } /* while (subfeature) */ } /* while (feature) */ } /* while (chip) */ #endif /* (SENSORS_API_VERSION >= 0x400) && (SENSORS_API_VERSION < 0x500) */ if (first_feature == NULL) { sensors_cleanup (); INFO ("sensors plugin: lm_sensors reports no " "features. Data will not be collected."); return (-1); } return (0); } /* int sensors_load_conf */