Advertisement
Masoko

OpenVPN Stats DB Update

Oct 2nd, 2016
152
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.41 KB | None | 0 0
  1. #!/usr/bin/env python
  2. # -*- coding: utf-8 -*-
  3. import os, sys, pickle
  4.  
  5. STATUS = "/var/log/openvpn-status.log" #location of open vpn status file
  6. #STATUS = "openvpn-status.log"
  7. db_folder = "db" #folder for storing data
  8.  
  9. sizes = [  
  10.     (1<<50L, 'PB'),
  11.     (1<<40L, 'TB'),
  12.     (1<<30L, 'GB'),
  13.     (1<<20L, 'MB'),
  14.     (1<<10L, 'KB'),
  15.     (1,       'B')
  16. ]
  17.  
  18. def byte2str(size):  
  19.     for f, suf in sizes:
  20.         if size >= f:
  21.             break
  22.  
  23.     return "%.2f %s" % (size / float(f), suf)
  24.  
  25. def read_stats():
  26.     status_file = open(STATUS, 'r')  
  27.     stats = status_file.readlines()  
  28.     status_file.close()
  29.  
  30.     hosts = []
  31.  
  32.     headers = {  
  33.         'cn':    'Common Name',
  34.         'virt':  'Virtual Address',
  35.         'real':  'Real Address',
  36.         'sent':  'Sent',
  37.         'recv':  'Received',
  38.         'since': 'Connected Since'
  39.     }
  40.  
  41.     for line in stats:  
  42.         cols = line.split(',')
  43.  
  44.         if len(cols) == 5 and not line.startswith('Common Name'):
  45.             host  = {}
  46.             host['cn']    = cols[0]
  47.             host['real']  = cols[1].split(':')[0]
  48.             host['recv']  = int(cols[2])
  49.             host['sent']  = int(cols[3])
  50.             host['since'] = cols[4].strip()
  51.             hosts.append(host)
  52.  
  53.         if len(cols) == 4 and not line.startswith('Virtual Address'):
  54.             for h in hosts:
  55.                 if h['cn'] == cols[1]:
  56.                     h['virt'] = cols[0]
  57.  
  58.  
  59.     fmt = "%(cn)-25s %(virt)-18s %(real)-15s %(sent)13s %(recv)13s %(since)25s"  
  60.  
  61.     print fmt % headers  
  62.     print "\n".join([fmt % h for h in hosts])  
  63.     return hosts
  64.  
  65. def getScriptPath(): #gets script directory
  66.     return os.path.dirname(os.path.realpath(sys.argv[0]))
  67.    
  68. def update_log(cn,vhost):
  69.     dhosts = []
  70.     fn = os.path.join(getScriptPath(), db_folder, cn) + ".log"
  71.     if os.path.exists(fn)
  72.         old_host = pickle.load(open( fn, "rb" )) #read data from file
  73.         print old_host
  74.        
  75.         if old_host[1]['since'] == vhost['since']
  76.             dhosts.append(old_host[0])
  77.             print "same session"
  78.             dhosts.append(vhost)
  79.         else:
  80.             old_host[0]['recv'] += old_host[1]['recv']
  81.             old_host[0]['sent'] += old_host[1]['sent']
  82.             old_host[0]['since'] = old_host[1]['since']
  83.             old_host[0]['real'] = old_host[1]['real']
  84.             dhosts.append(old_host[0])
  85.             dhosts.append(vhost)
  86.                        
  87.             print "new session"
  88.             print old_host[1]['since']
  89.         pickle.dump(dhosts, open( fn, "wb" )) # save data to file
  90.     else:
  91.         dhosts.append(vhost)
  92.         dhosts.append(vhost)
  93.         pickle.dump(dhosts, open( fn, "wb" ))
  94.    
  95.     return
  96.    
  97. if __name__ == '__main__':
  98.     hosts = read_stats()
  99.     for h in hosts:
  100.         print h['cn']
  101.         update_log(h['cn'],h)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement