Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

Zabbix user-defined script to realize SMS alarm reminder

2025-03-09 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Network Security >

Share

Shulou(Shulou.com)06/01 Report--

A brief introduction

Usually, after the web interface of zabbix is configured, in order to receive the alarm message of zabbix in time, we need to configure "alarm medium" to notify us. The two commonly used alarm media are Email and SMS, that is, email notification and SMS notification.

The configuration of email notification is very simple, you only need to set up SMTP, so I won't say much here. But zabbix's default SMS notification is a bit tricky, and the "SMS" alarm medium needs hardware support to send text messages. At the same time, in order to facilitate the development, the operation of sending a short message is generally encapsulated in the form of an interface. When you need to send a short message, you only need to call this API and pass in the recipient, SMS details and other information to complete the sending of a short message.

Therefore, in order to simplify the cost of zabbix SMS notification, we can consider letting zabbix call the external script request SMS interface to realize the SMS sending function.

The idea of realization is:

Write a script for sending text messages using a SMS interface

Create a trigger in the web interface to execute the above script when the problem is "generally serious" or above, thus triggering the SMS alarm

2 specific implementation steps

(1) add two scripts (PS: send SMS script and add it on zabbix_server):

[root@nmp01 scripts] # cd / usr/local/zabbix [root@nmp01 scripts] # cd mkdir scripts [root@nmp01 scripts] # vim / usr/local/zabbix/scripts/sendsms.py

Its contents are as follows:

#! / usr/bin/python#coding:utf-8 URL = 'http://192.168.150.2:8075/xxxx/wmgw.asmx'import sysimport urllibimport urllib2import timedef sendsms (mobile,content): content =' [% s]% s'% s'(time.strftime ('% Y%m%d% HV% MRO% S')) Content) data ='. + mobile +''+ content +'. # body = urllib.urlencode (data) request = urllib2.Request (URL,data) urldata = urllib2.urlopen (request) # print urldata.read () if _ name__ = ='_ main__': sendsms (sys.argv [1], sys.argv [2])

Note: this Python script is an actual SMS sending interface, so a lot of content is omitted in data in order to remove sensitive words. At the same time, the parameters that this script needs to receive from the outside are the mobile phone number and the content of the text message.

[root@nmp01 scripts] # vim / usr/local/zabbix/scripts/send.sh

Its contents are as follows:

#! / bin/shmobiles= (15812341204 13876541234) messages= "for i in $@; do messages=$messages$i"donefor m in ${mobiles [@]}; do. / sendsms.py $m" $messages "done

Note: this script mainly controls which users are sent text messages in the form of loop traversal, and the mobile phone number is placed in the "mobiles" array. The script traverses the parameters as the content of the SMS, and then sends the SMS through the sendsms.py above

Add executable permissions to the script:

[root@nmp01 scripts] # chmod adepx / usr/local/zabbix/scripts/*

(2) configure the Web site of zabbix_server:

Note: you need to select "trigger" when selecting "event Source" in the upper right corner of "Action".

Instead of selecting "send message" but "execute command" here, the command is to execute the above script, that is:

Cd / usr/local/zabbix/scripts./send.sh "{TRIGGER.STATUS}, server: {HOSTNAME1} occurred: {TRIGGER.NAME} failure"

At this point, the configuration of the zabbix SMS alarm is all over, and the personal test can receive the text message normally, so there is no screenshot here.

Note: in this way, it is no longer necessary to configure "alarm media configuration" and users'"alarm media".

PS: the watermark in the above picture is the domain name of my personal blog, so please show mercy not to mark me as "reprinted article", thank you!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Network Security

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report