autumnrd

Count traf from month and put in html

Jul 11th, 2014
205
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 6.69 KB | None | 0 0
  1. # http://scratch-tales.blogspot.ru/
  2. #!/usr/local/bin/python2.7
  3.  
  4. import sys
  5. import os
  6. import MySQLdb
  7. import datetime
  8.  
  9. #-----------------------------VAR--------------------------
  10. unique_ip_in = []
  11. unique_ip_out = []
  12. ip_list_in = []
  13. ip_list_out = []
  14. sorted_ip_list_in = []
  15. sorted_trafic_sum_in = []
  16. sorted_ip_list_out = []
  17. sorted_trafic_sum_out = []
  18. ip_count = 0
  19.  
  20. month_name = {'1' : 'January',
  21.     '2' : 'February',
  22.     '3' : 'March',
  23.     '4' : 'April',
  24.     '5' : 'May',
  25.     '6' : 'June',
  26.     '7' : 'July',
  27.     '8' : 'August',
  28.     '9' : 'September',
  29.     '10' : 'October',
  30.     '11' : 'November',
  31.     '12' : 'December'}
  32.  
  33. if datetime.datetime.now().day == 1:
  34. #-----------------------------format date for query-----------------------------------------------
  35.     month_number = str(datetime.datetime.now().month - 1)
  36.     year_number = str(datetime.datetime.now().year)
  37.  
  38.     if datetime.datetime.now().month == 1:
  39.         month_number = str(12)
  40.         year_number = str(datetime.datetime.now().year - 1)
  41.     last_month = "%-0" + month_number + "-" + year_number
  42.  
  43. #------------------connect to db---------------------------------------------------------------------
  44.     tempura_connect = MySQLdb.connect(host='127.0.0.1', port=3306, user='tempdbuser', passwd='tempdbuser', db='tempuradb')
  45.     tempura_query = tempura_connect.cursor()
  46.  
  47. #-----------------create temporary table for soreted ip list-------------------------------------------
  48.     tempura_query.execute("CREATE TEMPORARY TABLE ip_volume_tmp (ip VARCHAR(20), traffic_volume BIGINT, type VARCHAR(3))")
  49.  
  50. #-------------------fetch total trafic count. IN and OUT------------------------------------------------
  51.     rowcount = tempura_query.execute("SELECT SUM(traffic_volume) FROM net_local_traffic_in_volume WHERE measuring_date LIKE '" + last_month +"'")
  52.     total_trafic_in = tempura_query.fetchone()
  53.  
  54.     rowcount = tempura_query.execute("SELECT SUM(traffic_volume) FROM net_local_traffic_out_volume WHERE measuring_date LIKE '" + last_month +"'")
  55.     total_trafic_out = tempura_query.fetchone()
  56.  
  57. #-----------------fetch ip list. IN and OUT------------------------------------------------------------------
  58.     rowcount = tempura_query.execute("SELECT ip FROM net_local_traffic_in_volume WHERE measuring_date LIKE '" + last_month +"' AND traffic_volume > 1000000")
  59.     ip_list_in = tempura_query.fetchall()
  60.  
  61.     rowcount = tempura_query.execute("SELECT ip FROM net_local_traffic_out_volume WHERE measuring_date LIKE '" + last_month +"' AND traffic_volume > 1000000")
  62.     ip_list_out = tempura_query.fetchall()
  63.  
  64. #--------------------create unique ip list. IN and OUT------------------------------------
  65.     for current_ip in ip_list_in:
  66.         ip_count = unique_ip_in.count(current_ip[0])
  67.         if ip_count == 0:
  68.             unique_ip_in.append(current_ip[0])
  69.  
  70.     for current_ip in ip_list_out:
  71.         ip_count = unique_ip_out.count(current_ip[0])
  72.         if ip_count == 0:
  73.             unique_ip_out.append(current_ip[0])
  74.  
  75. #--------------------count total trafic sum for unique ip. IN. and insert in temp db---------------------------------------------------
  76.     for current_ip in unique_ip_in:
  77.         rowcount = tempura_query.execute("SELECT SUM(traffic_volume) FROM net_local_traffic_in_volume WHERE measuring_date LIKE '" + last_month +"' AND ip = '" + current_ip +"'" )
  78.         trafic_sum = tempura_query.fetchone()
  79.         tempura_query.execute("INSERT INTO ip_volume_tmp (ip, traffic_volume, type) VALUE (%s, %s, %s)", (current_ip, trafic_sum[0], 'in'))
  80.         tempura_connect.commit()
  81.  
  82. #----------------------------fetch sorted total trafic sum from temp db. IN--------------------------------------
  83.     rowcount = tempura_query.execute("SELECT ip, traffic_volume FROM ip_volume_tmp WHERE type = 'in' ORDER BY traffic_volume DESC")
  84.     qresult = tempura_query.fetchall()
  85.     sorted_ip_list_in, sorted_trafic_sum_in = zip(*qresult)
  86.  
  87. #--------------------count total trafic sum for unique ip. OUT. and insert in temp db---------------------------------------------------
  88.     for current_ip in unique_ip_out:
  89.         rowcount = tempura_query.execute("SELECT SUM(traffic_volume) FROM net_local_traffic_out_volume WHERE measuring_date LIKE '" + last_month +"' AND ip = '" + current_ip +"'" )
  90.         trafic_sum = tempura_query.fetchone()
  91.         tempura_query.execute("INSERT INTO ip_volume_tmp (ip, traffic_volume, type) VALUE (%s, %s, %s)", (current_ip, trafic_sum[0], 'out'))
  92.         tempura_connect.commit()
  93.  
  94. #----------------------------fetch sorted total trafic sum from temp db. OUT--------------------------------------
  95.     rowcount = tempura_query.execute("SELECT ip, traffic_volume FROM ip_volume_tmp WHERE type = 'out' ORDER BY traffic_volume DESC")
  96.     qresult = tempura_query.fetchall()
  97.     sorted_ip_list_out, sorted_trafic_sum_out = zip(*qresult)
  98.  
  99. #----------------------------some calc-------------------------
  100.     total_bytes_in_gb = round((float(total_trafic_in[0]) / 1024)/1024/1024, 3)
  101.     total_bytes_out_gb = round((float(total_trafic_out[0]) / 1024)/1024/1024, 3)
  102.  
  103. #---------------------form html-----------------------------
  104.     f = open('/var/www/htdocs/tempura/net_stat_local.html', 'r')
  105.     html_read = f.readlines()
  106.     f.close()
  107.  
  108.     f = open('/var/www/htdocs/tempura/net_stat_local.html', 'w')
  109.  
  110.     for get_line in html_read:
  111.         begin_pos = get_line.find('<div class="accordion">')
  112.         f.write(get_line)
  113.         if begin_pos != -1:
  114.             f.write('<h3 style="background-color : #FFCC66">' + month_name[month_number] + ' ' + year_number + '&nbsp;&nbsp;&nbsp;')
  115.             f.write('[ <font color="#CC3300" style="font-weight: 600"> in : ' + str(total_bytes_in_gb) + ' Gb</font> | ')
  116.             f.write('<font color="#336633" style="font-weight: 600">out : ' + str(total_bytes_out_gb) + '  Gb</font> ]' + '</h3>')
  117.             f.write('<table><tr><td valign="top">')
  118.             f.write('<font size="2" face="Verdana" color="#CC3300">')
  119.             for i in range(len(sorted_ip_list_in)):
  120.                 f.write(str(sorted_ip_list_in[i]) + ' : ' + str(round((float(sorted_trafic_sum_in[i]) / 1024)/1024, 3)) + ' Mb' + '<br>')
  121.             f.write('</font></td>')
  122.             f.write('<td><pre>         </pre></td>')
  123.             f.write('<td valign="top">')
  124.             f.write('<font size="2" face="Verdana" color="#336633">')
  125.             for i in range(len(sorted_ip_list_out)):
  126.                 f.write(str(sorted_ip_list_out[i]) + ' : ' + str(round((float(sorted_trafic_sum_out[i]) / 1024)/1024, 3)) + ' Mb' + '<br>')
  127.             f.write('</font></td></tr></table>')
  128.  
  129. #----------------------------------------END---------------------------------------
  130.     tempura_query.close()
  131.     tempura_connect.close()
Advertisement
Add Comment
Please, Sign In to add comment