def run(self):
        while 1:
            if self.shutdown_flag.isSet():
                self.logger.info("FeederThread shutting down")
                break

            self.logger.info("%s schedules, %s traceroutes to process" % (self.currentQueueCount(), self.currentTracerouteQueueCount()))

            # Get all the schedules that have a next_check_time within our time interval.
            cutoff_time = datetime.now()  # apt- + timedelta(seconds=int(self.__config.get("Checker", "feeder_sleep_interval")))

            num_total_service_schedules = LocalSchedule.select().count()
            num_total_resource_schedules = LocalResourceSchedule.select().count()

            service_schedules = LocalSchedule.select(LocalSchedule.q.next_check_time <= cutoff_time,
                                                     orderBy=LocalSchedule.q.next_check_time)

            resource_schedules = LocalResourceSchedule.select(LocalResourceSchedule.q.next_check_time <= cutoff_time,
                                                              orderBy=LocalResourceSchedule.q.next_check_time)

            snmp_schedules = {}
            other_schedules = []
            for s in resource_schedules:
                if s.resource_check_method in ('resource.snmp', 'resource.snmp.child'):
                    if s.fqdn not in snmp_schedules: snmp_schedules[s.fqdn] = []
                    snmp_schedules[s.fqdn].append(s)
                else:
                    other_schedules.append(s)

#            total_checks_ready = service_schedules.count() + other_schedules.count()
            total_checks = num_total_service_schedules + num_total_resource_schedules
            self.logger.info("Found ready checks of %d total: %d network, %d SNMP, %d other" %
                             (total_checks, service_schedules.count(), len(snmp_schedules.keys()), len(other_schedules)))

            for schedule in list(service_schedules) + other_schedules:
                if hasattr(schedule, 'resource_check_method') and schedule.resource_check_method == 'resource.snmp.dynamic':
                    self.putSNMPDiscovery(schedule)
                else:
                    schedule.next_check_time = datetime.now() + timedelta(seconds=schedule.frequency)
                    self.putWork(schedule)

            # Add SNMP schedule bundles and increment next check time
            for fqdn in snmp_schedules:
                self.putWork(snmp_schedules[fqdn])
                for schedule in snmp_schedules[fqdn]:
                    schedule.next_check_time = datetime.now() + timedelta(seconds=schedule.frequency)

            # Get all the traceroutes that need to be processed
            for traceroute in LocalTraceroute.select(LocalTraceroute.q.start_time == None):
                if not traceroute.ip_address:
                    # Need to do a DNS lookup so we can query
                    try:
                        traceroute.ip_address = socket.gethostbyname(traceroute.fqdn)
                    except:
                        pass
                self.logger.debug("Injecting traceroute %s:%s" % (traceroute.fqdn, traceroute.ip_address))
                traceroute.start_time = datetime.now()
                self.putTraceroute(traceroute)

            LocalSchedule._connection.cache.clear()

            # Sleep for a bit until we're needed again.  this is equivalent to
            # sleeping until the sleep interval or until the shutdown flag,
            # whichever comes first
            self.shutdown_flag.wait(self.sleep_interval)
from models.masterdb import *
from models.nodedb import LocalSchedule, LocalResourceSchedule
from datetime import datetime, date, timedelta
import traceback
import commands 
from pprint import pprint

import turbogears
turbogears.update_config(configfile="/root/scripts.cfg")

from sqlobject import IN, AND, OR

bad_counts = {}

for mn in MonitorNode.select(MonitorNode.q.status != 'shutdown'):
    print "Checking node %s" % mn.name

    try:
        db = mn.getDBConnection()
        local_resource_schedules = LocalResourceSchedule.select(connection = db)
        local_schedules = LocalSchedule.select(connection = db)
        c = local_schedules.count()
	print c
    except: 
        print "=============== Error connecting to %s =========================" % mn.name