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
- #-----------------------------VAR--------------------------
- unique_ip_in = []
- unique_ip_out = []
- ip_list_in = []
- ip_list_out = []
- sorted_ip_list_in = []
- sorted_trafic_sum_in = []
- sorted_ip_list_out = []
- sorted_trafic_sum_out = []
- ip_count = 0
- month_name = {'1' : 'January',
- '2' : 'February',
- '3' : 'March',
- '4' : 'April',
- '5' : 'May',
- '6' : 'June',
- '7' : 'July',
- '8' : 'August',
- '9' : 'September',
- '10' : 'October',
- '11' : 'November',
- '12' : 'December'}
- if datetime.datetime.now().day == 1:
- #-----------------------------format date for query-----------------------------------------------
- month_number = str(datetime.datetime.now().month - 1)
- year_number = str(datetime.datetime.now().year)
- if datetime.datetime.now().month == 1:
- month_number = str(12)
- year_number = str(datetime.datetime.now().year - 1)
- last_month = "%-0" + month_number + "-" + year_number
- #------------------connect to db---------------------------------------------------------------------
- tempura_connect = MySQLdb.connect(host='127.0.0.1', port=3306, user='tempdbuser', passwd='tempdbuser', db='tempuradb')
- tempura_query = tempura_connect.cursor()
- #-----------------create temporary table for soreted ip list-------------------------------------------
- tempura_query.execute("CREATE TEMPORARY TABLE ip_volume_tmp (ip VARCHAR(20), traffic_volume BIGINT, type VARCHAR(3))")
- #-------------------fetch total trafic count. IN and OUT------------------------------------------------
- rowcount = tempura_query.execute("SELECT SUM(traffic_volume) FROM net_local_traffic_in_volume WHERE measuring_date LIKE '" + last_month +"'")
- total_trafic_in = tempura_query.fetchone()
- rowcount = tempura_query.execute("SELECT SUM(traffic_volume) FROM net_local_traffic_out_volume WHERE measuring_date LIKE '" + last_month +"'")
- total_trafic_out = tempura_query.fetchone()
- #-----------------fetch ip list. IN and OUT------------------------------------------------------------------
- rowcount = tempura_query.execute("SELECT ip FROM net_local_traffic_in_volume WHERE measuring_date LIKE '" + last_month +"' AND traffic_volume > 1000000")
- ip_list_in = tempura_query.fetchall()
- rowcount = tempura_query.execute("SELECT ip FROM net_local_traffic_out_volume WHERE measuring_date LIKE '" + last_month +"' AND traffic_volume > 1000000")
- ip_list_out = tempura_query.fetchall()
- #--------------------create unique ip list. IN and OUT------------------------------------
- for current_ip in ip_list_in:
- ip_count = unique_ip_in.count(current_ip[0])
- if ip_count == 0:
- unique_ip_in.append(current_ip[0])
- for current_ip in ip_list_out:
- ip_count = unique_ip_out.count(current_ip[0])
- if ip_count == 0:
- unique_ip_out.append(current_ip[0])
- #--------------------count total trafic sum for unique ip. IN. and insert in temp db---------------------------------------------------
- for current_ip in unique_ip_in:
- rowcount = tempura_query.execute("SELECT SUM(traffic_volume) FROM net_local_traffic_in_volume WHERE measuring_date LIKE '" + last_month +"' AND ip = '" + current_ip +"'" )
- trafic_sum = tempura_query.fetchone()
- tempura_query.execute("INSERT INTO ip_volume_tmp (ip, traffic_volume, type) VALUE (%s, %s, %s)", (current_ip, trafic_sum[0], 'in'))
- tempura_connect.commit()
- #----------------------------fetch sorted total trafic sum from temp db. IN--------------------------------------
- rowcount = tempura_query.execute("SELECT ip, traffic_volume FROM ip_volume_tmp WHERE type = 'in' ORDER BY traffic_volume DESC")
- qresult = tempura_query.fetchall()
- sorted_ip_list_in, sorted_trafic_sum_in = zip(*qresult)
- #--------------------count total trafic sum for unique ip. OUT. and insert in temp db---------------------------------------------------
- for current_ip in unique_ip_out:
- rowcount = tempura_query.execute("SELECT SUM(traffic_volume) FROM net_local_traffic_out_volume WHERE measuring_date LIKE '" + last_month +"' AND ip = '" + current_ip +"'" )
- trafic_sum = tempura_query.fetchone()
- tempura_query.execute("INSERT INTO ip_volume_tmp (ip, traffic_volume, type) VALUE (%s, %s, %s)", (current_ip, trafic_sum[0], 'out'))
- tempura_connect.commit()
- #----------------------------fetch sorted total trafic sum from temp db. OUT--------------------------------------
- rowcount = tempura_query.execute("SELECT ip, traffic_volume FROM ip_volume_tmp WHERE type = 'out' ORDER BY traffic_volume DESC")
- qresult = tempura_query.fetchall()
- sorted_ip_list_out, sorted_trafic_sum_out = zip(*qresult)
- #----------------------------some calc-------------------------
- total_bytes_in_gb = round((float(total_trafic_in[0]) / 1024)/1024/1024, 3)
- total_bytes_out_gb = round((float(total_trafic_out[0]) / 1024)/1024/1024, 3)
- #---------------------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('<h3 style="background-color : #FFCC66">' + month_name[month_number] + ' ' + year_number + ' ')
- f.write('[ <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(sorted_ip_list_in)):
- f.write(str(sorted_ip_list_in[i]) + ' : ' + str(round((float(sorted_trafic_sum_in[i]) / 1024)/1024, 3)) + ' 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(sorted_ip_list_out)):
- f.write(str(sorted_ip_list_out[i]) + ' : ' + str(round((float(sorted_trafic_sum_out[i]) / 1024)/1024, 3)) + ' Mb' + '<br>')
- f.write('</font></td></tr></table>')
- #----------------------------------------END---------------------------------------
- tempura_query.close()
- tempura_connect.close()
Advertisement
Add Comment
Please, Sign In to add comment