How can I get information about an network interface uptime?

8

2

I have an Ubuntu machine and a Debian machine.

On both I want to be able to see for how long an network interface has been connected. (That is, connected to a network getting an IP etc. Not the physical state of a cabel). Uptime in seconds or date + time since last change or anything similar.

As of now I have written a little script to do the task but it seems there should be a more general way to check this. A program or something in /proc or such.

My script:

#!/bin/bash

if [ -f /etc/os-release ]; then

    if TMP=$(grep -i 'ubuntu' /etc/os-release); then

        # we are on ubuntu
        for i in $(/bin/ls -1 /var/log/syslog* | sort -r); do
                TMP=$(zgrep '(eth0): device state change: ip-config -> activated' "$i" | tail -1 | sed "s/ "$(hostname)"/*/")
        done

        WHEN=$(echo "$TMP" | cut -f1 -d '*')
        SEC=$(echo "$(date +%s) - $(date -d "$WHEN" +%s)" | bc)
        echo "Last link up: $WHEN ($SEC seconds ago)."

    elif TMP=$(grep -i 'debian' /etc/os-release); then

        # we are on debian
        TMP=$(grep 'eth0: link up' /var/log/syslog* | tail -1 | cut -f2- -d':' | sed "s/ "$(uname -n)" kernel:/*/")
        WHEN=$(echo "$TMP" | cut -f1 -d '*')
        SEC=$(echo "$(date +%s) - $(date -d "$WHEN" +%s)" | bc)
        echo "Last link up: $WHEN ($SEC seconds ago)."

    fi

else
    echo "File /etc/os-release not found."
fi

Stefan Lithén

Posted 2013-07-08T19:31:14.543

Reputation: 81

If your router reboots, you may want to see what effect it has on the network. I have a NAS that makes a series of beeps when I disconnect the cable from it. Right now, it has a crossover cable plugging it into a server. If I reboot the server, the NAS loses link, and makes noise. From what you describe, you don't really care about IP packets, nor whether a cable is plugged into your computer's NIC port, but you do want to know about link. That can be detected using technology called Media Sense. (The feature might also often be called "Media State".) Try logging changes to that. – TOOGAM – 2015-02-15T22:09:09.643

1There's no such thing as "being connected to a network" other than having a physical cable connection. Having an IP address assigned to a NIC is not an indicator of a network state. If you want to determine network connectivity for a NIC, you will have to monitor it actively (for example, by sending periodic pings to another target or having a persistent TCP connection). – Der Hochstapler – 2013-07-08T20:09:51.700

Hum, well then when my router reboots (takes about 1 minute), my NIC has it's IP removed and then reassigned again (byt NetworkManager). my iMac does something similar.

What I really want to know is when the router rebooted. In the logs in my iMac and in linux I can see when it was reassigned again, but I guess without something like NetworkManager it would still have it's IP assigned?

Maybe I should take a closer look at NetworkManager instead. Thank you. – Stefan Lithén – 2013-07-08T20:54:53.580

1

I'm assuming that NetworkManager (or another component) performs monitoring on active connections. If a connection breaks, it determines availability of your gateway (router), once it detects that it is down, it might release the IP address assigned through DHCP. Or maybe it waits for the gateway to be available again and then asks for the IP address to be reassigned. Either way, this question proposes using ip monitor (among other things), it might be worth a look.

– Der Hochstapler – 2013-07-08T21:14:12.157

1Maybe placing a script in /etc/dhcp3/dhclient-enter-hooks.d/ could also be an option. But I'm not finding enough information to say how exactly it works. – Der Hochstapler – 2013-07-08T21:21:07.040

Answers

1

On my machine dhclient is restarted by NetworkManager when re-connecting to the network. So maybe you can use the start time of the dhclient process?

ps -o start,cmd $(pgrep dhclient)

hfs

Posted 2013-07-08T19:31:14.543

Reputation: 338

1

The Linux kernel does not track the time an interface is started.

Within struct net_device there is no field which holds a jiffies value for when an interface is started.

The best you can manage is some inferred method from userspace scripts and logs.

suprjami

Posted 2013-07-08T19:31:14.543

Reputation: 543

0

Here is my variant (very similar to your):

~ # expr $(echo $(date +%s) - $(date -d "`grep 'eth0: leased' /var/log/messages | tail -1 | awk '{print $1, $2, $3}'`" +%s))
1116
~ #

1116 - seconds after IP leased.

september

Posted 2013-07-08T19:31:14.543

Reputation: 529

0

This should do what you want, in seconds:

#!/bin/bash
STARTTIME=`date +%s`
GATEWAY=`ip r s | grep default | cut -d' ' -f3`
INTERVAL=1
while ping -c 1 -W 1 ${GATEWAY} >/dev/null 2>&1; 
do
  awk -v STIME="$STARTTIME" 'BEGIN {
   DTIME=systime()-STIME;
   printf "Seconds of uptime since %s: %d\n",
     strftime("%a %b %e %H:%M:%S %Z %Y",STIME),
     DTIME; }';
  sleep $INTERVAL;
done

echo "HOST DROPPED!"

Which outputs:

user@host $ sh test-script.sh
Seconds of uptime since Sat Nov 15 01:14:26 EST 2014: 0
Seconds of uptime since Sat Nov 15 01:14:26 EST 2014: 1
Seconds of uptime since Sat Nov 15 01:14:26 EST 2014: 2
Seconds of uptime since Sat Nov 15 01:14:26 EST 2014: 3
Seconds of uptime since Sat Nov 15 01:14:26 EST 2014: 4
HOST DROPPED!

Theory: Get a timestamp STARTTIME, then test every INTERVAL whether the gateway (via ip route show) is still up, if so, substract the current timestamp from the original and print. If not, exit and indicate that the host has dropped its connection. See the manpages for explanation of each of the command options. If you don't want output every second, increase INTERVAL.

glallen

Posted 2013-07-08T19:31:14.543

Reputation: 1 886