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