def get_history4stat(): nowt = dt.today() endt = nowt - datetime.timedelta(hours=p.ml_end_hours) startt = endt - datetime.timedelta(days=p.ml_period_days) endsec = f.time2epoch(endt) startsec = f.time2epoch(startt) strsql = "select i.hostid, h.itemid, h.clock, h.value from zabbix.history as h \ join zabbix.items as i on h.itemid = i.itemid \ where h.clock<=\'%d\' and h.clock>=\'%d\' order by i.hostid, h.itemid, h.clock;" % (endsec, startsec) return sql.exec_selsql(strsql)
def detect(_logmode="n"): stat_data = get_statistics() hist_data = get_history4detect() start_clock = f.time2epoch(dt.now()) hist_i = 0 for stat_row in stat_data: stat_hostid = stat_row[0] stat_itemid = stat_row[1] avg = stat_row[2] var = stat_row[3] while True: if hist_i >= len(hist_data)-1: break hist_row = hist_data[hist_i] hist_hostid = hist_row[0] hist_itemid = hist_row[1] clock = hist_row[2] value = hist_row[3] if hist_hostid == stat_hostid and hist_itemid == stat_itemid: detect_proc(stat_hostid, stat_itemid, value, avg, var, clock) if hist_hostid > stat_hostid or hist_itemid > stat_itemid or hist_i>=len(hist_data): break hist_i = hist_i + 1 sql.exec_updsql("update zabbix.clock set clock=\'%s\'" % start_clock) sql.exec_updsql("truncate table zabbix.gaussian;") if _logmode=="y": strsql = "" for s in l_gaussian_sql: strsql = strsql + s + "\n" if strsql != "": sql.exec_updsql(strsql)
def get_clock(): data = sql.exec_selsql("select clock from zabbix.clock limit 1;", 0) if len(data) > 0: clock = data[0] else: clock = 0 sql.exec_updsql("insert into zabbix.clock value(\'0\');") if clock == 0: data = sql.exec_selsql("select min(clock) from zabbix.statistics;", 0) if len(data)>0 and data[0] != None: clock = data[0] else: clock = f.time2epoch(dt.now()) return clock