LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Software
User Name
Password
Linux - Software This forum is for Software issues.
Having a problem installing a new program? Want to know which application is best for the job? Post your question in this forum.

Notices


Reply
  Search this Thread
Old 12-05-2014, 01:06 PM   #1
jeevanism
Member
 
Registered: Nov 2010
Location: India
Distribution: Mandriva
Posts: 48

Rep: Reputation: 0
Internet Usage Monitor :- Data Usage Monitor / Calculator in Linux


Internet Usage Monitor :-

Hello All,

I am using MTS Monthly internet plan with 40GB usage. They will decrease speed once I pass 32GB. They dont provide any feature to calculate the data usage I do, atleast for linux.

So, I would like to have an internet usage monitor program for my linux. Is there any such program / tool available ? If not, is there any script or idea how to ?
 
Old 12-05-2014, 02:57 PM   #2
rknichols
Senior Member
 
Registered: Aug 2009
Distribution: CentOS
Posts: 4,748

Rep: Reputation: 2203Reputation: 2203Reputation: 2203Reputation: 2203Reputation: 2203Reputation: 2203Reputation: 2203Reputation: 2203Reputation: 2203Reputation: 2203Reputation: 2203
The way I do it is with two firewall rules that just accumulate the incoming and outgoing byte counts and a script that reads and resets those counts every hour, logging a cumulative result to a file. The rules go in the mangle table:
Code:
iptables -t mangle -A PREROUTING ! -s 192.168.0.0/16 -i eth1
iptables -t mangle -A POSTROUTING ! -d 192.168.0.0/16 -o eth1
The exclusion for 192.168.0.0/16 is so that traffic to and from the modem's internal web server is not counted. That traffic never goes out on the cable.

Here's the script that runs each hour. I also have a trigger that runs it when the interface is shut down so that I don't miss the final, partial hour.
Code:
#!/bin/bash
Ddir=/srv/log
unset Sudo
[ $(id -u) != 0 ] && Sudo=sudo
declare -a Pzero Pcnt
# A bit messy.  Timestamps in the file are local time, but the reset
# occurs at midnight UTC at the end of each month.
Reset=n
if [ -s $DDir/netcount ]; then
    Pzero=($(tail -1 $DDir/netcount))
        # Convert local timestamp to seconds since 1/1/1970 00:00:00 UTC
    Tsec=$(date -d "${Pzero[0]} ${Pzero[1]}" +%s)
        # Compare that UTC month with current UTC month
    [ $(date -u -d @$Tsec +%m) != $(date -u "+%m") ] && Reset=y
else
    Pzero=(mm/dd/yy hh:mm:ss 0 0 0 0)
fi
Pcnt=( $($Sudo /sbin/iptables -t mangle -vnxL -Z | awk '
/PREROUTING/ { dir = "in"; }
/POSTROUTING/ { dir = "out"; }
/ eth[01] / { count[dir] = $2; }
END { print count["in"], count["out"]; }') )
if [ ${#Pcnt[*]} != 2 ]; then
    echo "Bad counts (${#Pcnt[*]}) from iptables. Pcnt=(${Pcnt[*]})" >&2
    exit 1
fi
date "+%x %X ${Pcnt[0]} ${Pcnt[1]} $((${Pzero[4]}+${Pcnt[0]})) $((${Pzero[5]}+${Pcnt[1]}))" >>$DDir/netcount
[ $Reset = y ] && date -d "now + 1 second" "+%x %X 0 0 0 0" >>$DDir/netcount
That's actually a fragment from a larger script -- apologies if I missed something when extracting it. If the script is to be run from a non-root UID, then you need a NOPASSWD rule in /etc/sudoers to allow it.

Here's an excerpt from that log file, taken around the end of a month:
Code:
11/30/14 16:00:01 2729527 1100269 25212875259 968153775
11/30/14 17:00:01 297509 65356 25213172768 968219131
11/30/14 18:00:01 1008185 98442 25214180953 968317573
11/30/14 18:00:02 0 0 0 0
11/30/14 19:00:02 273945 64896 273945 64896
11/30/14 20:00:01 3312753 476234 3586698 541130
Each entry shows the in and out counts for the current hour plus the cumulative totals for the current month.
 
1 members found this post helpful.
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
script to monitor internet usage slac-in-the-box Programming 4 06-16-2011 04:03 PM
Monitor and Log Internet Bandwidth Usage Woodsman Slackware 26 01-04-2010 08:19 PM
Need an application to monitor internet usage firewiz87 Linux - Desktop 4 06-04-2008 02:57 PM
gkrellm Internet Usage Monitor CrownAmbassador Linux - Networking 2 04-13-2007 02:59 PM
Need Help: How to monitor Internet Usage subratabera Linux - Security 2 09-30-2006 09:46 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - Software

All times are GMT -5. The time now is 10:27 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration