diff options
author | Simon Quigley <tsimonq2@ubuntu.com> | 2017-08-16 04:32:39 -0500 |
---|---|---|
committer | Simon Quigley <tsimonq2@ubuntu.com> | 2017-08-16 04:32:39 -0500 |
commit | 9a9db98089717990cd5e0eef529f6bb0819ebe46 (patch) | |
tree | a9afaabce984d5fe552fa8bf1a9405db9bdd2699 /modules/devices/sensors.c | |
parent | 69a2124e9a081518297951256eb5c8d72d93361f (diff) |
New upstream version 0.5.1+git20170815
Diffstat (limited to 'modules/devices/sensors.c')
-rw-r--r-- | modules/devices/sensors.c | 535 |
1 files changed, 272 insertions, 263 deletions
diff --git a/modules/devices/sensors.c b/modules/devices/sensors.c index 84e89361..c9d78ff7 100644 --- a/modules/devices/sensors.c +++ b/modules/devices/sensors.c @@ -27,8 +27,7 @@ gchar *sensors = NULL; GHashTable *sensor_compute = NULL; GHashTable *sensor_labels = NULL; -static void read_sensor_labels(gchar * driver) -{ +static void read_sensor_labels(gchar *driver) { FILE *conf; gchar buf[256], *line, *p; gboolean lock = FALSE; @@ -38,7 +37,8 @@ static void read_sensor_labels(gchar * driver) conf = fopen("/etc/sensors3.conf", "r"); /* If it fails, try to open sensors.conf */ - if (!conf) conf = fopen("/etc/sensors.conf", "r"); + if (!conf) + conf = fopen("/etc/sensors.conf", "r"); if (!conf) { /* Cannot open config file. */ @@ -46,117 +46,129 @@ static void read_sensor_labels(gchar * driver) } while (fgets(buf, 256, conf)) { - line = buf; - - remove_linefeed(line); - strend(line, '#'); - - if (*line == '\0') { - continue; - } else if (lock && strstr(line, "label")) { /* label lines */ - gchar **names = g_strsplit(strstr(line, "label") + 5, " ", 0); - gchar *name = NULL, *value = NULL; - - for (i = 0; names[i]; i++) { - if (names[i][0] == '\0') - continue; - - if (!name) - name = g_strdup(names[i]); - else if (!value) - value = g_strdup(names[i]); - else - value = g_strconcat(value, " ", names[i], NULL); - } - - remove_quotes(value); - g_hash_table_insert(sensor_labels, name, value); - - g_strfreev(names); - } else if (lock && strstr(line, "ignore")) { /* ignore lines */ - p = strstr(line, "ignore") + 6; - if (!strchr(p, ' ')) - continue; - - while (*p == ' ') - p++; - g_hash_table_insert(sensor_labels, g_strdup(p), "ignore"); - } else if (lock && strstr(line, "compute")) { /* compute lines */ - gchar **formulas = - g_strsplit(strstr(line, "compute") + 7, " ", 0); - gchar *name = NULL, *formula = NULL; - - for (i = 0; formulas[i]; i++) { - if (formulas[i][0] == '\0') - continue; - if (formulas[i][0] == ',') - break; - - if (!name) - name = g_strdup(formulas[i]); - else if (!formula) - formula = g_strdup(formulas[i]); - else - formula = g_strconcat(formula, formulas[i], NULL); - } - - g_strfreev(formulas); - g_hash_table_insert(sensor_compute, name, - math_string_to_postfix(formula)); - } else if (g_str_has_prefix(line, "chip")) { /* chip lines (delimiter) */ - if (lock == FALSE) { - gchar **chips = g_strsplit(line, " ", 0); - - for (i = 1; chips[i]; i++) { - strend(chips[i], '*'); - - if (g_str_has_prefix(chips[i] + 1, driver)) { - lock = TRUE; - break; - } - } - - g_strfreev(chips); - } else { - break; - } - } + line = buf; + + remove_linefeed(line); + strend(line, '#'); + + if (*line == '\0') { + continue; + } else if (lock && strstr(line, "label")) { /* label lines */ + gchar **names = g_strsplit(strstr(line, "label") + 5, " ", 0); + gchar *name = NULL, *value = NULL; + + for (i = 0; names[i]; i++) { + if (names[i][0] == '\0') + continue; + + if (!name) + name = g_strdup(names[i]); + else if (!value) + value = g_strdup(names[i]); + else + value = g_strconcat(value, " ", names[i], NULL); + } + + remove_quotes(value); + g_hash_table_insert(sensor_labels, name, value); + + g_strfreev(names); + } else if (lock && strstr(line, "ignore")) { /* ignore lines */ + p = strstr(line, "ignore") + 6; + if (!strchr(p, ' ')) + continue; + + while (*p == ' ') + p++; + g_hash_table_insert(sensor_labels, g_strdup(p), "ignore"); + } else if (lock && strstr(line, "compute")) { /* compute lines */ + gchar **formulas = g_strsplit(strstr(line, "compute") + 7, " ", 0); + gchar *name = NULL, *formula = NULL; + + for (i = 0; formulas[i]; i++) { + if (formulas[i][0] == '\0') + continue; + if (formulas[i][0] == ',') + break; + + if (!name) + name = g_strdup(formulas[i]); + else if (!formula) + formula = g_strdup(formulas[i]); + else + formula = g_strconcat(formula, formulas[i], NULL); + } + + g_strfreev(formulas); + g_hash_table_insert(sensor_compute, name, + math_string_to_postfix(formula)); + } else if (g_str_has_prefix(line, + "chip")) { /* chip lines (delimiter) */ + if (lock == FALSE) { + gchar **chips = g_strsplit(line, " ", 0); + + for (i = 1; chips[i]; i++) { + strend(chips[i], '*'); + + if (g_str_has_prefix(chips[i] + 1, driver)) { + lock = TRUE; + break; + } + } + + g_strfreev(chips); + } else { + break; + } + } } fclose(conf); } -static gchar *get_sensor_label(gchar * sensor) -{ +static void add_sensor(const char *type, + const char *sensor, + const char *driver, + double value, + const char *unit) { + char key[64]; + + sensors = h_strdup_cprintf("%s/%s=%.2f%s|%s\n", sensors, + driver, sensor, value, unit, type); + + snprintf(key, sizeof(key), "%s/%s", driver, sensor); + moreinfo_add_with_prefix("DEV", key, g_strdup_printf("%.2f%s", value, unit)); + + lginterval = h_strdup_cprintf("UpdateInterval$%s=1000\n", lginterval, key); +} + +static gchar *get_sensor_label(gchar *sensor) { gchar *ret; ret = g_hash_table_lookup(sensor_labels, sensor); if (!ret) - ret = g_strdup(sensor); + ret = g_strdup(sensor); else - ret = g_strdup(ret); + ret = g_strdup(ret); return ret; } -static float adjust_sensor(gchar * name, float value) -{ +static float adjust_sensor(gchar *name, float value) { GSList *postfix; postfix = g_hash_table_lookup(sensor_compute, name); if (!postfix) - return value; + return value; return math_postfix_eval(postfix, value); } -static char *get_sensor_path(int number, const char *prefix) -{ +static char *get_sensor_path(int number, const char *prefix) { return g_strdup_printf("/sys/class/hwmon/hwmon%d/%s", number, prefix); } -static char *determine_driver_for_hwmon_path(char *path) -{ +static char *determine_driver_for_hwmon_path(char *path) { char *tmp, *driver; tmp = g_strdup_printf("%s/device/driver", path); @@ -190,165 +202,171 @@ struct HwmonSensor { const char *friendly_name; const char *path_format; const char *key_format; - const char *value_format; + const char *unit; const float adjust_ratio; const int begin_at; }; static const struct HwmonSensor hwmon_sensors[] = { - { "Cooling Fans", "%s/fan%d_input", "fan%d", "%s (%s)=%.0fRPM\n", 1.0, 1 }, - { "Temperature", "%s/temp%d_input", "temp%d", "%s (%s)=%.2f\302\260C\n", 1000.0, 1 }, - { "Voltage Values", "%s/in%d_input", "in%d", "%s (%s)=%.3fV\n", 1000.0, 0 }, - { NULL, NULL, NULL, NULL, 0.0, 0 }, + { + "Fan", + "%s/fan%d_input", + "fan%d", + "RPM", + 1.0, + 1 + }, + { + "Temperature", + "%s/temp%d_input", + "temp%d", + "\302\260C", + 1000.0, + 1 + }, + { + "Voltage", + "%s/in%d_input", + "in%d", + "V", + 1000.0, + 0 + }, + { } }; -static const char *hwmon_prefix[] = { "device", "", NULL }; +static const char *hwmon_prefix[] = {"device", "", NULL}; -static void read_sensors_hwmon(void) -{ +static void read_sensors_hwmon(void) { int hwmon, count; gchar *path_hwmon, *path_sensor, *tmp, *driver, *name, *mon; const char **prefix; - + for (prefix = hwmon_prefix; *prefix; prefix++) { - hwmon = 0; - path_hwmon = get_sensor_path(hwmon, *prefix); - while (path_hwmon && g_file_test(path_hwmon, G_FILE_TEST_EXISTS)) { - const struct HwmonSensor *sensor; - - driver = determine_driver_for_hwmon_path(path_hwmon); - DEBUG("hwmon%d has driver=%s", hwmon, driver); - - if (!sensor_labels) { - read_sensor_labels(driver); - } - - for (sensor = hwmon_sensors; sensor->friendly_name; sensor++) { - char *output = NULL; - DEBUG("current sensor type=%s", sensor->friendly_name); - - for (count = sensor->begin_at;; count++) { - path_sensor = g_strdup_printf(sensor->path_format, path_hwmon, count); - DEBUG("should be reading from %s", path_sensor); - if (!g_file_get_contents(path_sensor, &tmp, NULL, NULL)) { - g_free(path_sensor); - if (count<256) - continue; // brute-force find all - else - break; - } - - mon = g_strdup_printf(sensor->key_format, count); - name = get_sensor_label(mon); - if (!g_str_equal(name, "ignore")) { - output = h_strdup_cprintf(sensor->value_format, - output, name, driver, - adjust_sensor(mon, - atof(tmp) / sensor->adjust_ratio)); - } - - g_free(tmp); - g_free(mon); - g_free(name); - g_free(path_sensor); - } - - if (output) { - sensors = g_strconcat(sensors, "[", sensor->friendly_name, "]\n", output, "\n", NULL); - g_free(output); - } - } - - g_free(path_hwmon); - g_free(driver); - - path_hwmon = get_sensor_path(++hwmon, *prefix); - } - - g_free(path_hwmon); - } + hwmon = 0; + path_hwmon = get_sensor_path(hwmon, *prefix); + while (path_hwmon && g_file_test(path_hwmon, G_FILE_TEST_EXISTS)) { + const struct HwmonSensor *sensor; + + driver = determine_driver_for_hwmon_path(path_hwmon); + DEBUG("hwmon%d has driver=%s", hwmon, driver); + + if (!sensor_labels) { + read_sensor_labels(driver); + } + + for (sensor = hwmon_sensors; sensor->friendly_name; sensor++) { + DEBUG("current sensor type=%s", sensor->friendly_name); + + for (count = sensor->begin_at;; count++) { + path_sensor = + g_strdup_printf(sensor->path_format, path_hwmon, count); + DEBUG("should be reading from %s", path_sensor); + if (!g_file_get_contents(path_sensor, &tmp, NULL, NULL)) { + g_free(path_sensor); + if (count < 256) + continue; // brute-force find all + else + break; + } + + mon = g_strdup_printf(sensor->key_format, count); + name = get_sensor_label(mon); + if (!g_str_equal(name, "ignore")) { + float adjusted = adjust_sensor(mon, + atof(tmp) / sensor->adjust_ratio); + + add_sensor(sensor->friendly_name, + name, + driver, + adjusted, + sensor->unit); + } + + g_free(tmp); + g_free(mon); + g_free(name); + g_free(path_sensor); + } + } + + g_free(path_hwmon); + g_free(driver); + + path_hwmon = get_sensor_path(++hwmon, *prefix); + } + + g_free(path_hwmon); + } } -static void read_sensors_acpi(void) -{ +static void read_sensors_acpi(void) { const gchar *path_tz = "/proc/acpi/thermal_zone"; if (g_file_test(path_tz, G_FILE_TEST_EXISTS)) { - GDir *tz; - - if ((tz = g_dir_open(path_tz, 0, NULL))) { - const gchar *entry; - gchar *temp = g_strdup(""); - - while ((entry = g_dir_read_name(tz))) { - gchar *path = - g_strdup_printf("%s/%s/temperature", path_tz, entry); - gchar *contents; + GDir *tz; - if (g_file_get_contents(path, &contents, NULL, NULL)) { - int temperature; + if ((tz = g_dir_open(path_tz, 0, NULL))) { + const gchar *entry; - sscanf(contents, "temperature: %d C", &temperature); + while ((entry = g_dir_read_name(tz))) { + gchar *path = + g_strdup_printf("%s/%s/temperature", path_tz, entry); + gchar *contents; - temp = h_strdup_cprintf("\n%s=%d\302\260C\n", - temp, entry, temperature); + if (g_file_get_contents(path, &contents, NULL, NULL)) { + int temperature; - g_free(contents); - } - } + sscanf(contents, "temperature: %d C", &temperature); - if (*temp != '\0') - sensors = - h_strdup_cprintf("\n[ACPI Thermal Zone]\n%s", - sensors, temp); + add_sensor("Temperature", + entry, + "ACPI Thermal Zone", + temperature, + "\302\260C"); + } + } - g_dir_close(tz); - } + g_dir_close(tz); + } } - } -static void read_sensors_sys_thermal(void) -{ +static void read_sensors_sys_thermal(void) { const gchar *path_tz = "/sys/class/thermal"; if (g_file_test(path_tz, G_FILE_TEST_EXISTS)) { - GDir *tz; - - if ((tz = g_dir_open(path_tz, 0, NULL))) { - const gchar *entry; - gchar *temp = g_strdup(""); + GDir *tz; - while ((entry = g_dir_read_name(tz))) { - gchar *path = - g_strdup_printf("%s/%s/temp", path_tz, entry); - gchar *contents; + if ((tz = g_dir_open(path_tz, 0, NULL))) { + const gchar *entry; + gchar *temp = g_strdup(""); - if (g_file_get_contents(path, &contents, NULL, NULL)) { - int temperature; + while ((entry = g_dir_read_name(tz))) { + gchar *path = g_strdup_printf("%s/%s/temp", path_tz, entry); + gchar *contents; - sscanf(contents, "%d", &temperature); + if (g_file_get_contents(path, &contents, NULL, NULL)) { + int temperature; - temp = h_strdup_cprintf("\n%s=%.2f\302\260C\n", - temp, entry, (1.0*temperature/1000)); + sscanf(contents, "%d", &temperature); - g_free(contents); - } - } + add_sensor("Temperature", + entry, + "thermal", + temperature / 1000.0, + "\302\260C"); - if (*temp != '\0') - sensors = - h_strdup_cprintf("\n[ACPI Thermal Zone (sysfs)]\n%s", - sensors, temp); + g_free(contents); + } + } - g_dir_close(tz); - } + g_dir_close(tz); + } } - } -static void read_sensors_omnibook(void) -{ +static void read_sensors_omnibook(void) { const gchar *path_ob = "/proc/omnibook/temperature"; gchar *contents; @@ -357,88 +375,79 @@ static void read_sensors_omnibook(void) sscanf(contents, "CPU temperature: %d C", &temperature); - sensors = h_strdup_cprintf("\n[Omnibook]\n" - "CPU temperature=%d\302\260C\n", - sensors, temperature); + add_sensor("Temperature", + "CPU", + "omnibook", + temperature, + "\302\260C\n"); g_free(contents); } } -static void read_sensors_hddtemp(void) -{ +static void read_sensors_hddtemp(void) { Socket *s; - static gchar *old = NULL; gchar buffer[1024]; gint len = 0; - if ((s = sock_connect("127.0.0.1", 7634))) { - while (!len) - len = sock_read(s, buffer, sizeof(buffer)); - sock_close(s); - - if (len > 2 && buffer[0] == '|' && buffer[1] == '/') { - gchar **disks; - int i; - - g_free(old); - - old = g_strdup("[Hard Disk Temperature]\n"); - - disks = g_strsplit(buffer, "\n", 0); - for (i = 0; disks[i]; i++) { - gchar **fields = g_strsplit(disks[i] + 1, "|", 5); - - /* - * 0 -> /dev/hda - * 1 -> FUJITSU MHV2080AH - * 2 -> 41 - * 3 -> C - */ - old = h_strdup_cprintf("\n%s (%s)=%s\302\260%s\n", - old, - fields[1], fields[0], - fields[2], fields[3]); - - g_strfreev(fields); - } + if (!(s = sock_connect("127.0.0.1", 7634))) + return; - g_strfreev(disks); - } - } else { - g_free(old); - old = NULL; - } + while (!len) + len = sock_read(s, buffer, sizeof(buffer)); + sock_close(s); + + if (len > 2 && buffer[0] == '|' && buffer[1] == '/') { + gchar **disks; + int i; + + disks = g_strsplit(buffer, "\n", 0); + for (i = 0; disks[i]; i++) { + gchar **fields = g_strsplit(disks[i] + 1, "|", 5); + + /* + * 0 -> /dev/hda + * 1 -> FUJITSU MHV2080AH + * 2 -> 41 + * 3 -> C + */ + const gchar *unit = strcmp(fields[3], "C") + ? "\302\260C" : "\302\260F"; + add_sensor("Hard Drive", + fields[1], + "hddtemp", + atoi(fields[2]), + unit); + + g_strfreev(fields); + } - if (old) { - sensors = g_strconcat(sensors, "\n", old, NULL); + g_strfreev(disks); } } -void scan_sensors_do(void) -{ +void scan_sensors_do(void) { g_free(sensors); - sensors = g_strdup(""); + g_free(lginterval); + lginterval = g_strdup(""); + read_sensors_hwmon(); read_sensors_acpi(); read_sensors_sys_thermal(); read_sensors_omnibook(); read_sensors_hddtemp(); - /* FIXME: Add support for ibm acpi and more sensors */ } -void sensors_init(void) -{ - sensor_labels = g_hash_table_new_full(g_str_hash, g_str_equal, - g_free, g_free); +void sensors_init(void) { + sensor_labels = + g_hash_table_new_full(g_str_hash, g_str_equal, g_free, g_free); sensor_compute = g_hash_table_new(g_str_hash, g_str_equal); } -void sensors_shutdown(void) -{ +void sensors_shutdown(void) { g_hash_table_destroy(sensor_labels); g_hash_table_destroy(sensor_compute); } |