| 1 | // SPDX-License-Identifier: GPL-3.0-or-later |
| 2 | |
| 3 | #ifndef NETDATA_POWER_SUPPLY_H |
| 4 | #define NETDATA_POWER_SUPPLY_H |
| 5 | |
| 6 | #include "common-contexts.h" |
| 7 | |
| 8 | struct ps_property_dim { |
| 9 | char *name; |
| 10 | char *filename; |
| 11 | int fd; |
| 12 | |
| 13 | RRDDIM *rd; |
| 14 | unsigned long long value; |
| 15 | int always_zero; |
| 16 | |
| 17 | struct ps_property_dim *next; |
| 18 | }; |
| 19 | |
| 20 | struct ps_property { |
| 21 | char *name; |
| 22 | char *title; |
| 23 | char *units; |
| 24 | |
| 25 | long priority; |
| 26 | |
| 27 | RRDSET *st; |
| 28 | |
| 29 | struct ps_property_dim *property_dim_root; |
| 30 | |
| 31 | struct ps_property *next; |
| 32 | }; |
| 33 | |
| 34 | struct simple_property { |
| 35 | char *filename; |
| 36 | int fd; |
| 37 | |
| 38 | RRDSET *st; |
| 39 | RRDDIM *rd; |
| 40 | bool ok; |
| 41 | unsigned long long value; |
| 42 | }; |
| 43 | |
| 44 | struct power_supply { |
| 45 | char *name; |
| 46 | uint32_t hash; |
| 47 | int found; |
| 48 | |
| 49 | struct simple_property *capacity, *power; |
| 50 | |
| 51 | struct ps_property *property_root; |
| 52 | |
| 53 | struct power_supply *next; |
| 54 | }; |
| 55 | |
| 56 | static inline void add_labels_to_power_supply(struct power_supply *ps, RRDSET *st) { |
| 57 | rrdlabels_add(st->rrdlabels, "device", ps->name, RRDLABEL_SRC_AUTO); |
| 58 | } |
| 59 | |
| 60 | static inline void rrdset_create_simple_prop( |
| 61 | struct power_supply *ps, |
| 62 | struct simple_property *prop, |
| 63 | char *title, |
| 64 | char *dim, |
| 65 | collected_number divisor, |
| 66 | char *units, |
| 67 | long priority, |
| 68 | int update_every) |
| 69 | { |
| 70 | if (unlikely(!prop->st)) { |
| 71 | char id[RRD_ID_LENGTH_MAX + 1], context[RRD_ID_LENGTH_MAX + 1]; |
| 72 | snprintfz(id, RRD_ID_LENGTH_MAX, "powersupply_%s", dim); |
| 73 | snprintfz(context, RRD_ID_LENGTH_MAX, "powersupply.%s", dim); |
| 74 | |
| 75 | prop->st = rrdset_create_localhost( |
| 76 | id, |
| 77 | ps->name, |
| 78 | NULL, |
| 79 | dim, |
| 80 | context, |
| 81 | title, |
| 82 | units, |
| 83 | _COMMON_PLUGIN_NAME, |
| 84 | _COMMON_PLUGIN_MODULE_NAME, |
| 85 | priority, |
| 86 | update_every, |
| 87 | RRDSET_TYPE_LINE); |
| 88 | |
| 89 | add_labels_to_power_supply(ps, prop->st); |
| 90 | } |
| 91 | |
| 92 | if (unlikely(!prop->rd)) |
| 93 | prop->rd = rrddim_add(prop->st, dim, NULL, 1, divisor, RRD_ALGORITHM_ABSOLUTE); |
| 94 | rrddim_set_by_pointer(prop->st, prop->rd, prop->value); |
| 95 | |
| 96 | rrdset_done(prop->st); |
| 97 | } |
| 98 | |
| 99 | #endif //NETDATA_POWER_SUPPLY_H |