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

Source Code for Module zephir.monitor.agents.netstat

  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  Agents zephir affichant les Informations Système 
 11  """ 
 12   
 13  from zephir.monitor.agentmanager.agent import MultiRRDAgent 
 14  from zephir.monitor.agentmanager import status 
 15  from zephir.monitor.agentmanager.data import TableData, HTMLData 
 16  from zephir.monitor.agentmanager.util import percent 
 17   
 18  from twisted.python import log 
 19  from twisted.internet import defer 
 20  from twisted.internet.utils import getProcessOutput 
 21  import re 
 22   
 23  SECONDS_PER_DAY = 3600*24 
 24   
 25  # column titles in /proc/net/dev 
 26  (IBYTES, IPACKETS, IERRS, 
 27   IDROP, IFIFO, IFRAME, ICOMPRESSED, IMULTICAST, 
 28   OBYTES, OPACKETS, OERRS, 
 29   ODROP, OFIFO, OCOLLS, OCARRIER, OCOMPRESSED, 
 30   ) = range(0,16) 
 31   
 32  IPADDRESS_RE = re.compile('(?<=addr:)\d\d?\d?.\d\d?\d?.\d\d?\d?.\d\d?\d?') 
 33  ERROR_RATE_ALERT_THRESHOLD = 10 # in percent 
 34   
35 -def _mega(val):
36 """ transfo de la valeur passée (string en octets) en Mo 37 """ 38 return (int(val)/1024/1024)
39
40 -def _stat_format(x):
41 return "%.1f" % x
42
43 -class NetStat(MultiRRDAgent):
44 """ 45 Bilan de l'etat des cartes réseau 46 présentation en tableau 47 + graphe pour chaque carte 48 """ 49
50 - def __init__(self, name, **params):
51 MultiRRDAgent.__init__(self, name, **params) 52 self.last_measure = None 53 self.table = TableData([ 54 ('name', 'Nom', {'align':'right'}, None), 55 ('address', 'Adresse', {'align':'left'}, None), 56 ('input KB', 'Entrée (Ko)', {'align':'right'}, _stat_format), 57 ('input err%', '(% err)', {'align':'right'}, _stat_format), 58 ('output KB', 'Sortie (Ko)', {'align':'right'}, _stat_format), 59 ('output err%', '(% err)', {'align':'right'}, _stat_format) ]) 60 title1 = HTMLData("<h3>Interfaces réseau<h3>") 61 title2 = HTMLData("<h3>Statistiques réseau (Entrées/Sorties)<h3>") 62 self.data.extend([title1, self.table, title2])
63
64 - def init_data(self, archive_dir):
65 """on initialise les archives rrd, et on définit 66 la liste des données""" 67 MultiRRDAgent.init_data(self,archive_dir)
68
69 - def measure(self):
70 ifconfig = getProcessOutput('/sbin/ifconfig', 71 env = {'LC_ALL': 'C'}) 72 catproc = getProcessOutput('/bin/cat', 73 args = ['/proc/net/dev'], 74 env = {'LC_ALL': 'C'}) 75 cmds = defer.DeferredList([ifconfig, catproc]) 76 cmds.addCallback(self.measure_process) 77 return cmds
78
79 - def measure_process(self, cmds_results):
80 [(ifconfig_success, ifconfig), 81 (catproc_success, catproc)] = cmds_results 82 assert ifconfig_success and catproc_success #FIXME 83 # get address of each interface 84 addresses = {} 85 blocks = ifconfig.strip().split('\n\n') 86 for b in blocks: 87 name_line, addr_line = b.splitlines()[0:2] 88 name = name_line.split()[0].replace('.','_') 89 addr_match = IPADDRESS_RE.search(addr_line) 90 #FIXME set agent status 91 if addr_match is not None: 92 addr = addr_match.group() 93 addresses[name] = addr 94 95 # get interfaces statistics from /proc 96 statistics = [] 97 dico={} 98 lines = catproc.splitlines()[2:] # drop header lines 99 for l in lines: 100 if_name, stats = l.split(':') 101 if_name = if_name.strip().replace('.','_') 102 if addresses.has_key(if_name): 103 stats = stats.split() 104 inkb = int(float(stats[IBYTES])/1024.0) 105 outkb = int(float(stats[OBYTES])/1024.0) 106 iner = percent(stats[IERRS], stats[IPACKETS]) 107 outer = percent(stats[OERRS], stats[OPACKETS]) 108 if_stats = {'name': if_name, 109 'address': addresses[if_name], 110 'input KB': inkb, 111 'output KB': outkb, 112 'input err%': iner, 113 'output err%': outer } 114 statistics.append(if_stats) 115 dico['in_%s' % if_name] = inkb 116 dico['iner_%s' % if_name] = iner 117 dico['out_%s' % if_name] = outkb 118 dico['outer_%s' % if_name] = outer 119 self.measure_data.update(dico) 120 # données du tableau 121 dico['statistics'] = statistics 122 return dico
123
124 - def save_measure(self, measure):
125 MultiRRDAgent.save_measure(self, measure) 126 measure.value = {'statistics':measure.value['statistics']} 127 self.last_measure = measure
128
129 - def write_data(self):
130 MultiRRDAgent.write_data(self) 131 if self.last_measure is not None: 132 self.table.table_data = self.last_measure.value['statistics']
133
134 - def check_status(self):
135 return status.OK()
136