Advertisement
Guest User

Untitled

a guest
Jul 24th, 2014
204
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.32 KB | None | 0 0
  1. #!/usr/bin/env python
  2. # -*- coding: utf-8 -*-
  3.  
  4. STATUS = "/etc/openvpn/status.log"
  5.  
  6. status_file = open(STATUS, 'r')
  7. stats = status_file.readlines()
  8. status_file.close()
  9.  
  10. hosts = []
  11.  
  12. headers = {
  13. 'cn': 'Common Name',
  14. 'virt': 'Virtual Address',
  15. 'real': 'Real Address',
  16. 'sent': 'Sent',
  17. 'recv': 'Received',
  18. 'since': 'Connected Since'
  19. }
  20.  
  21. sizes = [
  22. (1<<50L, 'PB'),
  23. (1<<40L, 'TB'),
  24. (1<<30L, 'GB'),
  25. (1<<20L, 'MB'),
  26. (1<<10L, 'KB'),
  27. (1, 'B')
  28. ]
  29.  
  30. def byte2str(size):
  31. for f, suf in sizes:
  32. if size >= f:
  33. break
  34.  
  35. return "%.2f %s" % (size / float(f), suf)
  36.  
  37.  
  38. for line in stats:
  39. cols = line.split(',')
  40.  
  41. if len(cols) == 5 and not line.startswith('Common Name'):
  42. host = {}
  43. host['cn'] = cols[0]
  44. host['real'] = cols[1].split(':')[0]
  45. host['recv'] = byte2str(int(cols[2]))
  46. host['sent'] = byte2str(int(cols[3]))
  47. host['since'] = cols[4].strip()
  48. hosts.append(host)
  49.  
  50. if len(cols) == 4 and not line.startswith('Virtual Address'):
  51. for h in hosts:
  52. if h['cn'] == cols[1]:
  53. h['virt'] = cols[0]
  54.  
  55.  
  56. fmt = "%(cn)-25s %(virt)-18s %(real)-15s %(sent)13s %(recv)13s %(since)25s"
  57. print fmt % headers
  58. print "\n".join([fmt % h for h in hosts])
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement