Package zephir :: Package monitor :: Package agents :: Module services
[hide private]
[frames] | no frames]

Source Code for Module zephir.monitor.agents.services

  1  # -*- coding: UTF-8 -*- 
  2  ########################################################################### 
  3  # Eole NG - 2007 
  4  # Copyright Pole de Competence Eole  (Ministere Education - Academie Dijon) 
  5  # Licence CeCill  cf /root/LicenceEole.txt 
  6  # eole@ac-dijon.fr 
  7  ########################################################################### 
  8   
  9  """ 
 10  Agent zephir pour la surveillance de services via tcpcheck 
 11  """ 
 12   
 13  from twisted.python import log 
 14  from twisted.internet import defer 
 15  from twisted.internet.utils import getProcessOutput, getProcessValue 
 16   
 17  from zephir.monitor.agentmanager.agent import Agent 
 18  from zephir.monitor.agentmanager.data import HTMLData, TableData 
 19  from zephir.monitor.agentmanager.util import status_to_img, boolean_to_onoff 
 20  from zephir.monitor.agentmanager import status 
 21   
 22  TCPCHECK = '/usr/bin/tcpcheck' 
 23   
24 -def compare_hostports(hp1, hp2):
25 h1, p1 = hp1.split(':') 26 h2, p2 = hp2.split(':') 27 result = cmp(h1, h2) # alpha comparison on hosts 28 if result != 0: 29 return result 30 else: # numeric comparison on ports 31 return cmp(int(p1), int(p2))
32
33 -class TCPServices(Agent):
34 35
36 - def __init__(self, name, 37 tcp_services, # dict {"host:port": "comment"} 38 module, 39 **params):
40 Agent.__init__(self, name, **params) 41 self.tcp_services = tcp_services 42 self.module = module 43 self.table = TableData([ 44 ('status', "état", {'align':'center'}, status_to_img), 45 ('description', "Description", {'align':'left'}, None), 46 ('host', "Hôte", {'align':'left'}, None), 47 ('port', "Port", {'align':'left'}, None) ]) 48 self.data = [self.table]
49 50
51 - def measure(self):
52 tcpcheck = getProcessOutput(TCPCHECK, 53 args = ['1'] + self.tcp_services.keys(), 54 env = {'LC_ALL': 'C'}) 55 tcpcheck.addCallback(self.measure_process) 56 return tcpcheck
57
58 - def measure_process(self, tcpcheck_result):
59 lines = tcpcheck_result.splitlines() 60 hostports_status = [l.split(' ', 1) for l in lines] 61 hostports_status.sort(lambda (hp1, s1), (hp2, s2): 62 compare_hostports(hp1, hp2)) 63 for hostport, status in hostports_status: 64 status = boolean_to_onoff(status.endswith('alive')) 65 mesures=[{ 'description': self.tcp_services[hostport], 66 'host': hostport.split(':')[0], 67 'port': hostport.split(':')[1], 68 'status': status }] 69 self.measure_data['status'] = status 70 return {'services': mesures}
71
72 - def write_data(self):
73 Agent.write_data(self) 74 if self.last_measure is not None: 75 self.table.table_data = self.last_measure.value['services']
76
77 - def check_status(self):
78 """remonte une erreur si un des services est tombé""" 79 if self.last_measure is not None: 80 for service in self.last_measure.value['services']: 81 if service['status'] != 'On': 82 return status.Error() 83 else: 84 # pas de mesure connue 85 return status.Unknown() 86 return status.OK()
87 88
89 -class VPNService(Agent):
90
91 - def __init__(self, name, 92 tcp_services, # dict {"host:port": "comment"} 93 module, 94 **params):
95 Agent.__init__(self, name, **params) 96 self.tcp_services = tcp_services 97 self.module = module 98 self.table = TableData([ 99 ('status', "Etat", {'align':'center'}, status_to_img), 100 ('description', "Description", {'align':'left'}, None), 101 ('host', "Hôte", {'align':'left'}, None), 102 ('port', "Port", {'align':'left'}, None) ]) 103 self.data = [self.table]
104 105
106 - def measure(self):
107 cmd = "/usr/sbin/service" 108 vpncheck = getProcessOutput(cmd, 109 args=['ipsec','status'], 110 env = {'LC_ALL': 'C'}) 111 vpncheck.addCallbacks(self.measure_vpn,self.service_error) 112 return vpncheck
113
114 - def service_error(self,data):
115 """status a remonté une erreur 116 """ 117 log.msg("erreur status : %s" % str(data)) 118 status = 'Off' 119 mesures=[{ 'description': "Service Réseau Virtuel Privé (IpSec)", 120 'host': 'localhost', 121 'port': '', 122 'status': status }] 123 return {'services': mesures}
124
125 - def measure_vpn(self, data):
126 status = 'Off' 127 # si not running apparait dans les premières lignes de la sortie, ipsec est arreté 128 data = data.split('\n') 129 end = 5 130 if len(data) < 5: 131 end = len(data) 132 first_lines = "".join(data[0:end]) 133 if first_lines.count('not running') == 0: 134 status = 'On' 135 136 mesures=[{ 'description': "Service Réseau Virtuel Privé (IpSec)", 137 'host': 'localhost', 138 'port': '', 139 'status': status }] 140 return {'services': mesures}
141
142 - def write_data(self):
143 Agent.write_data(self) 144 if self.last_measure is not None: 145 self.table.table_data = self.last_measure.value['services']
146
147 - def check_status(self):
148 """remonte une erreur si un des services est tombé""" 149 if self.last_measure is not None: 150 for service in self.last_measure.value['services']: 151 if service['status'] != 'On': 152 return status.Error() 153 else: 154 # pas de mesure connue 155 return status.Unknown() 156 return status.OK()
157