Commit f59cced55f95c4fda76b0ec7da1a31ca47ab8951

Authored by Eduardo Santos
Committed by Eduardo Santos
2 parents e998ced2 7352198e
Exists in master

Versão que grava atributos e contdor. Primeiro beta que vai pra produção.

cocar/commands/scan_commands.py
@@ -62,6 +62,12 @@ class ScanCommands(command.Command): @@ -62,6 +62,12 @@ class ScanCommands(command.Command):
62 help='SNMP query to execute' 62 help='SNMP query to execute'
63 ) 63 )
64 64
  65 + parser.add_option('-t', '--timeout',
  66 + action='store',
  67 + dest='timeout',
  68 + help='Timeout da consulta SNMP'
  69 + )
  70 +
65 def __init__(self, name): 71 def __init__(self, name):
66 """ 72 """
67 Constructor method 73 Constructor method
@@ -249,6 +255,7 @@ class ScanCommands(command.Command): @@ -249,6 +255,7 @@ class ScanCommands(command.Command):
249 log.info("Host genérico com o IP %s já cadastrado", hostname) 255 log.info("Host genérico com o IP %s já cadastrado", hostname)
250 256
251 #session.flush() 257 #session.flush()
  258 + session.close()
252 259
253 def get_printers(self): 260 def get_printers(self):
254 """ 261 """
@@ -272,7 +279,8 @@ class ScanCommands(command.Command): @@ -272,7 +279,8 @@ class ScanCommands(command.Command):
272 log.info("Coletando informacoes da impressora %s", printer.network_ip) 279 log.info("Coletando informacoes da impressora %s", printer.network_ip)
273 #printer.network_ip = printer.ip_network 280 #printer.network_ip = printer.ip_network
274 snmp_session = SnmpSession( 281 snmp_session = SnmpSession(
275 - DestHost=printer.network_ip 282 + DestHost=printer.network_ip,
  283 + Timeout=self.options.timeout
276 ) 284 )
277 if snmp_session is None: 285 if snmp_session is None:
278 log.error("Erro na coleta SNMP da impressora %s", printer.network_ip) 286 log.error("Erro na coleta SNMP da impressora %s", printer.network_ip)
@@ -347,6 +355,8 @@ class ScanCommands(command.Command): @@ -347,6 +355,8 @@ class ScanCommands(command.Command):
347 log.info("Exportando impressora %s", printer.network_ip) 355 log.info("Exportando impressora %s", printer.network_ip)
348 printer.export_printer(server_url=self.cocar.config.get('cocar', 'server_url'), session=session) 356 printer.export_printer(server_url=self.cocar.config.get('cocar', 'server_url'), session=session)
349 357
  358 + session.close()
  359 +
350 def get_printer_attribute(self): 360 def get_printer_attribute(self):
351 """ 361 """
352 Retorna e grava um atributo n o valor da impressora 362 Retorna e grava um atributo n o valor da impressora
@@ -363,16 +373,22 @@ class ScanCommands(command.Command): @@ -363,16 +373,22 @@ class ScanCommands(command.Command):
363 Printer.network_ip == self.options.hosts 373 Printer.network_ip == self.options.hosts
364 ).all() 374 ).all()
365 375
  376 + if len(results) == 0:
  377 + log.error("Impressoras não encontradas")
  378 + log.error(self.options.hosts)
  379 +
366 for printer in results: 380 for printer in results:
367 log.info("Coletando informacoes da impressora %s", printer.network_ip) 381 log.info("Coletando informacoes da impressora %s", printer.network_ip)
368 #printer.network_ip = printer.ip_network 382 #printer.network_ip = printer.ip_network
369 snmp_session = SnmpSession( 383 snmp_session = SnmpSession(
370 - DestHost=printer.network_ip 384 + DestHost=printer.network_ip,
  385 + Timeout=self.options.timeout
371 ) 386 )
372 if snmp_session is None: 387 if snmp_session is None:
373 log.error("Erro na coleta SNMP da impressora %s", printer.network_ip) 388 log.error("Erro na coleta SNMP da impressora %s", printer.network_ip)
374 continue 389 continue
375 else: 390 else:
  391 + log.info("Iniciando coleta da impressora %s", printer.network_ip)
376 printer_dict = dict() 392 printer_dict = dict()
377 if type(self.options.query != list): 393 if type(self.options.query != list):
378 test = self.options.query 394 test = self.options.query
@@ -448,6 +464,8 @@ class ScanCommands(command.Command): @@ -448,6 +464,8 @@ class ScanCommands(command.Command):
448 log.error("Erro na atualizacao das informacoes para a impressora %s\n%s", printer.network_ip, e.message) 464 log.error("Erro na atualizacao das informacoes para a impressora %s\n%s", printer.network_ip, e.message)
449 continue 465 continue
450 466
  467 + session.close()
  468 +
451 469
452 def make_query(host): 470 def make_query(host):
453 """This does the actual snmp query 471 """This does the actual snmp query
cocar/model/printer.py
@@ -53,6 +53,7 @@ class Printer(Host): @@ -53,6 +53,7 @@ class Printer(Host):
53 ).all() 53 ).all()
54 54
55 for counter in counter_list: 55 for counter in counter_list:
  56 + print(counter)
56 result = counter.export_counter(server_url, session) 57 result = counter.export_counter(server_url, session)
57 if result: 58 if result:
58 log.info("Contador %s para a impressora %s exportado com sucesso") 59 log.info("Contador %s para a impressora %s exportado com sucesso")
@@ -88,6 +89,7 @@ class PrinterCounter(Printer): @@ -88,6 +89,7 @@ class PrinterCounter(Printer):
88 :param session: SQLAlchemy session 89 :param session: SQLAlchemy session
89 :return boolean: True if inserted 90 :return boolean: True if inserted
90 """ 91 """
  92 + retorno = False
91 results = session.query(self.__table__).filter( 93 results = session.query(self.__table__).filter(
92 and_( 94 and_(
93 self.__table__.c.counter == self.counter, 95 self.__table__.c.counter == self.counter,
@@ -104,7 +106,7 @@ class PrinterCounter(Printer): @@ -104,7 +106,7 @@ class PrinterCounter(Printer):
104 counter_time=self.counter_time 106 counter_time=self.counter_time
105 ) 107 )
106 ) 108 )
107 - return True 109 + retorno = True
108 110
109 session.execute( 111 session.execute(
110 Printer.__table__.update().values( 112 Printer.__table__.update().values(
@@ -117,7 +119,7 @@ class PrinterCounter(Printer): @@ -117,7 +119,7 @@ class PrinterCounter(Printer):
117 ) 119 )
118 120
119 session.flush() 121 session.flush()
120 - return False 122 + return retorno
121 123
122 def export_counter(self, server_url, session): 124 def export_counter(self, server_url, session):
123 """ 125 """
@@ -139,7 +141,7 @@ class PrinterCounter(Printer): @@ -139,7 +141,7 @@ class PrinterCounter(Printer):
139 141
140 # Envia a requisição HTTP 142 # Envia a requisição HTTP
141 headers = {'content-type': 'application/json'} 143 headers = {'content-type': 'application/json'}
142 - response = requests.put( 144 + response = requests.post(
143 export_url, 145 export_url,
144 data=json.dumps(counter_json), 146 data=json.dumps(counter_json),
145 headers=headers 147 headers=headers
@@ -148,16 +150,25 @@ class PrinterCounter(Printer): @@ -148,16 +150,25 @@ class PrinterCounter(Printer):
148 try: 150 try:
149 # Check if request has gone wrong 151 # Check if request has gone wrong
150 response.raise_for_status() 152 response.raise_for_status()
151 - except HTTPError: 153 + except HTTPError, e:
152 # Something got wrong, raise error 154 # Something got wrong, raise error
153 - log.error("Erro na insercao do contador para a impressora %s\n", self.network_ip, response.text) 155 + log.error("Erro na insercao do contador para a impressora %s\n%s", self.network_ip, response.text)
  156 + log.error(e.message)
154 return False 157 return False
155 158
156 if response.status_code == 200: 159 if response.status_code == 200:
157 log.info("Contador para a impressora %s com contador %s" 160 log.info("Contador para a impressora %s com contador %s"
158 "exportado com sucesso", self.network_ip, self.counter) 161 "exportado com sucesso", self.network_ip, self.counter)
159 # Remove o contador 162 # Remove o contador
160 - session.remove(self) 163 + session.execute(
  164 + PrinterCounter.__table__.delete().where(
  165 + and_(
  166 + PrinterCounter.__table__.c.network_ip == self.network_ip,
  167 + PrinterCounter.__table__.c.counter == self.counter,
  168 + PrinterCounter.__table__.c.counter_time == self.counter_time,
  169 + )
  170 + )
  171 + )
161 session.flush() 172 session.flush()
162 return True 173 return True
163 else: 174 else:
cocar/session.py
@@ -32,7 +32,7 @@ class SnmpSession(object): @@ -32,7 +32,7 @@ class SnmpSession(object):
32 DestHost="localhost", 32 DestHost="localhost",
33 Community="public", 33 Community="public",
34 Verbose=True, 34 Verbose=True,
35 - Timeout=10000000 35 + Timeout=1000000
36 ): 36 ):
37 """ 37 """
38 Sessão SNMP. Links úteis: 38 Sessão SNMP. Links úteis:
@@ -75,7 +75,7 @@ class SnmpSession(object): @@ -75,7 +75,7 @@ class SnmpSession(object):
75 Version=self.Version, 75 Version=self.Version,
76 DestHost=self.DestHost, 76 DestHost=self.DestHost,
77 Community=self.Community, 77 Community=self.Community,
78 - Timeout=self.Timeout) 78 + Timeout=int(self.Timeout))
79 self.hostrec.query = result 79 self.hostrec.query = result
80 except Exception, err: 80 except Exception, err:
81 if self.Verbose: 81 if self.Verbose: