]> arthur.barton.de Git - netdata.git/blob - src/proc_uptime.c
dns_query_time plugin: replace "." with "_" in dimensions
[netdata.git] / src / proc_uptime.c
1 #include "common.h"
2
3 int do_proc_uptime(int update_every, usec_t dt) {
4     (void)dt;
5
6     static RRDSET *st = NULL;
7     collected_number uptime = 0;
8
9 #ifdef CLOCK_BOOTTIME_IS_AVAILABLE
10     uptime = now_boottime_usec() / 1000;
11 #else
12     static procfile *ff = NULL;
13
14     if(unlikely(!ff)) {
15         char filename[FILENAME_MAX + 1];
16         snprintfz(filename, FILENAME_MAX, "%s%s", netdata_configured_host_prefix, "/proc/uptime");
17
18         ff = procfile_open(config_get("plugin:proc:/proc/uptime", "filename to monitor", filename), " \t", PROCFILE_FLAG_DEFAULT);
19         if(unlikely(!ff))
20             return 1;
21     }
22
23     ff = procfile_readall(ff);
24     if(unlikely(!ff))
25         return 0; // we return 0, so that we will retry to open it next time
26
27     if(unlikely(procfile_lines(ff) < 1)) {
28         error("/proc/uptime has no lines.");
29         return 1;
30     }
31     if(unlikely(procfile_linewords(ff, 0) < 1)) {
32         error("/proc/uptime has less than 1 word in it.");
33         return 1;
34     }
35
36     uptime = (collected_number)(strtold(procfile_lineword(ff, 0, 0), NULL) * 1000.0);
37 #endif
38
39     // --------------------------------------------------------------------
40
41     if(unlikely(!st))
42         st = rrdset_find_localhost("system.uptime");
43
44     if(unlikely(!st)) {
45         st = rrdset_create_localhost("system", "uptime", NULL, "uptime", NULL, "System Uptime", "seconds", 1000
46                                      , update_every, RRDSET_TYPE_LINE);
47         rrddim_add(st, "uptime", NULL, 1, 1000, RRD_ALGORITHM_ABSOLUTE);
48     }
49     else rrdset_next(st);
50
51     rrddim_set(st, "uptime", uptime);
52     rrdset_done(st);
53
54     return 0;
55 }