forked from netdata/netdata
-
Notifications
You must be signed in to change notification settings - Fork 0
/
sys_class_power_supply.c
410 lines (346 loc) · 15.3 KB
/
sys_class_power_supply.c
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
// SPDX-License-Identifier: GPL-3.0-or-later
#include "plugin_proc.h"
#define PLUGIN_PROC_MODULE_POWER_SUPPLY_NAME "/sys/class/power_supply"
const char *ps_property_names[] = { "charge", "energy", "voltage"};
const char *ps_property_titles[] = {"Battery charge", "Battery energy", "Power supply voltage"};
const char *ps_property_units[] = { "Ah", "Wh", "V"};
const char *ps_property_dim_names[] = {"empty_design", "empty", "now", "full", "full_design",
"empty_design", "empty", "now", "full", "full_design",
"min_design", "min", "now", "max", "max_design"};
struct ps_property_dim {
char *name;
char *filename;
int fd;
RRDDIM *rd;
unsigned long long value;
int always_zero;
struct ps_property_dim *next;
};
struct ps_property {
char *name;
char *title;
char *units;
RRDSET *st;
struct ps_property_dim *property_dim_root;
struct ps_property *next;
};
struct capacity {
char *filename;
int fd;
RRDSET *st;
RRDDIM *rd;
unsigned long long value;
};
struct power_supply {
char *name;
uint32_t hash;
int found;
struct capacity *capacity;
struct ps_property *property_root;
struct power_supply *next;
};
static struct power_supply *power_supply_root = NULL;
static int files_num = 0;
void power_supply_free(struct power_supply *ps) {
if(likely(ps)) {
// free capacity structure
if(likely(ps->capacity)) {
if(likely(ps->capacity->st)) rrdset_is_obsolete(ps->capacity->st);
freez(ps->capacity->filename);
if(likely(ps->capacity->fd != -1)) close(ps->capacity->fd);
files_num--;
freez(ps->capacity);
}
freez(ps->name);
struct ps_property *pr = ps->property_root;
while(likely(pr)) {
// free dimensions
struct ps_property_dim *pd = pr->property_dim_root;
while(likely(pd)) {
freez(pd->name);
freez(pd->filename);
if(likely(pd->fd != -1)) close(pd->fd);
files_num--;
struct ps_property_dim *d = pd;
pd = pd->next;
freez(d);
}
// free properties
if(likely(pr->st)) rrdset_is_obsolete(pr->st);
freez(pr->name);
freez(pr->title);
freez(pr->units);
struct ps_property *p = pr;
pr = pr->next;
freez(p);
}
// remove power supply from linked list
if(likely(ps == power_supply_root)) {
power_supply_root = ps->next;
}
else {
struct power_supply *last;
for(last = power_supply_root; last && last->next != ps; last = last->next);
if(likely(last)) last->next = ps->next;
}
freez(ps);
}
}
int do_sys_class_power_supply(int update_every, usec_t dt) {
(void)dt;
static int do_capacity = -1, do_property[3] = {-1};
static int keep_fds_open = CONFIG_BOOLEAN_NO, keep_fds_open_config = -1;
static char *dirname = NULL;
if(unlikely(do_capacity == -1)) {
do_capacity = config_get_boolean("plugin:proc:/sys/class/power_supply", "battery capacity", CONFIG_BOOLEAN_YES);
do_property[0] = config_get_boolean("plugin:proc:/sys/class/power_supply", "battery charge", CONFIG_BOOLEAN_NO);
do_property[1] = config_get_boolean("plugin:proc:/sys/class/power_supply", "battery energy", CONFIG_BOOLEAN_NO);
do_property[2] = config_get_boolean("plugin:proc:/sys/class/power_supply", "power supply voltage", CONFIG_BOOLEAN_NO);
keep_fds_open_config = config_get_boolean_ondemand("plugin:proc:/sys/class/power_supply", "keep files open", CONFIG_BOOLEAN_AUTO);
char filename[FILENAME_MAX + 1];
snprintfz(filename, FILENAME_MAX, "%s%s", netdata_configured_host_prefix, "/sys/class/power_supply");
dirname = config_get("plugin:proc:/sys/class/power_supply", "directory to monitor", filename);
}
DIR *dir = opendir(dirname);
if(unlikely(!dir)) {
error("Cannot read directory '%s'", dirname);
return 1;
}
struct dirent *de = NULL;
while(likely(de = readdir(dir))) {
if(likely(de->d_type == DT_DIR
&& (
(de->d_name[0] == '.' && de->d_name[1] == '\0')
|| (de->d_name[0] == '.' && de->d_name[1] == '.' && de->d_name[2] == '\0')
)))
continue;
if(likely(de->d_type == DT_LNK || de->d_type == DT_DIR)) {
uint32_t hash = simple_hash(de->d_name);
struct power_supply *ps;
for(ps = power_supply_root; ps; ps = ps->next) {
if(unlikely(ps->hash == hash && !strcmp(ps->name, de->d_name))) {
ps->found = 1;
break;
}
}
// allocate memory for power supply and initialize it
if(unlikely(!ps)) {
ps = callocz(sizeof(struct power_supply), 1);
ps->name = strdupz(de->d_name);
ps->hash = simple_hash(de->d_name);
ps->found = 1;
ps->next = power_supply_root;
power_supply_root = ps;
struct stat stbuf;
if(likely(do_capacity != CONFIG_BOOLEAN_NO)) {
char filename[FILENAME_MAX + 1];
snprintfz(filename, FILENAME_MAX, "%s/%s/%s", dirname, de->d_name, "capacity");
if (stat(filename, &stbuf) == 0) {
ps->capacity = callocz(sizeof(struct capacity), 1);
ps->capacity->filename = strdupz(filename);
ps->capacity->fd = -1;
files_num++;
}
}
// allocate memory and initialize structures for every property and file found
size_t pr_idx, pd_idx;
size_t prev_idx = 3; // there is no property with this index
for(pr_idx = 0; pr_idx < 3; pr_idx++) {
if(unlikely(do_property[pr_idx] != CONFIG_BOOLEAN_NO)) {
struct ps_property *pr = NULL;
int min_value_found = 0, max_value_found = 0;
for(pd_idx = pr_idx * 5; pd_idx < pr_idx * 5 + 5; pd_idx++) {
// check if file exists
char filename[FILENAME_MAX + 1];
snprintfz(filename, FILENAME_MAX, "%s/%s/%s_%s", dirname, de->d_name,
ps_property_names[pr_idx], ps_property_dim_names[pd_idx]);
if (stat(filename, &stbuf) == 0) {
if(unlikely(pd_idx == pr_idx * 5 + 1))
min_value_found = 1;
if(unlikely(pd_idx == pr_idx * 5 + 3))
max_value_found = 1;
// add chart
if(unlikely(prev_idx != pr_idx)) {
pr = callocz(sizeof(struct ps_property), 1);
pr->name = strdupz(ps_property_names[pr_idx]);
pr->title = strdupz(ps_property_titles[pr_idx]);
pr->units = strdupz(ps_property_units[pr_idx]);
prev_idx = pr_idx;
pr->next = ps->property_root;
ps->property_root = pr;
}
// add dimension
struct ps_property_dim *pd;
pd= callocz(sizeof(struct ps_property_dim), 1);
pd->name = strdupz(ps_property_dim_names[pd_idx]);
pd->filename = strdupz(filename);
pd->fd = -1;
files_num++;
pd->next = pr->property_dim_root;
pr->property_dim_root = pd;
}
}
// create a zero empty/min dimension
if(unlikely(max_value_found && !min_value_found)) {
struct ps_property_dim *pd;
pd= callocz(sizeof(struct ps_property_dim), 1);
pd->name = strdupz(ps_property_dim_names[pr_idx * 5 + 1]);
pd->always_zero = 1;
pd->next = pr->property_dim_root;
pr->property_dim_root = pd;
}
}
}
}
// read capacity file
if(likely(ps->capacity)) {
char buffer[30 + 1];
if(unlikely(ps->capacity->fd == -1)) {
ps->capacity->fd = open(ps->capacity->filename, O_RDONLY, 0666);
if(unlikely(ps->capacity->fd == -1)) {
error("Cannot open file '%s'", ps->capacity->filename);
power_supply_free(ps);
ps = NULL;
}
}
if (ps)
{
ssize_t r = read(ps->capacity->fd, buffer, 30);
if(unlikely(r < 1)) {
error("Cannot read file '%s'", ps->capacity->filename);
power_supply_free(ps);
ps = NULL;
}
else {
buffer[r] = '\0';
ps->capacity->value = str2ull(buffer);
if(unlikely(!keep_fds_open)) {
close(ps->capacity->fd);
ps->capacity->fd = -1;
}
else if(unlikely(lseek(ps->capacity->fd, 0, SEEK_SET) == -1)) {
error("Cannot seek in file '%s'", ps->capacity->filename);
close(ps->capacity->fd);
ps->capacity->fd = -1;
}
}
}
}
// read property files
int read_error = 0;
struct ps_property *pr;
if (ps)
{
for(pr = ps->property_root; pr && !read_error; pr = pr->next) {
struct ps_property_dim *pd;
for(pd = pr->property_dim_root; pd; pd = pd->next) {
if(likely(!pd->always_zero)) {
char buffer[30 + 1];
if(unlikely(pd->fd == -1)) {
pd->fd = open(pd->filename, O_RDONLY, 0666);
if(unlikely(pd->fd == -1)) {
error("Cannot open file '%s'", pd->filename);
read_error = 1;
power_supply_free(ps);
break;
}
}
ssize_t r = read(pd->fd, buffer, 30);
if(unlikely(r < 1)) {
error("Cannot read file '%s'", pd->filename);
read_error = 1;
power_supply_free(ps);
break;
}
buffer[r] = '\0';
pd->value = str2ull(buffer);
if(unlikely(!keep_fds_open)) {
close(pd->fd);
pd->fd = -1;
}
else if(unlikely(lseek(pd->fd, 0, SEEK_SET) == -1)) {
error("Cannot seek in file '%s'", pd->filename);
close(pd->fd);
pd->fd = -1;
}
}
}
}
}
}
}
closedir(dir);
keep_fds_open = keep_fds_open_config;
if(likely(keep_fds_open_config == CONFIG_BOOLEAN_AUTO)) {
if(unlikely(files_num > 32))
keep_fds_open = CONFIG_BOOLEAN_NO;
else
keep_fds_open = CONFIG_BOOLEAN_YES;
}
// --------------------------------------------------------------------
struct power_supply *ps = power_supply_root;
while(unlikely(ps)) {
if(unlikely(!ps->found)) {
struct power_supply *f = ps;
ps = ps->next;
power_supply_free(f);
continue;
}
if(likely(ps->capacity)) {
if(unlikely(!ps->capacity->st)) {
ps->capacity->st = rrdset_create_localhost(
"powersupply_capacity"
, ps->name
, NULL
, ps->name
, "powersupply.capacity"
, "Battery capacity"
, "percentage"
, PLUGIN_PROC_NAME
, PLUGIN_PROC_MODULE_POWER_SUPPLY_NAME
, NETDATA_CHART_PRIO_POWER_SUPPLY_CAPACITY
, update_every
, RRDSET_TYPE_LINE
);
}
else
rrdset_next(ps->capacity->st);
if(unlikely(!ps->capacity->rd)) ps->capacity->rd = rrddim_add(ps->capacity->st, "capacity", NULL, 1, 1, RRD_ALGORITHM_ABSOLUTE);
rrddim_set_by_pointer(ps->capacity->st, ps->capacity->rd, ps->capacity->value);
rrdset_done(ps->capacity->st);
}
struct ps_property *pr;
for(pr = ps->property_root; pr; pr = pr->next) {
if(unlikely(!pr->st)) {
char id[RRD_ID_LENGTH_MAX + 1], context[RRD_ID_LENGTH_MAX + 1];
snprintfz(id, RRD_ID_LENGTH_MAX, "powersupply_%s", pr->name);
snprintfz(context, RRD_ID_LENGTH_MAX, "powersupply.%s", pr->name);
pr->st = rrdset_create_localhost(
id
, ps->name
, NULL
, ps->name
, context
, pr->title
, pr->units
, PLUGIN_PROC_NAME
, PLUGIN_PROC_MODULE_POWER_SUPPLY_NAME
, NETDATA_CHART_PRIO_POWER_SUPPLY_CAPACITY
, update_every
, RRDSET_TYPE_LINE
);
}
else
rrdset_next(pr->st);
struct ps_property_dim *pd;
for(pd = pr->property_dim_root; pd; pd = pd->next) {
if(unlikely(!pd->rd)) pd->rd = rrddim_add(pr->st, pd->name, NULL, 1, 1000000, RRD_ALGORITHM_ABSOLUTE);
rrddim_set_by_pointer(pr->st, pd->rd, pd->value);
}
rrdset_done(pr->st);
}
ps->found = 0;
ps = ps->next;
}
return 0;
}