snetz-0.1/000755 001750 000144 00000000000 12012226652 010707 5ustar00000000 000000 snetz-0.1/snetz.py000644 001750 000144 00000016520 12012217370 012425 0ustar00000000 000000 #!/usr/bin/env python # # snetz.py 0.1 - simple bandwidth monitoring tool # Author by Agus Bimantoro (http://rndc.or.id, http://abi71.wordpress.com) # # This program is free software; you can redistribute it and/or modify # it under the terms of the GNU General Public License as published by # the Free Software Foundation; either version 3 of the License, or # (at your option) any later version. # # This program is distributed in the hope that it will be useful, # but WITHOUT ANY WARRANTY; without even the implied warranty of # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the # GNU General Public License for more details. # # You should have received a copy of the GNU General Public License # along with this program; if not, write to the Free Software # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, # MA 02110-1301, USA. # __version__ = "0.1" __author__ = "Agus Bimantoro" __site__ = "http://rndc.or.id/wiki" import os import sys import time import struct import fcntl from socket import * SOCK = socket(AF_INET, SOCK_DGRAM) SIOCGIFFLAGS = 0x8913 IFF_UP = 0x1 IFF_LOOP_BACK = "lo" DELAY_TIME = 1.5 FILE_PATH = "/proc/net/dev" class snetz(object): def __init__(self, delay, path): """ initialize configuration """ self.delay = delay self.path = path if (self.delay < 1): sys.stderr.write("TimeError: %s: Minimal 1 seconds.\n" % (self.delay)) sys.exit(1) if not (os.path.exists(self.path)): sys.stderr.write("PathError: %s: No such file or directory.\n" % (self.path)) sys.exit(1) def check_if_up(self, iface): """ check whether the interface is up """ ifreq = struct.pack('16sh', iface, 0) flags = struct.unpack('16sh', fcntl.ioctl(SOCK.fileno(), SIOCGIFFLAGS, ifreq))[1] if (iface == IFF_LOOP_BACK): return True elif (flags & IFF_UP): return True else: return False def convert_byte_to_kbits(self, new_bytes, old_bytes, diff_times): """ convert byte to kilobits """ kbits = (((new_bytes - old_bytes) * 8)/1000)/diff_times kbits = '%.2f' % kbits return float(kbits) def get_bytes(self, iface, data): """ get byte and packets """ for line in open(self.path, 'r'): if (iface in line): n = line.split('%s:' % iface)[1].split() data[iface]['rxbn'] = float(n[0]) data[iface]['txbn'] = float(n[8]) break def main(self): """ main """ # initial interface = [] first_run = True # read all interface and give zero value to data variables # # rxbn = rx bytes new # rxbo = rx bytes old # # txbn = tx bytes new # txbo = tx bytes old # # prevtime = previous time # curtime = current time # for line in open(self.path, 'r'): if (':' in line): iface = line.split(':')[0].lstrip(' ') iface = {iface: {'rxbn': 0,'rxbo': 0,'txbn': 0,'txbo': 0,'prevtime': 0.0,'curtime': 0.0}} interface.append(iface) while True: total_all_rx = 0 total_all_tx = 0 total_all_rxtx = 0 if not (first_run): time.sleep(self.delay) first_run = False os.system("clear") print("\nSNETZ - simple bandwidth monitoring tool (%s)\n" % (__site__)) print(" =========================================================================") print(" %-15s%-17s%-17s%-18s%s" % ("Interface","RX(Kbit/sec)","TX(Kbit/sec)","Total(Kbit/sec)","Status")) print(" =========================================================================") for data in interface: iface = str(data).split("'")[1] self.get_bytes(iface,data) data[iface]['curtime'] = time.time() diff_time = data[iface]['curtime'] - data[iface]['prevtime'] # RX if (data[iface]['rxbn'] > data[iface]['rxbo']): rx = self.convert_byte_to_kbits(data[iface]['rxbn'], data[iface]['rxbo'], diff_time) data[iface]['rxbo'] = data[iface]['rxbn'] else: rx = 0 # TX if (data[iface]['txbn'] > data[iface]['txbo']): tx = self.convert_byte_to_kbits(data[iface]['txbn'], data[iface]['txbo'], diff_time) data[iface]['txbo'] = data[iface]['txbn'] else: tx = 0 # total RX+TX total_rxtx = rx+tx # total all total_all_rx += rx total_all_tx += tx total_all_rxtx += total_rxtx data[iface]['prevtime'] = data[iface]['curtime'] if self.check_if_up(iface): status = "Up" else: status = "Down" print(" %-15s%-17s%-17s%-18s%s" % (iface,rx,tx,total_rxtx,status)) print(" =========================================================================") print(" %-15s%-17s%-17s%s" % ('Total:',total_all_rx,total_all_tx,total_all_rxtx)) print("\n\nPress 'ctrl+c' for quit.") def license(): """ program license """ print("""SNETZ License: This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation; either version 3 of the License, or (at your option) any later version. This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with this program; if not, write to the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. """) sys.exit(0) def manual(): """ manual or helps """ print("SNETZ %s by %s - simple bandwidth monitoring tool (%s)\n" % (__version__, __author__,__site__)) print("Usage: snetz ") print("Options:") print(" -h, --help\t Display this help.") print(" -t, --time\t Update display time/sec, defaults to 1.5 seconds if not specified.") print(" -p, --path\t The path of network device status information,") print(" \t\t Filename defaults to '/proc/net/dev' if not specified.") print(" -l, --license Display software license.") print(" -v, --version Display version number.") print("\nEx: snetz ") print(" snetz -t 2 -p /proc/net/dev\n") print("Please report bugs to ") sys.exit(0) def version(): """ program version """ print("SNETZ Version: %s" % (__version__)) sys.exit(0) # getopt and sanity if (__name__ == "__main__"): try: for arg in sys.argv: if (arg.lower() == "-h" or arg.lower() == "--help"): manual() if (arg.lower() == "-t" or arg.lower() == "--time"): DELAY_TIME = float(sys.argv[int(sys.argv[1:].index(arg))+2]) if (arg.lower() == "-p" or arg.lower() == "--path"): FILE_PATH = str(sys.argv[int(sys.argv[1:].index(arg))+2]) if (arg.lower() == "-l" or arg.lower() == "--license"): license() if (arg.lower() == "-v" or arg.lower() == "--version"): version() except ValueError: if (DELAY_TIME): sys.stderr.write("ValueError: %s: Update time must be integer.\n" % (sys.argv[int(sys.argv[1:].index(arg))+2])) sys.exit(1) except IndexError: manual() try: sntz = snetz(DELAY_TIME,FILE_PATH) sntz.main() except IndexError, err: print("InternalError: %s." % (err)) except IOError: print("FileError: '%s' is a directory." % (FILE_PATH)) except ValueError: print("FileError: %s: File doesn't contains network device status information." % (FILE_PATH)) except KeyboardInterrupt: print("") sys.exit(0) except: if not sys.exc_info()[1]: print("UnexpectedError: %s" % (sys.exc_info()[1])) ## EOF snetz-0.1/Remove.py000644 001750 000144 00000002042 12006643351 012516 0ustar00000000 000000 #!/usr/bin/env python # # This file is part of Snetz # by Agus Bimantoro # This program is published under a GPLv3 license import os import sys SNETZ_PROGRAM_FILE = "/bin/snetz" SNETZ_MANUAL_FILE = "/usr/share/man/man1/snetz.1.gz" USER = os.geteuid() if (USER != 0): print("Warning: You must be root to install this program.") sys.exit(1) agree = raw_input("Do you want to remove Snetz [Y/n]? ") if (agree.lower() == "y" or agree.lower() == "yes"): print("\nRemoving snetz:") print("\nChecking file...") if not (os.path.exists(SNETZ_PROGRAM_FILE )): sys.stderr.write("\nError: %s: No such file or directory.\n" % (SNETZ_PROGRAM_FILE)) sys.exit(1) elif not (os.path.exists(SNETZ_MANUAL_FILE)): sys.stderr.write("\nError: %s: No such file or directory.\n" % (SNETZ_MANUAL_FILE)) sys.exit(1) print("\nRemoving snetz program...") os.system("rm %s 2> /dev/null" % (SNETZ_PROGRAM_FILE)) print("Removing manual program...") os.system("rm %s 2> /dev/null" % (SNETZ_MANUAL_FILE)) print("\nAll done...") ## EOF ## snetz-0.1/README000644 001750 000144 00000000777 12012105142 011570 0ustar00000000 000000 ################################################################# # # snetz.py -- Simple Banwidth Monitoring. # Abi # # http://projects.gxrg.org # http://rndc.or.id # ################################################################ You'll need: Python The dev pseudo-file, in linux machine at "/proc/net/dev" How to: - Install python Setup.py - Remove python Remove.py Thanks to: g0wal as inspired me :D RNDC (Indonesia Research and Development Center) and others snetz-0.1/ChangeLog000644 001750 000144 00000000437 12012214222 012453 0ustar00000000 000000 -- Agus Bimantoro , Wed Jan 12 16:00:00 UTC 2012 Snetz (0.1) * Public Released. * Hosting on sourceforge.net, The url projects at http://sourceforge.net/projects/snetz * Development page at https://github.com/rndc/snetz/ snetz-0.1/snetz.1.gz000644 001750 000144 00000001105 12012226625 012550 0ustar00000000 000000 -)Psnetz.1}SM0Wr ت*le +`IblD}džҬe>޼ybó0V459"+@&*6AuS :9Ey`h8[ .foz~:!3&i(??f~ ^uV#'%C )07)/Sy}'#??zu+|'.B#O^)CPϡ%S@m% C8 ݣUc/i\+&'Q5aʉccRˍ 8"CZJ^Kq7#HllgeK^hO~aY)TjqxF >4+;HCJ  &@m<Ҙ犆Ql[ae|ڄCvˋ.Zbm*cZEZ+/#ĉJU8FxÙވupn͓7-<$\<C].'}-wq; F^vds~t7k >?fsnetz-0.1/Setup.py000644 001750 000144 00000002152 12012220551 012351 0ustar00000000 000000 #!/usr/bin/env python # # This file is part of Snetz # by Agus Bimantoro # This program is published under a GPLv3 license import os import sys import time SNETZ_PROGRAM_FILE = "snetz.py" SNETZ_MANUAL_FILE = "snetz.1.gz" USER = os.geteuid() if (USER != 0): print("Warning: You must be root to install this program.") sys.exit(1) agree = raw_input("Do you want to install Snetz [Y/n]? ") if (agree.lower() == "y" or agree.lower() == "yes"): print("\nInstalling snetz:") print("\nChecking file...") if not (os.path.exists(SNETZ_PROGRAM_FILE )): sys.stderr.write("\nError: %s: No such file or directory.\n" % (SNETZ_PROGRAM_FILE)) sys.exit(1) elif not (os.path.exists(SNETZ_MANUAL_FILE)): sys.stderr.write("\nError: %s: No such file or directory.\n" % (SNETZ_MANUAL_FILE)) sys.exit(1) print("\nCopying snetz program to '/bin'...") os.system("cp snetz.py /bin/snetz") os.system("chmod +x /bin/snetz") print("Copying manual program to '/usr/share/man/man1'...") os.system("cp snetz.1.gz /usr/share/man/man1") print("\nInstallation finished. Type snetz as any user to run.") ## EOF ##