Add configurable delay per client; improve try/catch
This commit is contained in:
		
							parent
							
								
									3f9adfa6c5
								
							
						
					
					
						commit
						4e11748b45
					
				
					 5 changed files with 97 additions and 58 deletions
				
			
		| 
						 | 
				
			
			@ -60,23 +60,11 @@ class TempmonServerDaemon(Daemon):
 | 
			
		|||
    def check_readings(self):
 | 
			
		||||
        self.now = make_utc()
 | 
			
		||||
        session = TempmonSession()
 | 
			
		||||
        probes = session.query(tempmon.Probe)\
 | 
			
		||||
                        .join(tempmon.Client)\
 | 
			
		||||
                        .filter(tempmon.Client.enabled == True)\
 | 
			
		||||
                        .filter(tempmon.Probe.enabled == True)\
 | 
			
		||||
                        .all()
 | 
			
		||||
 | 
			
		||||
        if probes:
 | 
			
		||||
            cutoff = self.now - datetime.timedelta(seconds=120)
 | 
			
		||||
            uuids = [probe.uuid for probe in probes]
 | 
			
		||||
            readings = session.query(tempmon.Reading)\
 | 
			
		||||
                              .filter(tempmon.Reading.probe_uuid.in_(uuids))\
 | 
			
		||||
                              .filter(tempmon.Reading.taken >= cutoff)\
 | 
			
		||||
                              .all()
 | 
			
		||||
            self.process_readings(probes, readings)
 | 
			
		||||
 | 
			
		||||
        else:
 | 
			
		||||
            log.warning("found no enabled probes!")
 | 
			
		||||
        clients = session.query(tempmon.Client)\
 | 
			
		||||
                         .filter(tempmon.Client.enabled == True)
 | 
			
		||||
        for client in clients:
 | 
			
		||||
            self.check_readings_for_client(session, client)
 | 
			
		||||
 | 
			
		||||
        session.commit()
 | 
			
		||||
        session.close()
 | 
			
		||||
| 
						 | 
				
			
			@ -84,27 +72,36 @@ class TempmonServerDaemon(Daemon):
 | 
			
		|||
        # TODO: not sure this is really necessary?
 | 
			
		||||
        self.set_last_checked()
 | 
			
		||||
 | 
			
		||||
    def process_readings(self, probes, readings):
 | 
			
		||||
        for probe in probes:
 | 
			
		||||
            probe_readings = [r for r in readings if r.probe is probe]
 | 
			
		||||
            if probe_readings:
 | 
			
		||||
                reading = sorted(probe_readings, key=lambda r: r.taken)[-1]
 | 
			
		||||
    def check_readings_for_client(self, session, client):
 | 
			
		||||
        delay = client.delay or 60
 | 
			
		||||
        cutoff = self.now - datetime.timedelta(seconds=delay + 60)
 | 
			
		||||
        for probe in client.enabled_probes():
 | 
			
		||||
            self.check_readings_for_probe(session, probe, cutoff)
 | 
			
		||||
 | 
			
		||||
                if (reading.degrees_f <= probe.critical_temp_min or
 | 
			
		||||
                      reading.degrees_f >= probe.critical_temp_max):
 | 
			
		||||
                    self.update_status(probe, self.enum.TEMPMON_PROBE_STATUS_CRITICAL_TEMP, reading)
 | 
			
		||||
    def check_readings_for_probe(self, session, probe, cutoff):
 | 
			
		||||
        readings = session.query(tempmon.Reading)\
 | 
			
		||||
                          .filter(tempmon.Reading.probe == probe)\
 | 
			
		||||
                          .filter(tempmon.Reading.taken >= cutoff)\
 | 
			
		||||
                          .all()
 | 
			
		||||
        if readings:
 | 
			
		||||
            # we really only care about the latest reading
 | 
			
		||||
            reading = sorted(readings, key=lambda r: r.taken)[-1]
 | 
			
		||||
 | 
			
		||||
                elif reading.degrees_f < probe.good_temp_min:
 | 
			
		||||
                    self.update_status(probe, self.enum.TEMPMON_PROBE_STATUS_LOW_TEMP, reading)
 | 
			
		||||
            if (reading.degrees_f <= probe.critical_temp_min or
 | 
			
		||||
                  reading.degrees_f >= probe.critical_temp_max):
 | 
			
		||||
                self.update_status(probe, self.enum.TEMPMON_PROBE_STATUS_CRITICAL_TEMP, reading)
 | 
			
		||||
 | 
			
		||||
                elif reading.degrees_f > probe.good_temp_max:
 | 
			
		||||
                    self.update_status(probe, self.enum.TEMPMON_PROBE_STATUS_HIGH_TEMP, reading)
 | 
			
		||||
            elif reading.degrees_f < probe.good_temp_min:
 | 
			
		||||
                self.update_status(probe, self.enum.TEMPMON_PROBE_STATUS_LOW_TEMP, reading)
 | 
			
		||||
 | 
			
		||||
                else: # temp is good
 | 
			
		||||
                    self.update_status(probe, self.enum.TEMPMON_PROBE_STATUS_GOOD_TEMP, reading)
 | 
			
		||||
                
 | 
			
		||||
            else: # no readings for probe
 | 
			
		||||
                self.update_status(probe, self.enum.TEMPMON_PROBE_STATUS_ERROR)
 | 
			
		||||
            elif reading.degrees_f > probe.good_temp_max:
 | 
			
		||||
                self.update_status(probe, self.enum.TEMPMON_PROBE_STATUS_HIGH_TEMP, reading)
 | 
			
		||||
 | 
			
		||||
            else: # temp is good
 | 
			
		||||
                self.update_status(probe, self.enum.TEMPMON_PROBE_STATUS_GOOD_TEMP, reading)
 | 
			
		||||
 | 
			
		||||
        else: # no readings for probe
 | 
			
		||||
            self.update_status(probe, self.enum.TEMPMON_PROBE_STATUS_ERROR)
 | 
			
		||||
 | 
			
		||||
    def update_status(self, probe, status, reading=None):
 | 
			
		||||
        data = {
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
		Loading…
	
	Add table
		Add a link
		
	
		Reference in a new issue