Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # http://scratch-tales.blogspot.ru/
- #!/usr/local/bin/python2.7
- import sys
- import os
- import MySQLdb
- import datetime
- import time
- #-----------------------------VAR--------------------------
- yesterday_date = (datetime.datetime.now() - datetime.timedelta(days=1)).strftime("%d-%m-%Y")
- day_name = (datetime.datetime.now() - datetime.timedelta(days=1)).strftime("%A")
- total_bytes_in = 0
- total_bytes_out = 0
- total_bytes_in_gb = 0
- total_bytes_out_gb = 0
- header_color = {'Monday':'<h3 style="background-color:#CCFFCC">',
- 'Tuesday' : '<h3 style="background-color:#FFFFFF">',
- 'Wednesday' : '<h3 style="background-color:#FFFFFF">',
- 'Thursday' : '<h3 style="background-color:#FFFFFF">',
- 'Friday' : '<h3 style="background-color:#FFFFFF">',
- 'Saturday' : '<h3 style="background-color:#FFFFFF">',
- 'Sunday' : '<h3 style="background-color:#FFFFFF">'
- }
- header_tab = {
- 'Monday' : ' ',
- 'Tuesday' : ' ',
- 'Wednesday' : ' ',
- 'Thursday' : ' ',
- 'Friday' : ' ',
- 'Saturday' : ' ',
- 'Sunday' : ' '
- }
- #------------------fetch trafic from db-----------------------
- tempura_connect = MySQLdb.connect(host='127.0.0.1', port=3306, user='tempdbuser', passwd='tempdbuser', db='tempuradb')
- tempura_query = tempura_connect.cursor()
- rowcount = tempura_query.execute("SELECT ip, traffic_volume FROM net_local_traffic_in_volume WHERE measuring_date = '" + yesterday_date +"' AND traffic_volume > 1000000 ORDER BY traffic_volume desc")
- qresult = tempura_query.fetchall()
- ip_list_in, bytes_volume_in = zip(*qresult)
- rowcount = tempura_query.execute("SELECT ip, traffic_volume FROM net_local_traffic_out_volume WHERE measuring_date = '" + yesterday_date +"' AND traffic_volume > 1000000 ORDER BY traffic_volume desc")
- qresult = tempura_query.fetchall()
- ip_list_out, bytes_volume_out = zip(*qresult)
- tempura_query.close()
- tempura_connect.close()
- #---------------------------calc total trafic count-------------------
- for i in bytes_volume_in:
- total_bytes_in = total_bytes_in + i
- for i in bytes_volume_out:
- total_bytes_out = total_bytes_out + i
- #----------------------------some calc-------------------------
- total_bytes_in_gb = round((float(total_bytes_in) / 1024)/1024/1024, 2)
- total_bytes_out_gb = round((float(total_bytes_out) / 1024)/1024/1024, 2)
- #---------------------form html-----------------------------
- f = open('/var/www/htdocs/tempura/net_stat_local.html', 'r')
- html_read = f.readlines()
- f.close()
- f = open('/var/www/htdocs/tempura/net_stat_local.html', 'w')
- for get_line in html_read:
- begin_pos = get_line.find('<div class="accordion">')
- f.write(get_line)
- if begin_pos != -1:
- f.write(header_color[day_name] + (datetime.datetime.now() - datetime.timedelta(days=1)).strftime("%d %B %Y, %A"))
- f.write(header_tab[day_name] + '[ <font color="#CC3300" style="font-weight: 600"> in : ' + str(total_bytes_in_gb) + ' Gb</font> | ')
- f.write('<font color="#336633" style="font-weight: 600">out : ' + str(total_bytes_out_gb) + ' Gb</font> ]' + '</h3>')
- f.write('<table><tr><td valign="top">')
- f.write('<font size="2" face="Verdana" color="#CC3300">')
- for i in range(len(ip_list_in)):
- f.write(str(ip_list_in[i]) + ' : ' + str(round((float(bytes_volume_in[i]) / 1024)/1024, 2)) + ' Mb' + '<br>')
- f.write('</font></td>')
- f.write('<td><pre> </pre></td>')
- f.write('<td valign="top">')
- f.write('<font size="2" face="Verdana" color="#336633">')
- for i in range(len(ip_list_out)):
- f.write(str(ip_list_out[i]) + ' : ' + str(round((float(bytes_volume_out[i]) / 1024)/1024, 2)) + ' Mb' + '<br>')
- f.write('</font></td></tr></table>')
- #---------------finish proc-------------
- f.close()
Advertisement
Add Comment
Please, Sign In to add comment