之前有一篇文章讲到使用update percent监控agent的数据提交状况,可以有效地发现agent的故障问题,而使用unreachable的时候,会因为unreachable process busy的情况造成误报(可以通过增大StartPollersUnreachable和UnreachablePeriod解决),附一个python小程序,用来计算host的update percent(遇到host update 为0时不能报警的情况,使用RIGHT JOIN+IFNULL解决).


#!/usr/bin/python
# -*- coding: utf8 -*-
#get zabbix agent update percent
import MySQLdb
import os
import sys
import smtplib
from email.MIMEText import MIMEText
reload(sys)
sys.setdefaultencoding('utf-8')
                                                      
def SendMail(sub,content,to_list = ["[email protected]"]):
          me=mail_user+"<"+mail_user+">"
          msg = MIMEText(content,'html','utf-8')
          msg['Subject'] = sub
          msg['From'] = me
          msg['To'] = ";".join(to_list)
          try:
               s = smtplib.SMTP()
               s.connect(mail_host)
               s.login(mail_user,mail_pass)
               s.sendmail(me, to_list, msg.as_string())
               s.close()
               return True
          except Exception, e:
               print str(e)
               return False                 
def getAll(sql):
          db = MySQLdb.connect('xxx','xxx','xxx','xxx')
          cursor = db.cursor()
          try:
               cursor.execute(sql)
               result = cursor.fetchall()
          except Exception,e:
               print "failed info %s" % (str(e))
          print result
          print type(result)
          return result
          cursor.close()
          db.close()
def getReport(allInfo):
          print allInfo
          mailcontent = ""
          print type(allInfo)
          if len(allInfo) == 0:
               pass
          else:
               mailcontent = """



Zabbix agent update percent
  


    
    """
               for  line in allInfo:
                   mailcontent += ""
                   mailcontent += "" % line[0]
                   mailcontent += "" % line[1]
                   if float(line[2]) <= 50:
                       mailcontent += """""" % line[2]
                   else:
                       mailcontent += "" % line[2]
                   mailcontent += ""
               mailcontent += " 
domain
ip
percent
%s
%s
%.2f
%.2f
" mailcontent += " " print mailcontent SendMail("Zabbix host update percent",mailcontent.encode('utf-8')) if __name__ == "__main__": mail_host="xxx" mail_user="xxxx" mail_pass="xxxx" mail_port="xxxx" allInfo = [] sql = """ select b.hostname ,c.ip,a.update_percent as uppercent from ( select b.hostid,ROUND(IFNULL(a.aa,0)*100/b.bb,2) as update_percent from (select hostid,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 group by hostid ) a RIGHT JOIN (select hostid,count(*) as bb from items where delay < 900 and status = 0 and hostid in (select hostid from hosts where status=0) group by hostid) b ON a.hostid=b.hostid)a,(select hostid,lower(host) as hostname from hosts where status=0)b, (select hostid,ip from interface where type='1')c where a.hostid=b.hostid and b.hostid=c.hostid having(a.update_percent) < 80 order by uppercent; """ allInfo = getAll(sql) getReport(allInfo)

产生的报警邮件如下:


172055483.png