zephir.monitor.agents.printers

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

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

     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  Affichage des imprimantes CUPS et de leur état
    10  """
    11  from twisted.internet.utils import getProcessOutput
    12  from zephir.monitor.agentmanager.agent import Agent
    13  from zephir.monitor.agentmanager.data import TableData
    14  from zephir.monitor.agentmanager import status
    15  from zephir.monitor.agentmanager.util import status_to_img
    16
    17
    18  PRINTER_CHECK = '/usr/share/zephir/monitor/bin/printers.sh'
    19
    20 -class Printers(Agent):
    21
    22 -    def __init__(self, name,
    23                   **params):
    24          Agent.__init__(self, name, **params)
    25          self.table = TableData([
    26              ('status', "état", {'align':'center'}, status_to_img),
    27              ('printer', "Imprimante", {'align':'left'}, None),
    28              ])
    29          self.data = [self.table]
    30
    31 -    def measure(self):
    32          test = getProcessOutput( PRINTER_CHECK,
    33                                   env  = {'LC_ALL': 'C'})
    34          test.addCallback(self.measure_process)
    35          test.addErrback(self.measure_error)
    36          return test
    37
    38 -    def measure_process(self, result):
    39          l = []
    40          lines = result.splitlines()
    41          for line in lines :
    42              if line.startswith('printer') or line.startswith("l’imprimante"):
    43                  if 'disabled' in line or 'désactivée' in line:
    44                      l.append( { 'printer' : line.split()[1],
    45                                  'status'  : 'Off' } )
    46                      self.measure_data[line.split()[1]] = 'Off'
    47                  elif 'is idle' in line or ' activée' in line:
    48                      l.append( { 'printer' : line.split()[1],
    49                                  'status'  : 'On' } )
    50                      self.measure_data[line.split()[1]] = 'On'
    51          if l == [] :
    52              l.append( { 'printer' : '** Aucune imprimante détectée **',
    53                          'status'  : '---' } )
    54
    55          return { 'statistics' : l}
    56
    57 -    def measure_error(self, result):
    58          return { 'statistics' : [{ 'printer' : '** Mesure impossible **',
    59                                     'status'  : '---' }] }
    60 -    def write_data(self):
    61          Agent.write_data(self)
    62          if self.last_measure is not None:
    63              self.table.table_data = self.last_measure.value['statistics']
    64
    65 -    def check_status(self):
    66          #if self.last_measure is not None:
    67          #    for service in self.last_measure.value['s']:
    68          #        if service['status'] != 'On':
    69          #            return status.Error()
    70          #else:
    71          #    # pas de mesure connue
    72          #    return status.Unknown()
    73          return status.OK()
    74