diff --git a/prog/sensors/chips.c b/prog/sensors/chips.c index 5b9f08f0..668f508f 100644 --- a/prog/sensors/chips.c +++ b/prog/sensors/chips.c @@ -67,6 +67,54 @@ void print_chip_raw(const sensors_chip_name *name) } } +void print_chip_json(const sensors_chip_name *name) +{ + int a, b, cnt, subCnt, err; + const sensors_feature *feature; + const sensors_subfeature *sub; + char *label; + double val; + + a = 0; + cnt = 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; + } + if (cnt > 0) + printf(",\n"); + printf(" \"%s\":{\n", label); + free(label); + + b = 0; + subCnt = 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 { + if (subCnt > 0) + printf(",\n"); + printf(" \"%s\": %.3f", sub->name, val); + } + + } else { + printf("(%s)", label); + } + subCnt++; + } + printf("\n }"); + cnt++; + } + printf("\n"); +} + static const char hyst_str[] = "hyst"; static inline double deg_ctof(double cel) diff --git a/prog/sensors/chips.h b/prog/sensors/chips.h index 6bf21bb4..62f52f99 100644 --- a/prog/sensors/chips.h +++ b/prog/sensors/chips.h @@ -48,6 +48,7 @@ struct sensor_subfeature_list { }; void print_chip_raw(const sensors_chip_name *name); +void print_chip_json(const sensors_chip_name *name); void print_chip(const sensors_chip_name *name); #endif /* def PROG_SENSORS_CHIPS_H */ diff --git a/prog/sensors/main.c b/prog/sensors/main.c index 8effb850..f535b665 100644 --- a/prog/sensors/main.c +++ b/prog/sensors/main.c @@ -41,7 +41,7 @@ #define PROGRAM "sensors" #define VERSION LM_VERSION -static int do_sets, do_raw, hide_adapter; +static int do_sets, do_raw, do_json, hide_adapter; int fahrenheit; char degstr[5]; /* store the correct string to print degrees */ @@ -61,6 +61,7 @@ static void print_long_help(void) " -A, --no-adapter Do not show adapter for each chip\n" " --bus-list Generate bus statements for sensors.conf\n" " -u Raw output\n" + " -j Json output\n" " -v, --version Display the program version\n" "\n" "Use `-' after `-c' to read the config file from stdin.\n" @@ -173,6 +174,20 @@ static void do_a_print(const sensors_chip_name *name) printf("\n"); } +static void do_a_json_print(const sensors_chip_name *name) +{ + printf(" \"%s\":{\n", sprintf_chip_name(name)); + if (!hide_adapter) { + const char *adap = sensors_get_adapter_name(&name->bus); + if (adap) + printf(" \"Adapter\": \"%s\",\n", adap); + else + fprintf(stderr, "Can't get adapter name\n"); + } + print_chip_json(name); + printf(" }"); +} + /* returns 1 on error */ static int do_a_set(const sensors_chip_name *name) { @@ -204,15 +219,26 @@ static int do_the_real_work(const sensors_chip_name *match, int *err) int chip_nr; int cnt = 0; + if (do_json) + printf("{\n"); chip_nr = 0; while ((chip = sensors_get_detected_chips(match, &chip_nr))) { if (do_sets) { if (do_a_set(chip)) *err = 1; - } else - do_a_print(chip); + } else { + if (do_json) { + if (cnt > 0) + printf(",\n"); + do_a_json_print(chip); + } else { + do_a_print(chip); + } + } cnt++; } + if (do_json) + printf("\n}\n"); return cnt; } @@ -261,11 +287,12 @@ int main(int argc, char *argv[]) setlocale(LC_CTYPE, ""); do_raw = 0; + do_json = 0; do_sets = 0; do_bus_list = 0; hide_adapter = 0; while (1) { - c = getopt_long(argc, argv, "hsvfAc:u", long_opts, NULL); + c = getopt_long(argc, argv, "hsvfAc:uj", long_opts, NULL); if (c == EOF) break; switch(c) { @@ -294,6 +321,9 @@ int main(int argc, char *argv[]) case 'u': do_raw = 1; break; + case 'j': + do_json = 1; + break; case 'B': do_bus_list = 1; break; diff --git a/prog/sensors/sensors.1 b/prog/sensors/sensors.1 index f10e7c8a..ea0e238e 100644 --- a/prog/sensors/sensors.1 +++ b/prog/sensors/sensors.1 @@ -66,6 +66,8 @@ Raw output. This mode is suitable for debugging and for post-processing 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. .IP "-v, --version" Print the program version and exit. .IP "-f, --fahrenheit"