mirror of
https://github.com/lm-sensors/lm-sensors.git
synced 2026-11-02 13:30:30 +00:00
sensors: New json format option
Using -J rather than -j gives JSON with new, hopefully more usable structure.
This commit is contained in:
1 parent
3764b44396
commit
c6c5dfcffd
4 files changed
+68
-6
No files matched your search
+57
-4
@@ -99,11 +99,27 @@ void print_chip_json(const sensors_chip_name *name)
|
||||
}
|
||||
if (cnt++)
|
||||
printf(",");
|
||||
printf("\"%s\":{", label);
|
||||
subCnt = 0;
|
||||
if (new_json) {
|
||||
printf("\"%s\":{", feature->name);
|
||||
if (strcmp(label, feature->name)) {
|
||||
if (subCnt++)
|
||||
printf(",");
|
||||
printf("\"label\":\"%s\"", label);
|
||||
}
|
||||
} else
|
||||
printf("\"%s\":{", label);
|
||||
|
||||
b = 0;
|
||||
subCnt = 0;
|
||||
while ((sub = sensors_get_all_subfeatures(name, feature, &b))) {
|
||||
char *fname = sub->name;
|
||||
if (new_json) {
|
||||
if (strlen(feature->name) < strlen(sub->name)) {
|
||||
fname += strlen(feature->name);
|
||||
if (*fname == '_') /* FIXME some sensors use other separator */
|
||||
fname++;
|
||||
}
|
||||
}
|
||||
if (sub->flags & SENSORS_MODE_R) {
|
||||
is_temp = !strcmp("°C", sensors_get_quantity(sub->type)->unit);
|
||||
|
||||
@@ -118,13 +134,50 @@ void print_chip_json(const sensors_chip_name *name)
|
||||
printf(",");
|
||||
if (is_temp && fahrenheit)
|
||||
val = deg_ctof(val);
|
||||
printf("\"%s\":%.3f", sub->name, val);
|
||||
if (new_json) {
|
||||
const char *unit = sensors_get_quantity(sub->type)->unit;
|
||||
const char *quantity = sensors_get_quantity(sub->type)->quantity;
|
||||
int quaCnt = 0;
|
||||
|
||||
printf("\"%s\":{", fname);
|
||||
|
||||
if (is_temp && fahrenheit)
|
||||
unit = "°F";
|
||||
if (strrchr(quantity, ' '))
|
||||
quantity = strrchr(quantity, ' ') + 1;
|
||||
|
||||
if (sub->type == SENSORS_SUBFEATURE_TEMP_TYPE) {
|
||||
if (quaCnt++)
|
||||
printf(",");
|
||||
printf("\"label\":\"%s\"", sensors_temp_type_name(val));
|
||||
}
|
||||
|
||||
if (strlen(quantity)) {
|
||||
if (quaCnt++)
|
||||
printf(",");
|
||||
printf("\"quantity\":\"%s\"", quantity);
|
||||
}
|
||||
|
||||
if (strlen(unit)) {
|
||||
if (quaCnt++)
|
||||
printf(",");
|
||||
printf("\"unit\":\"%s\"", unit);
|
||||
}
|
||||
|
||||
if (quaCnt++)
|
||||
printf(",");
|
||||
printf("\"value\":%.16g", val);
|
||||
printf("}");
|
||||
|
||||
} else {
|
||||
printf("\"%s\":%f", fname, val);
|
||||
}
|
||||
}
|
||||
|
||||
} else {
|
||||
if (subCnt++)
|
||||
printf(",");
|
||||
printf("\"%s\":NaN", sub->name);
|
||||
printf("\"%s\":NaN", fname);
|
||||
}
|
||||
}
|
||||
free(label);
|
||||
|
||||
+7
-1
@@ -42,6 +42,7 @@
|
||||
#define VERSION LM_VERSION
|
||||
|
||||
static int do_sets, do_raw, do_json, hide_adapter;
|
||||
int new_json;
|
||||
|
||||
int fahrenheit;
|
||||
char degstr[5]; /* store the correct string to print degrees */
|
||||
@@ -296,12 +297,13 @@ int main(int argc, char *argv[])
|
||||
|
||||
do_raw = 0;
|
||||
do_json = 0;
|
||||
new_json = 0;
|
||||
do_sets = 0;
|
||||
do_bus_list = 0;
|
||||
hide_adapter = 0;
|
||||
allow_no_sensors = 0;
|
||||
while (1) {
|
||||
c = getopt_long(argc, argv, "hsvfAc:ujn", long_opts, NULL);
|
||||
c = getopt_long(argc, argv, "hsvfAc:ujJn", long_opts, NULL);
|
||||
if (c == EOF)
|
||||
break;
|
||||
switch(c) {
|
||||
@@ -333,6 +335,10 @@ int main(int argc, char *argv[])
|
||||
case 'j':
|
||||
do_json = 1;
|
||||
break;
|
||||
case 'J':
|
||||
do_json = 1;
|
||||
new_json = 1;
|
||||
break;
|
||||
case 'B':
|
||||
do_bus_list = 1;
|
||||
break;
|
||||
|
||||
@@ -22,6 +22,7 @@
|
||||
#define PROG_SENSORS_MAIN_H
|
||||
|
||||
extern int fahrenheit;
|
||||
extern int new_json;
|
||||
extern char degstr[5];
|
||||
|
||||
#endif /* PROG_SENSORS_MAIN_H */
|
||||
@@ -67,7 +67,9 @@ of the output by scripts. It is also useful when writing a configuration
|
||||
file because it shows the raw input names which must be referenced in the
|
||||
configuration file.
|
||||
.IP -j
|
||||
Json output. This mode is suitable for post-processing of the output by scripts.
|
||||
Json output.
|
||||
.IP -J
|
||||
New json output. This mode is suitable for post-processing of the output by scripts.
|
||||
.IP "-v, --version"
|
||||
Print the program version and exit.
|
||||
.IP "-f, --fahrenheit"
|
||||
|
||||
Reference in new issue
Block a user