2
0
mirror of https://github.com/lm-sensors/lm-sensors synced 2025-09-03 15:55:15 +00:00

Added feature to sensors to allow json output.

* Added -j option to sensors that outputs json.
* -j option overrides raw option

Signed-off-by: Matthew SL Tiffany <shadethedemon@gmail.com>
Signed-off-by: Guenter Roeck <linux@roeck-us.net>
This commit is contained in:
Matthew Tiffany
2017-03-28 11:58:26 -04:00
committed by Guenter Roeck
parent 90f7e3c069
commit 1b36ea4299
4 changed files with 85 additions and 4 deletions

View File

@@ -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)

View File

@@ -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 */

View File

@@ -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
} 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;

View File

@@ -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"