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); } } }
/* Return 0 on success, and an exit error code otherwise */ static int read_config_file(const char *config_file_name) { FILE *config_file; int err; if (config_file_name) { if (!strcmp(config_file_name, "-")) config_file = stdin; else config_file = fopen(config_file_name, "r"); if (!config_file) { fprintf(stderr, "Could not open config file\n"); perror(config_file_name); return 1; } } else { /* Use libsensors default */ config_file = NULL; } err = sensors_init(config_file); if (err) { fprintf(stderr, "sensors_init: %s\n", sensors_strerror(err)); if (config_file) fclose(config_file); return 1; } if (config_file && fclose(config_file) == EOF) perror(config_file_name); return 0; }
int parseChips(int argc, char **argv) { int i, n = argc - optind, err; if (n == 0) { sensord_args.chipNames[0].prefix = SENSORS_CHIP_NAME_PREFIX_ANY; sensord_args.chipNames[0].bus.type = SENSORS_BUS_TYPE_ANY; sensord_args.chipNames[0].bus.nr = SENSORS_BUS_NR_ANY; sensord_args.chipNames[0].addr = SENSORS_CHIP_NAME_ADDR_ANY; sensord_args.numChipNames = 1; return 0; } if (n > MAX_CHIP_NAMES) { fprintf(stderr, "Too many chip names.\n"); return -1; } for (i = 0; i < n; ++i) { char *arg = argv[optind + i]; err = sensors_parse_chip_name(arg, sensord_args.chipNames + i); if (err) { fprintf(stderr, "Invalid chip name `%s': %s\n", arg, sensors_strerror(err)); for (i--; i >= 0; i--) sensors_free_chip_name(sensord_args.chipNames + i); return -1; } } sensord_args.numChipNames = n; return 0; }
/* A variant for input values, where we want to handle errors gracefully */ static int get_input_value(const sensors_chip_name *name, const sensors_subfeature *sub, double *val) { int err; err = sensors_get_value(name, sub->number, val); if (err && err != -SENSORS_ERR_ACCESS_R) { fprintf(stderr, "ERROR: Can't get value of subfeature %s: %s\n", sub->name, sensors_strerror(err)); } return err; }
/* returns 1 on error */ static int do_a_set(const sensors_chip_name *name) { int err; if ((err = sensors_do_chip_sets(name))) { if (err == -SENSORS_ERR_KERNEL) { fprintf(stderr, "%s: %s\n", sprintf_chip_name(name), sensors_strerror(err)); fprintf(stderr, "Run as root?\n"); return 1; } else if (err == -SENSORS_ERR_ACCESS_W) { fprintf(stderr, "%s: At least one \"set\" statement failed\n", sprintf_chip_name(name)); } else { fprintf(stderr, "%s: %s\n", sprintf_chip_name(name), sensors_strerror(err)); } } return 0; }
static double get_value(const sensors_chip_name *name, const sensors_subfeature *sub) { double val; int err; err = sensors_get_value(name, sub->number, &val); if (err) { fprintf(stderr, "ERROR: Can't get value of subfeature %s: %s\n", sub->name, sensors_strerror(err)); val = 0; } return val; }
static int setChip(const sensors_chip_name *chip) { int ret = 0; if ((ret = idChip(chip))) { sensorLog(LOG_ERR, "Error identifying chip: %s", chip->prefix); } else if ((ret = sensors_do_chip_sets(chip))) { sensorLog(LOG_ERR, "Error performing chip sets: %s: %s", chip->prefix, sensors_strerror(ret)); ret = 50; } else { sensorLog(LOG_INFO, "Set."); } return ret; }
static PyObject* do_chip_sets(ChipName *self, PyObject *args) { (void)args; int status = sensors_do_chip_sets(&self->chip_name); if (status < 0) { PyErr_SetString(SensorsException, sensors_strerror(status)); return NULL; } Py_RETURN_NONE; }
static int get_flag(const sensors_chip_name *chip, int num) { double val; int ret; if (num == -1) return 0; ret = sensors_get_value(chip, num, &val); if (ret) { sensorLog(LOG_ERR, "Error getting sensor data: %s/#%d: %s", chip->prefix, num, sensors_strerror(ret)); return -1; } return (int) (val + 0.5); }
static PyObject* str(ChipName *self) { char buffer[512]; int status; status = sensors_snprintf_chip_name(buffer, sizeof buffer, &(self->chip_name)); if (status < 0) { PyErr_SetString(SensorsException, sensors_strerror(status)); return NULL; } return PyString_FromString(buffer); }
static PyObject* get_value(ChipName *self, PyObject *args, PyObject *kwargs) { char *kwlist[] = {"subfeat_nr", NULL}; int subfeat_nr = -1; if (!PyArg_ParseTupleAndKeywords(args, kwargs, "i", kwlist, &subfeat_nr)) { return NULL; } double value = 0.0; int status = sensors_get_value(&self->chip_name, subfeat_nr, &value); if (status < 0) { PyErr_SetString(SensorsException, sensors_strerror(status)); return NULL; } return PyFloat_FromDouble(value); }
static PyObject* set_value(ChipName *self, PyObject *args, PyObject *kwargs) { char *kwlist[] = {"subfeat_nr", "value", NULL}; int subfeat_nr = -1; double value = 0.0; if (!PyArg_ParseTupleAndKeywords(args, kwargs, "id", kwlist, &subfeat_nr, &value)) { return NULL; } int status = sensors_set_value(&self->chip_name, subfeat_nr, value); if (status < 0) { PyErr_SetString(SensorsException, sensors_strerror(status)); return NULL; } Py_RETURN_NONE; }
static int do_features(const sensors_chip_name *chip, const FeatureDescriptor *feature, int action) { char *label; const char *formatted; int i, alrm, beep, ret; double val[MAX_DATA]; /* If only scanning, take a quick exit if alarm is off */ alrm = get_flag(chip, feature->alarmNumber); if (alrm == -1) return -1; if (action == DO_SCAN && !alrm) return 0; for (i = 0; feature->dataNumbers[i] >= 0; i++) { ret = sensors_get_value(chip, feature->dataNumbers[i], val + i); if (ret) { sensorLog(LOG_ERR, "Error getting sensor data: %s/#%d: %s", chip->prefix, feature->dataNumbers[i], sensors_strerror(ret)); return -1; } } /* For RRD, we don't need anything else */ if (action == DO_RRD) { if (feature->rrd) { const char *rrded = feature->rrd(val); sprintf(rrdBuff + strlen(rrdBuff), ":%s", rrded ? rrded : "U"); } return 0; } /* For scanning and logging, we need extra information */ beep = get_flag(chip, feature->beepNumber); if (beep == -1) return -1; formatted = feature->format(val, alrm, beep); if (!formatted) { sensorLog(LOG_ERR, "Error formatting sensor data"); return -1; } /* FIXME: It would be more efficient to store the label at * initialization time. */ label = sensors_get_label(chip, feature->feature); if (!label) { sensorLog(LOG_ERR, "Error getting sensor label: %s/%s", chip->prefix, feature->feature->name); return -1; } if (action == DO_READ) sensorLog(LOG_INFO, " %s: %s", label, formatted); else sensorLog(LOG_ALERT, "Sensor alarm: Chip %s: %s: %s", chipName(chip), label, formatted); free(label); return 0; }
/* * This is a static method; self is NULL. */ static PyObject* parse_chip_name(ChipName *self, PyObject *args, PyObject *kwargs) { char *kwlist[] = {"orig_name", NULL}; const char *orig_name = NULL; (void)self; if (!PyArg_ParseTupleAndKeywords(args, kwargs, "s", kwlist, &orig_name)) { return NULL; } sensors_chip_name name = {NULL, {0, 0}, 0, NULL}; int status = sensors_parse_chip_name(orig_name, &name); if (status < 0) { PyErr_SetString(SensorsException, sensors_strerror(status)); return NULL; } ChipName *py_chip_name = PyObject_New(ChipName, &ChipNameType); if (py_chip_name == NULL) { return NULL; } /* * Copy the content of the obtained name into py_chip_name. We * duplicate the strings, because name is supposed to freed by * sensors_free_chip_name(), instead of our own destructor. * Looking at the code of libsensors, it seems that it would * actually work just fine if our destructor cleaned it up, but I * prefer to be on the safe side. */ py_chip_name->chip_name = name; if (name.prefix == NULL) { py_chip_name->chip_name.prefix = NULL; py_chip_name->py_prefix = Py_None; Py_INCREF(py_chip_name->py_prefix); } else { py_chip_name->chip_name.prefix = strdup(name.prefix); py_chip_name->py_prefix = PyString_FromString(name.prefix); } if (name.path == NULL) { py_chip_name->chip_name.path = NULL; py_chip_name->py_path = Py_None; Py_INCREF(py_chip_name->py_path); } else { py_chip_name->chip_name.path = strdup(name.path); py_chip_name->py_path = PyString_FromString(name.path); } sensors_free_chip_name(&name); return (PyObject*)py_chip_name; }