]> arthur.barton.de Git - netdata.git/commitdiff
Add uptime badge to macOS plugin
authorVladimir Kobal <vlad@prokk.net>
Fri, 13 Jan 2017 00:12:24 +0000 (19:12 -0500)
committerVladimir Kobal <vlad@prokk.net>
Fri, 13 Jan 2017 00:12:24 +0000 (19:12 -0500)
src/macos_sysctl.c

index 1c680a825798ef02f2aa403fc884ea162dea99d7..3a8498efa96a3d7a65b25a4ee43e8a485aaf2f47 100644 (file)
@@ -17,6 +17,8 @@
 #include <netinet/icmp_var.h>
 // NEEDED BY do_icmp6...
 #include <netinet/icmp6.h>
+// NEEDED BY do_uptime
+#include <time.h>
 
 #define GETSYSCTL(name, var) getsysctl(name, &(var), sizeof(var))
 
@@ -35,7 +37,7 @@ int do_macos_sysctl(int update_every, usec_t dt) {
                do_ip_packets = -1, do_ip_fragsout = -1, do_ip_fragsin = -1, do_ip_errors = -1,
                do_ip6_packets = -1, do_ip6_fragsout = -1, do_ip6_fragsin = -1, do_ip6_errors = -1,
                do_icmp6 = -1, do_icmp6_redir = -1, do_icmp6_errors = -1, do_icmp6_echos = -1,
-               do_icmp6_router = -1, do_icmp6_neighbor = -1, do_icmp6_types = -1;
+               do_icmp6_router = -1, do_icmp6_neighbor = -1, do_icmp6_types = -1, do_uptime = -1;
 
 
     if (unlikely(do_loadavg == -1)) {
@@ -68,6 +70,7 @@ int do_macos_sysctl(int update_every, usec_t dt) {
         do_icmp6_router         = config_get_boolean_ondemand("plugin:macos:sysctl", "icmp router", CONFIG_ONDEMAND_ONDEMAND);
         do_icmp6_neighbor       = config_get_boolean_ondemand("plugin:macos:sysctl", "icmp neighbor", CONFIG_ONDEMAND_ONDEMAND);
         do_icmp6_types          = config_get_boolean_ondemand("plugin:macos:sysctl", "icmp types", CONFIG_ONDEMAND_ONDEMAND);
+        do_uptime               = config_get_boolean("plugin:macos:sysctl", "system uptime", 1);
     }
 
     RRDSET *st;
@@ -206,6 +209,9 @@ int do_macos_sysctl(int update_every, usec_t dt) {
         u_long  msgs_out;
     } icmp6_total = {0, 0};
 
+    // NEEDED BY: do_uptime
+    struct timespec boot_time, cur_time;
+
     // --------------------------------------------------------------------
 
     if (last_loadavg_usec <= dt) {
@@ -1065,6 +1071,27 @@ int do_macos_sysctl(int update_every, usec_t dt) {
         }
     }
 
+    // --------------------------------------------------------------------
+
+    if (likely(do_uptime)) {
+        if (unlikely(GETSYSCTL("kern.boottime", boot_time))) {
+            do_uptime = 0;
+            error("DISABLED: system.uptime");
+        } else {
+            clock_gettime(CLOCK_REALTIME, &cur_time);
+            st = rrdset_find("system.uptime");
+
+            if(unlikely(!st)) {
+                st = rrdset_create("system", "uptime", NULL, "uptime", NULL, "System Uptime", "seconds", 1000, update_every, RRDSET_TYPE_LINE);
+                rrddim_add(st, "uptime", NULL, 1, 1, RRDDIM_ABSOLUTE);
+            }
+            else rrdset_next(st);
+
+            rrddim_set(st, "uptime", cur_time.tv_sec - boot_time.tv_sec);
+            rrdset_done(st);
+        }
+    }
+
     return 0;
 }