Add uptime badge to macOS plugin
Vladimir Kobal committed
Jan 12, 2017 at 19:12 UTC
a9a932357acf7b378992179b6caf113012094c59
1 file changed
+28
-1
src/macos_sysctl.c
+28
-1
@@ -17,6 +17,8 @@
17
#include <netinet/icmp_var.h>
18
// NEEDED BY do_icmp6...
19
#include <netinet/icmp6.h>
20
+// NEEDED BY do_uptime
21
+#include <time.h>
22
23
#define GETSYSCTL(name, var) getsysctl(name, &(var), sizeof(var))
24
@@ -35,7 +37,7 @@ int do_macos_sysctl(int update_every, usec_t dt) {
37
do_ip_packets = -1, do_ip_fragsout = -1, do_ip_fragsin = -1, do_ip_errors = -1,
38
do_ip6_packets = -1, do_ip6_fragsout = -1, do_ip6_fragsin = -1, do_ip6_errors = -1,
39
do_icmp6 = -1, do_icmp6_redir = -1, do_icmp6_errors = -1, do_icmp6_echos = -1,
38
- do_icmp6_router = -1, do_icmp6_neighbor = -1, do_icmp6_types = -1;
40
+ do_icmp6_router = -1, do_icmp6_neighbor = -1, do_icmp6_types = -1, do_uptime = -1;
41
42
43
if (unlikely(do_loadavg == -1)) {
@@ -68,6 +70,7 @@ int do_macos_sysctl(int update_every, usec_t dt) {
70
do_icmp6_router = config_get_boolean_ondemand("plugin:macos:sysctl", "icmp router", CONFIG_ONDEMAND_ONDEMAND);
71
do_icmp6_neighbor = config_get_boolean_ondemand("plugin:macos:sysctl", "icmp neighbor", CONFIG_ONDEMAND_ONDEMAND);
72
do_icmp6_types = config_get_boolean_ondemand("plugin:macos:sysctl", "icmp types", CONFIG_ONDEMAND_ONDEMAND);
73
+ do_uptime = config_get_boolean("plugin:macos:sysctl", "system uptime", 1);
74
}
75
76
RRDSET *st;
@@ -206,6 +209,9 @@ int do_macos_sysctl(int update_every, usec_t dt) {
209
u_long msgs_out;
210
} icmp6_total = {0, 0};
211
212
+ // NEEDED BY: do_uptime
213
+ struct timespec boot_time, cur_time;
214
+
215
// --------------------------------------------------------------------
216
217
if (last_loadavg_usec <= dt) {
@@ -1065,6 +1071,27 @@ int do_macos_sysctl(int update_every, usec_t dt) {
1071
}
1072
}
1073
1074
+ // --------------------------------------------------------------------
1075
+
1076
+ if (likely(do_uptime)) {
1077
+ if (unlikely(GETSYSCTL("kern.boottime", boot_time))) {
1078
+ do_uptime = 0;
1079
+ error("DISABLED: system.uptime");
1080
+ } else {
1081
+ clock_gettime(CLOCK_REALTIME, &cur_time);
1082
+ st = rrdset_find("system.uptime");
1083
+
1084
+ if(unlikely(!st)) {
1085
+ st = rrdset_create("system", "uptime", NULL, "uptime", NULL, "System Uptime", "seconds", 1000, update_every, RRDSET_TYPE_LINE);
1086
+ rrddim_add(st, "uptime", NULL, 1, 1, RRDDIM_ABSOLUTE);
1087
+ }
1088
+ else rrdset_next(st);
1089
+
1090
+ rrddim_set(st, "uptime", cur_time.tv_sec - boot_time.tv_sec);
1091
+ rrdset_done(st);
1092
+ }
1093
+ }
1094
+
1095
return 0;
1096
}
1097