19.12 Adding custom monitoring items
The purpose of this configuration is to monitor monitoring items that are not in the template.
Requirement: Monitor the number of port 80 connections of a web site and draw a graph.
Steps:
1) The zabbix monitoring center creates a monitoring project
2) Graphically displays the monitoring project
Configure the monitoring script for port 80
Client: Count the number of connections on port 80
[root@z1 ~]# vim /usr/local/sbin/estab.sh
#!/bin/bash
##Get the number of concurrent connections on port 80
netstat -ant |grep ':80 ' |grep -c ESTABLISHED
Note: after 80 Followed by a space to ensure more accurate matching, if no space is added, port 8080 will be filtered out at the same time.
Change permissions:
[root@z1 ~]# chmod 755 /usr/local/sbin/estab.sh
Configure zabbix-agent.conf:
[root@z2 ~]# vim /etc/zabbix/zabbix_agentd.conf
UnsafeUserParameters=1 #means to use custom script
UserParameter=my.estab.count[ ],/usr/local/sbin/estab .sh
#The key of the custom monitoring item (the "key value" on the monitoring web side) is my.estab.count, and the script parameters are written in the following [ ] #If there is
no parameter, it can be omitted, the script is /usr/local/ sbin/estab.sh
Restart the zabbix service:
[root@z2 ~]# systemctl restart zabbix-agent.service
After the client configuration is complete, use the zabbix built-in command on the server to test whether the script takes effect:
[root@z1 ~]# zabbix_get -s 192.168.8.138 -p 10050 -k 'my.estab.count'
0
As shown above, the client script configuration is successful!
Configure web parameters
Browser access server zabbix homepage: 192.168.8.136/zabbix
Enter the client adai-02 "Monitoring item configuration" page:
mark
"Create a monitoring item":
mark
description: Set the name and key value of the monitoring item here, and keep other parameters by default (of course, after thorough research, all parameters can be changed according to personal needs).
"Create Graph":
After the "Add" is added, configure "Graph" - "Create Graph" for the monitoring item:
mark Note: After setting the name of the graph, you need to "Add" the graph first (the addition is Add) monitoring item under the parameter "Monitoring Item", and then click "Add" at the bottom to add the graphic parameter to the zabbix graphic monitoring parameter! ! !
Create a trigger:
The so-called trigger is to set the rules for triggering the alarm!
"Trigger" - "Create Trigger":
mark
click "Add" to set the object to be monitored by the trigger and the triggering rule:
mark
That is, when the latest value T of the number of concurrent connections is greater than N (customize this value, in This alarm is triggered when N=2).
19.13-19.4 Configuring Email Alerts
Use 163 mailbox or QQ mailbox to send alert emails.
Enable POP3, IMAP, SMTP services for mailboxes
Log in to 163 mailboxes and enable POP3, IMAP, and SMTP services:
mark
After logging in to the mailbox, "Settings" - "POP3, IMAP, SMTP" - "Save":
mark
description: User authentication is required to open the service for the first time. Perform authentication according to the prompt information and set the "client authorization password" (the special password used to log in to the third-party mail client), and record the password.
Configure Alert Scripts
After the mailbox-related services are configured, go to the monitoring center for alarm configuration.
Configure Alarm Media Type
"Management" - "Alarm Media Type" - "Create Media Type":
mark
description: Set the media type name, type, script name (consistent with the script name in the server), add script parameters (this parameter is a built-in parameter of zabbix, and will be configured in the mail.py script later), after the above content is configured Then click "Add" (blue at the bottom).
Add alarm script mail.py
Server:
View the alert script save path:
[root@z1 ~]# vim /etc/zabbix/zabbix_server.conf
AlertScriptsPath=/usr/lib/zabbix/alertscripts
Create alert script mail.py:
[root@z1 ~]# cd /usr/lib/zabbix/alertscripts
[root@z1 alertscripts]# vim mail.py
#!/usr/bin/python
#coding:utf-8
import smtplib
from email.mime.text import MIMEText
import sys
mail_host = 'smtp.163.com' #Specify the mailbox type used
mail_user = '[email protected]' #Specify the mailbox username
mail_pass = '111111111' #Specify the mailbox password
mail_postfix = '163.com'
def send_mail(to_list,subject,content):
me = "zabbix monitoring and alerting platform"+"<"+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])
#####Script Instructions######
#1. First define the email account and password in the
script#2. The script execution command is: python mail.py target mailbox "mail subject" "mail content"
Change script permissions:
[root@z1 alertscripts]# chmod 755 /usr/lib/zabbix/alertscripts/mail.py
Test whether the mail can be sent normally:
[root@z1 alertscripts]# python mail.py [email protected] "emerge" "emerge2" #Here,
let the 163 mailbox send mail to itself (to ensure that it does not have other faults)
mark
send success! So far, the script for sending emails is no problem! ! !
Configure users
Create a "user" in the monitoring center (web side) to receive alarm emails. Before creating a user, you need to create a "user group", and use an existing user group here. "Administration" - "Users" - "Create User":
mark After setting the above information, click "Add" at the bottom.
Configure Alarm Media
Open user adai - alarm media - add alarm media - update:
mark
Configure user permissions:
You need to go to the "User Group" where the user is located to change the user's permissions:
mark After selecting the permission, click "Add" - "Update", if the setting is wrong here, you will not be able to receive the alarm email.
configure action
Set the action to be performed when the trigger is fired!
"Configure" - "Action" - "Create Action":
mark
"Action":
The action to take when the client fails.
HOST:{HOST.NAME} {HOST.IP}
TIME:{EVENT.DATE} {EVENT.TIME}
LEVEL:{TRIGGER.SEVERITY}
NAME:{TRIGGER.NAME}
messages:{ITEM.NAME}:{ITEM.VALUE }
ID:{EVENT.ID}
ID:{EVENT.ID} Change
the "default information" in "operation" to the above parameters:
mark
Click "New" to configure the parameters specified in the above figure, and the rest of the parameters remain default (this part of the information is the specific behavior of sending emails). After configuring the above content, click "Add" (here == non-blue == Add button) .
Recovery operation: the operation
that needs to be performed after the client state is recovered.
"Default information": (same as in the operation)
HOST:{HOST.NAME} {HOST.IP}
TIME:{EVENT.DATE} {EVENT.TIME}
LEVEL:{TRIGGER.SEVERITY}
NAME:{TRIGGER.NAME}
messages:{ITEM.NAME}:{ITEM.VALUE }
ID:{EVENT.ID}
ID:{EVENT.ID}
The condition parameter is also the same as in Action.
mark
After the above "action" parameters are configured, click the blue button "Add"! ! !
mark
The monitoring center alarm preparation is complete! ! !
19.15 Test Alarms
Change the monitoring item to the fault item.
"Configuration" - "Host" - "Trigger" - "Create Trigger"
Note: This trigger is only used for testing, delete it after completing the test!
mark
Select the "Add" expression to set the triggering rule:
mark
Other parameters remain default, click the blue button "Add". After the configuration is complete, enter the "Dashboard" and wait...
mark
After many attempts, it shows that the action is complete, but I don't receive any emails when I check the mailbox! ! !
Troubleshoot
Send mail using server:
[root@z1 alertscripts]# python mail.py [email protected] "emergecome" "emerge333"
mark
That is, if the email can be sent normally, it means that the content of the script is fine!
Change script permissions:
[root@z1 alertscripts]# ls -l /usr/lib/zabbix/alertscripts/
total usage 4
-rwxr-xr-x 1 root root 1243 Sep 12 07:54 mail.py
[root@z1 alertscripts ]# chmod 777 /usr/lib/zabbix/alertscripts/mail.py
check user information
Add a mailbox that accepts mail:
mark
Check the user permissions, make sure it's "read and write"!
Check Action Configuration
Change the default information in the action to its own configuration:
mark
Do not change it in "Recovery Operation"! Tested after the update, the result is still unable to receive the email! ! !
Decisively restore the snapshot and do it again...
Check again after configuration:
mark
Fault:
mark
recover:
mark
Set active mode or passive mode in zabbix monitoring center
Configuration - Template - Monitoring Item:
mark