#!/usr/bin/env python2.7

import os
from time import sleep, time
import socket

from dt_utils.status import read_status
import redis

hostname = socket.gethostname()
hostname = hostname[:hostname.find('.')]


# Redis is <host> -> <service> -> <state>
#   Where state is:
#     <status>:<time>
#   Where status is:
#     up, down, flap
#   Where time is:
#     integer representing time in status

def monitor(root_path, host, port=6379, db=0):
    services = os.listdir(root_path)
    r = redis.StrictRedis(host=host, port=port, db=db)
    host_storage_name = 'dt-monitor:host:' + hostname
    r.sadd('dt-monitor:hosts', host_storage_name)

    flapping = {}
    while True:
        now = int(time())
        states = {}
        for service in services:
            status = read_status(os.path.join(root_path, service))
            up_string = 'up'
            if not status.up:
                up_string = 'down'
            if now - status.tai < 10:
                if service in flapping:
                    up_string = 'flap'
                flapping[service] = True
            else:
                if service in flapping:
                    del flapping[service]

            states[service] = "{0}:{1}".format(up_string, status.tai)
        r.hmset(host_storage_name, states)
        if not r.sismember('dt-monitor:hosts', host_storage_name):
            r.sadd('dt-monitor:hosts', host_storage_name)
        sleep(10)


if __name__ == "__main__":
    import sys
    monitor(sys.argv[1], sys.argv[2], int(sys.argv[3]))

@@ -660,19 +660,59 @@
     
-while True:
+states = %7B%7D%0A    while True:%0A        new_states = %7B%7D
 %0A   
@@ -738,28 +738,8 @@
 ())%0A
-        states = %7B%7D%0A
     
@@ -1208,18 +1208,15 @@
 tate
-s%5Bservice%5D
+_string
  = %22
@@ -1254,16 +1254,214 @@
 us.tai)%0A
+            if service in states:%0A                if states%5Bservice%5D == state_string:%0A                    continue%0A            new_states%5Bservice%5D = state_string%0A        if len(new_states) %3E 0:%0A    
         
@@ -1491,16 +1491,20 @@
 me, 
+new_
 states)%0A
     
@@ -1499,16 +1499,843 @@
 states)%0A
+            for service, state_string in new_states.items():%0A                states%5Bservice%5D = state_string%0A                if state_string.startswith('up:'):%0A                    r.srem('dt-monitor:services:down', host_storage_name + ':' + service)%0A                    r.srem('dt-monitor:services:flap', host_storage_name + ':' + service)%0A                elif state_string.startswith('down:'):%0A                    r.sadd('dt-monitor:services:down', host_storage_name + ':' + service)%0A                    r.srem('dt-monitor:services:flap', host_storage_name + ':' + service)%0A                elif state_string.startswith('flap:'):%0A                    r.sadd('dt-monitor:services:flap', host_storage_name + ':' + service)%0A                    r.srem('dt-monitor:services:down', host_storage_name + ':' + service)%0A        sleep(10)%0A
         
@@ -2459,25 +2459,31 @@
         
-sleep(10)
+    states = %7B%7D
 %0A%0A%0Aif __

De-normalize the states a bit