autumnrd

Parse pf.log and count traffic. Gray IP's

Jul 10th, 2014
280
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 4.30 KB | None | 0 0
  1. # http://scratch-tales.blogspot.ru/
  2. #!/usr/local/bin/python2.7
  3.  
  4. import datetime
  5. import time
  6. import MySQLdb
  7. import subprocess
  8. import os
  9. import shutil
  10.  
  11. def trafic_count(log_name, trafic_type):
  12. #--------------F_VAR-----------------------
  13.     trafic_sum = 0
  14.     i = 0
  15.     bytes_count = []
  16.     ip_split = []
  17.     ip_list = []
  18.     ip_cnt = []
  19.     bytes_cut = ''
  20.     ip_cut = ''
  21.     ip_tmp = ''
  22.  
  23. #----------set separator char and insert type----------------------
  24.     if trafic_type == 'in':
  25.         char_separator = ':'
  26.         insert_type = """INSERT INTO net_local_traffic_in_volume (ip, traffic_volume, measuring_date) VALUES (%s, %s, %s)"""
  27.     elif trafic_type == 'out':
  28.         char_separator = ' '
  29.         insert_type = """INSERT INTO net_local_traffic_out_volume (ip, traffic_volume, measuring_date) VALUES (%s, %s, %s)"""
  30.  
  31.     f = open(log_name, 'r')
  32.     trafic_read = f.xreadlines()
  33.  
  34. #-------------------parsing log--------------------------------------
  35.  
  36.     for get_string in trafic_read:
  37.         len_pos = get_string.rfind(' len ')
  38.         ip_pos = get_string.find('192.168.1.')
  39.  
  40.         if len_pos != -1:
  41.             bytes_cut = get_string[len_pos + 4:]
  42.             coma_pos = bytes_cut.find(',')
  43.             if coma_pos != -1:
  44.                 trafic_sum = trafic_sum + int(bytes_cut[0:coma_pos])
  45.             elif coma_pos == -1:
  46.                 coma_pos = bytes_cut.find(')')
  47.                 if coma_pos != -1:
  48.                     trafic_sum = trafic_sum + int(bytes_cut[0:coma_pos])
  49.  
  50.         if ip_pos != -1:
  51.             ip_tmp = get_string[ip_pos:]
  52.             dot_pos = ip_tmp.find(char_separator)
  53.             ip_cut = ip_tmp[0:dot_pos]
  54.             ip_split = ip_cut.split('.')
  55.             ip_clear = ".".join(ip_split[0:4])
  56.             ip_count = ip_list.count(ip_clear)
  57.  
  58.             if ip_count == 0:
  59.                 ip_cnt.append(i)
  60.                 i = i + 1
  61.                 ip_list.append(ip_clear)
  62.                 bytes_count.append(bytes_cut[0:coma_pos])
  63.  
  64.             if ip_count != 0:
  65.                 ip_pos = ip_list.index(ip_clear)
  66.                 bytes_tmp = float(bytes_count[ip_pos])
  67.                 bytes_count[ip_pos] = bytes_tmp + float(bytes_cut[0:coma_pos])
  68.  
  69. #------------------connect and insert in db-----------------------
  70.  
  71.     tempura_connect = MySQLdb.connect(host='127.0.0.1', port=3310, user='tempdbuser', passwd='tempdbuser', db='tempuradb')
  72.     tempura_query = tempura_connect.cursor()    
  73.  
  74.     for i in range(len(ip_list)):
  75.         tempura_query.execute(insert_type ,(ip_list[i], bytes_count[i], (datetime.datetime.now() - datetime.timedelta(days=1)).strftime("%d-%m-%Y")))
  76.         tempura_connect.commit()
  77.  
  78.     f.close()
  79.     tempura_query.close()
  80.     tempura_connect.close()
  81. #--------------------------------end of traffic count func---------------------------
  82.  
  83.  
  84. #----------------------------------------BODY----------------------------------------
  85. #-------------------------------gunzip and tcpdumping yesterday pf.log---------------
  86.  
  87. if os.path.exists('/var/log/pflog.0.gz') == True:
  88.     rm_result = subprocess.Popen('rm /usr/local/scripts/tmp/*', shell=True, stdout=subprocess.PIPE, stderr=subprocess.STDOUT)
  89.     ret = rm_result.wait()
  90.  
  91.     ret = shutil.copy('/var/log/pflog.0.gz', '/usr/local/scripts/tmp/pflog.0.gz')
  92.  
  93.     gunzip_log = subprocess.Popen('gunzip /usr/local/scripts/tmp/pflog.0.gz -o /usr/local/scripts/tmp/pflog', shell=True, stdout=subprocess.PIPE, stderr=subprocess.STDOUT)
  94.     ret = gunzip_log.wait()
  95.  
  96.     tcpdump_log_in = subprocess.Popen("tcpdump -n -t -v -r /usr/local/scripts/tmp/pflog 'dst net 192.168.1.0 mask 255.255.255.0 and not (src net 192.168.1.0 mask 255.255.255.0)' > /usr/local/scripts/tmp/trafic_in.log" , shell=True, stdout=subprocess.PIPE,
  97. stderr=subprocess.STDOUT)
  98.     ret = tcpdump_log_in.wait()
  99.  
  100.     tcpdump_log_out = subprocess.Popen("tcpdump -n -t -v -r /usr/local/scripts/tmp/pflog 'src net 192.168.1.0 mask 255.255.255.0 and not (dst net 192.168.1.0 mask 255.255.255.0)' > /usr/local/scripts/tmp/trafic_out.log" ,shell=True, stdout=subprocess.PIPE,
  101. stderr=subprocess.STDOUT)
  102.     ret = tcpdump_log_out.wait()
  103.  
  104. #-----------------------parsing tcpdumping and count traff-----------------------------
  105. trafic_count('/usr/local/scripts/tmp/trafic_out.log', 'out')
  106. trafic_count('/usr/local/scripts/tmp/trafic_in.log', 'in')
Advertisement
Add Comment
Please, Sign In to add comment