add interface uptime to the status info
authorFelix Fietkau <nbd@openwrt.org>
Mon, 10 Oct 2011 17:12:43 +0000 (19:12 +0200)
committerFelix Fietkau <nbd@openwrt.org>
Mon, 10 Oct 2011 17:12:43 +0000 (19:12 +0200)
interface.c
interface.h
system-dummy.c
system-linux.c
system.h
ubus.c

index 3820050..17cb754 100644 (file)
@@ -9,6 +9,7 @@
 #include "proto.h"
 #include "ubus.h"
 #include "config.h"
 #include "proto.h"
 #include "ubus.h"
 #include "config.h"
+#include "system.h"
 
 struct vlist_tree interfaces;
 
 
 struct vlist_tree interfaces;
 
@@ -238,6 +239,7 @@ interface_proto_cb(struct interface_proto_state *state, enum interface_proto_eve
                        return;
 
                iface->state = IFS_UP;
                        return;
 
                iface->state = IFS_UP;
+               iface->start_time = system_get_rtime();
                interface_event(iface, IFEV_UP);
                break;
        case IFPEV_DOWN:
                interface_event(iface, IFEV_UP);
                break;
        case IFPEV_DOWN:
index 4d53c83..0e81304 100644 (file)
@@ -48,6 +48,7 @@ struct interface {
        bool autostart;
        bool config_autostart;
 
        bool autostart;
        bool config_autostart;
 
+       time_t start_time;
        enum interface_state state;
        enum interface_config_state config_state;
 
        enum interface_state state;
        enum interface_config_state config_state;
 
index 31bfac8..4c9a8fc 100644 (file)
@@ -1,3 +1,4 @@
+#include <sys/time.h>
 #include <stdio.h>
 #include <string.h>
 
 #include <stdio.h>
 #include <string.h>
 
@@ -165,3 +166,13 @@ int system_del_route(struct device *dev, struct device_route *route)
        D(SYSTEM, "route del %s%s%s\n", addr, gw, devstr);
        return 0;
 }
        D(SYSTEM, "route del %s%s%s\n", addr, gw, devstr);
        return 0;
 }
+
+time_t system_get_rtime(void)
+{
+       struct timeval tv;
+
+       if (gettimeofday(&tv, NULL) == 0)
+               return tv.tv_sec;
+
+       return 0;
+}
index 9f1aa9e..aba603f 100644 (file)
@@ -1,6 +1,7 @@
 #include <sys/socket.h>
 #include <sys/ioctl.h>
 #include <sys/stat.h>
 #include <sys/socket.h>
 #include <sys/ioctl.h>
 #include <sys/stat.h>
+#include <sys/syscall.h>
 
 #include <linux/rtnetlink.h>
 #include <linux/sockios.h>
 
 #include <linux/rtnetlink.h>
 #include <linux/sockios.h>
@@ -395,3 +396,17 @@ int system_del_route(struct device *dev, struct device_route *route)
 {
        return system_rt(dev, route, RTM_DELROUTE);
 }
 {
        return system_rt(dev, route, RTM_DELROUTE);
 }
+
+time_t system_get_rtime(void)
+{
+       struct timespec ts;
+       struct timeval tv;
+
+       if (syscall(__NR_clock_gettime, CLOCK_MONOTONIC, &ts) == 0)
+               return ts.tv_sec;
+
+       if (gettimeofday(&tv, NULL) == 0)
+               return tv.tv_sec;
+
+       return 0;
+}
index 7600417..392e46b 100644 (file)
--- a/system.h
+++ b/system.h
@@ -1,6 +1,7 @@
 #ifndef __NETIFD_SYSTEM_H
 #define __NETIFD_SYSTEM_H
 
 #ifndef __NETIFD_SYSTEM_H
 #define __NETIFD_SYSTEM_H
 
+#include <sys/time.h>
 #include <sys/socket.h>
 #include "device.h"
 #include "interface-ip.h"
 #include <sys/socket.h>
 #include "device.h"
 #include "interface-ip.h"
@@ -43,4 +44,6 @@ int system_del_address(struct device *dev, struct device_addr *addr);
 int system_add_route(struct device *dev, struct device_route *route);
 int system_del_route(struct device *dev, struct device_route *route);
 
 int system_add_route(struct device *dev, struct device_route *route);
 int system_del_route(struct device *dev, struct device_route *route);
 
+time_t system_get_rtime(void);
+
 #endif
 #endif
diff --git a/ubus.c b/ubus.c
index ca5d110..da00b80 100644 (file)
--- a/ubus.c
+++ b/ubus.c
@@ -4,6 +4,7 @@
 #include "interface.h"
 #include "proto.h"
 #include "ubus.h"
 #include "interface.h"
 #include "proto.h"
 #include "ubus.h"
+#include "system.h"
 
 static struct ubus_context *ctx = NULL;
 static struct blob_buf b;
 
 static struct ubus_context *ctx = NULL;
 static struct blob_buf b;
@@ -177,6 +178,12 @@ netifd_handle_status(struct ubus_context *ctx, struct ubus_object *obj,
        blobmsg_add_u8(&b, "pending", iface->state == IFS_SETUP);
        blobmsg_add_u8(&b, "available", iface->available);
        blobmsg_add_u8(&b, "autostart", iface->autostart);
        blobmsg_add_u8(&b, "pending", iface->state == IFS_SETUP);
        blobmsg_add_u8(&b, "available", iface->available);
        blobmsg_add_u8(&b, "autostart", iface->autostart);
+
+       if (iface->state == IFS_UP) {
+               time_t cur = system_get_rtime();
+               blobmsg_add_u32(&b, "uptime", cur - iface->start_time);
+       }
+
        if (iface->main_dev.dev) {
                struct device *dev = iface->main_dev.dev;
                const char *field;
        if (iface->main_dev.dev) {
                struct device *dev = iface->main_dev.dev;
                const char *field;