From b5bad4ebbc6a41b686cd8238b1294a13b06abaa6 Mon Sep 17 00:00:00 2001 From: Eduardo Santos Date: Wed, 8 Oct 2014 00:43:18 -0300 Subject: [PATCH] Delete .gitignore --- .gitignore | 71 ++++++++++++++++++++++++++++++++++++++++++++--------------------------- README.md | 8 ++++++++ cocar/__init__.py | 1 + cocar/host.py | 57 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++ cocar/query.py | 64 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ cocar/tests/__init__.py | 1 + cocar/tests/test_discover.py | 45 +++++++++++++++++++++++++++++++++++++++++++++ setup.py | 18 ++++++++++++++++++ 8 files changed, 238 insertions(+), 27 deletions(-) create mode 100644 cocar/__init__.py create mode 100644 cocar/host.py create mode 100644 cocar/query.py create mode 100644 cocar/tests/__init__.py create mode 100644 cocar/tests/test_discover.py create mode 100644 setup.py diff --git a/.gitignore b/.gitignore index 59d842b..34b8ed1 100644 --- a/.gitignore +++ b/.gitignore @@ -1,28 +1,45 @@ -# Logs -logs +# Byte-compiled / optimized / DLL files +__pycache__/ +*.py[cod] +# C extensions +*.so +# Distribution / packaging +.Python +env/ +build/ +develop-eggs/ +dist/ +downloads/ +eggs/ +lib/ +lib64/ +parts/ +sdist/ +var/ +*.egg-info/ +.installed.cfg +*.egg +# PyInstaller +# Usually these files are written by a python script from a template +# before PyInstaller builds the exe, so as to inject date/other infos into it. +*.manifest +*.spec +# Installer logs +pip-log.txt +pip-delete-this-directory.txt +# Unit test / coverage reports +htmlcov/ +.tox/ +.coverage +.cache +nosetests.xml +coverage.xml +# Translations +*.mo +*.pot +# Django stuff: *.log - -# Runtime data -pids -*.pid -*.seed - -# Directory for instrumented libs generated by jscoverage/JSCover -lib-cov - -# Coverage directory used by tools like istanbul -coverage - -# Grunt intermediate storage (http://gruntjs.com/creating-plugins#storing-task-files) -.grunt - -# Compiled binary addons (http://nodejs.org/api/addons.html) -build/Release - -# Dependency directory -# Commenting this out is preferred by some people, see -# https://www.npmjs.org/doc/misc/npm-faq.html#should-i-check-my-node_modules-folder-into-git- -node_modules - -# Users Environment Variables -.lock-wscript +# Sphinx documentation +docs/_build/ +# PyBuilder +target/ diff --git a/README.md b/README.md index c90001a..19886a6 100644 --- a/README.md +++ b/README.md @@ -2,3 +2,11 @@ cocar-agente ============ Módulo agente coletor para o software Cocar + +* Dependência: python-netsnmp + +Para funcionar é necessário primeiro instalar o pacote da distribuição e só depois criar o virtualenv: + +
+virtualenv --system-site-packages -p /usr/bin/python2.7 cocar-agente
+
\ No newline at end of file diff --git a/cocar/__init__.py b/cocar/__init__.py new file mode 100644 index 0000000..2231c87 --- /dev/null +++ b/cocar/__init__.py @@ -0,0 +1 @@ +__author__ = 'eduardo' diff --git a/cocar/host.py b/cocar/host.py new file mode 100644 index 0000000..aa2f037 --- /dev/null +++ b/cocar/host.py @@ -0,0 +1,57 @@ +#!/bin/env python +# -*- coding: utf-8 -*- +# Inspired by the code in http://www.copyandwaste.com/posts/view/multiprocessing-snmp-with-python/ +__author__ = 'eduardo' + +import netsnmp + + + +class Host(object): + """ + Creates a host record + """ + + def __init__(self, + hostname=None, + query=None): + self.hostname = hostname + self.query = query + + +class SnmpSession(): + """A SNMP Session""" + def __init__(self, + oid=".1.3.6.1.2.1.1.1.0", + iid=None, + Version=2, + DestHost="localhost", + Community="public", + Verbose=True, + ): + self.oid = oid + self.Version = Version + self.DestHost = DestHost + self.Community = Community + self.Verbose = Verbose + self.var = netsnmp.Varbind(oid, iid) + self.hostrec = Host() + self.hostrec.hostname = self.DestHost + + def query(self): + """Creates SNMP query + + Fills out a Host Object and returns result + """ + try: + result = netsnmp.snmpget(self.var, + Version=self.Version, + DestHost=self.DestHost, + Community=self.Community) + self.hostrec.query = result + except Exception, err: + if self.Verbose: + print err + self.hostrec.query = None + finally: + return self.hostrec \ No newline at end of file diff --git a/cocar/query.py b/cocar/query.py new file mode 100644 index 0000000..edb1ab1 --- /dev/null +++ b/cocar/query.py @@ -0,0 +1,64 @@ +#!/bin/env python +# -*- coding: utf-8 -*- +# Inspired by the code in http://www.copyandwaste.com/posts/view/multiprocessing-snmp-with-python/ +__author__ = 'eduardo' + +from host import SnmpSession +from multiprocessing import Process, Queue, current_process + + +def make_query(host): + """This does the actual snmp query + + This is a bit fancy as it accepts both instances + of SnmpSession and host/ip addresses. This + allows a user to customize mass queries with + subsets of different hostnames and community strings + """ + if isinstance(host, SnmpSession): + return host.query() + else: + s = SnmpSession(DestHost=host) + return s.query() + + +# Function run by worker processes +def worker(inp, output): + for func in iter(inp.get, 'STOP'): + result = make_query(func) + output.put(result) + +def main(): + """Runs everything""" + + #clients + hosts = [ + 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"), + 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") + ] + NUMBER_OF_PROCESSES = len(hosts) + + # Create queues + task_queue = Queue() + done_queue = Queue() + + #submit tasks + for host in hosts: + task_queue.put(host) + + #Start worker processes + for i in range(NUMBER_OF_PROCESSES): + Process(target=worker, args=(task_queue, done_queue)).start() + + # Get and print results + print 'Unordered results:' + for i in range(len(hosts)): + print '\t', done_queue.get().query + + # Tell child processes to stop + for i in range(NUMBER_OF_PROCESSES): + task_queue.put('STOP') + #print "Stopping Process #%s" % i + +if __name__ == "__main__": + main() \ No newline at end of file diff --git a/cocar/tests/__init__.py b/cocar/tests/__init__.py new file mode 100644 index 0000000..2231c87 --- /dev/null +++ b/cocar/tests/__init__.py @@ -0,0 +1 @@ +__author__ = 'eduardo' diff --git a/cocar/tests/test_discover.py b/cocar/tests/test_discover.py new file mode 100644 index 0000000..5958b33 --- /dev/null +++ b/cocar/tests/test_discover.py @@ -0,0 +1,45 @@ +#!/bin/env python +# -*- coding: utf-8 -*- +__author__ = 'eduardo' + +import unittest +from ..host import Host, SnmpSession + + +class TestDiscover(unittest.TestCase): + """ + Testa descoberta de ativos de rede utilizando snmp + """ + + def setUp(self): + """ + Parâmetros iniciais + """ + + def test_active(self): + """ + Teste que verifica se o ativo de rede está ativo + """ + session = SnmpSession() + result = session.query() + print(result.query[0]) + self.assertIsNotNone(result.query[0]) + + def test_inactive(self): + """ + Teste que identifica que um nó inativo + """ + session = SnmpSession(DestHost="192.168.0.201") + result = session.query() + print(result.query[0]) + self.assertIsNone(result.query[0]) + + def test_identify(self): + """ + Teste que identifica qual é o ativo + """ + + def tearDown(self): + """ + Apaga dados inicias + """ \ No newline at end of file diff --git a/setup.py b/setup.py new file mode 100644 index 0000000..a505366 --- /dev/null +++ b/setup.py @@ -0,0 +1,18 @@ +from distutils.core import setup + +requires = [ + 'multiprocessing', +] + + +setup( + name='cocar-agente', + version='1.0', + packages=['cocar', 'cocar.tests'], + url='http://github.com/lightbase/cocar-agente', + license='CC-GPL v2.0', + author='Lightbase Consultoria', + author_email='info@lightbase.com.br', + description='Agente coletor do software Cocar', + install_requires=requires, +) -- libgit2 0.21.2