Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add new sensors listing feature #2038

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/api/api.h
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ int api_info_metrics(struct ftl_conn *api);
int api_info_login(struct ftl_conn *api);
cJSON *read_sys_property(const char *path);
int get_system_obj(struct ftl_conn *api, cJSON *system);
int get_sensors_obj(struct ftl_conn *api, cJSON *sensors, const bool add_list);
int get_sensors_obj(cJSON *sensors, const bool add_list);
int get_version_obj(struct ftl_conn *api, cJSON *version);

// Config methods
Expand Down
55 changes: 27 additions & 28 deletions src/api/info.c
Original file line number Diff line number Diff line change
Expand Up @@ -240,8 +240,7 @@ int get_system_obj(struct ftl_conn *api, cJSON *system)
return 0;
}

static int read_hwmon_sensors(struct ftl_conn *api,
cJSON *array,
static int read_hwmon_sensors(cJSON *array,
const char *path,
const char *value_path,
const unsigned int sensor_id)
Expand Down Expand Up @@ -279,11 +278,11 @@ static int read_hwmon_sensors(struct ftl_conn *api,
// Remove newline if present
char *p = strchr(label, '\n');
if (p != NULL) *p = '\0';
JSON_COPY_STR_TO_OBJECT(item, "name", label);
cJSON_AddStringToObject(item, "name", label);
}
else
{
JSON_ADD_NULL_TO_OBJECT(item, "name");
cJSON_AddNullToObject(item, "name");
}
if(f_label != NULL)
fclose(f_label);
Expand Down Expand Up @@ -330,18 +329,18 @@ static int read_hwmon_sensors(struct ftl_conn *api,
max += 273.15;
crit += 273.15;
}
JSON_ADD_NUMBER_TO_OBJECT(item, "value", temp);
cJSON_AddNumberToObject(item, "value", temp);
if(has_max)
JSON_ADD_NUMBER_TO_OBJECT(item, "max", max);
cJSON_AddNumberToObject(item, "max", max);
else
JSON_ADD_NULL_TO_OBJECT(item, "max");
cJSON_AddNullToObject(item, "max");
if(has_crit)
JSON_ADD_NUMBER_TO_OBJECT(item, "crit", crit);
cJSON_AddNumberToObject(item, "crit", crit);
else
JSON_ADD_NULL_TO_OBJECT(item, "crit");
JSON_COPY_STR_TO_OBJECT(item, "sensor", name);
cJSON_AddNullToObject(item, "crit");
cJSON_AddStringToObject(item, "sensor", name);

JSON_ADD_ITEM_TO_ARRAY(array, item);
cJSON_AddItemToArray(array, item);
}

if(f_value != NULL)
Expand All @@ -351,7 +350,7 @@ static int read_hwmon_sensors(struct ftl_conn *api,
return 0;
}

static int get_hwmon_sensors(struct ftl_conn *api, cJSON *sensors)
static int get_hwmon_sensors(cJSON *sensors)
{
int ret;
// Source available temperatures, we try to read temperature sensors from
Expand Down Expand Up @@ -411,18 +410,18 @@ static int get_hwmon_sensors(struct ftl_conn *api, cJSON *sensors)
break;

// Create sensor array item
cJSON *hwmon = JSON_NEW_OBJECT();
JSON_COPY_STR_TO_OBJECT(hwmon, "name", name);
JSON_COPY_STR_TO_OBJECT(hwmon, "path", dircontent->d_name);
cJSON *hwmon = cJSON_CreateObject();
cJSON_AddStringToObject(hwmon, "name", name);
cJSON_AddStringToObject(hwmon, "path", dircontent->d_name);

// Get symlink target
char *target = get_hwmon_target(dirpath);
JSON_COPY_STR_TO_OBJECT(hwmon, "source", target);
cJSON_AddStringToObject(hwmon, "source", target);
free(target);

cJSON *temps = JSON_NEW_ARRAY();
JSON_ADD_ITEM_TO_OBJECT(hwmon, "temps", temps);
JSON_ADD_ITEM_TO_ARRAY(sensors, hwmon);
cJSON *temps = cJSON_CreateArray();
cJSON_AddItemToObject(hwmon, "temps", temps);
cJSON_AddItemToArray(sensors, hwmon);

// Iterate over /sys/class/hwmon/hwmonX/tempY_...
DIR *sensor_dir = opendir(dirpath);
Expand Down Expand Up @@ -451,7 +450,7 @@ static int get_hwmon_sensors(struct ftl_conn *api, cJSON *sensors)
strncat(value_path, dircontent_sensor->d_name, sizeof(value_path)-strlen(value_path)-1);

// Read sensor
ret = read_hwmon_sensors(api, temps, dirpath, value_path, sensor_id);
ret = read_hwmon_sensors(temps, dirpath, value_path, sensor_id);
if(ret != 0)
break;
}
Expand Down Expand Up @@ -641,11 +640,11 @@ int api_info_host(struct ftl_conn *api)
JSON_SEND_OBJECT(json);
}

int get_sensors_obj(struct ftl_conn *api, cJSON *sensors, const bool add_list)
int get_sensors_obj(cJSON *sensors, const bool add_list)
{
// Get sensors array
cJSON *list = JSON_NEW_ARRAY();
int ret = get_hwmon_sensors(api, list);
int ret = get_hwmon_sensors(list);
if (ret != 0)
return ret;
if(add_list)
Expand Down Expand Up @@ -687,25 +686,25 @@ int get_sensors_obj(struct ftl_conn *api, cJSON *sensors, const bool add_list)
cJSON *first_sensor = cJSON_GetArrayItem(sensors_array, 0);
cJSON *first_sensor_value = cJSON_GetObjectItemCaseSensitive(first_sensor, "value");
if(cJSON_IsNumber(first_sensor_value))
JSON_ADD_NUMBER_TO_OBJECT(sensors, "cpu_temp", first_sensor_value->valuedouble);
cJSON_AddNumberToObject(sensors, "cpu_temp", first_sensor_value->valuedouble);
else
JSON_ADD_NULL_TO_OBJECT(sensors, "cpu_temp");
cJSON_AddNullToObject(sensors, "cpu_temp");
}
else
{
JSON_ADD_NULL_TO_OBJECT(sensors, "cpu_temp");
cJSON_AddNullToObject(sensors, "cpu_temp");
}

// Add hot limit
JSON_ADD_NUMBER_TO_OBJECT(sensors, "hot_limit", config.webserver.api.temp.limit.v.d);
cJSON_AddNumberToObject(sensors, "hot_limit", config.webserver.api.temp.limit.v.d);

// Add unit
const char *unit = "C";
if(config.webserver.api.temp.unit.v.temp_unit == TEMP_UNIT_F)
unit = "F";
else if(config.webserver.api.temp.unit.v.temp_unit == TEMP_UNIT_K)
unit = "K";
JSON_REF_STR_IN_OBJECT(sensors, "unit", unit);
cJSON_AddStringReferenceToObject(sensors, "unit", unit);

if(!add_list)
cJSON_Delete(list);
Expand All @@ -716,7 +715,7 @@ int get_sensors_obj(struct ftl_conn *api, cJSON *sensors, const bool add_list)
int api_info_sensors(struct ftl_conn *api)
{
cJSON *sensors = JSON_NEW_OBJECT();
int ret = get_sensors_obj(api, sensors, true);
int ret = get_sensors_obj(sensors, true);
if (ret != 0)
return ret;

Expand Down
2 changes: 1 addition & 1 deletion src/api/padd.c
Original file line number Diff line number Diff line change
Expand Up @@ -301,7 +301,7 @@ int api_padd(struct ftl_conn *api)

// info/sensors -> CPU temp sensor
cJSON *sensors = JSON_NEW_OBJECT();
get_sensors_obj(api, sensors, false);
get_sensors_obj(sensors, false);
JSON_ADD_ITEM_TO_OBJECT(json, "sensors", sensors);

JSON_SEND_OBJECT(json);
Expand Down
26 changes: 25 additions & 1 deletion src/args.c
Original file line number Diff line number Diff line change
Expand Up @@ -302,7 +302,6 @@ void parse_args(int argc, char* argv[])
exit(printTOTP());
}


// Create teleporter archive through CLI
if(argc == 2 && strcmp(argv[1], "--teleporter") == 0)
{
Expand Down Expand Up @@ -568,6 +567,30 @@ void parse_args(int argc, char* argv[])
exit(EXIT_SUCCESS);
}


// Export sensors in human-readable JSON format
if(argc == 2 && strcmp(argv[1], "--sensors") == 0)
{
// Enable stdout printing
cli_mode = true;
log_ctrl(false, true);

cJSON *sensors = cJSON_CreateObject();
int ret = get_sensors_obj(sensors, true);

// Print result
if(ret == 0)
{
char *json = cJSON_Print(sensors);
printf("%s\n", json);
free(json);
}
else
printf("No sensors found\n");
cJSON_Delete(sensors);
exit(EXIT_SUCCESS);
}

// start from 1, as argv[0] is the executable name
for(int i = 1; i < argc; i++)
{
Expand Down Expand Up @@ -1081,6 +1104,7 @@ void parse_args(int argc, char* argv[])
printf("\t authentication (if enabled)\n");
printf("\t%s--perf%s Run performance-tests based on the\n", green, normal);
printf("\t BALLOON password-hashing algorithm\n");
printf("\t%s--sensors%s Return JSON-formatted sensor data\n", green, normal);
printf("\t%s--%s [OPTIONS]%s Pass OPTIONS to internal dnsmasq resolver\n", green, cyan, normal);
printf("\t%s-h%s, %shelp%s Display this help and exit\n\n", green, normal, green, normal);
exit(EXIT_SUCCESS);
Expand Down
Loading