autumnrd

Chanel load by pf and python

Jul 10th, 2014
271
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.04 KB | None | 0 0
  1. # http://scratch-tales.blogspot.ru/
  2. #!/usr/local/bin/python2.7
  3.  
  4. import subprocess
  5. import MySQLdb
  6. import datetime
  7.  
  8. #connect to db and fetch result
  9. tempura_connect = MySQLdb.connect(host='127.0.0.1', port=3310, user='tempdbuser', passwd='tempdbuser', db='tempuradb')
  10. tempura_query = tempura_connect.cursor()
  11.  
  12. #qresult = tempura_query.fetchall()
  13.  
  14. pf_out = []
  15.  
  16. p = subprocess.Popen('pfctl -sI -vv -i xl0', shell=True, stdout=subprocess.PIPE, stderr=subprocess.STDOUT)
  17.  
  18. for line in p.stdout.readlines():
  19.     pf_out.append(line)
  20.  
  21. iBytes = pf_out[3].find('Bytes')
  22. BytesIn = pf_out[3].replace(']',' ')
  23. BytesIn = BytesIn[iBytes+6:]
  24. BytesIn = BytesIn.strip()
  25.  
  26. iBytes = pf_out[5].find('Bytes')
  27. BytesOut = pf_out[5].replace(']',' ')
  28. BytesOut = BytesOut[iBytes+6:]
  29. BytesOut = BytesOut.strip()
  30.  
  31. tempura_query.execute("""INSERT INTO net_storage (measure_time, bytes_in, bytes_out) VALUES (%s, %s, %s)""",(datetime.datetime.now(), BytesIn, BytesOut))
  32. tempura_connect.commit()
  33.  
  34. retval = p.wait()
  35.  
  36. tempura_query.close()
  37. tempura_connect.close()
Advertisement
Add Comment
Please, Sign In to add comment