由于最近在研究zabbix,学习python,于是就拿python练练手,写了下面这个脚本
脚本作用:发送tigger内容并附加发送tirgger所对应的graph
脚本思路:通过zabbix传递的"Default subject"取出触发的tirgger对应的主机host,以及对应的graphname,通过host和graphname在数据库中查询出所对应的graphid,之后通过graphid使用curl通过web页面下载对应graph的图片,最后将图片作为邮件内容发送出去。
脚本依赖:由于获取host和graphname依赖于zabbix传递的参数形式及内容,所以需要做如下设置
    设置1:修改tirgger名字和graph名字对应,grap名字中不包含tirgger中所使用的宏,如:
          tirgger名:Running processes on {HOST.NAME}
          graph名:Running processes on
    设置2:创建Actions时,"Default subject"和"Recovery subject"要设置为:"{HOST.NAME1} {TRIGGER.STATUS}: {TRIGGER.NAME}"
    设置3:在zabbix上创建一个具有只读权限的用户,在zabbix数据库上创建一个具有查询权限的用户
脚本进度:由于个人技能还未达到一定的层次,该脚本在手动执行是正常的,但是在zabbix的Action触发调用时,却不能成功执行,希望有兴趣的朋友给我一些建议和指导,或者为我指出哪里考虑不周全导致的脚本不能正常执行。希望能够得到广大IT爱好者的帮助与建议。
感谢: VV   http://www.178-go.com    提供的SQL语句帮助
 
 
  
  1. #!/usr/bin/python 
  2. # 
  3. # When: 2012/12/17 
  4. # Who: czlinux@163.com 
  5. # 
  6.  
  7. import os,sys,smtplib,MySQLdb 
  8. from email.MIMEMultipart import MIMEMultipart 
  9. from email.MIMEText import MIMEText 
  10. from email.MIMEImage import MIMEImage 
  11.  
  12. ######  Variable declaration 
  13. hostname=os.popen('echo $HOSTNAME').read() 
  14.  
  15. mysql_server="127.0.0.1" 
  16. mysql_user="scripts" 
  17. mysql_pass="135246" 
  18. db_name="zabbix" 
  19.  
  20. zabbix_url="XXXXXXXXXX" 
  21. zabbix_user="XXXXX" 
  22. zabbix_pass="XXXXXX" 
  23.  
  24. cookie="/tmp/cookie" 
  25. p_w_picpath_path="/tmp/zabbix-graph/" 
  26. Stime=os.popen('date +%Y%m%d%H%M%S').read().rstrip() 
  27. Period=3600 
  28. Width=1222 
  29.  
  30. mail_server="127.0.0.1" 
  31. mail_port=25 
  32. mail_from="root"+"@"+"%s" % hostname.rstrip() 
  33.  
  34. tag = 0 
  35.  
  36. ###### get Host 
  37. def _getHost(Subject): 
  38.         Host = Subject.split(" ")[0
  39.         return Host 
  40.  
  41. ###### get Graphname 
  42. def _getgraphname(Subject): 
  43.         tmp1 = Subject.split(" "
  44.         tmp2 = tmp1[2:-1
  45.         Graphname = " ".join(tmp2) 
  46.         return Graphname 
  47.  
  48. ###### get graphid by Graphname and Host 
  49. def _getgraphid(Graphname,Host): 
  50.         db = MySQLdb.connect(mysql_server,mysql_user,mysql_pass,db_name) 
  51.         cursor = db.cursor() 
  52.         sql = "select graphs.graphid from hosts,items,graphs,graphs_items where hosts.host= '%s' and graphs.name = '%s' and hosts.hostid=items.hostid and items.itemid=graphs_items.itemid and graphs.graphid=graphs_items.graphid;" % (Host,Graphname) 
  53.         cursor.execute(sql) 
  54.         results = cursor.fetchall() 
  55.         db.close() 
  56.         for id in results: 
  57.                 graphid = int(id[0]) 
  58.         return graphid 
  59.  
  60. ###### get graph using by curl 
  61. def _getgraph(graphid): 
  62.         os.popen("""curl -c '%s' -b '%s' -d "request=&name='%s'&password='%s'&autologin=1&enter=Sign+in" '%s'/index.php""" % (cookie,cookie,zabbix_user,zabbix_pass,zabbix_url)) 
  63.         os.popen("""curl -b '%s' -F "graphid=%d" -F "period=%d" -F "stime='%s'" -F "width=%d" '%s'/chart2.php > '%s''%s''.png'""" % (cookie,graphid,Period,Stime,Width,zabbix_url,p_w_picpath_path,Stime)) 
  64.         p_w_picpath_name = '%s.png' % Stime 
  65. return p_w_picpath_name 
  66.  
  67. ###### Creating email content 
  68. def _content(Subject,body,mail_to,*img): 
  69.         msgRoot = MIMEMultipart() 
  70.         msgRoot['From'] = mail_from 
  71.         msgRoot['To'] = mail_to 
  72.         msgRoot['Subject'] = Subject 
  73.  
  74.         msgText = MIMEText(body) 
  75.         msgRoot.attach(msgText) 
  76.  
  77.         if tag == 0
  78.                 p_w_picpath_path = img[0
  79.                 p_w_picpath_name = img[1
  80.                 f = open('%s%s' % (p_w_picpath_path,p_w_picpath_name),'rb'
  81.                 msgimg = MIMEImage(f.read()) 
  82.                 f.close() 
  83.                 msgimg.add_header('Content-Disposition''p_w_upload', filename = p_w_picpath_name) 
  84.                 msgRoot.attach(msgimg) 
  85.         return msgRoot 
  86.  
  87. ######  definition sendmail function 
  88. def send_mail(mail_server,mail_port,mail_from,mail_to,content): 
  89.         mail=smtplib.SMTP(mail_server,mail_port) 
  90.         mail.sendmail(mail_from,mail_to,content) 
  91.         mail.quit() 
  92.  
  93. ###### definition main function 
  94. def main(mail_to,Subject,body): 
  95.         Host = _getHost(Subject) 
  96.         if tag == 0
  97.                 Graphname = _getgraphname(Subject) 
  98.                 graphid = _getgraphid(Graphname,Host) 
  99.                 p_w_picpath_name = _getgraph(graphid) 
  100.                 msgRoot = _content(Subject,body,mail_to,p_w_picpath_path,p_w_picpath_name) 
  101.         else
  102.                 msgRoot = _content(Subject,body,mail_to) 
  103.         send_mail(mail_server,mail_port,mail_from,mail_to,msgRoot.as_string()) 
  104.  
  105. if __name__ == "__main__"
  106.         if "Zabbix agent" in Subject: 
  107.                 tag = 1 
  108.         main(sys.argv[1],sys.argv[2],sys.argv[3]) 
 
邮件内容:
 

 

 

希望得到广大IT爱好者的建议与指导。