Commit b5bad4ebbc6a41b686cd8238b1294a13b06abaa6
1 parent
4d5ceade
Exists in
master
Delete .gitignore
Arquivo para JS que não será mais utilizado
Showing
8 changed files
with
238 additions
and
27 deletions
Show diff stats
.gitignore
| 1 | -# Logs | |
| 2 | -logs | |
| 1 | +# Byte-compiled / optimized / DLL files | |
| 2 | +__pycache__/ | |
| 3 | +*.py[cod] | |
| 4 | +# C extensions | |
| 5 | +*.so | |
| 6 | +# Distribution / packaging | |
| 7 | +.Python | |
| 8 | +env/ | |
| 9 | +build/ | |
| 10 | +develop-eggs/ | |
| 11 | +dist/ | |
| 12 | +downloads/ | |
| 13 | +eggs/ | |
| 14 | +lib/ | |
| 15 | +lib64/ | |
| 16 | +parts/ | |
| 17 | +sdist/ | |
| 18 | +var/ | |
| 19 | +*.egg-info/ | |
| 20 | +.installed.cfg | |
| 21 | +*.egg | |
| 22 | +# PyInstaller | |
| 23 | +# Usually these files are written by a python script from a template | |
| 24 | +# before PyInstaller builds the exe, so as to inject date/other infos into it. | |
| 25 | +*.manifest | |
| 26 | +*.spec | |
| 27 | +# Installer logs | |
| 28 | +pip-log.txt | |
| 29 | +pip-delete-this-directory.txt | |
| 30 | +# Unit test / coverage reports | |
| 31 | +htmlcov/ | |
| 32 | +.tox/ | |
| 33 | +.coverage | |
| 34 | +.cache | |
| 35 | +nosetests.xml | |
| 36 | +coverage.xml | |
| 37 | +# Translations | |
| 38 | +*.mo | |
| 39 | +*.pot | |
| 40 | +# Django stuff: | |
| 3 | 41 | *.log |
| 4 | - | |
| 5 | -# Runtime data | |
| 6 | -pids | |
| 7 | -*.pid | |
| 8 | -*.seed | |
| 9 | - | |
| 10 | -# Directory for instrumented libs generated by jscoverage/JSCover | |
| 11 | -lib-cov | |
| 12 | - | |
| 13 | -# Coverage directory used by tools like istanbul | |
| 14 | -coverage | |
| 15 | - | |
| 16 | -# Grunt intermediate storage (http://gruntjs.com/creating-plugins#storing-task-files) | |
| 17 | -.grunt | |
| 18 | - | |
| 19 | -# Compiled binary addons (http://nodejs.org/api/addons.html) | |
| 20 | -build/Release | |
| 21 | - | |
| 22 | -# Dependency directory | |
| 23 | -# Commenting this out is preferred by some people, see | |
| 24 | -# https://www.npmjs.org/doc/misc/npm-faq.html#should-i-check-my-node_modules-folder-into-git- | |
| 25 | -node_modules | |
| 26 | - | |
| 27 | -# Users Environment Variables | |
| 28 | -.lock-wscript | |
| 42 | +# Sphinx documentation | |
| 43 | +docs/_build/ | |
| 44 | +# PyBuilder | |
| 45 | +target/ | ... | ... |
README.md
| ... | ... | @@ -2,3 +2,11 @@ cocar-agente |
| 2 | 2 | ============ |
| 3 | 3 | |
| 4 | 4 | Módulo agente coletor para o software Cocar |
| 5 | + | |
| 6 | +* Dependência: python-netsnmp | |
| 7 | + | |
| 8 | +Para funcionar é necessário primeiro instalar o pacote da distribuição e só depois criar o virtualenv: | |
| 9 | + | |
| 10 | +<pre> | |
| 11 | +virtualenv --system-site-packages -p /usr/bin/python2.7 cocar-agente | |
| 12 | +</pre> | |
| 5 | 13 | \ No newline at end of file | ... | ... |
| ... | ... | @@ -0,0 +1 @@ |
| 1 | +__author__ = 'eduardo' | ... | ... |
| ... | ... | @@ -0,0 +1,57 @@ |
| 1 | +#!/bin/env python | |
| 2 | +# -*- coding: utf-8 -*- | |
| 3 | +# Inspired by the code in http://www.copyandwaste.com/posts/view/multiprocessing-snmp-with-python/ | |
| 4 | +__author__ = 'eduardo' | |
| 5 | + | |
| 6 | +import netsnmp | |
| 7 | + | |
| 8 | + | |
| 9 | + | |
| 10 | +class Host(object): | |
| 11 | + """ | |
| 12 | + Creates a host record | |
| 13 | + """ | |
| 14 | + | |
| 15 | + def __init__(self, | |
| 16 | + hostname=None, | |
| 17 | + query=None): | |
| 18 | + self.hostname = hostname | |
| 19 | + self.query = query | |
| 20 | + | |
| 21 | + | |
| 22 | +class SnmpSession(): | |
| 23 | + """A SNMP Session""" | |
| 24 | + def __init__(self, | |
| 25 | + oid=".1.3.6.1.2.1.1.1.0", | |
| 26 | + iid=None, | |
| 27 | + Version=2, | |
| 28 | + DestHost="localhost", | |
| 29 | + Community="public", | |
| 30 | + Verbose=True, | |
| 31 | + ): | |
| 32 | + self.oid = oid | |
| 33 | + self.Version = Version | |
| 34 | + self.DestHost = DestHost | |
| 35 | + self.Community = Community | |
| 36 | + self.Verbose = Verbose | |
| 37 | + self.var = netsnmp.Varbind(oid, iid) | |
| 38 | + self.hostrec = Host() | |
| 39 | + self.hostrec.hostname = self.DestHost | |
| 40 | + | |
| 41 | + def query(self): | |
| 42 | + """Creates SNMP query | |
| 43 | + | |
| 44 | + Fills out a Host Object and returns result | |
| 45 | + """ | |
| 46 | + try: | |
| 47 | + result = netsnmp.snmpget(self.var, | |
| 48 | + Version=self.Version, | |
| 49 | + DestHost=self.DestHost, | |
| 50 | + Community=self.Community) | |
| 51 | + self.hostrec.query = result | |
| 52 | + except Exception, err: | |
| 53 | + if self.Verbose: | |
| 54 | + print err | |
| 55 | + self.hostrec.query = None | |
| 56 | + finally: | |
| 57 | + return self.hostrec | |
| 0 | 58 | \ No newline at end of file | ... | ... |
| ... | ... | @@ -0,0 +1,64 @@ |
| 1 | +#!/bin/env python | |
| 2 | +# -*- coding: utf-8 -*- | |
| 3 | +# Inspired by the code in http://www.copyandwaste.com/posts/view/multiprocessing-snmp-with-python/ | |
| 4 | +__author__ = 'eduardo' | |
| 5 | + | |
| 6 | +from host import SnmpSession | |
| 7 | +from multiprocessing import Process, Queue, current_process | |
| 8 | + | |
| 9 | + | |
| 10 | +def make_query(host): | |
| 11 | + """This does the actual snmp query | |
| 12 | + | |
| 13 | + This is a bit fancy as it accepts both instances | |
| 14 | + of SnmpSession and host/ip addresses. This | |
| 15 | + allows a user to customize mass queries with | |
| 16 | + subsets of different hostnames and community strings | |
| 17 | + """ | |
| 18 | + if isinstance(host, SnmpSession): | |
| 19 | + return host.query() | |
| 20 | + else: | |
| 21 | + s = SnmpSession(DestHost=host) | |
| 22 | + return s.query() | |
| 23 | + | |
| 24 | + | |
| 25 | +# Function run by worker processes | |
| 26 | +def worker(inp, output): | |
| 27 | + for func in iter(inp.get, 'STOP'): | |
| 28 | + result = make_query(func) | |
| 29 | + output.put(result) | |
| 30 | + | |
| 31 | +def main(): | |
| 32 | + """Runs everything""" | |
| 33 | + | |
| 34 | + #clients | |
| 35 | + hosts = [ | |
| 36 | + SnmpSession(DestHost="10.71.1.1", Community="my-pub-community", oid="1.3.6.1.4.1.9.9.42.1.2.10.1.1", iid="1"), | |
| 37 | + SnmpSession(DestHost="10.81.1.1", Community="my-pub-community", oid="1.3.6.1.4.1.9.9.42.1.2.10.1.1", iid="123") | |
| 38 | + ] | |
| 39 | + NUMBER_OF_PROCESSES = len(hosts) | |
| 40 | + | |
| 41 | + # Create queues | |
| 42 | + task_queue = Queue() | |
| 43 | + done_queue = Queue() | |
| 44 | + | |
| 45 | + #submit tasks | |
| 46 | + for host in hosts: | |
| 47 | + task_queue.put(host) | |
| 48 | + | |
| 49 | + #Start worker processes | |
| 50 | + for i in range(NUMBER_OF_PROCESSES): | |
| 51 | + Process(target=worker, args=(task_queue, done_queue)).start() | |
| 52 | + | |
| 53 | + # Get and print results | |
| 54 | + print 'Unordered results:' | |
| 55 | + for i in range(len(hosts)): | |
| 56 | + print '\t', done_queue.get().query | |
| 57 | + | |
| 58 | + # Tell child processes to stop | |
| 59 | + for i in range(NUMBER_OF_PROCESSES): | |
| 60 | + task_queue.put('STOP') | |
| 61 | + #print "Stopping Process #%s" % i | |
| 62 | + | |
| 63 | +if __name__ == "__main__": | |
| 64 | + main() | |
| 0 | 65 | \ No newline at end of file | ... | ... |
| ... | ... | @@ -0,0 +1 @@ |
| 1 | +__author__ = 'eduardo' | ... | ... |
| ... | ... | @@ -0,0 +1,45 @@ |
| 1 | +#!/bin/env python | |
| 2 | +# -*- coding: utf-8 -*- | |
| 3 | +__author__ = 'eduardo' | |
| 4 | + | |
| 5 | +import unittest | |
| 6 | +from ..host import Host, SnmpSession | |
| 7 | + | |
| 8 | + | |
| 9 | +class TestDiscover(unittest.TestCase): | |
| 10 | + """ | |
| 11 | + Testa descoberta de ativos de rede utilizando snmp | |
| 12 | + """ | |
| 13 | + | |
| 14 | + def setUp(self): | |
| 15 | + """ | |
| 16 | + Parâmetros iniciais | |
| 17 | + """ | |
| 18 | + | |
| 19 | + def test_active(self): | |
| 20 | + """ | |
| 21 | + Teste que verifica se o ativo de rede está ativo | |
| 22 | + """ | |
| 23 | + session = SnmpSession() | |
| 24 | + result = session.query() | |
| 25 | + print(result.query[0]) | |
| 26 | + self.assertIsNotNone(result.query[0]) | |
| 27 | + | |
| 28 | + def test_inactive(self): | |
| 29 | + """ | |
| 30 | + Teste que identifica que um nó inativo | |
| 31 | + """ | |
| 32 | + session = SnmpSession(DestHost="192.168.0.201") | |
| 33 | + result = session.query() | |
| 34 | + print(result.query[0]) | |
| 35 | + self.assertIsNone(result.query[0]) | |
| 36 | + | |
| 37 | + def test_identify(self): | |
| 38 | + """ | |
| 39 | + Teste que identifica qual é o ativo | |
| 40 | + """ | |
| 41 | + | |
| 42 | + def tearDown(self): | |
| 43 | + """ | |
| 44 | + Apaga dados inicias | |
| 45 | + """ | |
| 0 | 46 | \ No newline at end of file | ... | ... |
| ... | ... | @@ -0,0 +1,18 @@ |
| 1 | +from distutils.core import setup | |
| 2 | + | |
| 3 | +requires = [ | |
| 4 | + 'multiprocessing', | |
| 5 | +] | |
| 6 | + | |
| 7 | + | |
| 8 | +setup( | |
| 9 | + name='cocar-agente', | |
| 10 | + version='1.0', | |
| 11 | + packages=['cocar', 'cocar.tests'], | |
| 12 | + url='http://github.com/lightbase/cocar-agente', | |
| 13 | + license='CC-GPL v2.0', | |
| 14 | + author='Lightbase Consultoria', | |
| 15 | + author_email='info@lightbase.com.br', | |
| 16 | + description='Agente coletor do software Cocar', | |
| 17 | + install_requires=requires, | |
| 18 | +) | ... | ... |