zephir.monitor.agents.connexions

Package zephir :: Package monitor :: Package agents :: Module connexions |
[frames] | no frames]

# Source Code for Module zephir.monitor.agents.connexions

      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 de surveillance des connexions (ip conntrack)
     11  """
     12
     13  from twisted.internet import defer
     14  from twisted.internet.utils import getProcessOutput
     15  from socket import ntohs, ntohl
     16  from IPy import IP
     17
     18  from zephir.monitor.agentmanager.agent import Agent
     19  from zephir.monitor.agentmanager import status
     20  from zephir.monitor.agentmanager.data import HTMLData, TableData
     21  from zephir.monitor.agentmanager.util import percent
     22
     23  from pyctd.conntracking import DataCollector
     24  from pynetfilter_conntrack import constant
     25
     26  IPPROTO_NAMES = {}
     27  IPPROTO_FAMILIES = {}
     28  TCPSTATES = {}
     29
     30  # Python dictionnary to convert IP protocol to string
     31  for attr_name in dir(constant):
     32      if attr_name.startswith('IPPROTO_'):
     33          IPPROTO_NAMES[getattr(constant,attr_name)] = attr_name[8:]
     34      if attr_name.startswith('PF_'):
     35          IPPROTO_FAMILIES[getattr(constant,attr_name)] = attr_name[3:]
     36      if attr_name.startswith('NFCT_TCP_ST_'):
     37          TCPSTATES[getattr(constant,attr_name)] = attr_name[12:]
     38
     39 -class Connexions(Agent):
     40
     41 -    def __init__(self, name,
     42                   **params):
     43          Agent.__init__(self, name, **params)
     44          self.table = TableData([
     45              ('id', 'Id_cnx', {'align':'right'}, None),
     46              ('user', 'Utilisateur', {'align':'left'}, None),
     47              ('rate_in', 'Débit Entrant', {'align':'right'}, None),
     48              ('rate_out', 'Débit Sortant', {'align':'right'}, None),
     49              ('port', 'Port', {'align':'left'}, None),
     50              ('src', 'Source', {'align':'right'}, None),
     51              ('dst', 'Destination', {'align':'right'}, None),
     52              ('status', 'Etat', {'align':'left'}, None),
     53              ('proto', 'Protocole', {'align':'left'}, None),
     54              ('mark', 'Marquage', {'align':'left'}, None),
     55              ('timeout', 'Timeout', {'align':'left'}, None),
     56              ])
     57          self.data = [self.table]
     58          self.collector = None
     59
     60 -    def measure(self):
     61          if self.collector == None:
     62              self.collector = DataCollector('conntrack', None)
     63          self.collector.refresh()
     64          meas_data = []
     65          conn_set = self.collector.current
     66          # connections = conn_set.itervalues()
     67          for id, conn in conn_set.items():
     68              conn_data = conn.conntrack
     69              fields = {}
     70              fields['id'] = id
     71              fields['mark'] = conn_data.mark
     72              fields['user'] = conn.username
     73              fields['timeout'] = str(int(conn_data.timeout))
     74              states = []
     75              # FIXME  : inverser les lignes si bug sur status corrigé
     76              # for state in str(conn_data.status):
     77              #for state in str(ntohl(conn_data.status)):
     78              #    states.append(TCPSTATES[int(state)])
     79              fields['status'] = TCPSTATES[conn_data.tcp_state]
     80              fields['src'] = str(IP(conn_data.orig_ipv4_src))
     81              fields['dst'] = str(IP(conn_data.orig_ipv4_dst))
     82              if conn_data.orig_l3proto == constant.IPPROTO_ICMP:
     83                  fields["l3src"] = int(conn_data.icmp_id)
     84                  fields["l4src"] = int(conn_data.icmp_type)
     85              else:
     86                  try:
     87                      l3src = str(IPPROTO_FAMILIES[conn_data.orig_l3proto])
     88                  except KeyError:
     89                      l3src = str(conn_data.orig_l3proto)
     90                  try:
     91                      l4src = str(IPPROTO_NAMES[conn_data.orig_l4proto])
     92                  except KeyError:
     93                      l4src = str(conn_data.orig_l4proto)
     94                  fields["l3src"] = l3src
     95                  fields["l4src"] = l4src
     96              fields['proto'] = "%s/%s" % (fields["l3src"],fields["l4src"])
     97              fields['port'] = "%s/%s" % (str(int(conn_data.orig_port_src)),str(int(conn_data.orig_port_dst)))
     98              # FIXME : afficher en % de la b.p disponible
     99              # ou en % par rapport à l'ensemble des connexions ?
    100              fields['rate_in'] = float(conn.orig_byterate)
    101              fields['rate_out'] = float(conn.repl_byterate)
    102              meas_data.append(fields)
    103          return {'statistics': meas_data}
    104
    105 -    def check_status(self):
    106          stat = status.OK()
    107          return stat
    108
    109 -    def save_measure(self, measure):
    110          Agent.save_measure(self, measure)
    111          self.last_measure = measure
    112
    113 -    def write_data(self):
    114          Agent.write_data(self)
    115          if self.last_measure is not None:
    116              self.table.table_data = self.last_measure.value['statistics']
    117