用来衡量zabbix server和proxy的update 性能,在数据delay比较严重的时候,update percent会比较低。可以做成item用来监控性能。
import MySQLdb
import sys
import time
class dbUtil:
def __init__(self,host,user,passwd,db):
self.host = host
self.user = user
self.passwd = passwd
self.db = db
def db_connect(self):
self.conn = MySQLdb.connect(host=self.host,user=self.user,passwd=self.passwd,db=self.db)
self.cursor = self.conn.cursor()
def db_fetch_one(self,sql):
self.cursor.execute(sql)
try:
result = float(self.cursor.fetchone()[0])
except:
result = "-1"
return result
def db_close(self):
self.cursor.close()
self.conn.close()
def getData(sql):
#print sql
conn = dbUtil(ip,user,password,db)
conn.db_connect()
re = conn.db_fetch_one(sql)
return re
conn.db_close()
def getPercent(hostname):
if hostname == "server_hostname":
sql = """ select round(a.aa*100/b.bb,2) from
(select count(*) as aa from items
where lastclock > UNIX_TIMESTAMP()-1800 and delay < 900
and hostid in (select hostid from hosts where status=0)
and status = 0
) a,
(select count(*) as bb from items
where delay < 900 and status = 0
and hostid in (select hostid from hosts where status=0)
) b"""
print getData(sql)
else:
hostsql= "select hostid from hosts where host='%s'" % (hostname)
hostid = getData(hostsql)
sql = """ select round(a.aa*100/b.bb,2) from
(select count(*) as aa from items
where lastclock > UNIX_TIMESTAMP()-1800 and delay < 900
and hostid in (select hostid from hosts where status=0 and proxy_hostid = %s)
and status = 0
) a,
(select count(*) as bb from items
where delay < 900 and status = 0
and hostid in (select hostid from hosts where status=0 and proxy_hostid = %s)
) b""" % (hostid,hostid)
print getData(sql)
if __name__ == "__main__":
hostname = sys.argv[1]
getPercent(hostname)