Skip to content
Browse files

Add some tests for various SNMP states, make sure all the plugins ret…

…urn something that evaluates to false if there is no data. Bump to version 0.4
  • Loading branch information...
2 parents fbe2b32 + 752b543 commit 3abb4550548324d44cfaca448eec36241e7f129c @cread committed
View
6 README.md
@@ -91,4 +91,8 @@ All base plugins have been tested to work with the default SNMP agent for:
* Windows Server
Note: For the windows systems, the load average has no meaning and so does not work.
-The way CPU and Memory are reported are different, so use `wincpu` and `winmemory` instead of `cpu` and `memory`
+The way CPU and Memory are reported are different, so use `wincpu` and `winmemory` instead of `cpu` and `memory`
+
+## Copyright
+
+Copyright (c) 2011 Chris Read. See LICENSE for details.
View
2 ecks/__init__.py
@@ -26,7 +26,7 @@
limitations under the License.
"""
-__version__ = '0.4-dev'
+__version__ = '0.4'
__author__ = 'Chris Read <chris.read@gmail.com>'
from ecks import Ecks
View
7 ecks/ecks.py
@@ -46,7 +46,8 @@ class Ecks():
plugins = []
- def __init__(self):
+ def __init__(self, timeout = 1):
+ self.timeout = timeout
if logging._handlers == {}:
logging.basicConfig()
self.logger = logging.getLogger(__name__ + ".Ecks")
@@ -106,11 +107,11 @@ def get_snmp_data(self, host, community, query_oid, query_oid_only = None):
"""
error_indication, error_status, error_index, var_binds_list = cmdgen.CommandGenerator().bulkCmd(
cmdgen.CommunityData(host, community),
- cmdgen.UdpTransportTarget((host, 161)),
+ cmdgen.UdpTransportTarget((host, 161), timeout = self.timeout),
0, 25, query_oid)
if error_indication:
- self.logger.warning(error_indication)
+ self.logger.error(error_indication)
elif error_status:
self.logger.error(
'%s at %s\n' % (error_status.prettyPrint(), error_index and var_binds_list[int(error_index) - 1] or '?'))
View
12 ecks/plugins/cpu.py
@@ -25,8 +25,10 @@ def get_cpu(parent, host, community):
"""
cpu = (1,3,6,1,4,1,2021,11) # UCD-SNMP-MIB
data = parent.get_snmp_data(host, community, cpu, 1)
- return (
- parent._extract(data, int, 9)[0],
- parent._extract(data, int, 10)[0],
- parent._extract(data, int, 11)[0],
- )
+
+ if data:
+ return (
+ parent._extract(data, int, 9)[0],
+ parent._extract(data, int, 10)[0],
+ parent._extract(data, int, 11)[0],
+ )
View
14 ecks/plugins/memory.py
@@ -25,9 +25,11 @@ def get_memory(parent, host, community):
"""
memory = (1,3,6,1,4,1,2021,4) # UCD-SNMP-MIB
data = parent.get_snmp_data(host, community, memory, 1)
- return map(parent._build_answer,
- parent._extract(data, int, 3),
- parent._extract(data, int, 4),
- parent._extract(data, int, 5),
- parent._extract(data, int, 6),
- )[0]
+
+ if data:
+ return map(parent._build_answer,
+ parent._extract(data, int, 3),
+ parent._extract(data, int, 4),
+ parent._extract(data, int, 5),
+ parent._extract(data, int, 6),
+ )[0]
View
5 ecks/plugins/tcpstats.py
@@ -39,6 +39,9 @@ def get_tcpstats(parent, host, community):
stats = []
for id in ids:
- stats = stats + [long(parent.get_snmp_data(host, community, (1,3,6,1,2,1,6,id), 1)[0][2])]
+ try:
+ stats = stats + [long(parent.get_snmp_data(host, community, (1,3,6,1,2,1,6,id), 1)[0][2])]
+ except:
+ return
return stats
View
5 ecks/plugins/uptime.py
@@ -25,4 +25,7 @@ def get_uptime(parent, host, community):
uptime = (1,3,6,1,2,1,25,1,1) # HOST-RESOURCE-MIB
data = parent.get_snmp_data(host, community, uptime, 1)
- return int(data[0][2])
+ if data:
+ return int(data[0][2])
+ else:
+ return None
View
3 ecks/plugins/wincpu.py
@@ -25,4 +25,5 @@ def get_wincpu(parent, host, community):
"""
cpu = (1,3,6,1,2,1,25,3,3,1,2) # HOST-RESOURCES-MIB
data = parent.get_snmp_data(host, community, cpu, 1)
- return tuple([ int(load) for (oid, num, load) in data ])
+ if data:
+ return tuple([ int(load) for (oid, num, load) in data ])
View
4 ecks/plugins/winmemory.py
@@ -28,6 +28,10 @@ def get_winmemory(parent, host, community):
"""
blocks = (1,3,6,1,2,1,25,2,3,1) # HOST-RESOURCES-MIB
data = parent.get_snmp_data(host, community, blocks, 1)
+
+ if not data:
+ return
+
all_blocks = map(parent._build_answer,
[t[-1] for t in parent._extract(data, tuple, 2)],
parent._extract(data, str, 3),
View
37 tests/plugins.py
@@ -0,0 +1,37 @@
+#!/usr/bin/env python
+
+import unittest
+import logging
+
+import ecks
+
+from pprint import pprint
+
+logging.basicConfig(level=logging.FATAL)
+
+class TestEcks(unittest.TestCase):
+ def test_plugins_with_responding_host(self):
+ e = ecks.Ecks(5)
+ for plugin in e.plugins:
+ data = e.get_data("localhost", "public", plugin)
+ #self.assertTrue((data != None) and (data != []))
+ self.assertTrue(data)
+
+ def test_plugins_with_unresponding_host(self):
+ e = ecks.Ecks()
+ for plugin in e.plugins:
+ data = e.get_data("localhost", "private", plugin)
+
+ #self.assertTrue((data == None) or (data == []))
+ self.assertTrue(not data)
+
+ def test_plugins_with_unreachable_host(self):
+ e = ecks.Ecks()
+ for plugin in e.plugins:
+ data = e.get_data("127.1.1.1", "public", plugin)
+
+ #self.assertTrue((data == None) or (data == []))
+ self.assertTrue(not data)
+
+if __name__ == '__main__':
+ unittest.main()

0 comments on commit 3abb455

Please sign in to comment.
Something went wrong with that request. Please try again.