diff options
author | Vladimir Kobal <vlad@prokk.net> | 2019-03-06 19:51:56 +0200 |
---|---|---|
committer | GitHub <noreply@github.com> | 2019-03-06 19:51:56 +0200 |
commit | c7ddf8d2ea407ba2717e07a51aa631ca26739c3a (patch) | |
tree | c1fc66d787d4a2e60bb3dc2461ce1e61b1183157 | |
parent | e7158e772e55059895bb662b08f810e5530b13fe (diff) |
Add zero minimum (#5395)
-rw-r--r-- | collectors/proc.plugin/sys_class_power_supply.c | 65 |
1 files changed, 42 insertions, 23 deletions
diff --git a/collectors/proc.plugin/sys_class_power_supply.c b/collectors/proc.plugin/sys_class_power_supply.c index 09cdc7c0d0..5e37ad5aba 100644 --- a/collectors/proc.plugin/sys_class_power_supply.c +++ b/collectors/proc.plugin/sys_class_power_supply.c @@ -19,6 +19,7 @@ struct ps_property_dim { RRDDIM *rd; unsigned long long value; + int always_zero; struct ps_property_dim *next; }; @@ -184,6 +185,7 @@ int do_sys_class_power_supply(int update_every, usec_t dt) { 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++) { @@ -193,6 +195,11 @@ int do_sys_class_power_supply(int update_every, usec_t dt) { 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); @@ -215,6 +222,16 @@ int do_sys_class_power_supply(int update_every, usec_t dt) { 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; + } } } } @@ -258,36 +275,38 @@ int do_sys_class_power_supply(int update_every, usec_t dt) { 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) { - char buffer[30 + 1]; + 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); + 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); - 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; + 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; + } } } } |