#!/usr/bin/python ####################################################################### # This is a program I wrote to know how much of internet time I have # been using. It generates statistics for the past few days, depending # on your /var/log/messages. # # 29th June, 2004 # # Please send comments and suggestions for improvement to: # ee03b091 at eedot iitm dot ac dot in ####################################################################### ####################################################################### # Issues: # I am using my own date-wise sorting mechanism. So, in long reports, # at the end of the year, December will appear *after* January of the # new year. TODO ####################################################################### """ This is a program I wrote to know how much of internet time I have been using. It generates statistics for the past few days, depending on your /var/log/messages. """ ####################################################################### # Customization: # EMAIL="root@localhost" # This is the recipients' address FROMEMAIL="root@localhost" # This is the sender's address SMTPSERV="localhost" # This is the smtp server which is conteacted for sending the message ####################################################################### import os, sys, string, re, smtplib monthno = { 'Jan': 1, 'Feb': 2, 'Mar': 3, 'Apr': 4, 'May': 5, 'Jun': 6, 'Jul': 7, 'Aug': 8, 'Sep': 9, 'Oct': 10, 'Nov': 11, 'Dec': 12 } logfilelist = [ "/var/log/messages" ] files = [] if len(sys.argv) > 1: if sys.argv[1] == "-a": files = os.listdir("/var/log") reobj = re.compile("^messages\.\d+$") for i in range(1, len(files)): if reobj.search(files[i]): logfilelist.append("/var/log/" + files[i]) del files logfilelist.reverse() leastday = leastmonth = 0 firstmonth = "" firstday = 0 dailyConns = {} dailyUsage = {} for filename in logfilelist: logfile = open(filename, "r") line = logfile.readline() while line != "": (month, date, time, host, prog, info) = string.split(line, None, 5) if firstday == 0: # get the earliest date in the logs firstday = date firstmonth = month if re.search("^pppd", prog) and re.search("^Connect time", info): if leastday == 0: leastday = date leastmonth = month thisdate = "%2d %2d" % (monthno[month], int(date)) if not dailyConns.has_key(thisdate): dailyConns[thisdate] = 1 dailyUsage[thisdate] = float(info.split()[2]) else: dailyConns[thisdate] = dailyConns[thisdate] + 1 dailyUsage[thisdate] = dailyUsage[thisdate] + float(info.split()[2]) line = logfile.readline() logfile.close() datelist = dailyConns.keys() datelist.sort() # This will sort using month and date tottime = 0 connections = 0 for i in datelist: connections = connections + dailyConns[i] tottime = dailyUsage[i] + tottime if connections == 0: message = "You have not connected to the internet since " + firstmonth + " " + str(firstday) else: ending = "" if connections > 1: ending = "s." # message = "Between " + monthno.items()[int(datelist[0][0:2]) - 1][0] + " " + datelist[0][3:] + \ message = "Between " + firstmonth + " " + str(firstday) + \ " and now, you have connected to the internet " \ + str(connections) + " time" + ending message = message + "\nDuring this time, you have used the internet for a total of " + str(tottime) + " minutes." message = message + "\r\n\r\n" for i in datelist: message = message + " " + monthno.items()[int(i[0:2]) - 1][0] + ' ' + i[3:] + ": " + str(dailyConns[i]) + \ " connections, " + str(dailyUsage[i]) + " minutes\r\n" smtpserv = smtplib.SMTP(SMTPSERV) head = "From: Internet usage watcher <" + FROMEMAIL + ">\r\n" head = head + "Subject: Internet usage information\r\n" message = head + message smtpserv.sendmail(FROMEMAIL, EMAIL, message) smtpserv.quit()