Advertisement
Masoko

openvpn stats

Sep 21st, 2016
162
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.87 KB | None | 0 0
  1. #!/usr/bin/python
  2. # -*- coding: utf-8 -*-
  3.  
  4. import web
  5. import os
  6. import subprocess
  7.  
  8. web.config.debug = False
  9. urls = ('/', 'cputemp','/vpn', 'vpn')
  10. app = web.application(urls, globals(),autoreload=True)
  11.  
  12. def getvpnstats():
  13.     STATUS = "/var/log/openvpn-status.log"
  14.  
  15.     status_file = open(STATUS, 'r')
  16.     stats = status_file.readlines()
  17.     status_file.close()
  18.  
  19.     hosts = []
  20.  
  21.     headers = {
  22.         'cn':    'Common Name',
  23.         'virt':  'Virtual Address',
  24.         'real':  'Real Address',
  25.         'sent':  'Sent',
  26.         'recv':  'Received',
  27.         'since': 'Connected Since'
  28.     }
  29.  
  30.     sizes = [
  31.         (1<<50L, 'PB'),
  32.         (1<<40L, 'TB'),
  33.         (1<<30L, 'GB'),
  34.         (1<<20L, 'MB'),
  35.         (1<<10L, 'KB'),
  36.         (1,       'B')
  37.     ]
  38.  
  39.     def byte2str(size):
  40.         for f, suf in sizes:
  41.             if size >= f:
  42.                 break
  43.  
  44.         return "%.2f %s" % (size / float(f), suf)
  45.  
  46.     for line in stats:
  47.         cols = line.split(',')
  48.  
  49.         if len(cols) == 5 and not line.startswith('Common Name'):
  50.             host  = {}
  51.             host['cn']    = cols[0]
  52.             host['real']  = cols[1].split(':')[0]
  53.             host['recv']  = byte2str(int(cols[2]))
  54.             host['sent']  = byte2str(int(cols[3]))
  55.             host['since'] = cols[4].strip()
  56.             hosts.append(host)
  57.  
  58.         if len(cols) == 4 and not line.startswith('Virtual Address'):
  59.             for h in hosts:
  60.                 if h['cn'] == cols[1]:
  61.                     h['virt'] = cols[0]
  62.  
  63.  
  64.     fmt = "%(cn)-25s %(virt)-18s %(real)-15s %(sent)13s %(recv)13s %(since)25s"
  65. #   print fmt % headers
  66.     return fmt % headers +"\n\n"+ "\n".join([fmt % h for h in hosts])
  67.  
  68. # Return CPU temperature as a character string
  69. def getCPUtemperature():
  70.         s = subprocess.check_output(["/opt/vc/bin/vcgencmd","measure_temp"])
  71.         return s.split('=')[1][:-3]
  72.  
  73.  
  74. class cputemp:
  75.     def GET(self):
  76.         temp1=getCPUtemperature()
  77.                 return "CPU temp "+str(temp1)
  78. class vpn:
  79.     def GET(self):
  80.         stats=getvpnstats()
  81.                 return stats
  82.                
  83. if __name__ == '__main__':
  84.     app.run()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement