Esempio n. 1
0
 def init_senders(self):
     self.sender_manager = SenderManager(
         squeue=self.senderq,
         tsd_list=self.conf.tsd_list()
     )
     self.sender_manager.run()
Esempio n. 2
0
class Main:
    def __init__(self, readers=5, conf=None, interval=300):
        self.readerq = Queue()
        self.pool = []
        self.senderq = Queue()
        self.readers = readers
        self.interval = interval
        if conf:
            self.conf = ConfigReader(conf)
        self.resolvers = self.load_resolvers()
        self.value_modifiers = self.load_value_modifiers()

    def init_readers(self):
        for i in range(0, self.readers):
            readth = ReaderThread(self.readerq, self.senderq)
            readth.start()
            self.pool.append(readth)

    def stop_readers(self):
        for w in self.pool:
            w.stop()

    def init_senders(self):
        self.sender_manager = SenderManager(
            squeue=self.senderq,
            tsd_list=self.conf.tsd_list()
        )
        self.sender_manager.run()

    def stop_senders(self):
        self.sender_manager.stop()

    def load_resolvers(self):
        resolvers = {}
        for entry in iter_entry_points(group="resolvers"):
            resolvers[entry.name] = entry.load()()
        return resolvers

    def load_value_modifiers(self):
        mods = {}
        for entry in iter_entry_points(group="value_modifiers"):
            mods[entry.name] = entry.load()()
        return mods

    def load_devices(self):
        self.devices = []
        for d in self.conf.devicelist():
            d = Device(d, self.resolvers,
                       self.value_modifiers, self.conf.metrics())
            self.devices.append(d)
        return self.devices

    def run(self, once=False):
        self.init_senders()
        self.init_readers()
        self.load_devices()
        try:
            while(True):
                start_time = time.time()
                """fill reader queue"""
                for d in self.devices:
                    self.readerq.put(d)
                self.readerq.join()
                delta = time.time() - start_time
                if delta < self.interval:
                    time.sleep(self.interval - delta)
                if (once):
                    break
        except (KeyboardInterrupt, SystemExit):
            self.stop_readers()
            self.stop_senders()
        self.stop_readers()
        self.stop_senders()