Exemplo n.º 1
0
 def setUp(self):
     os.system("rm -f /tmp/poller_last.unittest")
     os.system(
         "kill -9 $(cat /tmp/redis-unittest.pid 2>/dev/null) 2>/dev/null")
     os.system('kill -9 $(pgrep -f "redis-server ' + myPath +
               '/redis_unittest.conf")')
     os.system("redis-server " + myPath + "/redis_unittest.conf")
     os.system(
         "while ! netstat -laputn | grep 8888 > /dev/null; do true; done ")
     os.system("redis-cli -a password -p 8888 ping >/dev/null")
     os.system("redis-cli -a password -p 8888 FLUSHALL >/dev/null")
     self.poller = Poller(myPath + "/poller_unittest.cfg")
Exemplo n.º 2
0
 def setUp(self):
     os.system("rm -f /tmp/poller_last.unittest")
     os.system("kill -9 $(cat /tmp/redis-unittest.pid 2>/dev/null) 2>/dev/null")
     os.system('kill -9 $(pgrep -f "redis-server '+myPath+'/redis_unittest.conf")')
     os.system("redis-server "+myPath+"/redis_unittest.conf")
     os.system("while ! netstat -laputn | grep 8888 > /dev/null; do true; done ")
     os.system("redis-cli -a password -p 8888 ping >/dev/null")
     os.system("redis-cli -a password -p 8888 FLUSHALL >/dev/null")
     self.poller = Poller(myPath+"/poller_unittest.cfg")
Exemplo n.º 3
0
class PollerTestCase(unittest.TestCase):

    def setUp(self):
        os.system("rm -f /tmp/poller_last.unittest")
        os.system("kill -9 $(cat /tmp/redis-unittest.pid 2>/dev/null) 2>/dev/null")
        os.system('kill -9 $(pgrep -f "redis-server '+myPath+'/redis_unittest.conf")')
        os.system("redis-server "+myPath+"/redis_unittest.conf")
        os.system("while ! netstat -laputn | grep 8888 > /dev/null; do true; done ")
        os.system("redis-cli -a password -p 8888 ping >/dev/null")
        os.system("redis-cli -a password -p 8888 FLUSHALL >/dev/null")
        self.poller = Poller(myPath+"/poller_unittest.cfg")

    def tearDown(self):
        os.system("kill -9 $(cat /tmp/redis-unittest.pid)")
        os.system('kill -9 $(pgrep -f "redis-server '+myPath+'/redis_unittest.conf")')
        os.system("rm -f /tmp/poller_last.unittest")

    def test_poller_rediscleanDataExpired(self):
        # Get now TS
        now              = time.strftime("%Y %m %d %H:%M", time.localtime())
        nowTimestamp     = "%.0f" % time.mktime(time.strptime(now, '%Y %m %d %H:%M')) # "%.0f" % supprime le .0 aprés le
        # Start connexion
        self.poller._redis_connexion = self.poller.redisStartConnexion()
        # Send one new and one old TS
        self.poller._redis_connexion.redis_zadd("TimeStamp","0000000000",0000000000)
        self.poller._redis_connexion.redis_zadd("TimeStamp",nowTimestamp,nowTimestamp)
        # Check
        result = self.poller._redis_connexion.redis_zrangebyscore("TimeStamp",'-inf','+inf')
        self.assertEqual(result, ['0000000000', nowTimestamp])
        # Send one new and one old Data
        self.poller._redis_connexion.redis_zadd("DATAS","oldData",0000000000)
        self.poller._redis_connexion.redis_zadd("DATAS","newData",nowTimestamp)
        # Check
        result = self.poller._redis_connexion.redis_zrangebyscore("DATAS",'-inf','+inf')
        self.assertEqual(result, ['oldData', 'newData'])
        # Clear old data only
        self.poller.rediscleanDataExpired()
        # Check TimeStamp and DATAS
        result = self.poller._redis_connexion.redis_zrangebyscore("TimeStamp",'-inf','+inf')
        self.assertEqual(result, [nowTimestamp])
        result = self.poller._redis_connexion.redis_zrangebyscore("DATAS",'-inf','+inf')
        self.assertEqual(result, ['newData'])

    def test_poller_pollerTimeToGo(self):
        now              = time.strftime("%Y %m %d %H:%M", time.localtime())
        nowTimestamp     = "%.0f" % time.mktime(time.strptime(now, '%Y %m %d %H:%M')) # "%.0f" % supprime le .0 aprés le
        # Init
        self.poller._poller_time            = 60
        self.poller._plugins_refresh_time   = 300
        # At start refresh plugin not needed
        self.assertFalse(self.poller._need_refresh)
        # First start no file (need refresh and true)
        os.system("rm -f /tmp/poller_last.unittest")
        result = self.poller.pollerTimeToGo()
        self.assertTrue(result)
        self.assertTrue(self.poller._need_refresh)
        # Make corrupt /tmp/poller_last.unittest
        os.system(":>/tmp/poller_last.unittest")
        result = self.poller.pollerTimeToGo()
        self.assertTrue(result)
        self.assertTrue(self.poller._need_refresh)
        # Try to run quick poll (too soon)
        result = self.poller.pollerTimeToGo()
        self.assertFalse(result)
        self.assertFalse(self.poller._need_refresh)
        # change time in file for poller only, no refresh
        os.system("echo '"+str(int(nowTimestamp)-60)+" "+nowTimestamp+"' >/tmp/poller_last.unittest")
        result = self.poller.pollerTimeToGo()
        self.assertTrue(result)
        self.assertFalse(self.poller._need_refresh)
        # change time in file for poller and refresh
        os.system("echo '"+str(int(nowTimestamp)-60)+" "+str(int(nowTimestamp)-300)+"' >/tmp/poller_last.unittest")
        result = self.poller.pollerTimeToGo()
        self.assertTrue(result)
        self.assertTrue(self.poller._need_refresh)

    def test_poller_writeInfo(self):
        os.system("redis-cli -a password -p 8888 FLUSHALL >/dev/null")
        self.poller._redis_connexion = self.poller.redisStartConnexion()
        # Test good values 2 plugins
        result = self.poller.writeInfo([{'Plugin':"foo", },{'Plugin':"bar"}])
        self.assertEquals(result,['foo', 'bar'])
        result = self.poller._redis_connexion.redis_hkeys("INFOS")
        self.assertEquals(result, ['foo', 'bar'])
        result = self.poller._redis_connexion.redis_hget("INFOS","foo")
        self.assertEquals(result, '{"Plugin": "foo"}') # (json string)
        # Test with empty value
        os.system("redis-cli -a password -p 8888 FLUSHALL >/dev/null")
        result = self.poller.writeInfo([])
        self.assertEquals(result,[])
        result = self.poller._redis_connexion.redis_hkeys("INFOS")
        self.assertEquals(result, [])
        # Good value without plugin name
        self.poller.writeInfo([{'Title':"foo", }])
        result = self.poller._redis_connexion.redis_hkeys("INFOS")
        self.assertEquals(result, [])

    def test_poller_cleanInfo(self):
        self.poller._redis_connexion = self.poller.redisStartConnexion()
        pollerRedis = self.poller._redis_connexion
        # No data before (dont delete no data after)
        os.system("redis-cli -a password -p 8888 FLUSHALL >/dev/null")
        self.poller.cleanInfo([{'Plugin':"foo", },{'Plugin':"bar"}])
        result = pollerRedis.redis_hkeys("INFOS")
        self.assertEquals(result, [])
        # 2 data before and same data now
        os.system("redis-cli -a password -p 8888 FLUSHALL >/dev/null")
        pollerRedis.redis_hset("INFOS",'foo','foo')
        pollerRedis.redis_hset("INFOS",'bar','bar')
        self.poller.cleanInfo(["foo","bar"])
        result = pollerRedis.redis_hkeys("INFOS")
        self.assertEquals(result, ['foo', 'bar'])
        # 2 data before and only one now
        os.system("redis-cli -a password -p 8888 FLUSHALL >/dev/null")
        pollerRedis.redis_hset("INFOS",'foo','foo')
        pollerRedis.redis_hset("INFOS",'bar','bar')
        self.poller.cleanInfo(["foo"])
        result = pollerRedis.redis_hkeys("INFOS")
        self.assertEquals(result, ['foo'])
        # 2 data before, delete one and add a new
        os.system("redis-cli -a password -p 8888 FLUSHALL >/dev/null")
        pollerRedis.redis_hset("INFOS",'foo','foo')
        pollerRedis.redis_hset("INFOS",'bar','bar')
        self.poller.cleanInfo(["foo","gnu"])
        result = pollerRedis.redis_hkeys("INFOS")
        self.assertEquals(result, ['foo'])

    def test_poller_writeData(self):
        os.system("redis-cli -a password -p 8888 FLUSHALL >/dev/null")
        self.poller._redis_connexion = self.poller.redisStartConnexion()
        # set Empty value
        self.poller.writeData([])
        result = self.poller._redis_connexion.redis_zrangebyscore("DATAS", '-inf','+inf')
        self.assertEquals(result, [])
        result = self.poller._redis_connexion.redis_zrangebyscore("TimeStamp", '-inf','+inf')
        self.assertEquals(result, [])
        # set good value
        self.poller.writeData([{"TimeStamp": "1328624580", "Values": {"entropy": "146"}, "Plugin": "entropy"},
                                {"TimeStamp": "1328624760", "Values": {"load": "0.00"}, "Plugin": "load"}])
        result = self.poller._redis_connexion.redis_zrangebyscore("DATAS", '-inf','+inf')
        self.assertEquals(result, ['{"TimeStamp": "1328624580", "Values": {"entropy": "146"}, "Plugin": "entropy"}',
                                   '{"TimeStamp": "1328624760", "Values": {"load": "0.00"}, "Plugin": "load"}'])
        result = self.poller._redis_connexion.redis_zrangebyscore("TimeStamp", '-inf','+inf')
        self.assertEquals(result, ['1328624580', '1328624760'])
        # send value without Plugin name
        os.system("redis-cli -a password -p 8888 FLUSHALL >/dev/null")
        self.poller.writeData([{"TimeStamp": "1328624580", "Values": {"entropy": "146"}}])
        result = self.poller._redis_connexion.redis_zrangebyscore("DATAS", '-inf','+inf')
        self.assertEquals(result, [])
        result = self.poller._redis_connexion.redis_zrangebyscore("TimeStamp", '-inf','+inf')
        self.assertEquals(result, [])
        # set good value but non integer timestamp
        os.system("redis-cli -a password -p 8888 FLUSHALL >/dev/null")
        self.poller.writeData([{"TimeStamp": "error", "Plugin": "entropy"}])
        result = self.poller._redis_connexion.redis_zrangebyscore("DATAS", '-inf','+inf')
        self.assertEquals(result, [])
        result = self.poller._redis_connexion.redis_zrangebyscore("TimeStamp", '-inf','+inf')
        self.assertEquals(result, [])


    def test_poller_redisStartConnexion(self):
        # Connect with 0
        self.poller._redis_db = 0
        zeroConnect = self.poller.redisStartConnexion()
        zeroConnect.redis_hset("DB","foo","bar0")
        # Connect with 1
        self.poller._redis_db = 1
        oneConnect = self.poller.redisStartConnexion()
        oneConnect.redis_hset("DB","foo","bar1")
        # Check
        result = zeroConnect.redis_hget("DB","foo")
        self.assertEquals(result, "bar0")
        result = oneConnect.redis_hget("DB","foo")
        self.assertEquals(result, "bar1")
Exemplo n.º 4
0
class PollerTestCase(unittest.TestCase):
    def setUp(self):
        os.system("rm -f /tmp/poller_last.unittest")
        os.system(
            "kill -9 $(cat /tmp/redis-unittest.pid 2>/dev/null) 2>/dev/null")
        os.system('kill -9 $(pgrep -f "redis-server ' + myPath +
                  '/redis_unittest.conf")')
        os.system("redis-server " + myPath + "/redis_unittest.conf")
        os.system(
            "while ! netstat -laputn | grep 8888 > /dev/null; do true; done ")
        os.system("redis-cli -a password -p 8888 ping >/dev/null")
        os.system("redis-cli -a password -p 8888 FLUSHALL >/dev/null")
        self.poller = Poller(myPath + "/poller_unittest.cfg")

    def tearDown(self):
        os.system("kill -9 $(cat /tmp/redis-unittest.pid)")
        os.system('kill -9 $(pgrep -f "redis-server ' + myPath +
                  '/redis_unittest.conf")')
        os.system("rm -f /tmp/poller_last.unittest")

    def test_poller_rediscleanDataExpired(self):
        # Get now TS
        now = time.strftime("%Y %m %d %H:%M", time.localtime())
        nowTimestamp = "%.0f" % time.mktime(
            time.strptime(
                now, '%Y %m %d %H:%M'))  # "%.0f" % supprime le .0 aprés le
        # Start connexion
        self.poller._redis_connexion = self.poller.redisStartConnexion()
        # Send one new and one old TS
        self.poller._redis_connexion.redis_zadd("TimeStamp", "0000000000",
                                                0000000000)
        self.poller._redis_connexion.redis_zadd("TimeStamp", nowTimestamp,
                                                nowTimestamp)
        # Check
        result = self.poller._redis_connexion.redis_zrangebyscore(
            "TimeStamp", '-inf', '+inf')
        self.assertEqual(result, ['0000000000', nowTimestamp])
        # Send one new and one old Data
        self.poller._redis_connexion.redis_zadd("DATAS", "oldData", 0000000000)
        self.poller._redis_connexion.redis_zadd("DATAS", "newData",
                                                nowTimestamp)
        # Check
        result = self.poller._redis_connexion.redis_zrangebyscore(
            "DATAS", '-inf', '+inf')
        self.assertEqual(result, ['oldData', 'newData'])
        # Clear old data only
        self.poller.rediscleanDataExpired()
        # Check TimeStamp and DATAS
        result = self.poller._redis_connexion.redis_zrangebyscore(
            "TimeStamp", '-inf', '+inf')
        self.assertEqual(result, [nowTimestamp])
        result = self.poller._redis_connexion.redis_zrangebyscore(
            "DATAS", '-inf', '+inf')
        self.assertEqual(result, ['newData'])

    def test_poller_pollerTimeToGo(self):
        now = time.strftime("%Y %m %d %H:%M", time.localtime())
        nowTimestamp = "%.0f" % time.mktime(
            time.strptime(
                now, '%Y %m %d %H:%M'))  # "%.0f" % supprime le .0 aprés le
        # Init
        self.poller._poller_time = 60
        self.poller._plugins_refresh_time = 300
        # At start refresh plugin not needed
        self.assertFalse(self.poller._need_refresh)
        # First start no file (need refresh and true)
        os.system("rm -f /tmp/poller_last.unittest")
        result = self.poller.pollerTimeToGo()
        self.assertTrue(result)
        self.assertTrue(self.poller._need_refresh)
        # Make corrupt /tmp/poller_last.unittest
        os.system(":>/tmp/poller_last.unittest")
        result = self.poller.pollerTimeToGo()
        self.assertTrue(result)
        self.assertTrue(self.poller._need_refresh)
        # Try to run quick poll (too soon)
        result = self.poller.pollerTimeToGo()
        self.assertFalse(result)
        self.assertFalse(self.poller._need_refresh)
        # change time in file for poller only, no refresh
        os.system("echo '" + str(int(nowTimestamp) - 60) + " " + nowTimestamp +
                  "' >/tmp/poller_last.unittest")
        result = self.poller.pollerTimeToGo()
        self.assertTrue(result)
        self.assertFalse(self.poller._need_refresh)
        # change time in file for poller and refresh
        os.system("echo '" + str(int(nowTimestamp) - 60) + " " +
                  str(int(nowTimestamp) - 300) +
                  "' >/tmp/poller_last.unittest")
        result = self.poller.pollerTimeToGo()
        self.assertTrue(result)
        self.assertTrue(self.poller._need_refresh)

    def test_poller_writeInfo(self):
        os.system("redis-cli -a password -p 8888 FLUSHALL >/dev/null")
        self.poller._redis_connexion = self.poller.redisStartConnexion()
        # Test good values 2 plugins
        result = self.poller.writeInfo([{
            'Plugin': "foo",
        }, {
            'Plugin': "bar"
        }])
        self.assertEquals(result, ['foo', 'bar'])
        result = self.poller._redis_connexion.redis_hkeys("INFOS")
        self.assertEquals(result, ['foo', 'bar'])
        result = self.poller._redis_connexion.redis_hget("INFOS", "foo")
        self.assertEquals(result, '{"Plugin": "foo"}')  # (json string)
        # Test with empty value
        os.system("redis-cli -a password -p 8888 FLUSHALL >/dev/null")
        result = self.poller.writeInfo([])
        self.assertEquals(result, [])
        result = self.poller._redis_connexion.redis_hkeys("INFOS")
        self.assertEquals(result, [])
        # Good value without plugin name
        self.poller.writeInfo([{
            'Title': "foo",
        }])
        result = self.poller._redis_connexion.redis_hkeys("INFOS")
        self.assertEquals(result, [])

    def test_poller_cleanInfo(self):
        self.poller._redis_connexion = self.poller.redisStartConnexion()
        pollerRedis = self.poller._redis_connexion
        # No data before (dont delete no data after)
        os.system("redis-cli -a password -p 8888 FLUSHALL >/dev/null")
        self.poller.cleanInfo([{
            'Plugin': "foo",
        }, {
            'Plugin': "bar"
        }])
        result = pollerRedis.redis_hkeys("INFOS")
        self.assertEquals(result, [])
        # 2 data before and same data now
        os.system("redis-cli -a password -p 8888 FLUSHALL >/dev/null")
        pollerRedis.redis_hset("INFOS", 'foo', 'foo')
        pollerRedis.redis_hset("INFOS", 'bar', 'bar')
        self.poller.cleanInfo(["foo", "bar"])
        result = pollerRedis.redis_hkeys("INFOS")
        self.assertEquals(result, ['foo', 'bar'])
        # 2 data before and only one now
        os.system("redis-cli -a password -p 8888 FLUSHALL >/dev/null")
        pollerRedis.redis_hset("INFOS", 'foo', 'foo')
        pollerRedis.redis_hset("INFOS", 'bar', 'bar')
        self.poller.cleanInfo(["foo"])
        result = pollerRedis.redis_hkeys("INFOS")
        self.assertEquals(result, ['foo'])
        # 2 data before, delete one and add a new
        os.system("redis-cli -a password -p 8888 FLUSHALL >/dev/null")
        pollerRedis.redis_hset("INFOS", 'foo', 'foo')
        pollerRedis.redis_hset("INFOS", 'bar', 'bar')
        self.poller.cleanInfo(["foo", "gnu"])
        result = pollerRedis.redis_hkeys("INFOS")
        self.assertEquals(result, ['foo'])

    def test_poller_writeData(self):
        os.system("redis-cli -a password -p 8888 FLUSHALL >/dev/null")
        self.poller._redis_connexion = self.poller.redisStartConnexion()
        # set Empty value
        self.poller.writeData([])
        result = self.poller._redis_connexion.redis_zrangebyscore(
            "DATAS", '-inf', '+inf')
        self.assertEquals(result, [])
        result = self.poller._redis_connexion.redis_zrangebyscore(
            "TimeStamp", '-inf', '+inf')
        self.assertEquals(result, [])
        # set good value
        self.poller.writeData([{
            "TimeStamp": "1328624580",
            "Values": {
                "entropy": "146"
            },
            "Plugin": "entropy"
        }, {
            "TimeStamp": "1328624760",
            "Values": {
                "load": "0.00"
            },
            "Plugin": "load"
        }])
        result = self.poller._redis_connexion.redis_zrangebyscore(
            "DATAS", '-inf', '+inf')
        self.assertEquals(result, [
            '{"TimeStamp": "1328624580", "Values": {"entropy": "146"}, "Plugin": "entropy"}',
            '{"TimeStamp": "1328624760", "Values": {"load": "0.00"}, "Plugin": "load"}'
        ])
        result = self.poller._redis_connexion.redis_zrangebyscore(
            "TimeStamp", '-inf', '+inf')
        self.assertEquals(result, ['1328624580', '1328624760'])
        # send value without Plugin name
        os.system("redis-cli -a password -p 8888 FLUSHALL >/dev/null")
        self.poller.writeData([{
            "TimeStamp": "1328624580",
            "Values": {
                "entropy": "146"
            }
        }])
        result = self.poller._redis_connexion.redis_zrangebyscore(
            "DATAS", '-inf', '+inf')
        self.assertEquals(result, [])
        result = self.poller._redis_connexion.redis_zrangebyscore(
            "TimeStamp", '-inf', '+inf')
        self.assertEquals(result, [])
        # set good value but non integer timestamp
        os.system("redis-cli -a password -p 8888 FLUSHALL >/dev/null")
        self.poller.writeData([{"TimeStamp": "error", "Plugin": "entropy"}])
        result = self.poller._redis_connexion.redis_zrangebyscore(
            "DATAS", '-inf', '+inf')
        self.assertEquals(result, [])
        result = self.poller._redis_connexion.redis_zrangebyscore(
            "TimeStamp", '-inf', '+inf')
        self.assertEquals(result, [])

    def test_poller_redisStartConnexion(self):
        # Connect with 0
        self.poller._redis_db = 0
        zeroConnect = self.poller.redisStartConnexion()
        zeroConnect.redis_hset("DB", "foo", "bar0")
        # Connect with 1
        self.poller._redis_db = 1
        oneConnect = self.poller.redisStartConnexion()
        oneConnect.redis_hset("DB", "foo", "bar1")
        # Check
        result = zeroConnect.redis_hget("DB", "foo")
        self.assertEquals(result, "bar0")
        result = oneConnect.redis_hget("DB", "foo")
        self.assertEquals(result, "bar1")
Exemplo n.º 5
0
    def test_mastertest_collect(self):
        # Init
        self.poller = Poller(myPath+"/poller_unittest.cfg")
        self.collector = myCollector(myPath+"/collector_unittest.cfg")
        self.storage = Storage(myPath+"/storage_unittest.cfg")
        self.collector._logger = myFakeLogger()
        self.poller._logger = myFakeLogger()
        #
        # Poller
        #
        hostname = socket.gethostname()
        hostID   = "123456"
        self.poller.startPoller()
        pollerRedis = self.poller.redisStartConnexion()
        # Check infos keys
        result = pollerRedis.redis_hkeys("INFOS")
        P_KEYS = ['foo_unittest', 'bar_unittest', 'MyInfo']
        self.assertEquals(sorted(result),sorted(P_KEYS))
        # Check info foo

        result = pollerRedis.redis_hget("INFOS",'foo_unittest')
        P_INFO_foo = '{"Describ": "", "Title": "foo_test", "Plugin": "foo_unittest", "Vlabel": "foo vlabel", "Base": "1000", "Infos": {"foo": {"draw": "AREA", "id": "foo", "label": "foo label"}}, "Order": "foo"}'
        self.assertEquals(result,P_INFO_foo)
        # Check info bar
        result = pollerRedis.redis_hget("INFOS",'bar_unittest')
        P_INFO_bar = '{"Describ": "", "Title": "bar gnu test", "Plugin": "bar_unittest", "Vlabel": "bar vlabel", "Base": "1024", "Infos": {"bar": {"draw": "LINE", "type": "COUNTER", "id": "bar", "label": "bar label"}, "gnu": {"id": "gnu"}}, "Order": "gnu bar"}'
        self.assertEquals(result,P_INFO_bar)
        # Check info MyInfos
        result = pollerRedis.redis_hget("INFOS",'MyInfo')
        P_INFO_myinfo = '{"Description": "", "ID": "'+hostID+'", "Name": "'+hostname+'", "Plugin": "MyInfo"}'
        C_INFO_myinfo = '{"Address": "127.0.0.1", "Name": "'+hostname+'", "Plugin": "MyInfo", "Description": "", "ID": "'+hostID+'"}'
        self.assertEquals(result,P_INFO_myinfo)
        # Check des TimeStamp
        result = pollerRedis.redis_zrangebyscore("TimeStamp",'-inf','+inf')
        self.assertTrue(re.match("[0-9]+", result[0]))
        P_TS = result[0]
        # Check des datas
        result = pollerRedis.redis_zrangebyscore("DATAS",'-inf','+inf')
        P_DATAS = ['{"TimeStamp": "'+P_TS+'", "Values": {"bar": "-4.2", "gnu": "4.2"}, "Plugin": "bar_unittest"}', '{"TimeStamp": "'+P_TS+'", "Values": {"foo": "42"}, "Plugin": "foo_unittest"}']
        self.assertEquals(result,P_DATAS)

        #
        # Collector
        #
        os.system("echo '127.0.0.1:0:password' > /tmp/poller-list.unittest")
        self.collector.startCollector()
        self.collector._redis_server_db = 1
        collectorRedis = self.collector.redisStartConnexion()
        # Check HOSTS
        result = collectorRedis.redis_hkeys("HOSTS")
        self.assertEquals(result,['127.0.0.1'])
        # Check TS@host
        result = collectorRedis.redis_zrangebyscore("TS@"+hostID, '-inf', '+inf')
        self.assertEquals(result,[P_TS])
        # Check DATAS@host
        result = collectorRedis.redis_zrangebyscore("DATAS@"+hostID, '-inf', '+inf')
        self.assertEquals(result,P_DATAS)
        # Check INFOS@host
        result = collectorRedis.redis_hkeys("INFOS@"+hostID)
        self.assertEquals(sorted(result),sorted(P_KEYS))
        result = collectorRedis.redis_hget("INFOS@"+hostID,"bar_unittest")
        self.assertEquals(result, P_INFO_bar)
        result = collectorRedis.redis_hget("INFOS@"+hostID,"foo_unittest")
        self.assertEquals(result, P_INFO_foo)
        result = collectorRedis.redis_hget("INFOS@"+hostID,"MyInfo")
        self.assertEquals(result, C_INFO_myinfo)

        # Check SERVER TS in poller
        result = pollerRedis.redis_hget("SERVER","unittest")
        self.assertEquals(result,P_TS)

        #
        # Storage
        #
        os.system("rm -Rf /tmp/numeter_rrds")
        os.system("echo '127.0.0.1:1:password' > /tmp/collector-list.unittest")
        self.storage.startStorage()
        self.storage._redis_server_db = 2
        storageRedis = self.storage.redisStartConnexion()
        # Check HOSTS
        S_INFO_myinfo = { hostID: '{"Description": "", "Plugin": "MyInfo", "HostIDHash": "e1", "HostIDFiltredName": "'+hostID+'", "Address": "127.0.0.1", "ID": "'+hostID+'", "Name": "'+hostname+'"}'}
        result = storageRedis.redis_hgetall("HOSTS")
        self.assertEquals(result, S_INFO_myinfo)

        # Check HOST_ID hash
        S_HOST_ID =  {hostID: 'e1'}
        result = storageRedis.redis_hgetall("HOST_ID")
        self.assertEquals(result, S_HOST_ID)
        # Check RRD_PATH
        S_RRD_PATH = {hostID: '/tmp/numeter_rrds/e1/'+hostID}
        result = storageRedis.redis_hgetall("RRD_PATH")
        self.assertEquals(result, S_RRD_PATH)
        # Check INFOS@host
        result = storageRedis.redis_hkeys("INFOS@"+hostID)
        P_KEYS_without_myinfo = P_KEYS[:]
        P_KEYS_without_myinfo.remove("MyInfo")
        self.assertEquals(sorted(result),sorted(P_KEYS_without_myinfo))
        result = storageRedis.redis_hget("INFOS@"+hostID,"bar_unittest")
        self.assertEquals(result, P_INFO_bar)
        result = storageRedis.redis_hget("INFOS@"+hostID,"foo_unittest")
        self.assertEquals(result, P_INFO_foo)
        result = storageRedis.redis_hget("HOSTS",hostID)
        self.assertEquals(result, S_INFO_myinfo[hostID])
        # Test rrd write
        result = os.path.isfile(S_RRD_PATH[hostID]+"/foo_unittest/foo.rrd")
        self.assertTrue(result)
        result = rrdtool.info(S_RRD_PATH[hostID]+"/foo_unittest/foo.rrd")
        self.assertEqual(result['last_update'], int(P_TS))
        self.assertEqual(result['ds[42].last_ds'], "42")
        self.assertEqual(result['ds[42].type'], "GAUGE")
        #
        result = os.path.isfile(S_RRD_PATH[hostID]+"/bar_unittest/gnu.rrd")
        self.assertTrue(result)
        result = os.path.isfile(S_RRD_PATH[hostID]+"/bar_unittest/bar.rrd")
        self.assertTrue(result)
        result = rrdtool.info(S_RRD_PATH[hostID]+"/bar_unittest/gnu.rrd")
        self.assertEqual(result['last_update'], int(P_TS))
        self.assertEqual(result['ds[42].last_ds'], "4.2")
        self.assertEqual(result['ds[42].type'], "GAUGE")
        result = rrdtool.info(S_RRD_PATH[hostID]+"/bar_unittest/bar.rrd")
        self.assertEqual(result['last_update'], int(P_TS))
        self.assertEqual(result['ds[42].last_ds'], "-4.2")
        self.assertEqual(result['ds[42].type'], "COUNTER")
        # Test clear db collecteur
        result = collectorRedis.redis_zrangebyscore("TS@"+hostID,'+inf','+inf')
        self.assertEqual(result, [])
        result = collectorRedis.redis_zrangebyscore("DATAS@"+hostID,'+inf','+inf')
        self.assertEqual(result, [])
Exemplo n.º 6
0
class MasterTestCase(unittest.TestCase):

    def get_init_munin(self):
        # Start munin
        process = subprocess.Popen(myPath+"/mastertest_makeMuninScript.sh", shell=True, stdout=subprocess.PIPE)
        (result, stderr) =  process.communicate()
        self.assertEquals(result,"True")

    def setUp(self):
        os.system("rm -f /tmp/poller_last.unittest")
        os.system("kill -9 $(cat /tmp/redis-unittest.pid 2>/dev/null) 2>/dev/null")
        os.system('kill -9 $(pgrep -f "redis-server '+myPath+'/redis_unittest.conf")')
        os.system("redis-server "+myPath+"/redis_unittest.conf")
        os.system("while ! netstat -laputn | grep 8888 > /dev/null; do true; done ")
        os.system("redis-cli -a password -p 8888 ping >/dev/null")
        os.system("redis-cli -a password -p 8888 FLUSHALL >/dev/null")
        self.get_init_munin()


    def tearDown(self):
        os.system("kill -9 $(cat /tmp/redis-unittest.pid)")
        os.system('kill -9 $(pgrep -f "redis-server '+myPath+'/redis_unittest.conf")')
        os.system("rm -f /etc/munin/plugins/bar_unittest")
        os.system("rm -f /etc/munin/plugins/foo_unittest")
        os.system("/etc/init.d/munin-node restart >/dev/null 2>/dev/null")
        os.system("rm -f /tmp/poller_last.unittest")

    def test_mastertest_collect(self):
        # Init
        self.poller = Poller(myPath+"/poller_unittest.cfg")
        self.collector = myCollector(myPath+"/collector_unittest.cfg")
        self.storage = Storage(myPath+"/storage_unittest.cfg")
        self.collector._logger = myFakeLogger()
        self.poller._logger = myFakeLogger()
        #
        # Poller
        #
        hostname = socket.gethostname()
        hostID   = "123456"
        self.poller.startPoller()
        pollerRedis = self.poller.redisStartConnexion()
        # Check infos keys
        result = pollerRedis.redis_hkeys("INFOS")
        P_KEYS = ['foo_unittest', 'bar_unittest', 'MyInfo']
        self.assertEquals(sorted(result),sorted(P_KEYS))
        # Check info foo

        result = pollerRedis.redis_hget("INFOS",'foo_unittest')
        P_INFO_foo = '{"Describ": "", "Title": "foo_test", "Plugin": "foo_unittest", "Vlabel": "foo vlabel", "Base": "1000", "Infos": {"foo": {"draw": "AREA", "id": "foo", "label": "foo label"}}, "Order": "foo"}'
        self.assertEquals(result,P_INFO_foo)
        # Check info bar
        result = pollerRedis.redis_hget("INFOS",'bar_unittest')
        P_INFO_bar = '{"Describ": "", "Title": "bar gnu test", "Plugin": "bar_unittest", "Vlabel": "bar vlabel", "Base": "1024", "Infos": {"bar": {"draw": "LINE", "type": "COUNTER", "id": "bar", "label": "bar label"}, "gnu": {"id": "gnu"}}, "Order": "gnu bar"}'
        self.assertEquals(result,P_INFO_bar)
        # Check info MyInfos
        result = pollerRedis.redis_hget("INFOS",'MyInfo')
        P_INFO_myinfo = '{"Description": "", "ID": "'+hostID+'", "Name": "'+hostname+'", "Plugin": "MyInfo"}'
        C_INFO_myinfo = '{"Address": "127.0.0.1", "Name": "'+hostname+'", "Plugin": "MyInfo", "Description": "", "ID": "'+hostID+'"}'
        self.assertEquals(result,P_INFO_myinfo)
        # Check des TimeStamp
        result = pollerRedis.redis_zrangebyscore("TimeStamp",'-inf','+inf')
        self.assertTrue(re.match("[0-9]+", result[0]))
        P_TS = result[0]
        # Check des datas
        result = pollerRedis.redis_zrangebyscore("DATAS",'-inf','+inf')
        P_DATAS = ['{"TimeStamp": "'+P_TS+'", "Values": {"bar": "-4.2", "gnu": "4.2"}, "Plugin": "bar_unittest"}', '{"TimeStamp": "'+P_TS+'", "Values": {"foo": "42"}, "Plugin": "foo_unittest"}']
        self.assertEquals(result,P_DATAS)

        #
        # Collector
        #
        os.system("echo '127.0.0.1:0:password' > /tmp/poller-list.unittest")
        self.collector.startCollector()
        self.collector._redis_server_db = 1
        collectorRedis = self.collector.redisStartConnexion()
        # Check HOSTS
        result = collectorRedis.redis_hkeys("HOSTS")
        self.assertEquals(result,['127.0.0.1'])
        # Check TS@host
        result = collectorRedis.redis_zrangebyscore("TS@"+hostID, '-inf', '+inf')
        self.assertEquals(result,[P_TS])
        # Check DATAS@host
        result = collectorRedis.redis_zrangebyscore("DATAS@"+hostID, '-inf', '+inf')
        self.assertEquals(result,P_DATAS)
        # Check INFOS@host
        result = collectorRedis.redis_hkeys("INFOS@"+hostID)
        self.assertEquals(sorted(result),sorted(P_KEYS))
        result = collectorRedis.redis_hget("INFOS@"+hostID,"bar_unittest")
        self.assertEquals(result, P_INFO_bar)
        result = collectorRedis.redis_hget("INFOS@"+hostID,"foo_unittest")
        self.assertEquals(result, P_INFO_foo)
        result = collectorRedis.redis_hget("INFOS@"+hostID,"MyInfo")
        self.assertEquals(result, C_INFO_myinfo)

        # Check SERVER TS in poller
        result = pollerRedis.redis_hget("SERVER","unittest")
        self.assertEquals(result,P_TS)

        #
        # Storage
        #
        os.system("rm -Rf /tmp/numeter_rrds")
        os.system("echo '127.0.0.1:1:password' > /tmp/collector-list.unittest")
        self.storage.startStorage()
        self.storage._redis_server_db = 2
        storageRedis = self.storage.redisStartConnexion()
        # Check HOSTS
        S_INFO_myinfo = { hostID: '{"Description": "", "Plugin": "MyInfo", "HostIDHash": "e1", "HostIDFiltredName": "'+hostID+'", "Address": "127.0.0.1", "ID": "'+hostID+'", "Name": "'+hostname+'"}'}
        result = storageRedis.redis_hgetall("HOSTS")
        self.assertEquals(result, S_INFO_myinfo)

        # Check HOST_ID hash
        S_HOST_ID =  {hostID: 'e1'}
        result = storageRedis.redis_hgetall("HOST_ID")
        self.assertEquals(result, S_HOST_ID)
        # Check RRD_PATH
        S_RRD_PATH = {hostID: '/tmp/numeter_rrds/e1/'+hostID}
        result = storageRedis.redis_hgetall("RRD_PATH")
        self.assertEquals(result, S_RRD_PATH)
        # Check INFOS@host
        result = storageRedis.redis_hkeys("INFOS@"+hostID)
        P_KEYS_without_myinfo = P_KEYS[:]
        P_KEYS_without_myinfo.remove("MyInfo")
        self.assertEquals(sorted(result),sorted(P_KEYS_without_myinfo))
        result = storageRedis.redis_hget("INFOS@"+hostID,"bar_unittest")
        self.assertEquals(result, P_INFO_bar)
        result = storageRedis.redis_hget("INFOS@"+hostID,"foo_unittest")
        self.assertEquals(result, P_INFO_foo)
        result = storageRedis.redis_hget("HOSTS",hostID)
        self.assertEquals(result, S_INFO_myinfo[hostID])
        # Test rrd write
        result = os.path.isfile(S_RRD_PATH[hostID]+"/foo_unittest/foo.rrd")
        self.assertTrue(result)
        result = rrdtool.info(S_RRD_PATH[hostID]+"/foo_unittest/foo.rrd")
        self.assertEqual(result['last_update'], int(P_TS))
        self.assertEqual(result['ds[42].last_ds'], "42")
        self.assertEqual(result['ds[42].type'], "GAUGE")
        #
        result = os.path.isfile(S_RRD_PATH[hostID]+"/bar_unittest/gnu.rrd")
        self.assertTrue(result)
        result = os.path.isfile(S_RRD_PATH[hostID]+"/bar_unittest/bar.rrd")
        self.assertTrue(result)
        result = rrdtool.info(S_RRD_PATH[hostID]+"/bar_unittest/gnu.rrd")
        self.assertEqual(result['last_update'], int(P_TS))
        self.assertEqual(result['ds[42].last_ds'], "4.2")
        self.assertEqual(result['ds[42].type'], "GAUGE")
        result = rrdtool.info(S_RRD_PATH[hostID]+"/bar_unittest/bar.rrd")
        self.assertEqual(result['last_update'], int(P_TS))
        self.assertEqual(result['ds[42].last_ds'], "-4.2")
        self.assertEqual(result['ds[42].type'], "COUNTER")
        # Test clear db collecteur
        result = collectorRedis.redis_zrangebyscore("TS@"+hostID,'+inf','+inf')
        self.assertEqual(result, [])
        result = collectorRedis.redis_zrangebyscore("DATAS@"+hostID,'+inf','+inf')
        self.assertEqual(result, [])