-
Notifications
You must be signed in to change notification settings - Fork 0
/
list-sensors.cpp
692 lines (636 loc) · 18.9 KB
/
list-sensors.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
#include "config.h"
#include <getopt.h>
#include <unistd.h>
#include <cmath>
#include <cstdint>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <sdbusplus/bus.hpp>
#include <sdbusplus/exception.hpp>
#include <string>
// Bus handler singleton
static sdbusplus::bus::bus systemBus = sdbusplus::bus::new_default();
static bool useColors = false;
using PropertyValue = std::variant<int64_t, std::string, bool, double>;
using PropertyName = std::string;
using PropertiesMap = std::map<PropertyName, PropertyValue>;
static constexpr auto SYSTEMD_PROPERTIES = "org.freedesktop.DBus.Properties";
/**
* @brief Gives a simple access to sensor properties.
*/
class Properties : public PropertiesMap
{
public:
using PropertiesMap::PropertiesMap;
/**
* @brief Check if sensor Available and Functional
*/
std::string functional() const
{
std::string ret = "OK";
auto it = this->find("Functional");
if (it != this->end() && std::get<bool>(it->second) == false)
{
ret = "FAIL";
}
it = this->find("Available");
if (it != this->end() && std::get<bool>(it->second) == false)
{
ret = "N/A";
}
return ret;
}
/**
* @brief Current sensor state
*/
std::string status() const
{
std::string ret = functional();
if ("OK" == ret)
{
if (getBool("FatalAlarmHigh"))
{
ret = "Fatal";
}
else if (getBool("CriticalAlarmLow") || getBool("CriticalAlarmHigh"))
{
ret = "Critical";
}
else if (getBool("WarningAlarmLow") || getBool("WarningAlarmHigh"))
{
ret = "Warning";
}
}
return ret;
}
/**
* @brief Sensor's scale factor
*/
float scale() const
{
float ret = 1.f;
auto it = this->find("Scale");
if (it != this->end())
{
auto value = std::get<int64_t>(it->second);
ret = powf(10, static_cast<float>(value));
}
return ret;
}
std::string value() const
{
if ("OK" != functional())
{
return "N/A";
}
std::string ret(getValue("Value"));
if (useColors)
{
std::string state = status();
if (state == "Warning")
{
// mark Orange
ret = "\033[0;33m" + ret + "\033[0m";
}
else if (state == "Critical")
{
// mark Red
ret = "\033[0;31m" + ret + "\033[0m";
}
else if (state == "Fatal")
{
// mark Blinking Red
ret = "\033[0;31;5m" + ret + "\033[0m";
}
}
return ret;
}
std::string criticalLow() const
{
return getValue("CriticalLow");
}
std::string criticalHigh() const
{
return getValue("CriticalHigh");
}
std::string warningLow() const
{
return getValue("WarningLow");
}
std::string warningHigh() const
{
return getValue("WarningHigh");
}
std::string fatalHigh() const
{
return getValue("FatalHigh");
}
/**
* @brief Short sensors unit name
*/
std::string unit() const
{
std::string ret;
auto it = this->find("Unit");
if (it != this->end())
{
auto name = std::get<std::string>(it->second);
name = name.substr(name.rfind('.') + 1);
if ("Volts" == name)
{
ret = "V";
}
else if ("DegreesC" == name)
{
// The degrees character takes two bytes, but only one place
// on the screen. It breaks the alignment.
// Force fit the string to 3 screen characters long.
ret = "°C ";
}
else if ("Amperes" == name)
{
ret = "A";
}
else if ("RPMS" == name)
{
ret = "RPM";
}
else if ("Watts" == name)
{
ret = "W";
}
else if ("Joules" == name)
{
ret = "J";
}
else if ("Meters" == name)
{
ret = "m";
}
else if ("Percent" == name)
{
ret = "%";
}
else
{
ret = name;
}
}
return ret;
}
protected:
/**
* @brief Check is the specified boolean property has true value
*
* @param name - Property name
*/
bool getBool(const PropertyName& name) const
{
auto it = this->find(name);
return (it == this->end() ? false : std::get<bool>(it->second));
}
/**
* @brief Format a sensor value or threshold
*
* @param name - Property name
*
* @return String with formatted value of property
*/
std::string getValue(const PropertyName& name) const
{
std::string ret(8, '\0');
auto it = this->find(name);
if (it == this->end())
{
ret = "N/A";
}
else if (std::holds_alternative<double>(it->second))
{
auto value = std::get<double>(it->second);
if (std::isnan(value))
{
ret = "N/A";
}
else if (value < 1000)
{
const size_t len =
snprintf(ret.data(), ret.size(), "%7.03f", value);
ret.resize(len);
}
else
{
const size_t len =
snprintf(ret.data(), ret.size(), "%7d", (int)(value));
ret.resize(len);
}
}
else
{
auto factor = scale();
auto value = std::get<int64_t>(it->second);
if (factor < 1.f)
{
const size_t len =
snprintf(ret.data(), ret.size(), "%7.03f", value * factor);
ret.resize(len);
}
else
{
const size_t len = snprintf(ret.data(), ret.size(), "%7d",
(int)(value * factor));
ret.resize(len);
}
}
return ret;
}
};
/**
* @brief Show sensor's data
*
* @param service - Sensor's object service
* @param path - Sensor's object path
*/
void printSensorData(const std::string& service, const std::string& path)
{
// Ask DBus for all sensors properties
auto m = systemBus.new_method_call(service.c_str(), path.c_str(),
SYSTEMD_PROPERTIES, "GetAll");
m.append("");
auto r = systemBus.call(m);
if (r.is_method_error())
{
fprintf(stderr, "Get properties for %s failed\n", path.c_str());
return;
}
Properties props;
r.read(props);
size_t name_pos = path.rfind('/');
size_t folder_pos = path.rfind('/', name_pos - 1);
// row format string, limit sensor name to 19 characters
constexpr auto row_fmt = "%-19.19s %8s %7s %-3s %7s %7s %7s %7s %7s\n";
// header format, let the Unit column header overlap the LC column a bit
constexpr auto hdr_fmt = "%-19.19s %8s %7s %-4s%7s %7s %7s %7s %7s\n";
// Show group header if it is a new type
static std::string typeName;
std::string currentType =
path.substr(folder_pos + 1, name_pos - folder_pos - 1);
if (typeName != currentType)
{
if (!typeName.empty())
{
printf("\n");
}
printf("=== %s ===\n", currentType.c_str());
printf(hdr_fmt, "Name", "Status", "Value", "Unit", "LC", "LNC", "UNC",
"UC", "NR");
printf("\n");
typeName = currentType;
}
// Show sensor data
printf(row_fmt, path.c_str() + name_pos + 1, props.status().c_str(),
props.value().c_str(), props.unit().c_str(),
props.criticalLow().c_str(), props.warningLow().c_str(),
props.warningHigh().c_str(), props.criticalHigh().c_str(),
props.fatalHigh().c_str());
}
/**
* @brief compare sensors path with numbers
*/
struct CmpSensorsName
{
bool operator()(const std::string& a, const std::string& b) const
{
const char* strA = a.c_str();
const char* strB = b.c_str();
while (true)
{
const char& chrA = *strA;
const char& chrB = *strB;
// check for end of name
if (!chrA || !chrB)
{
return !!chrB;
}
const bool isNumA = (chrA >= '0' && chrA <= '9');
const bool isNumB = (chrB >= '0' && chrB <= '9');
if (isNumA && isNumB)
{
// both names have numbers at the same position
char* endA = nullptr;
char* endB = nullptr;
const unsigned long valA = strtoul(strA, &endA, 10);
const unsigned long valB = strtoul(strB, &endB, 10);
if (valA != valB)
{
return valA < valB;
}
strA = endA;
strB = endB;
}
else if (isNumA || isNumB)
{
// only one of names has a number
return isNumA;
}
else
{
// no digits at position
if (chrA != chrB)
{
return chrA < chrB;
}
++strA;
++strB;
}
}
}
};
using Path = std::string;
using Service = std::string;
using Interface = std::string;
using Interfaces = std::vector<Interface>;
using ObjectsMap = std::map<Service, Interfaces>;
using Objects = std::map<Path, ObjectsMap, CmpSensorsName>;
/**
* @brief Run infinite loop to print sensor values each \p watch_interval
* seconds
*
* @param watch_list - List of sensor names to print
* @param watch_interval - Interval to wait
* @param objects - List of all sensors in the system
* @return EXIT_FAILURE
*/
static int watch_senors(const std::vector<std::string>& watch_list,
const int& watch_interval, const Objects& objects)
{
std::vector<std::pair<Service, Path>> sensors;
// we want to display sensors in order they were specified by user, so we
// cant just loop over objects and then use find on sensors_list
for (const auto& name : watch_list)
{
bool found = false;
for (const auto& obj : objects)
{
size_t name_pos = obj.first.rfind('/');
if (name == obj.first.substr(name_pos + 1))
{
found = true;
for (const auto& service : obj.second)
{
sensors.emplace_back(service.first, obj.first);
}
}
}
if (!found)
{
fprintf(stderr, "Failed to find sensor %s!\n", name.c_str());
return EXIT_FAILURE;
}
}
while (true)
{
time_t t;
time(&t);
char date_str[20];
strftime(date_str, sizeof(date_str), "%Y-%m-%d %H:%M:%S",
localtime(&t));
printf("%s", date_str);
for (const auto& [service, path] : sensors)
{
// Ask DBus for all sensors properties
auto m = systemBus.new_method_call(service.c_str(), path.c_str(),
SYSTEMD_PROPERTIES, "GetAll");
m.append("");
auto r = systemBus.call(m);
if (r.is_method_error())
{
fprintf(stderr, "Get properties for %s failed\n", path.c_str());
return EXIT_FAILURE;
}
Properties props;
r.read(props);
printf("\t%s", props.value().c_str());
}
printf("\n");
sleep(watch_interval);
}
return EXIT_SUCCESS;
}
/**
* @brief Prints the application usage help
*
* @return EXIT_FAILURE
*/
static int usage(char *progname, bool cli_mode)
{
if (cli_mode)
{
fprintf(stderr, "Sensor readings\n"
" [TYPE] - An optional type of sensors to list\n"
" Available types are:\n"
" voltage\n"
" current\n"
" power\n"
" temperature\n"
" fan_pwm\n"
" fan_tach\n"
" Options:\n"
" -C, --color Enable colors\n"
" -w, --watch <sensors> Print sensors values "
"each n seconds (comma-separated list)\n"
" -n, --interval <secs> Seconds to wait "
"between updates in watch mode\n");
}
else
{
fprintf(stderr,
"Usage: %s [options] [sensors-type]\n"
" Shows all sensors of the specified type.\n"
" If the type is not specified shows all found sensors.\n"
"Options:\n"
#ifdef WITH_REMOTE_HOST
" -H, --host=[USER@]HOST Operate on remote host (over ssh)\n"
#endif
" -c, --cli CLI mode for obmc-yadro-cli\n"
" -C, --color Enable colors\n"
" -w, --watch <sensors> Print sensors values each n "
"seconds (comma-separated list)\n"
" -n, --interval <secs> Seconds to wait between updates in "
"watch mode\n"
" -h, --help Show this help\n",
progname);
}
return EXIT_FAILURE;
}
/**
* @brief Application entry point
*
* @return
*/
int main(int argc, char* argv[])
{
#ifdef WITH_REMOTE_HOST
const char* host = nullptr;
#endif
bool showhelp = false;
bool cli_mode = false;
bool watch_mode = false;
std::vector<std::string> watch_list;
int watch_interval = 1;
const struct option opts[] = {
#ifdef WITH_REMOTE_HOST
{"host", required_argument, nullptr, 'H'},
#endif
{"cli", no_argument, nullptr, 'c'},
{"color", no_argument, nullptr, 'C'},
{"watch", required_argument, nullptr, 'w'},
{"interval", required_argument, nullptr, 'n'},
{"help", no_argument, nullptr, 'h'},
// --- end of array ---
{nullptr, 0, nullptr, '\0'}};
int c;
#ifdef WITH_REMOTE_HOST
while ((c = getopt_long(argc, argv, "H:cCw:n:h", opts, nullptr)) != -1)
#else
while ((c = getopt_long(argc, argv, "cCw:n:h", opts, nullptr)) != -1)
#endif
{
switch (c)
{
#ifdef WITH_REMOTE_HOST
case 'H':
if (optarg)
{
host = optarg;
}
else
{
fprintf(stderr, "Remote host required with this option!\n");
showhelp = true;
}
break;
#endif
case 'c':
cli_mode = true;
break;
case 'C':
// ignore "color" flag if output redirected
if (isatty(STDOUT_FILENO))
{
useColors = true;
}
break;
case 'w': {
watch_mode = true;
std::string line(optarg);
size_t start;
size_t end = 0;
const char delim = ',';
while ((start = line.find_first_not_of(delim, end)) !=
std::string::npos)
{
end = line.find(delim, start);
watch_list.emplace_back(line.substr(start, end - start));
}
break;
}
case 'n':
try
{
watch_interval = std::stoi(optarg);
}
catch (...)
{
fprintf(stderr,
"Can't read interval '%s', should be number of "
"seconds!\n",
optarg);
showhelp = true;
}
if (watch_interval <= 0)
{
fprintf(stderr, "Invalid interval value: %d!\n",
watch_interval);
showhelp = true;
}
break;
case 'h':
showhelp = true;
break;
default:
fprintf(stderr, "Unknown option found '%c'!\n", c);
showhelp = true;
break;
}
}
// In CLI mode the 'help' word works just like -h/--help
if (cli_mode && optind < argc && !strcmp(argv[optind], "help"))
{
optind++;
showhelp = true;
}
if (showhelp)
{
return usage(argv[0], cli_mode);
}
#ifdef WITH_REMOTE_HOST
if (host)
{
printf("Open DBus session to %s\n", host);
sd_bus* b = nullptr;
sd_bus_open_system_remote(&b, host);
systemBus = sdbusplus::bus::bus(b, std::false_type());
}
#endif
std::string root_path = SENSORS_PATH;
if (optind < argc)
{
const size_t len = strlen(argv[optind]);
for (size_t i = 0; i < len; ++i)
{
const auto& c = argv[optind][i];
if (!isalnum(c) && c != '_')
{
fprintf(stderr, "Invalid sensor type is specified!\n");
return EXIT_FAILURE;
}
}
root_path += "/";
root_path += argv[optind];
}
auto method = systemBus.new_method_call(MAPPER_SERVICE, MAPPER_PATH,
MAPPER_IFACE, "GetSubTree");
const std::vector<std::string> ifaces = {SENSOR_VALUE_IFACE};
method.append(root_path, 0, ifaces);
Objects objects;
try
{
systemBus.call(method).read(objects);
}
catch (const sdbusplus::exception::SdBusError& ex)
{
if (!strcmp(ex.name(), "org.freedesktop.DBus.Error.FileNotFound"))
{
fprintf(stderr, "No sensors of selected type are present\n");
return usage(argv[0], cli_mode);
}
else
{
fprintf(stderr, "Error: %s\n", ex.what());
return EXIT_FAILURE;
}
}
if (watch_mode)
{
return watch_senors(watch_list, watch_interval, objects);
}
for (const auto& obj : objects)
{
for (const auto& service : obj.second)
{
printSensorData(service.first, obj.first);
}
}
return EXIT_SUCCESS;
}