LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Networking (http://www.linuxquestions.org/questions/linux-networking-3/)
-   -   Finding out interface uptime (http://www.linuxquestions.org/questions/linux-networking-3/finding-out-interface-uptime-606059/)

GSMD 12-11-2007 10:30 AM

Finding out interface uptime
 
Is there any way to find out how long is an interface (e.g. ppp0) up?
I've looked through /proc/net and found nothing.
The only way I see so far, is getting bytes/packets received from /proc/net/dev along with a time stamp and then comparing against these values. Which seems like a dirty hack.
Anything better, please?

TIA.

David1357 12-11-2007 01:06 PM

Quote:

Originally Posted by GSMD (Post 2987179)
Is there any way to find out how long is an interface (e.g. ppp0) up?

For wired interface (e.g. eth0) you will see a line like this in the output of "dmesg":

[ 80.836871] e1000: eth0: e1000_watchdog: NIC Link is Up 1000 Mbps Full Duplex, Flow Control: RX/TX
You will also see a similar line if the interface goes down. The number in brackets is the time in seconds since the system booted. Just write a script or program that can parse dmesg and diff the times. I would use a C program, but I am a programmer.

GSMD 12-13-2007 08:27 AM

Thanks for your reply. Unfortunately, not the case for ppp.

nx5000 12-13-2007 08:55 AM

Well then look at the creation time of the log of ppp?
In some cases, maybe checking /etc/resolv.conf modification time can do the trick.

GSMD 12-13-2007 05:06 PM

Nope, got another idea :)
Code:

date +%s
gives the universal timestamp while
Code:

cat /proc/net/dev | grep ppp0 | cut -d":" -f2 | cut -d" " -f1
gives the number of bytes received.
For each round (BTW, this will be a Munin plugin) the current number of bytes received gets compared with the number of bytes received during the previous round (stored somewhere in file in /tmp/). If it's <, then the connection has been dropped and reestablished. Total uptime gets calculated during each round.


All times are GMT -5. The time now is 11:12 PM.