class ImbalanceTrigger(Trigger): def __init__(self): super(ImbalanceTrigger, self).__init__() self._dbs = DBQuery(index="sensors", office=office, host=dbhost) self._dba = DBQuery(index="algorithms", office=office, host=dbhost) def trigger(self, stop): stop.wait(service_interval[2]) info = [] try: nsensors = { "total": self._dbs.count("type='camera'"), "streaming": self._dbs.count("type='camera' and status='streaming'"), "idle": self._dbs.count("type='camera' and status='idle'"), } nalgorithms = { "total": self._dba.count("name:*"), } except Exception as e: print("Exception: " + str(e), flush=True) return info if nsensors["total"] > nsensors["streaming"] + nsensors["idle"]: info.append({ "fatal": [{ "message": text["check sensor"].format(nsensors["total"] - nsensors["streaming"] - nsensors["idle"]), "args": nsensors, }] }) if nalgorithms["total"] > nsensors["streaming"] + nsensors["idle"]: info.append({ "warning": [{ "message": text("imbalance").format( nalgorithms["total"], nsensors["streaming"] + nsensors["idle"]), "args": { "nalgorithms": nalgorithms["total"], "nsensors": nsensors["streaming"] + nsensors["idle"], }, }], }) return info
class ImbalanceTrigger(Trigger): def __init__(self): super(ImbalanceTrigger,self).__init__() self._dbs=DBQuery(index="sensors",office=office,host=dbhost) self._dba=DBQuery(index="algorithms",office=office,host=dbhost) def trigger(self): time.sleep(service_interval[2]) info=[] try: nsensors={ "total": self._dbs.count("sensor:*"), "streaming": self._dbs.count("status:'streaming'"), "idle": self._dbs.count("status:'idle'"), } nalgorithms={ "total": self._dba.count("name:*"), } except Exception as e: print("Exception: "+str(e), flush=True) return info if nsensors["total"]>nsensors["streaming"]+nsensors["idle"]: info.append({ "fatal": [{ "message": "Check sensor: #disconnected="+str(nsensors["total"]-nsensors["streaming"]-nsensors["idle"]), "args": nsensors, }] }) if nalgorithms["total"]>nsensors["streaming"]+nsensors["idle"]: info.append({ "warning": [{ "message": "Imbalance: #analytics="+str(nalgorithms["total"])+",#sensors="+str(nsensors["streaming"]+nsensors["idle"]), "args": { "nalgorithms": nalgorithms["total"], "nsensors": nsensors["streaming"]+nsensors["idle"], }, }], }) return info
"name": "health_check", "status": "processing", }) break except Exception as e: print("Exception: "+str(e), flush=True) time.sleep(10) dbs=DBQuery(index="sensors",office=office,host=dbhost) dba=DBQuery(index="algorithms",office=office,host=dbhost) dbat=DBIngest(index="alerts",office=office,host=dbhost) while True: try: nsensors={ "total": dbs.count("sensor:*"), "streaming": dbs.count("status:'streaming'"), "idle": dbs.count("status:'idle'"), } nalgorithms={ "total": dba.count("name:*"), } alerts={} if nsensors["total"]>nsensors["streaming"]+nsensors["idle"]: alerts["maintenance_required"]=nsensors if nalgorithms["total"]!=nsensors["streaming"]+nsensors["idle"]: alerts["balancing_required"]={ "nalgorithms": nalgorithms["total"], "nsensors": nsensors["streaming"]+nsensors["idle"],