Wechat official account: Operation and maintenance development story, author: Wanger

When zabbix have happened, we can according to the event to perform the corresponding action, according to the event source can be divided into the trigger action, automatically discover, automatic registration actions, internal event action, automatically discover action told it in the previous automatically discover there, here introduce a trigger action, when necessary to trigger event to perform an action, Will perform the corresponding action.

Configure the email alarm action

Start by creating a trigger action that triggers an alarm and sends an email



Define action trigger conditions



Defines the operations performed by the action. In this case, the message is sent. Steps 1-5 indicate that the message is sent five times, and the default interval is 30 minutes

Here we define steps 1-5 to send messages to the Admin user every 30 minutes via the script ‘zabbix_send-.py’



If the problem is not confirmed within two hours, the Zabbix management team will be notified twice every fifteen minutes after two hours



Here we see that step 5 of the two operations overlaps, where the shorter custom step duration of 10 minutes overrides the longer step duration, that is, step 5 of the second operation overrides step 5 of the first operation



Define a recovery operation, and send a message to the Admin user when the problem is resolved



Define update operations to be notified when other users update a problem, such as when the problem is closed or the severity of the problem changes



At this point, the action part is complete, and you need to configure the user and alarm media if you want it to send mail successfully

Configure the user



Configure the user alarm media



Configure user information, such as the recipient

Create an alarm media type



Configure the alarm media type. The three parameters passed in are recipient, message subject, and message content



The Zabbix mail alarm Web interface configuration is complete, and the Zabbix_Server configuration file needs to be modified to support the use of scripts

vim/etc/zabbix/zabbix_server.conf

AlertScriptsPath=/usr/lib/zabbix/alertscripts  

Modify the restart after the completion of zabbix server in/usr/lib/zabbix/alertscripts add to use the alarm script directory And give the mail to grant execute permissions chmod + xzabbix_send. Py

Create a graph directory and grant permissions mkdir graphchmod 777-r graph



The email and detailed notes are as follows:

#! /usr/bin/python#coding=utf-8from email.mime.text import MIMETextfrom email.mime.multipart import MIMEMultipartfrom email.mime.image import MIMEImageimport smtplib,sys,os,time,re,requestsfrom smtplib import SMTP user='Admin' # define zabbix user name password = 'zabbix' # define zabbix user password graph_path = '/ usr/lib/zabbix alertscripts/graph' = 'http://192.168.179.132/chart.php' # define photo storage path graph_url # define chart url# api_url = "http://10.127.0.119/api_jsonrpc.php" # define the API url# header = {" content-type ":" application/json "} # define the API headersloginurl = "http://192.168.179.132/index.php" Argv [1] # define logon urlhost='192.168.179.132'to_email=sys.argv[2] # define logon urlhost='192.168.179.132'to_email=sys.argv[1] # pass the first argument to the recipient email Subject =subject.decode(' UTF-8 ') smtp_host = 'smtp.163.com' # define SMTP host address from_email = '[email protected]' # define sender address mail_pass = Def get_itemid() def get_itemid(): Itemid =re.search(r'ITEM ID:(\d+)',sys.argv[3]).group(1) return ItemidDef get_graph(ItemId): Session =requests.Session() loginheaders={ "Host":host, "Accept":"text/html,application/xhtml+xml,application/xml; Q = 0.9, image/webp image/apng, * / *; Payload = {"name":user, "password":password, "autologin":"1", "enter":"Sign in", Post (url= loginURL,headers= loginurl, data=payload) # login= {"from" :"now-10m", "to" : "now", "itemids" : itemid, "width" : "400", } # define graph_req=session.get(url=graph_url,params=graph_params time_tag=time.strftime("%Y%m%d%H%M%S", Time.localtime ()) graph_name='baojing_'+time_tag+'.png' With open(graph_name,'wb') as f: F. write(graph_req.content) # return graph_name except Exception as e: print e return Falsedef text_to_html(text): D =text.splitlines() I = "+ I + '<br>' html_text+= I + '\n' # return HTML_textDef send_mail(graph_name): MSG = MIMEMultipart('related') with open(graph_name,'rb') as f: Graph. Add_header (' content-id ','imgid1') # Add the title field and value to the image object text=text_to_html(sys.argv[3]) html=""" <html> <body> %s <br><img src="cid:imgid1"> </body> </html> """ % text HTML =MIMEText(HTML,' HTML ',' utF-8 ') # Attach (HTML) # add HTML to MSG instance MSG. Attach (graph) MSG ['Subject'] = Subject MSG ['From'] = from_email try: Server =SMTP(smtp_host,"25") # create an SMTP object server.starttls() # enable secure transfer mode server.login(from_email,mail_pass) # Server.sendmail (from_email,to_email,msg.as_string()) # sendmail server.quit() # disconnect SMTP connection except smtplib.smtpexception as a: print adef run(): itemid=get_itemid() graph_name=get_graph(itemid) send_mail(graph_name)if __name__ =='__main__': run()Copy the code

Configure the action for executing remote commands

When the trigger reaches the threshold alarm, we can execute the relevant commands according to the alarm to make the failure to achieve self-healing effect. Here I give an example of SSH port closing and performing SSH restart

Configure it on the system

Uncomment the following statement in the Zabbix client configuration file, Conf EnableRemoteCommands=1 The Zabbix user runs the remote command vim /etc/zabbix/zabbix_agentd.conf Ensure that user ‘Zabbix’ has permission to execute the configured commands. vim/etc/sudoers

Defaults ! Requiretty # No prompt for terminal login is required

Zabbix ALL=(ALL) NOPASSWD: ALL # Allows the 'zabbix' user to run ALL commands without a password.

After the configuration is complete, use zabbix-get to test whether the remote command can be run. If data is returned, the remote command is available

Zabbix_get -s 192.168.179.132 -k "system-run [sudo df -h]"Copy the code

The configuration script

vim /restart_ssh.sh

#/bin/bash
systemctl restart sshd
Copy the code

Grant executable permission to the script chmod +x /restart_sshd.sh

Example Create SSH monitoring items

Create trigger



Configure the action

Create action



The action triggering condition is configured



The command for configuring action execution is delayed for two minutes for viewing the effect

Trigger the alarm

The SSH service is disabled to trigger the alarm systemCtlStop SSHD

The alarm is triggered and the script will be executed in two minutes

Here you can use Zabbix-get to get the value of the monitor. To check whether the command was successfully executed

Zabbix_get -s 192.168.179.132 -k.net. TCP. The port [192.168.179.132, 22]Copy the code

As you can see, Zabbix has successfully executed the script to restart SSH

This article uses the article synchronization assistant to synchronize