]> granicus.if.org Git - pdns/commitdiff
rec: Add regression tests for SNMP
authorRemi Gacogne <remi.gacogne@powerdns.com>
Wed, 6 Jun 2018 15:15:51 +0000 (17:15 +0200)
committerRemi Gacogne <remi.gacogne@powerdns.com>
Wed, 4 Jul 2018 08:46:55 +0000 (10:46 +0200)
(cherry picked from commit aa7a54c910f03af7c71a9bec0e4f8afda320aeb3)

build-scripts/travis.sh
regression-tests.recursor-dnssec/requirements.txt
regression-tests.recursor-dnssec/snmpd.conf [new file with mode: 0644]
regression-tests.recursor-dnssec/test_SNMP.py [new file with mode: 0644]

index 8ac1347a71f74b7f358b3eca347b316f1433d331..fdd7c3710acefbabfab4971a0d95dcf5deaab640 100755 (executable)
@@ -343,6 +343,12 @@ install_recursor() {
   run "sudo touch /etc/authbind/byport/53"
   run "sudo chmod 755 /etc/authbind/byport/53"
   run "cd ${TRAVIS_BUILD_DIR}"
+  # install SNMP
+  run "sudo sed -i \"s/agentxperms 0700 0755 recursor/agentxperms 0700 0755 ${USER}/g\" regression-tests.recursor-dnssec/snmpd.conf"
+  run "sudo cp -f regression-tests.recursor-dnssec/snmpd.conf /etc/snmp/snmpd.conf"
+  run "sudo service snmpd restart"
+  ## fun story, the directory perms are only applied if it doesn't exist yet, and it is created by the init script, so..
+  run "sudo chmod 0755 /var/agentx"
 }
 
 install_dnsdist() {
index 67690fba379c88f7ddda9f36cc645653691d3ddd..8bd274ed75f571fcd7575d0d8e2d73a13bee7df8 100644 (file)
@@ -1,3 +1,4 @@
 dnspython>=1.11
 nose>=1.3.7
+pysnmp>=4.3.4
 Twisted>0.15.0
diff --git a/regression-tests.recursor-dnssec/snmpd.conf b/regression-tests.recursor-dnssec/snmpd.conf
new file mode 100644 (file)
index 0000000..b881dd7
--- /dev/null
@@ -0,0 +1,13 @@
+
+# act as an Agent X master so that the recursor can export SNMP statistics
+master agentx
+
+# allow the recursor to connect to the Agent X master socket
+agentxperms 0700 0755 recursor
+
+# SNMPv2c community
+rocommunity secretcommunity
+
+# SNMPv3 user
+createUser secretuser SHA "mysecretauthkey" AES "mysecretenckey"
+rouser secretuser
diff --git a/regression-tests.recursor-dnssec/test_SNMP.py b/regression-tests.recursor-dnssec/test_SNMP.py
new file mode 100644 (file)
index 0000000..df68882
--- /dev/null
@@ -0,0 +1,78 @@
+import time
+
+from pysnmp.hlapi import *
+
+from recursortests import RecursorTest
+
+class TestSNMP(RecursorTest):
+
+    _snmpTimeout = 2.0
+    _snmpServer = '127.0.0.1'
+    _snmpPort = 161
+    _snmpV2Community = 'secretcommunity'
+    _snmpV3User = 'secretuser'
+    _snmpV3AuthKey = 'mysecretauthkey'
+    _snmpV3EncKey = 'mysecretenckey'
+    _snmpOID = '1.3.6.1.4.1.43315.2'
+    _queriesSent = 0
+    _confdir = 'SNMP'
+    _config_template = """
+    snmp-agent=yes
+    """
+
+    def _checkStatsValues(self, results):
+        for i in list(range(1, 93)):
+            oid = self._snmpOID + '.1.' + str(i) + '.0'
+            self.assertTrue(oid in results)
+            self.assertTrue(isinstance(results[oid], Counter64))
+
+        # check uptime > 0
+        self.assertGreater(results['1.3.6.1.4.1.43315.2.1.75.0'], 0)
+        # check memory usage > 0
+        self.assertGreater(results['1.3.6.1.4.1.43315.2.1.76.0'], 0)
+
+    def _getSNMPStats(self, auth):
+        results = {}
+        for (errorIndication, errorStatus, errorIndex, varBinds) in nextCmd(SnmpEngine(),
+                                                                            auth,
+                                                                            UdpTransportTarget((self._snmpServer, self._snmpPort), timeout=self._snmpTimeout),
+                                                                            ContextData(),
+                                                                            ObjectType(ObjectIdentity(self._snmpOID)),
+                                                                            lookupMib=False):
+            self.assertFalse(errorIndication)
+            self.assertFalse(errorStatus)
+            self.assertTrue(varBinds)
+            for key, value in varBinds:
+                keystr = key.prettyPrint()
+                if not keystr.startswith(self._snmpOID):
+                    continue
+                results[keystr] = value
+
+        return results
+
+    def _checkStats(self, auth):
+        # wait 1s so that the uptime is > 0
+        time.sleep(1)
+
+        results = self._getSNMPStats(auth)
+        self._checkStatsValues(results)
+
+    def testSNMPv2Stats(self):
+        """
+        SNMP: Retrieve statistics via SNMPv2c
+        """
+
+        auth = CommunityData(self._snmpV2Community, mpModel=1)
+        self._checkStats(auth)
+
+    def testSNMPv3Stats(self):
+        """
+        SNMP: Retrieve statistics via SNMPv3
+        """
+
+        auth = UsmUserData(self._snmpV3User,
+                               authKey=self._snmpV3AuthKey,
+                               privKey=self._snmpV3EncKey,
+                               authProtocol=usmHMACSHAAuthProtocol,
+                               privProtocol=usmAesCfb128Protocol)
+        self._checkStats(auth)