| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
#ifndef _GNU_SOURCE |
| 22 |
# define _GNU_SOURCE |
| 23 |
#endif |
| 24 |
|
| 25 |
#include <linux/limits.h> |
| 26 |
#include <linux/magic.h> |
| 27 |
|
| 28 |
#include <sys/types.h> |
| 29 |
#include <sys/vfs.h> |
| 30 |
|
| 31 |
#include <ctype.h> |
| 32 |
#include <dirent.h> |
| 33 |
#include <errno.h> |
| 34 |
#include <stdarg.h> |
| 35 |
#include <stdio.h> |
| 36 |
#include <stdlib.h> |
| 37 |
#include <string.h> |
| 38 |
#include <unistd.h> |
| 39 |
|
| 40 |
#include "common.h" |
| 41 |
#include "logging.h" |
| 42 |
#include "string-macros.h" |
| 43 |
#include "messages.h" |
| 44 |
#include "sysfsparser.h" |
| 45 |
#include "xasprintf.h" |
| 46 |
|
| 47 |
#define PATH_SYS_SYSTEM PATH_SYS "/devices/system" |
| 48 |
#define PATH_SYS_CPU PATH_SYS_SYSTEM "/cpu" |
| 49 |
|
| 50 |
enum sysfsparser_cpufreq_sysfile_numeric_id |
| 51 |
{ |
| 52 |
CPUINFO_CUR_FREQ, |
| 53 |
CPUINFO_MIN_FREQ, |
| 54 |
CPUINFO_MAX_FREQ, |
| 55 |
CPUINFO_LATENCY, |
| 56 |
SCALING_CUR_FREQ, |
| 57 |
SCALING_MIN_FREQ, |
| 58 |
SCALING_MAX_FREQ, |
| 59 |
MAX_VALUE_FILES |
| 60 |
}; |
| 61 |
|
| 62 |
static const char * |
| 63 |
sysfsparser_devices_system_cpu_file_numeric[MAX_VALUE_FILES] = { |
| 64 |
[CPUINFO_CUR_FREQ] = "cpuinfo_cur_freq", |
| 65 |
[CPUINFO_MIN_FREQ] = "cpuinfo_min_freq", |
| 66 |
[CPUINFO_MAX_FREQ] = "cpuinfo_max_freq", |
| 67 |
[CPUINFO_LATENCY] = "cpuinfo_transition_latency", |
| 68 |
[SCALING_CUR_FREQ] = "scaling_cur_freq", |
| 69 |
[SCALING_MIN_FREQ] = "scaling_min_freq", |
| 70 |
[SCALING_MAX_FREQ] = "scaling_max_freq" |
| 71 |
}; |
| 72 |
|
| 73 |
enum sysfsparser_cpufreq_sysfile_string_id |
| 74 |
{ |
| 75 |
SCALING_DRIVER, |
| 76 |
SCALING_GOVERNOR, |
| 77 |
SCALING_AVAILABLE_GOVERNORS, |
| 78 |
SCALING_AVAILABLE_FREQS, |
| 79 |
MAX_STRING_FILES |
| 80 |
}; |
| 81 |
|
| 82 |
static const char * |
| 83 |
sysfsparser_devices_system_cpu_file_string[MAX_STRING_FILES] = { |
| 84 |
[SCALING_DRIVER] = "scaling_driver", |
| 85 |
[SCALING_GOVERNOR] = "scaling_governor", |
| 86 |
[SCALING_AVAILABLE_GOVERNORS] = "scaling_available_governors", |
| 87 |
[SCALING_AVAILABLE_FREQS] = "scaling_available_frequencies" |
| 88 |
}; |
| 89 |
|
| 90 |
void |
| 91 |
sysfsparser_check_for_sysfs (void) |
| 92 |
{ |
| 93 |
struct statfs statfsbuf; |
| 94 |
char sysfs_mount[NAME_MAX]; |
| 95 |
|
| 96 |
snprintf (sysfs_mount, NAME_MAX, "%s", PATH_SYS); |
| 97 |
if (statfs (sysfs_mount, &statfsbuf) < 0 |
| 98 |
|| statfsbuf.f_type != SYSFS_MAGIC) |
| 99 |
plugin_error (STATE_UNKNOWN, 0, |
| 100 |
"The sysfs filesystem (" PATH_SYS ") is not mounted"); |
| 101 |
} |
| 102 |
|
| 103 |
bool |
| 104 |
sysfsparser_path_exist (const char *path, ...) |
| 105 |
{ |
| 106 |
char *filename; |
| 107 |
va_list args; |
| 108 |
|
| 109 |
va_start (args, path); |
| 110 |
if (vasprintf (&filename, path, args) < 0) |
| 111 |
plugin_error (STATE_UNKNOWN, errno, "vasprintf has failed"); |
| 112 |
va_end (args); |
| 113 |
|
| 114 |
return access (filename, F_OK) == 0; |
| 115 |
} |
| 116 |
|
| 117 |
void |
| 118 |
sysfsparser_opendir(DIR **dirp, const char *path, ...) |
| 119 |
{ |
| 120 |
char *dirname; |
| 121 |
va_list args; |
| 122 |
|
| 123 |
va_start (args, path); |
| 124 |
if (vasprintf (&dirname, path, args) < 0) |
| 125 |
plugin_error (STATE_UNKNOWN, errno, "vasprintf has failed"); |
| 126 |
va_end (args); |
| 127 |
|
| 128 |
if ((*dirp = opendir (dirname)) == NULL) |
| 129 |
plugin_error (STATE_UNKNOWN, errno, "Cannot open %s", dirname); |
| 130 |
} |
| 131 |
|
| 132 |
void sysfsparser_closedir(DIR *dirp) |
| 133 |
{ |
| 134 |
closedir(dirp); |
| 135 |
} |
| 136 |
|
| 137 |
struct dirent * |
| 138 |
sysfsparser_readfilename(DIR *dirp, unsigned int flags) |
| 139 |
{ |
| 140 |
for (;;) |
| 141 |
{ |
| 142 |
struct dirent *dp; |
| 143 |
errno = 0; |
| 144 |
if ((dp = readdir (dirp)) == NULL) |
| 145 |
{ |
| 146 |
if (errno != 0) |
| 147 |
plugin_error (STATE_UNKNOWN, errno, "readdir() failure"); |
| 148 |
else |
| 149 |
return NULL; |
| 150 |
} |
| 151 |
|
| 152 |
|
| 153 |
if (STREQ (dp->d_name, ".") || STREQ (dp->d_name, "..")) |
| 154 |
continue; |
| 155 |
|
| 156 |
if (dp->d_type & flags) |
| 157 |
return dp; |
| 158 |
} |
| 159 |
} |
| 160 |
|
| 161 |
char * |
| 162 |
sysfsparser_getline (const char *format, ...) |
| 163 |
{ |
| 164 |
FILE *fp; |
| 165 |
char *filename, *line = NULL; |
| 166 |
size_t len = 0; |
| 167 |
ssize_t chread; |
| 168 |
va_list args; |
| 169 |
|
| 170 |
va_start (args, format); |
| 171 |
if (vasprintf (&filename, format, args) < 0) |
| 172 |
plugin_error (STATE_UNKNOWN, errno, "vasprintf has failed"); |
| 173 |
va_end (args); |
| 174 |
|
| 175 |
dbg ("reading sysfs data: %s\n", filename); |
| 176 |
if ((fp = fopen (filename, "r")) == NULL) |
| 177 |
{ |
| 178 |
dbg (" \\ error: %s\n", strerror (errno)); |
| 179 |
return NULL; |
| 180 |
} |
| 181 |
|
| 182 |
chread = getline (&line, &len, fp); |
| 183 |
fclose (fp); |
| 184 |
|
| 185 |
if (chread < 1) |
| 186 |
return NULL; |
| 187 |
|
| 188 |
len = strlen (line); |
| 189 |
if (line[len-1] == '\n') |
| 190 |
line[len-1] = '\0'; |
| 191 |
|
| 192 |
dbg (" \\ \"%s\"\n", line); |
| 193 |
return line; |
| 194 |
} |
| 195 |
|
| 196 |
int |
| 197 |
sysfsparser_getvalue (unsigned long long *value, const char *format, ...) |
| 198 |
{ |
| 199 |
char *line, *endptr, *filename; |
| 200 |
int retvalue = 0; |
| 201 |
va_list args; |
| 202 |
|
| 203 |
va_start (args, format); |
| 204 |
if (vasprintf (&filename, format, args) < 0) |
| 205 |
plugin_error (STATE_UNKNOWN, errno, "vasprintf has failed"); |
| 206 |
va_end (args); |
| 207 |
|
| 208 |
if (NULL == (line = sysfsparser_getline ("%s", filename))) |
| 209 |
return -1; |
| 210 |
|
| 211 |
errno = 0; |
| 212 |
*value = strtoull (line, &endptr, 0); |
| 213 |
if ((endptr == line) || (errno == ERANGE)) |
| 214 |
retvalue = -1; |
| 215 |
|
| 216 |
free (line); |
| 217 |
return retvalue; |
| 218 |
} |
| 219 |
|
| 220 |
int |
| 221 |
sysfsparser_linelookup_numeric (char *line, char *pattern, long long *value) |
| 222 |
{ |
| 223 |
char *p; |
| 224 |
size_t len = strlen (pattern); |
| 225 |
|
| 226 |
if (!*line || (len + 1 > strlen (line))) |
| 227 |
return 0; |
| 228 |
|
| 229 |
p = line + len; |
| 230 |
if (strncmp (line, pattern, len) || !isspace (*p)) |
| 231 |
return 0; |
| 232 |
|
| 233 |
*value = strtoll (p, NULL, 10); |
| 234 |
return 1; |
| 235 |
} |
| 236 |
|
| 237 |
static unsigned long |
| 238 |
sysfsparser_cpufreq_get_value (unsigned int cpunum, |
| 239 |
enum sysfsparser_cpufreq_sysfile_numeric_id which) |
| 240 |
{ |
| 241 |
char *line, *endptr; |
| 242 |
long value; |
| 243 |
|
| 244 |
if (which >= MAX_VALUE_FILES) |
| 245 |
return 0; |
| 246 |
|
| 247 |
line = |
| 248 |
sysfsparser_getline (PATH_SYS_CPU "/cpu%u/cpufreq/%s", cpunum, |
| 249 |
sysfsparser_devices_system_cpu_file_numeric[which]); |
| 250 |
if (NULL == line) |
| 251 |
return 0; |
| 252 |
|
| 253 |
errno = 0; |
| 254 |
value = strtoul (line, &endptr, 10); |
| 255 |
if ((endptr == line) || (errno == ERANGE)) |
| 256 |
value = 0; |
| 257 |
|
| 258 |
free (line); |
| 259 |
return value; |
| 260 |
} |
| 261 |
|
| 262 |
static char * |
| 263 |
sysfsparser_cpufreq_get_string (unsigned int cpunum, |
| 264 |
enum sysfsparser_cpufreq_sysfile_string_id which) |
| 265 |
{ |
| 266 |
char *line; |
| 267 |
size_t len = 0; |
| 268 |
|
| 269 |
if (which >= MAX_STRING_FILES) |
| 270 |
return NULL; |
| 271 |
|
| 272 |
line = |
| 273 |
sysfsparser_getline (PATH_SYS_CPU "/cpu%u/cpufreq/%s", cpunum, |
| 274 |
sysfsparser_devices_system_cpu_file_string[which]); |
| 275 |
if (NULL == line) |
| 276 |
return 0; |
| 277 |
|
| 278 |
len = strlen (line); |
| 279 |
if (line[len-1] == '\n') |
| 280 |
line[len-1] = '\0'; |
| 281 |
|
| 282 |
return line; |
| 283 |
} |
| 284 |
|
| 285 |
|
| 286 |
|
| 287 |
int |
| 288 |
sysfsparser_cpufreq_get_hardware_limits (unsigned int cpu, |
| 289 |
unsigned long *min, |
| 290 |
unsigned long *max) |
| 291 |
{ |
| 292 |
if ((!min) || (!max)) |
| 293 |
return -EINVAL; |
| 294 |
|
| 295 |
*min = sysfsparser_cpufreq_get_value (cpu, CPUINFO_MIN_FREQ); |
| 296 |
if (!*min) |
| 297 |
return -ENODEV; |
| 298 |
|
| 299 |
*max = sysfsparser_cpufreq_get_value (cpu, CPUINFO_MAX_FREQ); |
| 300 |
if (!*max) |
| 301 |
return -ENODEV; |
| 302 |
|
| 303 |
return 0; |
| 304 |
} |
| 305 |
|
| 306 |
unsigned long |
| 307 |
sysfsparser_cpufreq_get_freq_kernel (unsigned int cpu) |
| 308 |
{ |
| 309 |
return sysfsparser_cpufreq_get_value (cpu, SCALING_CUR_FREQ); |
| 310 |
} |
| 311 |
|
| 312 |
char * |
| 313 |
sysfsparser_cpufreq_get_available_freqs (unsigned int cpu) |
| 314 |
{ |
| 315 |
return sysfsparser_cpufreq_get_string (cpu, SCALING_AVAILABLE_FREQS); |
| 316 |
} |
| 317 |
|
| 318 |
unsigned long |
| 319 |
sysfsparser_cpufreq_get_transition_latency (unsigned int cpu) |
| 320 |
{ |
| 321 |
return sysfsparser_cpufreq_get_value (cpu, CPUINFO_LATENCY); |
| 322 |
} |
| 323 |
|
| 324 |
char * |
| 325 |
sysfsparser_cpufreq_get_driver (unsigned int cpu) |
| 326 |
{ |
| 327 |
return sysfsparser_cpufreq_get_string (cpu, SCALING_DRIVER); |
| 328 |
} |
| 329 |
|
| 330 |
char * |
| 331 |
sysfsparser_cpufreq_get_governor (unsigned int cpu) |
| 332 |
{ |
| 333 |
return sysfsparser_cpufreq_get_string (cpu, SCALING_GOVERNOR); |
| 334 |
} |
| 335 |
|
| 336 |
char * |
| 337 |
sysfsparser_cpufreq_get_available_governors (unsigned int cpu) |
| 338 |
{ |
| 339 |
return sysfsparser_cpufreq_get_string (cpu, SCALING_AVAILABLE_GOVERNORS); |
| 340 |
} |
| 341 |
|
| 342 |
|
| 343 |
|
| 344 |
#define PATH_SYS_ACPI PATH_SYS "/class" |
| 345 |
#define PATH_SYS_ACPI_THERMAL PATH_SYS_ACPI "/thermal" |
| 346 |
|
| 347 |
|
| 348 |
|
| 349 |
|
| 350 |
|
| 351 |
|
| 352 |
|
| 353 |
|
| 354 |
|
| 355 |
|
| 356 |
|
| 357 |
|
| 358 |
|
| 359 |
bool |
| 360 |
sysfsparser_thermal_kernel_support () |
| 361 |
{ |
| 362 |
if (chdir (PATH_SYS_ACPI_THERMAL) < 0) |
| 363 |
return false; |
| 364 |
|
| 365 |
return true; |
| 366 |
} |
| 367 |
|
| 368 |
const char * |
| 369 |
sysfsparser_thermal_sysfs_path () |
| 370 |
{ |
| 371 |
return PATH_SYS_ACPI_THERMAL; |
| 372 |
} |
| 373 |
|
| 374 |
int |
| 375 |
sysfsparser_thermal_get_critical_temperature (unsigned int thermal_zone) |
| 376 |
{ |
| 377 |
int i, err; |
| 378 |
unsigned long long crit_temp = 0; |
| 379 |
|
| 380 |
|
| 381 |
|
| 382 |
|
| 383 |
for (i = 0; i < 4; i++) |
| 384 |
{ |
| 385 |
char *type; |
| 386 |
type = sysfsparser_getline (PATH_SYS_ACPI_THERMAL |
| 387 |
"/thermal_zone%u/trip_point_%d_type", |
| 388 |
thermal_zone, i); |
| 389 |
if (NULL == type) |
| 390 |
continue; |
| 391 |
|
| 392 |
|
| 393 |
|
| 394 |
|
| 395 |
|
| 396 |
if (!STRPREFIX (type, "critical")) |
| 397 |
continue; |
| 398 |
|
| 399 |
err = sysfsparser_getvalue (&crit_temp, PATH_SYS_ACPI_THERMAL |
| 400 |
"/thermal_zone%u/trip_point_%d_temp", |
| 401 |
thermal_zone, i); |
| 402 |
if (err < 0) |
| 403 |
plugin_error (STATE_UNKNOWN, 0, |
| 404 |
"an error has occurred while reading " |
| 405 |
PATH_SYS_ACPI_THERMAL |
| 406 |
"/thermal_zone%u/trip_point_%d_temp", thermal_zone, i); |
| 407 |
|
| 408 |
if (crit_temp > 0) |
| 409 |
dbg ("a critical trip point has been found: %.2f degrees C\n", |
| 410 |
(float) (crit_temp / 1000.0)); |
| 411 |
|
| 412 |
free (type); |
| 413 |
break; |
| 414 |
} |
| 415 |
|
| 416 |
return crit_temp; |
| 417 |
} |
| 418 |
|
| 419 |
const char * |
| 420 |
sysfsparser_thermal_get_device (unsigned int thermal_zone) |
| 421 |
{ |
| 422 |
char *device; |
| 423 |
device = sysfsparser_getline (PATH_SYS_ACPI_THERMAL |
| 424 |
"/thermal_zone%u/device/path", |
| 425 |
thermal_zone); |
| 426 |
if (NULL == device) |
| 427 |
return "Virtual device"; |
| 428 |
|
| 429 |
return STRTRIMPREFIX (device, "\\_TZ_."); |
| 430 |
} |
| 431 |
|
| 432 |
int |
| 433 |
sysfsparser_thermal_get_temperature (unsigned int selected_zone, |
| 434 |
unsigned int *zone, char **type) |
| 435 |
{ |
| 436 |
DIR *d; |
| 437 |
struct dirent *de; |
| 438 |
bool found_data = false; |
| 439 |
unsigned int thermal_zone; |
| 440 |
unsigned long long max_temp = 0, temp = 0; |
| 441 |
|
| 442 |
if (!sysfsparser_thermal_kernel_support ()) |
| 443 |
plugin_error (STATE_UNKNOWN, 0, "no ACPI thermal support in kernel " |
| 444 |
"or incorrect path (\"%s\")", PATH_SYS_ACPI_THERMAL); |
| 445 |
|
| 446 |
if ((d = opendir (".")) == NULL) |
| 447 |
plugin_error (STATE_UNKNOWN, errno, |
| 448 |
"cannot open() " PATH_SYS_ACPI_THERMAL); |
| 449 |
|
| 450 |
while ((de = readdir (d))) |
| 451 |
{ |
| 452 |
|
| 453 |
if (STREQ (de->d_name, ".") || STREQ (de->d_name, "..")) |
| 454 |
continue; |
| 455 |
|
| 456 |
if (!STRPREFIX (de->d_name, "thermal_zone")) |
| 457 |
continue; |
| 458 |
|
| 459 |
thermal_zone = strtoul (de->d_name + 12, NULL, 10); |
| 460 |
if ((selected_zone != ALL_THERMAL_ZONES) && |
| 461 |
(selected_zone != thermal_zone)) |
| 462 |
continue; |
| 463 |
|
| 464 |
|
| 465 |
|
| 466 |
sysfsparser_getvalue (&temp, PATH_SYS_ACPI_THERMAL "/%s/temp", |
| 467 |
de->d_name); |
| 468 |
*type = sysfsparser_getline (PATH_SYS_ACPI_THERMAL "/%s/type", |
| 469 |
de->d_name); |
| 470 |
|
| 471 |
|
| 472 |
|
| 473 |
dbg ("thermal information found: %.2f°C, zone: %u, type: %s\n", |
| 474 |
(float) (temp / 1000.0), thermal_zone, *type); |
| 475 |
found_data = true; |
| 476 |
if (max_temp < temp || 0 == max_temp) |
| 477 |
{ |
| 478 |
max_temp = temp; |
| 479 |
*zone = thermal_zone; |
| 480 |
} |
| 481 |
} |
| 482 |
closedir (d); |
| 483 |
|
| 484 |
if (false == found_data) |
| 485 |
{ |
| 486 |
if (selected_zone == ALL_THERMAL_ZONES) |
| 487 |
plugin_error (STATE_UNKNOWN, 0, |
| 488 |
"no thermal information has been found"); |
| 489 |
else |
| 490 |
plugin_error (STATE_UNKNOWN, 0, |
| 491 |
"no thermal information for zone '%u'", selected_zone); |
| 492 |
} |
| 493 |
|
| 494 |
return max_temp; |
| 495 |
} |
| 496 |
|
| 497 |
void |
| 498 |
sysfsparser_thermal_listall () |
| 499 |
{ |
| 500 |
struct dirent **namelist; |
| 501 |
unsigned int thermal_zone; |
| 502 |
char *type; |
| 503 |
int i, n, crit_temp; |
| 504 |
|
| 505 |
if (!sysfsparser_thermal_kernel_support ()) |
| 506 |
plugin_error (STATE_UNKNOWN, errno, |
| 507 |
"no ACPI thermal support in kernel " |
| 508 |
"or incorrect path (\"%s\")", PATH_SYS_ACPI_THERMAL); |
| 509 |
|
| 510 |
n = scandir (PATH_SYS_ACPI_THERMAL, &namelist, 0, alphasort); |
| 511 |
if (-1 == n) |
| 512 |
plugin_error (STATE_UNKNOWN, errno, |
| 513 |
"cannot scandir() " PATH_SYS_ACPI_THERMAL); |
| 514 |
|
| 515 |
printf ("Thermal zones reported by the linux kernel (%s):\n", |
| 516 |
sysfsparser_thermal_sysfs_path ()); |
| 517 |
|
| 518 |
for (i = 0; i < n; i++) |
| 519 |
{ |
| 520 |
if (STRPREFIX (namelist[i]->d_name, "thermal_zone")) |
| 521 |
{ |
| 522 |
thermal_zone = strtoul (namelist[i]->d_name + 12, NULL, 10); |
| 523 |
type = sysfsparser_getline (PATH_SYS_ACPI_THERMAL "/%s/type", |
| 524 |
namelist[i]->d_name); |
| 525 |
crit_temp = |
| 526 |
sysfsparser_thermal_get_critical_temperature (thermal_zone); |
| 527 |
|
| 528 |
fprintf (stdout, " - zone %2u [%s], type \"%s\"" |
| 529 |
, thermal_zone |
| 530 |
, sysfsparser_thermal_get_device (thermal_zone) |
| 531 |
, type ? type : "n/a"); |
| 532 |
if (crit_temp > 0) |
| 533 |
fprintf (stdout, ", critical trip point at %d°C", crit_temp / 1000); |
| 534 |
fputs ("\n", stdout); |
| 535 |
} |
| 536 |
|
| 537 |
free (namelist[i]); |
| 538 |
} |
| 539 |
|
| 540 |
free(namelist); |
| 541 |
} |