1. Add custom monitoring items
One of the reasons why zabbix is very popular is that it can customize monitoring projects. Many monitoring projects we have seen before are presented in templates, but when we need some personalized monitoring projects, such as monitoring of a certain business Data requirements: monitor the number of 80-port connections of a web, and draw a picture in two steps: 1) zabbix monitoring center creates a monitoring project; 2) displays the monitoring project in graphics
1) Create a monitoring project
- For the first step, you need to define the script on the client. The name and location of the script can be customized vim /usr/local/sbin/estab.sh //The content is as follows
#!/bin/bash
##获取80端口并发连接数,也就是查看80端口中处于ESTABLISHED状态的链接有多少个,一般正常的也不会超过100
netstat -ant |grep ':80 ' |grep -c ESTABLISHED
- Change the permission to execute the script, mainly to grant the zabbix user the permission to execute the script
# chmod 755 /usr/local/sbin/estab.sh
- Edit the configuration file on the client
# vim /etc/zabbix/zabbix_agentd.conf
Modify the values of the following parameters in the configuration file,
#表示使用自定义脚本,相当于开关,如果等于0,他就没办法使用脚本
UnsafeUserParameters=1
Also find the UserParameter parameter in the configuration file, and set the key value key under the custom monitoring item to my.estab.count, and write the parameters of the script in the following [ ]. Generally, the script has parameters. If there are no parameters, then Can be written , the script is /usr/local/sbin/estab.sh,
UserParameter=my.estab.count[*],/usr/local/sbin/estab.sh
key as shown below
- Restart the zabbix-agent service
# systemctl restart zabbix-agent
2) Graphically display the monitoring item
- First go to the server to verify, execute the following command, make sure that there are no firewall rules and no selinux
[root@lijie-01 ~]# zabbix_get -s 192.168.75.134 -p 10050 -k 'my.estab.count'
0
[root@lijie-01 ~]#
- Then add monitoring items in the zabbix monitoring center (browser) configuration and then jump to the following interface, write the name, the key value write my.estab.count, the others can be left as default, and finally click the add button at the bottom of the page
- After adding the item, go to "Monitoring"--"Latest Data" to see if there is data for the newly added item
. From the above picture, you can see that data has already been generated. - With the data, you can add graphs. Click "Configuration" "Host" "Graph" "Create Graph" in this order
to enter the graphical display interface for creating custom monitoring items. The name can write the number of concurrent connections, the
monitoring item Click the Add button in the box to find the number of concurrent connections we defined in the monitoring item and confirm it, the
page will look like the following image
Finally, click the Add button at the bottom of the page to successfully add the graph
- In the same way, we can also set triggers for the number of concurrent connections of the monitoring item to tell the system when to alarm, but when the expression is selected, we
need to fill in the information in the figure below, and set the alarm when the number of concurrent connections is greater than 200
2. Configure email alerts
- Using 163 or QQ mailbox to send alarm emails is equivalent to calling a third-party mailbox to send alarm emails. Take QQ as an example:
first log in to QQ mailbox, set to enable POP3, IMAP, SMTP services, and use SMS to verify
SMS for the first time. After verification, we need to open and record the authorization code
- Then go to the monitoring center to set up email alerts, "Management", "Alarm Media Type", "Create Media Type", we can see in the following figure that there is an Email alert method by default, but it is not very easy to use, so we can write a python by ourselves The script
then pops up the following page. At this time, we have not written the script yet. The three parameters are {ALERT.SENDTO}, {ALERT.SUBJECT}, {ALERT.MESSAGE}, indicating who to send to, subject, email content, These three things are distributed in the three parameters of the script we wrote
- To create the alarm script mail.py,
we can go to the zabbix configuration file /etc/zabbix/zabbix_server.conf to look for alert and find the following line
AlertScriptsPath=/usr/lib/zabbix/alertscripts
The meaning of this line of code is that the path of the alert script is /usr/lib/zabbix/alertscripts, which means that the alert scripts of zabbix should be placed under this path
# vim /usr/lib/zabbix/alertscripts/mail.py //内容参考https://coding.net/u/aminglinux/p/aminglinux-book/git/tree/master/D22Z?public=true
There are two mail-related python scripts in the above path, both of which need to change gserver to the domain name server of the mailbox used. The first one,
#!/usr/bin/env python
#-*- coding: UTF-8 -*-
import os,sys
reload(sys)
sys.setdefaultencoding('utf8')
import getopt
import smtplib
from email.MIMEText import MIMEText
from email.MIMEMultipart import MIMEMultipart
from subprocess import *
def sendqqmail(username,password,mailfrom,mailto,subject,content):
gserver = 'smtp.qq.com'
gport = 25
try:
msg = MIMEText(unicode(content).encode('utf-8'))
msg['from'] = mailfrom
msg['to'] = mailto
msg['Reply-To'] = mailfrom
msg['Subject'] = subject
smtp = smtplib.SMTP(gserver, gport)
smtp.set_debuglevel(0)
smtp.ehlo()
smtp.login(username,password)
smtp.sendmail(mailfrom, mailto, msg.as_string())
smtp.close()
except Exception,err:
print "Send mail failed. Error: %s" % err
def main():
to=sys.argv[1]
subject=sys.argv[2]
content=sys.argv[3]
##定义QQ邮箱的账号和密码,你需要修改成你自己的账号和密码(请不要把真实的用户名和密码放到网上公开,否则你会死的很惨)
sendqqmail('[email protected]','aaaaaaaaaa','[email protected]',to,subject,content)
if __name__ == "__main__":
main()
#####脚本使用说明######
#1. 首先定义好脚本中的邮箱账号和密码
#2. 脚本执行命令为:python mail.py 目标邮箱 "邮件主题" "邮件内容"
the second
#!/usr/bin/python
#coding:utf-8
import smtplib
from email.mime.text import MIMEText
import sys
mail_host = 'smtp.163.com'
mail_user = '[email protected]'
mail_pass = '1111111'
mail_postfix = '163.com'
def send_mail(to_list,subject,content):
me = "zabbix 监控告警平台"+"<"+mail_user+"@"+mail_postfix+">"
msg = MIMEText(content, 'plain', 'utf-8')
msg['Subject'] = subject
msg['From'] = me
msg['to'] = 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
if __name__ == "__main__":
send_mail(sys.argv[1], sys.argv[2], sys.argv[3])
- Permission to modify configuration files
# chmod 755 /usr/lib/zabbix/alertscripts/mail.py
Then let's test whether the email can be sent successfully, if it is sent successfully, there will be no prompt
[root@lijie-01 alertscripts]# python mail.py ******@qq.com "title" "content"
I used qq mailbox to send emails to test several times without success, then I changed to use 163 mailbox to send emails to my qq mailbox OK
- Create a user to receive alert emails, "Administration", "User", "Create User", "Alarm Media", select "QQalert" for the type,
and then fill in the following information
There are also alarm media
. Note that the user's permission is currently no permission, if here If the settings are incorrect, you will not receive the alarm email in the end. In this case, you
need to go to the user group to set permissions.
Jump to the following page
- After the user configuration is completed, you need to set the action. The action is the action that needs to be done after the alarm trigger is triggered. "Configure", "Action", and "Create Action"
name write "sendmail" (custom), Condition A is [Maintenance] Status not under maintenance] means that there is some maintenance that does not require sending emails during real maintenance. For example, when we restart the network service, it is not real maintenance, so there is no need to send emails; Condition B [trigger Alert degree>=Uncategorized] means that all alerts in the state need to send emails
- "Operation", select the user to send to the newly created user, only to select "QQalert" The
above defines the action of sending emails, we come to the "Operation" page, define who to send to, subject, content and other information, the default Information content We fill in the following content:
HOST:{HOST.NAME} {HOST.IP}
TIME:{EVENT.DATE} {EVENT.TIME}
LEVEL:{TRIGGER.SEVERITY}
NAME:{TRIGGER.NAME}
messages:{ITEM.NAME}:{ITEM.VALUE}
ID:{EVENT.ID}
The content to be filled in is as shown in the figure below, in which the condition can be set without setting, and condition A indicates that the event is in an unconfirmed state to send an email
- Switch to "Restore Operation" and change the default information to the following
HOST:{HOST.NAME} {HOST.IP}
TIME:{EVENT.DATE} {EVENT.TIME}
LEVEL:{TRIGGER.SEVERITY}
NAME:{TRIGGER.NAME}
messages:{ITEM.NAME}:{ITEM.VALUE}
ID:{EVENT.ID}
The other information to be filled in is as follows: After the
above operations are completed, click the Add button at the bottom of the page to add successfully. After success, the following figure is shown.
- Test the alarm In order to test the alarm, we create a new trigger in the host lijie-02. The configuration information is as follows. We have not done any operation on the zabbix-agent here, so the system load is 0. Below we set when the system load is less than 1. Send email alerts from time to time After
waiting for a while, we can receive the email, and we can see the following prompt on the zabbix home page:
If the [Complete] in the above picture becomes empty, it is very likely that the script does not have permission. If it is [Failure], you need to move the mouse over the word "failed" to check the specific failure reason. The received alarm email information is as follows:
We have tested the problem alarm above, and now we will test whether the notification message after the problem is solved can be sent successfully. Let's first change the trigger created above in lijie-02 to send an email alarm when the system load is greater than 1.
Since the current system load is 0, so the alarm is cleared, we can receive the email of the alarm cancellation
and can see Go to the page below and the prompt has been resolved