Posted by iggykoopa on Sun 26th Apr 08:54 (modification of post by iggykoopa view diff)
diff | download | new post
- #!/usr/bin/env python
- import os,commands,pygtk,pynotify,gtk,datetime
- def check(interval):
- sizes = ["kB", "MB", "GB"]
- tx,rx,total = 0.0,0.0,0.0
- tx_size,rx_size,total_size = "kB","kB","kB"
- date = datetime.date.today()
- day,month,year = date.day,date.month,date.year
- if day < 10:
- day = "0" + str(day)
- else:
- day = str(day)
- if month < 10:
- month = "0" + str(month)
- else:
- month = str(month)
- if interval == "daily":
- vnstat = commands.getoutput("vnstat -d").split("\n")
- for line in vnstat:
- if "%s.%s." % (day, month) in line:
- stats = line.replace("|","").split()
- if rx_size == stats[2]:
- rx = rx + float(stats[1])
- elif sizes.index(rx_size) > sizes.index(stats[2]):
- difference = float(sizes.index(rx_size) - sizes.index(stats[2]))
- rx = rx + (float(stats[1]) / 1024.0 ** difference)
- else:
- difference = float(sizes.index(stats[2]) - sizes.index(rx_size))
- rx = float(stats[1]) + ( rx / 1024.0 ** difference)
- rx_size = stats[2]
- if tx_size == stats[4]:
- tx = tx + float(stats[3])
- elif sizes.index(tx_size) > sizes.index(stats[4]):
- difference = float(sizes.index(tx_size) - sizes.index(stats[4]))
- tx = tx + (float(stats[3]) / 1024.0 ** difference)
- else:
- difference = float(sizes.index(stats[4]) - sizes.index(tx_size))
- tx = float(stats[3]) + ( tx / 1024.0 ** difference)
- tx_size = stats[4]
- if total_size == stats[6]:
- total = total + float(stats[5])
- elif sizes.index(total_size) > sizes.index(stats[6]):
- difference = float(sizes.index(total_size) - sizes.index(stats[6]))
- total = total + (float(stats[5]) / 1024.0 ** difference)
- else:
- difference = float(sizes.index(stats[6]) - sizes.index(total_size))
- total = float(stats[5]) + ( total / 1024.0 ** difference)
- total_size = stats[6]
- if interval == "weekly":
- vnstat = commands.getoutput("vnstat -w").split("\n")
- for line in vnstat:
- if "current week" in line:
- stats = line.replace("|","").split()
- if rx_size == stats[3]:
- rx = rx + float(stats[2])
- elif sizes.index(rx_size) > sizes.index(stats[3]):
- difference = float(sizes.index(rx_size) - sizes.index(stats[3]))
- rx = rx + (float(stats[2]) / 1024.0 ** difference)
- else:
- difference = float(sizes.index(stats[3]) - sizes.index(rx_size))
- rx = float(stats[2]) + ( rx / 1024.0 ** difference)
- rx_size = stats[3]
- if tx_size == stats[5]:
- tx = tx + float(stats[4])
- elif sizes.index(tx_size) > sizes.index(stats[5]):
- difference = float(sizes.index(tx_size) - sizes.index(stats[5]))
- tx = tx + (float(stats[4]) / 1024.0 ** difference)
- else:
- difference = float(sizes.index(stats[5]) - sizes.index(tx_size))
- tx = float(stats[4]) + ( tx / 1024.0 ** difference)
- tx_size = stats[5]
- if total_size == stats[7]:
- total = total + float(stats[6])
- elif sizes.index(total_size) > sizes.index(stats[7]):
- difference = float(sizes.index(total_size) - sizes.index(stats[7]))
- total = total + (float(stats[6]) / 1024.0 ** difference)
- else:
- difference = float(sizes.index(stats[7]) - sizes.index(total_size))
- total = float(stats[6]) + ( total / 1024.0 ** difference)
- total_size = stats[7]
- if interval == "monthly":
- vnstat = commands.getoutput("vnstat -m").split("\n")
- for line in vnstat:
- if date.strftime("%b") in line:
- stats = line.replace("|","").split()
- if rx_size == stats[3]:
- rx = rx + float(stats[2])
- elif sizes.index(rx_size) > sizes.index(stats[3]):
- difference = float(sizes.index(rx_size) - sizes.index(stats[3]))
- rx = rx + (float(stats[2]) / 1024.0 ** difference)
- else:
- difference = float(sizes.index(stats[3]) - sizes.index(rx_size))
- rx = float(stats[2]) + ( rx / 1024.0 ** difference)
- rx_size = stats[3]
- if tx_size == stats[5]:
- tx = tx + float(stats[4])
- elif sizes.index(tx_size) > sizes.index(stats[5]):
- difference = float(sizes.index(tx_size) - sizes.index(stats[5]))
- tx = tx + (float(stats[4]) / 1024.0 ** difference)
- else:
- difference = float(sizes.index(stats[5]) - sizes.index(tx_size))
- tx = float(stats[4]) + ( tx / 1024.0 ** difference)
- tx_size = stats[5]
- if total_size == stats[7]:
- total = total + float(stats[6])
- elif sizes.index(total_size) > sizes.index(stats[7]):
- difference = float(sizes.index(total_size) - sizes.index(stats[7]))
- total = total + (float(stats[6]) / 1024.0 ** difference)
- else:
- difference = float(sizes.index(stats[7]) - sizes.index(total_size))
- total = float(stats[6]) + ( total / 1024.0 ** difference)
- total_size = stats[7]
- pynotify.init("VNstat-icon")
- n = pynotify.Notification("recieve: %s%s\ntransmit: %s%s\ntotal: %s%s" % (rx, rx_size, tx, tx_size, total, total_size))
- n.attach_to_status_icon(icon)
- n.show()
- def change_interval(new_interval):
- global interval
- interval = new_interval
- def exit_app(data=None):
- "exit application"
- gtk.main_quit()
- def make_menu(event_button, event_time, data=None):
- "make the right click menu"
- menu = gtk.Menu()
- daily = gtk.MenuItem("Daily")
- weekly = gtk.MenuItem("Weekly")
- monthly = gtk.MenuItem("Monthly")
- menu.append(daily)
- menu.append(weekly)
- menu.append(monthly)
- daily.connect_object("activate", change_interval, "daily")
- weekly.connect_object("activate", change_interval, "weekly")
- monthly.connect_object("activate", change_interval, "monthly")
- separator1 = gtk.MenuItem()
- menu.append(separator1)
- separator1.show()
- #show the exit option no matter what is configured
- exit_item = gtk.MenuItem("Exit")
- menu.append(exit_item)
- exit_item.connect_object("activate", exit_app, "Exit App")
- daily.show()
- weekly.show()
- monthly.show()
- exit_item.show()
- #Popup the menu
- menu.popup(None, None, None, event_button,
- event_time)
- def on_right_click(data, event_button, event_time):
- "set the right click tray icon menu"
- make_menu(event_button, event_time)
- def on_left_click(event):
- "make the preferences menu"
- global interval
- check(interval)
- if __name__ == "__main__":
- "main section of program"
- global interval
- interval = "daily"
- # Set icon, right and left click menus
- icon = gtk.status_icon_new_from_stock(gtk.STOCK_NETWORK)
- icon.connect('popup-menu', on_right_click)
- icon.connect('activate', on_left_click)
- gtk.main()
Submit a correction or amendment below (click here to make a fresh posting)
After submitting an amendment, you'll be able to view the differences between the old and new posts easily.