A simple pythonic script to log internet bandwidth usage on Linux

We often want to log internet bandwidth on our laptops or PCs in order to track our usage over a period of time. This also comes handy to cross-check our usage with that of our ISP’s invoice. Now how to do it on linux? While the Windows folk have all kinds of utilities for this kind of thing, we lunix folk have to invent it ourselves.

Some time ago, I wrote this small python script that writes your bandwidth stats to a log file each time you disconnect your internet connection. This, I’ve tested on ubuntu 12.04 and debian wheezy:

Source file: Tata.py

#!/usr/bin/python
# author: Prahlad Yeri
import subprocess, os, datetime, sys
def execute(command):
    try:
        p=subprocess.Popen(command, shell=True,stdout=subprocess.PIPE,stderr=subprocess.PIPE)
        p.wait()
        result=p.communicate()
        #print str(result)
        if len(result[0])>0:
            return result[0].strip()
        else:
            return result[1].strip()
    except Exception as e:
        print ‘error occured:’ + errorstring
        return errorstring
#main
mypath = ‘/etc/NetworkManager/dispatcher.d/’
iface = sys.argv[1].strip()
status = sys.argv[2].strip()
log=open(mypath + ‘tata.log’,’a’)
#log.write(iface + ‘:’ + status + ‘\n’)
#log.close()
for part in sys.argv:
        log.write(‘argv:’ + part + ‘\n’)
if (iface == ‘ppp0’ or iface == ‘ttyUSB0’) and status == ‘down’:
        s = execute(‘iptables -L -v’)
        rules = s.splitlines()
        rx,tx = 0,0
        rxdrop, txdrop = 0,0
        inrule = False
        outrule = False
        for rule in rules:
                log.write(‘rule:’ + rule.strip() + ‘\n’)
                words = rule.strip().split()
                if rule.strip().startswith(“Chain OUTPUT”):
                        log.write(‘switch to outrule’)
                        outrule = True
                        inrule = False
                elif rule.strip().startswith(“Chain INPUT”):
                        log.write(‘switch to inrule’)
                        inrule = True
                        outrule = False
                elif rule.strip().startswith(“Chain FORWARD”):
                        log.write(‘switch to no rule’)
                        inrule=False
                        outrule=False
                elif len(words) >=3:
                        if words[6].strip() == “ppp0”:
                                #log.write(‘bytes=’ + words[1]
                                if inrule:
                                        if words[2].strip() == ‘DROP’:
                                                rxdrop += int(words[1].replace(‘K’,’000′).replace(‘M’,’000000′))
                                        else:
                                                rx += int(words[1].replace(‘K’,’000′).replace(‘M’,’000000′))
                                elif outrule:
                                        if words[2].strip() == ‘DROP’:
                                                txdrop += int(words[1].replace(‘K’,’000′).replace(‘M’,’000000′))
                                        else:
                                                tx += int(words[1].replace(‘K’,’000′).replace(‘M’,’000000′))
        #print ‘rx=’ + str(rx) + ‘ tx=’ + str(tx)
        #print ‘rxdrop=’ + str(rxdrop) + ‘ txdrop=’ + str(txdrop)
        execute(“iptables –zero”)
        header=”
        lines=[]
        if not os.path.exists(mypath + ‘usage.csv’):
                #create header
                header = ‘date,rx,tx,rx-dropped,tx-dropped,rx-tot,tx-tot\n’
        f = open(mypath + ‘usage.csv’,’a’)
        if len(header) > 0:
                lines.append(header)
        lines.append(str(datetime.datetime.now()).split(‘.’)[0] + ‘,’ + str(rx) + ‘,’ + str(tx) + ‘,’ + str(rxdrop) + ‘,’ + str(txdrop) + ‘,’ + str(rx+rxdrop) + ‘,’ + str(tx+txdrop) + ‘\n’)
        #rx = execute(‘cat /sys/class/net/ppp0/statistics/rx_bytes’)
        #tx = execute(‘cat /sys/class/net/ppp0/statistics/tx_bytes’)
        #strdate=str(datetime.datetime.now())
        #line = strdate + ‘,’ + rx + ‘,’ + tx + ‘\n’
        f.writelines(lines)
        f.close()
        log.close()
        sys.exit(0)

In the above script, replace ppp0 with the interface-name for the name of interface through which you access internet. For mobile broadbands it is typically ppp0, but you can issue the “ifconfig” command to list them all and make sure.

You have to place this script at /etc/NetworkManager/dispatcher.d on debian/ubuntu systems so that it runs each time you stop using the modem. In case of other systems, or in case you don’t use NetworkManager, refer to your distro documentation to check the folder/script which is executed when an interface is down.

Advertisements

How to add or remove launchers from your gnome classic/fallback desktop panel

This is a nice little hack that comes handy when you want to add or remove launchers (shortcuts to your favorite programs such as firefox or gedit) to an existing panel, or add an entirely new panel to your gnome-classic or fallback desktop. On my Linux Mint workstation, I prefer the gnome-classic version instead of the default cinnammon desktop for reasons of speed and simplicity. I soon found out, however, that process of adding/removing launchers or adding a new panel requires a special key combination that is not easily found without going through some extensive documentation!! Here is how you do it:

To add a new launcher:

1. Press the WIN (Super) and ALT keys together.
2. While keeping them pressed, right-click the panel.
3. You will then get a popup menu saying “New Panel”, or “Add to Panel”.
4. Click on the second option and select your program.

To edit/remove an existing launcher:

1. Follow steps 1 & 2 from above.
2. You will get a popup menu saying “Move” and “Remove from Panel”.
3. Click on your choice.

How to make brightness changes permanent in your Linux Desktop?

Quite recently, I’ve faced this nagging little issue on all the distros I tried. These included Ubuntu 12.04 LTS, Linux Mint 11.1 and Fedora 18. Whenever I changed my computer’s brightness setting either through the gnome-settings applet or by using hotkeys, the changes were only temporary. On next reboot, they would vanish!! Now what is the easiest way to make your chosen setting permanent? After much googling and head-scratching, I finally arrived at the answer.

First of all, NEVER place anything like this in a startup script:

echo 4 > /sys/class/backlight/acpi_video0/brightness

(See this update if you found this folder empty or missing)

Although this command might change your brightness, again the effects are only temporary and will be lost on your next reboot. If you attempt to place this in /etc/rc.local, it may not always work due to permission issues. Instead of ending up chmod-ing the permissions to the brightness file and cause any other issues in turn, the recommended approach is to use xbacklight, the tool fit for this purpose.

Here is the easy way:

1. Install xbacklight from your package repository. On Linux Mint, I did this:

sudo apt-get install xbacklight

2. Place xbacklight command with your chosen settings in your “startup-applications” applet. In my case I had the command: xbacklight -set 50

On Mint Linux, it looked something like this:

Image for xbacklight-startup
Configure xbacklight in startup-applications applet

Thats all. This effectively sets brightness to 50% by default upon your each login. Enjoy!!

UPDATE on 04-mar-2013: I came to know today that the 3.4 kernel has broken some things related to acpi-support for setting brightness. As a result of that the /sys/class/backlight folder is empty and the xbacklight program also won't work!! Solution? Either upgrade/downgrade your kernel or add "acpi_backlight=vendor" to your menu.lst. See this link for more information:

http://superuser.com/questions/548459/how-to-change-screen-brightness-on-toshiba-satellite-l850-djs-in-linux/556745#comment683067_556745