Zabbix Implementing Python Mail Alerts

Source: Internet
Author: User

Zabbix is a very powerful monitoring tool that can monitor server data for Linux and Windows, and can also extend the default monitoring by customizing keys, but the messages provided by your own email alerts are not very friendly. In this paper, we want to send the corresponding image and URL connection at the same time by the custom script.

The steps are as follows:

  1. Create a Script Media alert: Modify the Zabbix Script Alert configuration path: Alertscritppath as the path to the script

  2. # # option:alertscriptspath# alertscriptspath=${datadir}/zabbix/alertscripts#alertscriptspath=/usr/lib/zabbix/ Alertscriptsalertscriptspath=/etc/zabbix/alert
  3. Click Create Media type and associate the default action alarm

    650) this.width=650; "src=" http://s3.51cto.com/wyfs02/M01/49/39/wKioL1QRF4ig1tVhAALOZnV-xPk727.jpg "title=" zbx_ Media "alt=" Wkiol1qrf4ig1tvhaaloznv-xpk727.jpg "/>

    650) this.width=650; "src=" http://s3.51cto.com/wyfs02/M01/49/38/wKiom1QRFy-T_AU1AAE0v3PRQRA480.jpg "title=" zbx_ Action "style=" White-space:normal;float:none; "alt=" wkiom1qrfy-t_au1aae0v3prqra480.jpg "/>

  4. Enable alert scripting for users

    650) this.width=650; "src=" http://s3.51cto.com/wyfs02/M02/49/3A/wKioL1QRGpyjsqb0AALCzCHpM_g276.jpg "title=" zbx_ User.jpg "alt=" Wkiol1qrgpyjsqb0aalczchpm_g276.jpg "/>

  5. Modify the first step of the associated alarm action, let it use footsteps to send an alarm, notice the change only sent to the alarm mode, but also to modify the alarm in the trigger template with Itemid this parameter (to provide parameters for the script alarm)

    650) this.width=650; "src=" http://s3.51cto.com/wyfs02/M01/49/38/wKiom1QRG9STM-_ZAAI8ClOoF1U093.jpg "title=" zbx_ Bj.jpg "alt=" Wkiom1qrg9stm-_zaai8cloof1u093.jpg "/>

    650) this.width=650; "src=" http://s3.51cto.com/wyfs02/M02/49/39/wKiom1QRG-jBTfrEAAKknSc_fgI962.jpg "title=" zbx_ Baojing.jpg "alt=" Wkiom1qrg-jbtfreaakknsc_fgi962.jpg "/>

    650) this.width=650; "src=" http://s3.51cto.com/wyfs02/M02/49/3A/wKioL1QRHfyS1uB1AANq42qxq9E862.jpg "title=" zbx_ Bjset.jpg "alt=" Wkiol1qrhfys1ub1aanq42qxq9e862.jpg "/>

  6. Finally, the preparation is completed, the Python script is uploaded to the 1th step of the path, the idea is to use the action alarm Itmeid to get the item value of the monitoring item, and get the graph through history.php, and provide a URL connection

    Modify the following Zabbix address to your actual Zabbix server address in the following script # Note:

    #注意修改日志路径 Modify the path to your actual log


#! /usr/bin/python# -*- coding: utf-8 -*-import sys,os,re,smtplib,mimetypes, pycurl,json,urllib,stringio,time,tracebackfrom email.mime.text import mimetextfrom  email.mime.multipart import mimemultipartfrom email.mime.image import mimeimageleng= Len (SYS.ARGV) def log4py (logpath,logtxt):    f=open (LogPath, ' a ')    logtime= Time.strftime ('%y%m%d%h%m%s ', Time.localtime ())    f.writelines (logtime+ ":")     F.writelines (logtxt)    f.writelines (' \ n ')    f.close () Def printmlinestr ( MLINESTR):    d=mlinestr.splitlines ()    res= ""    for i  In d:      i= "<br>" +i+ "</br>"        res+=i+ ' \ n '    return res#getvalue  gets the corresponding Itemiddef getvalue (string) from the regular expression REXP):     r=re.compIle (Rexp,re. M)      #r =re.compile ("^itemid:[0-9]*", re. M)     log4py (LogPath, ' Method getvalue ')     log4py (LogPath, ' string ')     log4py (logpath,string)      #for  i in string:     d=r.findall (String)     log4py  (LogPath, ' rexp: ')      log4py  (LOGPATH,REXP)     str1=d[0]     log4py (LogPath,   ' Rexresult: ')     log4py (LOGPATH,STR1)     print str1    # arrstr=str.split ("\xa3\xba")     arrstr=str1.split (":")      result=arrstr[1]    log4py (LogPath, ' Result: ')     log4py ( Logpath,result)     log4py (LogPath, ' end method getvalue ')      return result#getpic  get alarm picture, write file Def getpic (iTemid):         log4py (LogPath, ' method getpic ')    #  try:           c = pycurl. Curl ()         pathroot= '/etc/zabbix/alert/'           #path = ' c:/alert.png '          #head = ' Content-type:application/json '         global stime         endtime=time.strftime ('%y%m%d%h%m%s ', Time.localtime ())          tmptime=int (Endtime) -10000        stime  = repr (tmptime)         post_data_dic={"itemid": Itemid, " Period ":" 3600 "," Stime ": stime}picpath= (Pathroot) + (stime) + (itemid) + '. png ' print  ' Picpath: ' +picpath         str=file (Picpath, ' WB ')          #注意修改下面的zabbix地址          c.setopt (Pycurl. url,  "http://10.1.1.67/zabbix/chart.php")         c.setopt (Pycurl. Writefunction, str.write)         c.setopt (Pycurl. followlocation, 1)          #c. setopt (Pycurl. Header, true)         c.setopt (C.postfields,urllib.urlencode (post_ Data_dic))         c.perform ()          log4py (LogPath, ' end method getpic ') return picpath   # except  exception,e:   #     f.writelines (' expforgetpic:\n ')     #     f.writelines (e) def sendmail (receivers,subject,data,picpath,itemid):     msG = mimemultipart ()     msg[' from '] =  "[email protected]"       #msg [' to '] =  "[email protected]"     receives=receivers     msg[' to '] = receivers    msg[' Subject '] =  SUBJECT&NBSP;&NBSP;&NBSP;&NBSP;BODY=PRINTMLINESTR (data)     file1 = picpath     image = mimeimage (Open (File1, ' RB '). Read ())     image.add_header (' Content-id ', ' <image1> ')     msg.attach (image)      #print   txt     #print   ' txt end '      #print  type  (TXT)       #注意修改下面的zabbix地址     url= "      #msg. Attach (TXT)     html =   "" "     


Fill in:

650) this.width=650; "src=" http://s3.51cto.com/wyfs02/M02/49/3A/wKioL1QRH_rTIN1VAAOVFEhlAto285.jpg "title=" zbx_ Affect.jpg "alt=" Wkiol1qrh_rtin1vaaovfehlato285.jpg "/>

Zabbix Implementing Python Mail Alerts

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.