autumnrd

Count traf from day and put in html

Jul 11th, 2014
184
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 3.95 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. import time
  9.  
  10. #-----------------------------VAR--------------------------
  11. yesterday_date = (datetime.datetime.now() - datetime.timedelta(days=1)).strftime("%d-%m-%Y")
  12. day_name = (datetime.datetime.now() - datetime.timedelta(days=1)).strftime("%A")
  13. total_bytes_in = 0
  14. total_bytes_out = 0
  15. total_bytes_in_gb = 0
  16. total_bytes_out_gb = 0
  17.  
  18.  
  19. header_color = {'Monday':'<h3 style="background-color:#CCFFCC">',
  20.     'Tuesday' : '<h3 style="background-color:#FFFFFF">',
  21.     'Wednesday' : '<h3 style="background-color:#FFFFFF">',
  22.     'Thursday' : '<h3 style="background-color:#FFFFFF">',
  23.     'Friday' : '<h3 style="background-color:#FFFFFF">',
  24.     'Saturday' : '<h3 style="background-color:#FFFFFF">',
  25.     'Sunday' : '<h3 style="background-color:#FFFFFF">'
  26. }
  27.  
  28. header_tab = {
  29.     'Monday'    : '&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;',
  30.     'Tuesday'   : '&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;',
  31.     'Wednesday' : '&nbsp;&nbsp;&nbsp;',
  32.     'Thursday'  : '&nbsp;&nbsp;&nbsp;&nbsp;',
  33.     'Friday'    : '&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;',
  34.     'Saturday'  : '&nbsp;&nbsp;&nbsp;&nbsp;',
  35.     'Sunday'    : '&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;'
  36. }
  37.  
  38. #------------------fetch trafic from db-----------------------
  39. tempura_connect = MySQLdb.connect(host='127.0.0.1', port=3306, user='tempdbuser', passwd='tempdbuser', db='tempuradb')
  40. tempura_query = tempura_connect.cursor()
  41.  
  42. 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")
  43.  
  44. qresult = tempura_query.fetchall()
  45. ip_list_in, bytes_volume_in = zip(*qresult)
  46.  
  47. 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")
  48. qresult = tempura_query.fetchall()
  49. ip_list_out, bytes_volume_out = zip(*qresult)
  50.  
  51. tempura_query.close()
  52. tempura_connect.close()
  53. #---------------------------calc total trafic count-------------------
  54. for i in bytes_volume_in:
  55.     total_bytes_in = total_bytes_in + i
  56.  
  57. for i in bytes_volume_out:
  58.     total_bytes_out = total_bytes_out + i
  59.  
  60. #----------------------------some calc-------------------------
  61. total_bytes_in_gb = round((float(total_bytes_in) / 1024)/1024/1024, 2)
  62. total_bytes_out_gb = round((float(total_bytes_out) / 1024)/1024/1024, 2)
  63.  
  64. #---------------------form html-----------------------------
  65. f = open('/var/www/htdocs/tempura/net_stat_local.html', 'r')
  66. html_read = f.readlines()
  67. f.close()
  68.  
  69. f = open('/var/www/htdocs/tempura/net_stat_local.html', 'w')
  70.  
  71. for get_line in html_read:
  72.     begin_pos = get_line.find('<div class="accordion">')
  73.     f.write(get_line)
  74.     if begin_pos != -1:
  75.         f.write(header_color[day_name] + (datetime.datetime.now() - datetime.timedelta(days=1)).strftime("%d %B %Y, %A"))  
  76.         f.write(header_tab[day_name] + '[ <font color="#CC3300" style="font-weight: 600"> in : ' + str(total_bytes_in_gb) + ' Gb</font> | ')
  77.         f.write('<font color="#336633" style="font-weight: 600">out : ' + str(total_bytes_out_gb) + '  Gb</font> ]' + '</h3>')
  78.         f.write('<table><tr><td valign="top">')
  79.         f.write('<font size="2" face="Verdana" color="#CC3300">')
  80.         for i in range(len(ip_list_in)):
  81.             f.write(str(ip_list_in[i]) + ' : ' + str(round((float(bytes_volume_in[i]) / 1024)/1024, 2)) + ' Mb' + '<br>')
  82.         f.write('</font></td>')
  83.         f.write('<td><pre>         </pre></td>')
  84.         f.write('<td valign="top">')
  85.         f.write('<font size="2" face="Verdana" color="#336633">')
  86.         for i in range(len(ip_list_out)):
  87.             f.write(str(ip_list_out[i]) + ' : ' + str(round((float(bytes_volume_out[i]) / 1024)/1024, 2)) + ' Mb' + '<br>')
  88.         f.write('</font></td></tr></table>')
  89.  
  90. #---------------finish proc-------------
  91. f.close()
Advertisement
Add Comment
Please, Sign In to add comment