forked from furlongm/openvpn-monitor
-
Notifications
You must be signed in to change notification settings - Fork 0
/
openvpn-monitor.py
executable file
·729 lines (624 loc) · 28.1 KB
/
openvpn-monitor.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
#!/usr/bin/env python
# -*- coding: utf-8 -*-
# Licensed under GPL v3
# Copyright 2011 VPAC <http://www.vpac.org>
# Copyright 2012-2016 Marcus Furlong <[email protected]>
from __future__ import absolute_import
from __future__ import division
from __future__ import print_function
from __future__ import unicode_literals
try:
import ConfigParser as configparser
except ImportError:
import configparser
try:
from ipaddr import IPAddress as ip_address
from ipaddr import IPv6Address
except ImportError:
from ipaddress import ip_address, IPv6Address
import socket
import re
import argparse
import GeoIP
import sys
import os
from uuid import uuid4
from datetime import datetime
from humanize import naturalsize
from collections import OrderedDict
from pprint import pformat
if sys.version_info[0] == 2:
reload(sys)
sys.setdefaultencoding('utf-8')
def output(s):
global wsgi, wsgi_output
if not wsgi:
print(s)
else:
wsgi_output += s
def info(*objs):
print("INFO:", *objs, file=sys.stderr)
def warning(*objs):
print("WARNING:", *objs, file=sys.stderr)
def debug(*objs):
print("DEBUG:\n", *objs, file=sys.stderr)
def get_date(date_string, uts=False):
if not uts:
return datetime.strptime(date_string, "%a %b %d %H:%M:%S %Y")
else:
return datetime.fromtimestamp(float(date_string))
def get_str(s):
if sys.version_info[0] == 2 and s is not None:
return s.decode('ISO-8859-1')
else:
return s
class ConfigLoader(object):
def __init__(self, config_file):
self.settings = {}
self.vpns = OrderedDict()
config = configparser.RawConfigParser()
contents = config.read(config_file)
if not contents and config_file == './openvpn-monitor.conf':
warning('Config file does not exist or is unreadable: {0!s}'.format(config_file))
if sys.prefix == '/usr':
conf_path = '/etc/'
else:
conf_path = sys.prefix + '/etc/'
config_file = conf_path + 'openvpn-monitor.conf'
contents = config.read(config_file)
if contents:
info('Using config file: {0!s}'.format(config_file))
else:
warning('Config file does not exist or is unreadable: {0!s}'.format(config_file))
self.load_default_settings()
for section in config.sections():
if section == 'OpenVPN-Monitor':
self.parse_global_section(config)
else:
self.parse_vpn_section(config, section)
def load_default_settings(self):
info('Using default settings => localhost:5555')
self.settings = {'site': 'Default Site',
'geoip_data': '/usr/share/GeoIP/GeoIPCity.dat'}
self.vpns['Default VPN'] = {'name': 'default',
'host': 'localhost',
'port': '5555',
'order': '1'}
def parse_global_section(self, config):
global_vars = ['site', 'logo', 'latitude', 'longitude', 'maps', 'geoip_data']
for var in global_vars:
try:
self.settings[var] = config.get('OpenVPN-Monitor', var)
except configparser.NoOptionError:
pass
if args.debug:
debug("=== begin section\n{0!s}\n=== end section".format(self.settings))
def parse_vpn_section(self, config, section):
self.vpns[section] = {}
vpn = self.vpns[section]
options = config.options(section)
for option in options:
try:
vpn[option] = config.get(section, option)
if vpn[option] == -1:
warning('CONFIG: skipping {0!s}'.format(option))
except configparser.Error as e:
warning('CONFIG: {0!s} on option {1!s}: '.format(e, option))
vpn[option] = None
if args.debug:
debug("=== begin section\n{0!s}\n=== end section".format(vpn))
class OpenvpnMonitor(object):
def __init__(self, cfg):
self.vpns = cfg.vpns
self.geoip_data = cfg.settings['geoip_data']
for key, vpn in list(self.vpns.items()):
self._socket_connect(vpn)
if self.s:
self.collect_data(vpn)
self._socket_disconnect()
def collect_data(self, vpn):
version = self.send_command('version\n')
vpn['version'] = self.parse_version(version)
state = self.send_command('state\n')
vpn['state'] = self.parse_state(state)
stats = self.send_command('load-stats\n')
vpn['stats'] = self.parse_stats(stats)
status = self.send_command('status 3\n')
vpn['sessions'] = self.parse_status(status, self.geoip_data)
def _socket_send(self, command):
if sys.version_info[0] == 2:
self.s.send(command)
else:
self.s.send(bytes(command, 'utf-8'))
def _socket_recv(self, length):
if sys.version_info[0] == 2:
return self.s.recv(length)
else:
return self.s.recv(length).decode('utf-8')
def _socket_connect(self, vpn):
host = vpn['host']
port = int(vpn['port'])
timeout = 3
try:
self.s = socket.create_connection((host, port), timeout)
vpn['socket_connected'] = True
except socket.error:
self.s = False
vpn['socket_connected'] = False
def _socket_disconnect(self):
self._socket_send('quit\n')
self.s.close()
def send_command(self, command):
self._socket_send(command)
data = ''
while 1:
socket_data = self._socket_recv(1024)
socket_data = re.sub('>INFO(.)*\r\n', '', socket_data)
data += socket_data
if command == 'load-stats\n' and data != '':
break
elif data.endswith("\nEND\r\n"):
break
if args.debug:
debug("=== begin raw data\n{0!s}\n=== end raw data".format(data))
return data
@staticmethod
def parse_state(data):
state = {}
for line in data.splitlines():
parts = line.split(',')
if args.debug:
debug("=== begin split line\n{0!s}\n=== end split line".format(parts))
if parts[0].startswith('>INFO') or \
parts[0].startswith('END') or \
parts[0].startswith('>CLIENT'):
continue
else:
state['up_since'] = get_date(date_string=parts[0], uts=True)
state['connected'] = parts[1]
state['success'] = parts[2]
if parts[3]:
state['local_ip'] = ip_address(parts[3])
else:
state['local_ip'] = ''
if parts[4]:
state['remote_ip'] = ip_address(parts[4])
state['mode'] = 'Client'
else:
state['remote_ip'] = ''
state['mode'] = 'Server'
return state
@staticmethod
def parse_stats(data):
stats = {}
line = re.sub('SUCCESS: ', '', data)
parts = line.split(',')
if args.debug:
debug("=== begin split line\n{0!s}\n=== end split line".format(parts))
stats['nclients'] = int(re.sub('nclients=', '', parts[0]))
stats['bytesin'] = int(re.sub('bytesin=', '', parts[1]))
stats['bytesout'] = int(re.sub('bytesout=', '', parts[2]).replace('\r\n', ''))
return stats
@staticmethod
def parse_status(data, geoip_data):
client_section = False
routes_section = False
status_version = 1
sessions = {}
client_session = {}
gi = GeoIP.open(geoip_data, GeoIP.GEOIP_STANDARD)
for line in data.splitlines():
if ',' in line:
parts = line.split(',')
else:
parts = line.split('\t')
if args.debug:
debug("=== begin split line\n{0!s}\n=== end split line".format(parts))
if parts[0].startswith('GLOBAL'):
break
if parts[0] == 'HEADER':
status_version = 3
if parts[1] == 'CLIENT_LIST':
client_section = True
routes_section = False
if parts[1] == 'ROUTING_TABLE':
client_section = False
routes_section = True
continue
if parts[0] == 'Updated':
continue
if parts[0] == 'Common Name':
status_version = 1
client_section = True
routes_section = False
continue
if parts[0] == 'ROUTING TABLE' or parts[0] == 'Virtual Address':
status_version = 1
client_section = False
routes_section = True
continue
if parts[0].startswith('>CLIENT'):
continue
session = {}
if parts[0] == 'TUN/TAP read bytes':
client_session['tuntap_read'] = int(parts[1])
continue
if parts[0] == 'TUN/TAP write bytes':
client_session['tuntap_write'] = int(parts[1])
continue
if parts[0] == 'TCP/UDP read bytes':
client_session['tcpudp_read'] = int(parts[1])
continue
if parts[0] == 'TCP/UDP write bytes':
client_session['tcpudp_write'] = int(parts[1])
continue
if parts[0] == 'Auth read bytes':
client_session['auth_read'] = int(parts[1])
sessions['Client'] = client_session
continue
if client_section and not routes_section:
if status_version == 1:
ident = parts[1]
sessions[ident] = session
session['username'] = parts[0]
remote_ip, port = parts[1].split(':')
session['bytes_recv'] = int(parts[2])
session['bytes_sent'] = int(parts[3])
session['connected_since'] = get_date(parts[4])
elif status_version == 3:
local_ip = parts[3]
if local_ip:
ident = local_ip
else:
ident = str(uuid4())
sessions[ident] = session
if parts[8] != 'UNDEF':
session['username'] = parts[8]
else:
session['username'] = parts[1]
if parts[2].count(':') == 1:
remote_ip, port = parts[2].split(':')
else:
remote_ip = parts[2]
port = None
remote_ip_address = ip_address(remote_ip)
if local_ip:
session['local_ip'] = ip_address(local_ip)
else:
session['local_ip'] = ''
session['bytes_recv'] = int(parts[4])
session['bytes_sent'] = int(parts[5])
session['connected_since'] = get_date(parts[7], uts=True)
session['last_seen'] = session['connected_since']
session['location'] = 'Unknown'
if isinstance(remote_ip_address, IPv6Address) and \
remote_ip_address.ipv4_mapped is not None:
session['remote_ip'] = remote_ip_address.ipv4_mapped
else:
session['remote_ip'] = remote_ip_address
if port:
session['port'] = int(port)
else:
session['port'] = ''
if session['remote_ip'].is_private:
session['location'] = 'RFC1918'
else:
try:
gir = gi.record_by_addr(str(session['remote_ip']))
except SystemError:
gir = None
if gir is not None:
session['location'] = gir['country_code']
session['city'] = get_str(gir['city'])
session['country_name'] = gir['country_name']
session['longitude'] = gir['longitude']
session['latitude'] = gir['latitude']
if routes_section and not client_section:
if status_version == 1:
ident = parts[2]
sessions[ident]['local_ip'] = ip_address(parts[0])
sessions[ident]['last_seen'] = get_date(parts[3])
elif status_version == 3:
local_ip = parts[1]
if local_ip in sessions:
sessions[local_ip]['last_seen'] = get_date(parts[5], uts=True)
if args.debug:
if sessions:
pretty_sessions = pformat(sessions)
debug("=== begin sessions\n{0!s}\n=== end sessions".format(pretty_sessions))
else:
debug("no sessions")
return sessions
@staticmethod
def parse_version(data):
for line in data.splitlines():
if line.startswith('OpenVPN'):
return line.replace('OpenVPN Version: ', '')
class OpenvpnHtmlPrinter(object):
def __init__(self, cfg, monitor):
self.init_vars(cfg.settings, monitor)
self.print_html_header()
for key, vpn in self.vpns:
if vpn['socket_connected']:
self.print_vpn(vpn)
else:
self.print_unavailable_vpn(vpn)
if self.maps:
self.print_maps_html()
self.print_html_footer()
def init_vars(self, settings, monitor):
self.vpns = list(monitor.vpns.items())
self.site = 'Example'
if 'site' in settings:
self.site = settings['site']
self.logo = None
if 'logo' in settings:
self.logo = settings['logo']
self.maps = False
if 'maps' in settings and settings['maps'] == 'True':
self.maps = True
self.latitude = -37.8067
self.longitude = 144.9635
if 'latitude' in settings:
self.latitude = settings['latitude']
if 'longitude' in settings:
self.longitude = settings['longitude']
def print_html_header(self):
global wsgi
if not wsgi:
output("Content-Type: text/html\n")
output('<!doctype html>')
output('<html><head>')
output('<meta charset="utf-8">')
output('<meta http-equiv="X-UA-Compatible" content="IE=edge">')
output('<meta name="viewport" content="width=device-width, initial-scale=1">')
output('<title>{0!s} OpenVPN Status Monitor</title>'.format(self.site))
output('<meta http-equiv="refresh" content="300" />')
# css
output('<link rel="stylesheet" href="//maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" integrity="sha384-BVYiiSIFeK1dGmJRAkycuHAHRg32OmUcww7on3RYdg4Va+PmSTsz/K68vbdEjh4u" crossorigin="anonymous" />')
output('<link rel="stylesheet" href="//maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap-theme.min.css" integrity="sha384-rHyoN1iRsVXV4nD0JutlnGaslCJuC7uwjduW9SVrLvRYooPp2bWYgmgJQIXwl/Sp" crossorigin="anonymous" />')
output('<link rel="stylesheet" href="//cdnjs.cloudflare.com/ajax/libs/jquery.tablesorter/2.28.1/css/theme.bootstrap.min.css" integrity="sha256-wRcWKLfgokdRBDrdAtn6uj3yBfHBgkEZIqFXFrxKAFs=" crossorigin="anonymous" />')
if self.maps:
output('<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/leaflet/1.0.2/leaflet.css" integrity="sha256-9mfj77orHLh2GsN7CbMvpjO/Wny/ZZhR7Pu7hy0Yig4=" crossorigin="anonymous" />')
# js
output('<script src="//cdnjs.cloudflare.com/ajax/libs/jquery/3.1.1/jquery.min.js" integrity="sha256-hVVnYaiADRTO2PzUGmuLJr8BLUSjGIZsDYGmIJLv2b8=" crossorigin="anonymous"></script>')
output('<script src="//cdnjs.cloudflare.com/ajax/libs/jquery.tablesorter/2.28.1/js/jquery.tablesorter.min.js" integrity="sha256-UNRzZUOuTX5SlMJanVulIVc5Cr290jlWRgHwh1y+xtk=" crossorigin="anonymous"></script>')
output('<script src="//cdnjs.cloudflare.com/ajax/libs/jquery.tablesorter/2.28.1/js/jquery.tablesorter.widgets.min.js" integrity="sha256-XHLVNGORaSZRLHTJmkTRsOBm1DvZ7oeTWwvhPvwmkQc=" crossorigin="anonymous"></script>')
output('<script src="//maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js" integrity="sha384-Tc5IQib027qvyjSMfHjOMaLkfuWVxZxUPnCJA7l2mCWNIpG9mGCD8wGNIcPD7Txa" crossorigin="anonymous"></script>')
output('<script>$(document).ready(function(){')
output('$("#sessions").tablesorter({theme:"bootstrap", headerTemplate:"{content} {icon}", widgets:["uitheme"]});')
output('});</script>')
if self.maps:
output('<script src="https://cdnjs.cloudflare.com/ajax/libs/leaflet/1.0.2/leaflet.js" integrity="sha256-RS5bDpN9YmmUIdtdu8ESPjNp1Bg/Fqu90PwN3uawdSQ=" crossorigin="anonymous"></script>')
output('</head><body>')
output('<nav class="navbar navbar-inverse">')
output('<div class="container-fluid">')
output('<div class="navbar-header">')
output('<button type="button" class="navbar-toggle" ')
output('data-toggle="collapse" data-target="#myNavbar">')
output('<span class="icon-bar"></span>')
output('<span class="icon-bar"></span>')
output('<span class="icon-bar"></span>')
output('</button>')
output('<a class="navbar-brand" href="#">')
output('{0!s} OpenVPN Status Monitor</a>'.format(self.site))
output('</div><div class="collapse navbar-collapse" id="myNavbar">')
output('<ul class="nav navbar-nav"><li class="dropdown">')
output('<a class="dropdown-toggle" data-toggle="dropdown" href="#">VPN')
output('<span class="caret"></span></a>')
output('<ul class="dropdown-menu">')
for key, vpn in self.vpns:
if vpn['name']:
anchor = vpn['name'].lower().replace(' ', '_')
output('<li><a href="#{0!s}">{1!s}</a></li>'.format(anchor, vpn['name']))
output('</ul></li>')
if self.maps:
output('<li><a href="#map_canvas">Map View</a></li>')
output('</ul>')
if self.logo:
output('<a href="#" class="pull-right"><img alt="self.logo" ')
output('style="max-height:46px; padding-top:3px;" ')
output('src="{0!s}"></a>'.format(self.logo))
output('</div></div></nav>')
output('<div class="container-fluid">')
@staticmethod
def print_session_table_headers(vpn_mode):
server_headers = ['Username / Hostname', 'VPN IP Address',
'Remote IP Address', 'Location', 'Bytes In',
'Bytes Out', 'Connected Since', 'Last Ping', 'Time Online']
client_headers = ['Tun-Tap-Read', 'Tun-Tap-Write', 'TCP-UDP-Read',
'TCP-UDP-Write', 'Auth-Read']
if vpn_mode == 'Client':
headers = client_headers
elif vpn_mode == 'Server':
headers = server_headers
output('<table id="sessions" class="table table-striped table-bordered ')
output('table-hover table-condensed table-responsive ')
output('tablesorter tablesorter-bootstrap">')
output('<thead><tr>')
for header in headers:
output('<th>{0!s}</th>'.format(header))
output('</tr></thead><tbody>')
@staticmethod
def print_session_table_footer():
output('</tbody></table>')
@staticmethod
def print_unavailable_vpn(vpn):
anchor = vpn['name'].lower().replace(' ', '_')
output('<div class="panel panel-danger" id="{0!s}">'.format(anchor))
output('<div class="panel-heading">')
output('<h3 class="panel-title">{0!s}</h3></div>'.format(vpn['name']))
output('<div class="panel-body">')
output('Connection refused to {0!s}:{1!s} </div></div>'.format(vpn['host'], vpn['port']))
def print_vpn(self, vpn):
if vpn['state']['success'] == 'SUCCESS':
pingable = 'Yes'
else:
pingable = 'No'
connection = vpn['state']['connected']
nclients = vpn['stats']['nclients']
bytesin = vpn['stats']['bytesin']
bytesout = vpn['stats']['bytesout']
vpn_mode = vpn['state']['mode']
vpn_sessions = vpn['sessions']
local_ip = vpn['state']['local_ip']
remote_ip = vpn['state']['remote_ip']
up_since = vpn['state']['up_since']
anchor = vpn['name'].lower().replace(' ', '_')
output('<div class="panel panel-success" id="{0!s}">'.format(anchor))
output('<div class="panel-heading"><h3 class="panel-title">{0!s}</h3>'.format(
vpn['name']))
output('</div><div class="panel-body">')
output('<table class="table table-condensed table-responsive">')
output('<thead><tr><th>VPN Mode</th><th>Status</th><th>Pingable</th>')
output('<th>Clients</th><th>Total Bytes In</th><th>Total Bytes Out</th>')
output('<th>Up Since</th><th>Local IP Address</th>')
if vpn_mode == 'Client':
output('<th>Remote IP Address</th>')
output('</tr></thead><tbody>')
output('<tr><td>{0!s}</td>'.format(vpn_mode))
output('<td>{0!s}</td>'.format(connection))
output('<td>{0!s}</td>'.format(pingable))
output('<td>{0!s}</td>'.format(nclients))
output('<td>{0!s} ({1!s})</td>'.format(bytesin, naturalsize(bytesin, binary=True)))
output('<td>{0!s} ({1!s})</td>'.format(bytesout, naturalsize(bytesout, binary=True)))
output('<td>{0!s}</td>'.format(up_since.strftime('%d/%m/%Y %H:%M:%S')))
output('<td>{0!s}</td>'.format(local_ip))
if vpn_mode == 'Client':
output('<td>{0!s}</td>'.format(remote_ip))
output('</tr></tbody></table>')
if vpn_mode == 'Client' or nclients > 0:
self.print_session_table_headers(vpn_mode)
self.print_session_table(vpn_mode, vpn_sessions)
self.print_session_table_footer()
output('<span class="label label-default">{0!s}</span>'.format(vpn['version']))
output('</div></div>')
@staticmethod
def print_client_session(session):
output('<td>{0!s}</td>'.format(session['tuntap_read']))
output('<td>{0!s}</td>'.format(session['tuntap_write']))
output('<td>{0!s}</td>'.format(session['tcpudp_read']))
output('<td>{0!s}</td>'.format(session['tcpudp_write']))
output('<td>{0!s}</td>'.format(session['auth_read']))
@staticmethod
def print_server_session(session):
total_time = str(datetime.now() - session['connected_since'])[:-7]
bytes_recv = session['bytes_recv']
bytes_sent = session['bytes_sent']
output('<td>{0!s}</td>'.format(session['username']))
output('<td>{0!s}</td>'.format(session['local_ip']))
output('<td>{0!s}</td>'.format(session['remote_ip']))
if 'city' in session and 'country_name' in session:
country = session['country_name']
city = session['city']
if city:
full_location = '{0!s}, {1!s}'.format(city, country)
else:
full_location = country
flag = '{0!s}flags/{1!s}.png'.format(image_path, session['location'].lower())
output('<td><img src="{0!s}" title="{1!s}" alt="{1!s}" /> '.format(flag, full_location))
output('{0!s}</td>'.format(full_location))
else:
output('<td>{0!s}</td>'.format(session['location']))
output('<td>{0!s} ({1!s})</td>'.format(bytes_recv, naturalsize(bytes_recv, binary=True)))
output('<td>{0!s} ({1!s})</td>'.format(bytes_sent, naturalsize(bytes_sent, binary=True)))
output('<td>{0!s}</td>'.format(
session['connected_since'].strftime('%d/%m/%Y %H:%M:%S')))
if 'last_seen' in session:
output('<td>{0!s}</td>'.format(
session['last_seen'].strftime('%d/%m/%Y %H:%M:%S')))
else:
output('<td>ERROR</td>')
output('<td>{0!s}</td>'.format(total_time))
def print_session_table(self, vpn_mode, sessions):
for key, session in list(sessions.items()):
output('<tr>')
if vpn_mode == 'Client':
self.print_client_session(session)
elif vpn_mode == 'Server':
self.print_server_session(session)
output('</tr>')
def print_maps_html(self):
output('<div class="panel panel-info"><div class="panel-heading">')
output('<h3 class="panel-title">Map View</h3></div><div class="panel-body">')
output('<div id="map_canvas" style="height:500px"></div>')
output('<script type="text/javascript">')
output('var map = L.map("map_canvas");')
output('var centre = L.latLng({0!s}, {1!s});'.format(self.latitude, self.longitude))
output('map.setView(centre, 8);')
output('url = "https://{s}.tile.openstreetmap.org/{z}/{x}/{y}.png";')
output('var layer = new L.TileLayer(url, {});')
output('map.addLayer(layer);')
output('var bounds = L.latLngBounds(centre);')
for vkey, vpn in self.vpns:
if 'sessions' in vpn:
output('bounds.extend(centre);')
for skey, session in list(vpn['sessions'].items()):
if 'longitude' in session and 'latitude' in session:
output('var latlng = new L.latLng({0!s}, {1!s});'.format(
session['latitude'], session['longitude']))
output('bounds.extend(latlng);')
output('var marker = L.marker(latlng).addTo(map);')
output('var popup = L.popup().setLatLng(latlng);')
output('popup.setContent("{0!s} - {1!s}");'.format(
session['username'], session['remote_ip']))
output('marker.bindPopup(popup);')
output('map.fitBounds(bounds);')
output('</script>')
output('</div></div>')
@staticmethod
def print_html_footer():
output('<div class="well well-sm">')
output('Page automatically reloads every 5 minutes.')
output('Last update: <b>{0!s}</b></div>'.format(
datetime.now().strftime('%a %d/%m/%Y %H:%M:%S')))
output('</div></body></html>')
def main():
cfg = ConfigLoader(args.config)
monitor = OpenvpnMonitor(cfg)
OpenvpnHtmlPrinter(cfg, monitor)
if args.debug:
pretty_vpns = pformat((dict(monitor.vpns)))
debug("=== begin vpns\n{0!s}\n=== end vpns".format(pretty_vpns))
def get_args():
parser = argparse.ArgumentParser(
description='Display a html page with openvpn status and connections')
parser.add_argument('-d', '--debug', action='store_true',
required=False, default=False,
help='Run in debug mode')
parser.add_argument('-c', '--config', type=str,
required=False, default='./openvpn-monitor.conf',
help='Path to config file openvpn-monitor.conf')
return parser.parse_args()
if __name__ == '__main__':
args = get_args()
wsgi = False
image_path = 'images/'
main()
else:
from bottle import route, response, get, static_file, default_app
class args:
debug = False
config = './openvpn-monitor.conf'
wsgi = True
wsgi_output = ''
image_path = ''
owd = os.getcwd()
os.chdir(os.path.dirname(__file__))
sys.path.append(os.path.dirname(__file__))
if owd != os.getcwd() and sys.prefix != '/usr':
# virtualenv
images_dir = owd + '/share/openvpn-monitor/images/'
else:
images_dir = 'images'
application = default_app()
@route('/')
def root():
global wsgi_output
wsgi_output = ''
main()
response.content_type = 'text/html;'
return wsgi_output
@get('/<filename:re:.*\.(jpg|png)>')
def images(filename):
return static_file(filename, root=images_dir)