1
1
import networkx
2
- from openvpn_status import ParsingError , parse_status
2
+ from openvpn_status import parse_status
3
3
4
- from ..exceptions import ConversionException , ParserError
5
4
from .base import BaseParser
6
5
7
6
8
7
class OpenvpnParser (BaseParser ):
9
8
""" OpenVPN status log parser """
10
- protocol = 'openvpn'
11
- server_address = '127.0.0.1' # Testing purposes
9
+ protocol = 'OpenVPN Status Log'
12
10
version = '1'
13
- metric = '0'
11
+ metric = 'static'
12
+ # for internal use only
13
+ _server_common_name = 'openvpn-server'
14
14
15
15
def to_python (self , data ):
16
- status = parse_status (data )
17
- nodes = []
18
- for i in status .client_list :
19
- node = status .client_list [i ]
20
- nodes .append ({
21
- 'commonName' : node .common_name ,
22
- 'bytesSent' : node .bytes_sent ,
23
- 'bytesReceived' : node .bytes_received ,
24
- 'connectedSince' : node .connected_since ,
25
- 'realAddress' : str (node .real_address )
26
- })
27
-
28
- return {
29
- 'type' : 'OpenVPN' ,
30
- 'nodes' : nodes ,
31
- 'updated_at' : status .updated_at ,
32
- 'server' : self .server_address
33
- }
16
+ return parse_status (data )
34
17
35
18
def parse (self , data ):
36
19
"""
@@ -39,20 +22,24 @@ def parse(self, data):
39
22
"""
40
23
# initialize graph and list of aggregated nodes
41
24
graph = networkx .Graph ()
42
- if 'type' not in data or data ['type' ] != 'OpenVPN' :
43
- raise ParserError ('Parse Error, not a OpenVPN object' )
44
- required_keys = ['nodes' , 'server' ]
45
- for key in required_keys :
46
- if key not in data :
47
- raise ParserError ('Parse Error, "%s" key not found' % key )
48
-
49
- graph .add_node (data ['server' ])
50
- for link in data ['nodes' ]:
51
- try :
52
- source = data ['server' ]
53
- target = link ['realAddress' ]
54
- except KeyError as e :
55
- raise ParserError ('Parse Error, "%s" key not found' % key )
56
- graph .add_node (target )
57
- graph .add_edge (source , target , weight = 1 )
25
+ server = self ._server_common_name
26
+ # add server (central node) to graph
27
+ graph .add_node (server )
28
+ # add clients in graph as nodes
29
+ for common_name , client in data .client_list .items ():
30
+ client_properties = {
31
+ 'real_address' : str (client .real_address .host ),
32
+ 'port' : client .real_address .port ,
33
+ 'connected_since' : client .connected_since .strftime ('%Y-%m-%dT%H:%M:%SZ' ),
34
+ 'bytes_received' : client .bytes_received ,
35
+ 'bytes_sent' : client .bytes_sent
36
+ }
37
+ if common_name in data .routing_table :
38
+ client_properties ['local_addresses' ] = [
39
+ str (data .routing_table .get (common_name ).virtual_address )
40
+ ]
41
+ graph .add_node (common_name , ** client_properties )
42
+ # add links in routing table to graph
43
+ for common_name , link in data .routing_table .items ():
44
+ graph .add_edge (server , common_name , weight = 1 )
58
45
return graph
0 commit comments