Advertisement
Masoko

openvpn stats 2

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