def exec_updsql(self, _sqlstr): try: cursor = conn.cursor() cursor.execute(_sqlstr) cursor.close() conn.commit() except MySQLdb.Error, e: fc.log(_sqlstr) cursor.close() raise Exception("SQL ERROR %d: %s" % (e.args[0], e.args[1]))
def exec_selsql(self, _sqlstr): try: cursor = conn.cursor() cursor.execute(_sqlstr) result = self.cur2arr(cursor) cursor.close() return result except MySQLdb.Error, e: fc.log(_sqlstr) cursor.close() raise Exception("SQL ERROR %d: %s" % (e.args[0], e.args[1]))
def detect_proc(_hostid, _itemid, _value, _avg, _var, _clock): gaussian = f.calc_gaussian(_value, _avg, _var) #print "hostid=%s, itemid=%s, value=%s, clock=%s, gaussian=%s, average=%s, variance=%s" % \ # (str(_hostid), str(_itemid), str(_value), str(_clock), str(gaussian), str(_avg), str(_var)) if gaussian < p.detect_limit/100: msg = "Detected anormal: hostid=%s, itemid=%s, value=%s, clock=%s, gaussian=%s, average=%s, variance=%s" % \ (str(_hostid), str(_itemid), str(_value), str(_clock), str(gaussian), str(_avg), str(_var)) f.log(msg) strsql = "insert into zabbix.gaussian(hostid, itemid, clock, value, avg, var, gaussian) \ values(\'%s\', \'%s\', \'%s\', \'%s\', \'%s\', \'%s\', \'%s\');" % \ (str(_hostid), str(_itemid), str(_clock), str(_value), str(_avg), str(_var), str(gaussian)) l_gaussian_sql.append(strsql)
def exec_selsqlh(self, _sqlstr): try: cursor = conn.cursor() ret = cursor.execute(_sqlstr) body = map(list, list(cursor.fetchall())) desc = [d[0] for d in cursor.description] data = [] header = {} i = 0 for h in desc: header[h.lower()] = i i = i + 1 data.append(header) data.append(body) cursor.close() return data except MySQLdb.Error, e: fc.log(_sqlstr) cursor.close() raise Exception("SQL ERROR %d: %s" % (e.args[0], e.args[1]))