1
2
3
4
5
6
7
8
9 """
10 Agent zephir pour le test des tunnels
11 """
12
13 from twisted.python import log
14 from twisted.internet.utils import getProcessOutput
15 from zephir.monitor.agentmanager.agent import Agent, RRDAgent
16 from zephir.monitor.agentmanager.data import HTMLData, TableData
17 from zephir.monitor.agentmanager.util import status_to_img
18 from zephir.monitor.agentmanager import status
19
20 import os
21
23 """parses ipsec status info
24 response format :
25 ['18:', '"C-amon1-0-sphynx"', 'STATE_QUICK_R2', '(IPsec', 'SA', 'established);', 'EVENT_SA_REPLACE', 'in', '8219s;', 'newest', 'IPSEC;', 'eroute', 'owner']
26 ['18:', '"C-amon1-0-sphynx"', 'esp.9c0c70fd@192.168.230.59', '(5016', 'bytes,', '519s', 'ago)', 'esp.c4cacef1@192.168.230.49', '(2772', 'bytes,', '519s', 'ago);', 'tunnel']
27 ['C-amon1-0-sphynx":', '172.16.36.0/24===192.168.230.49[@sphynxNG.agriates.lan]---192.168.230.254...192.168.230.254---192.168.230.59[@amon1]===10.21.10.0/24;', 'erouted;', 'eroute', 'owner:', '#18'] *** si owner = #0 --> tunnel down ***
28 """
29 response = response.split('\n')
30 tunnels = []
31 total=up=down=0
32 for line in response:
33 if not line[4:].startswith('#') and line.count('routed') > 0:
34 data = line[4:].split(';')
35 name, adresses = data[0].split(':')
36 src = adresses[:adresses.index('---')]
37 dst = adresses[adresses.rindex('---')+3:]
38 if 'erouted' in [line.strip() for line in data] or 'unrouted' in [line.strip() for line in data]:
39 conn_id = line[line.index('owner:')+6:].strip()
40 if conn_id != '#0':
41
42 status = 'On'
43 up += 1
44 else:
45 status = 'Off'
46 down += 1
47 total += 1
48 tunnels.append({'name':name.replace('"',''), 'src':src, 'dst':dst, 'status':status})
49 return tunnels, total, up, down
50
52
55 Agent.__init__(self, name, **params)
56 self.status = status.Unknown()
57 self.table = TableData([
58 ('name', "Identifiant", {'align':'left'}, None),
59 ('status', "Etat", {'align':'center'}, status_to_img),
60 ('src', "Source", {'align':'left'}, None),
61 ('dst', "Destination", {'align':'left'}, None),
62 ])
63 self.data = [self.table]
64 self.measure_data = {}
65
71
73 tunnels, total, up, down = parse_ipsec_status(response)
74 if down > 0:
75 self.status=status.Error()
76 for data in tunnels:
77 self.measure_data[data['name']] = (data['src'], data['dst'], data['status'])
78 self.measure_data['ok'] = up
79 self.measure_data['bad'] = down
80 return {'statistics':tunnels}
81
83 self.status = status.Error("Erreur d'execution : ipsec status")
84 return {'statistics':None}
85
87 Agent.write_data(self)
88 if self.last_measure is not None:
89 self.table.table_data = self.last_measure.value['statistics']
90
93
95
96 pourcentok = 100
97
103
105 tunnels, total, up, down = parse_ipsec_status(response)
106 self.measure_data['ok'] = up
107 self.measure_data['bad'] = down
108 if total != 0:
109 self.pourcentok = (100*up)/total
110 else:
111 self.pourcentok = 100
112 return {'ok':up,'bad':down,'total':total,'pourcentok':self.pourcentok}
113
115 self.status = status.Error("Erreur d'execution : ipsec status")
116 res = None
117 self.pourcentok = 100
118 return {'statistics':res}
119
126