Sixteen four classes

Sixteen four-time classes (April 16)

19.12 Adding custom monitoring items
19.13/19.14 Configuring email alerts
19.15 Testing alerts
19.16 Handling the problem of not sending emails

Add custom monitoring items

  • Requirement: Monitor the number of port 80 connections of a web site and plot the graph
  • Two steps: 1) zabbix monitoring center creates a monitoring project; 2) displays the monitoring project graphically
  • For the first step, you need to define the script to the client
  • vim /usr/local/sbin/estab.sh //The content is as follows
#!/bin/bash
##获取80端口并发连接数
netstat -ant |grep ':80 ' |grep -c ESTABLISHED
  • chmod 755 /usr/local/sbin/estab.sh
  • Edit the configuration file vim /etc/zabbix/zabbix_agentd.conf// on the client to add
UnsafeUserParameters=1  //表示使用自定义脚本
UserParameter=my.estab.count[*],/usr/local/sbin/estab.sh 
//自定义监控项的key为my.estab.count,后面的[*]里面写脚本的参数,如果没有参数则可以省略,脚本为/usr/local/sbin/estab.sh
  • Restart zabbix-agent service systemctl restart zabbix-agent
  • First go to the server to verify and execute the command
  • zabbix_get -s 192.168.133.132 -p 10050 -k 'my.estab.count‘
  • Then add monitoring items in the zabbix monitoring center (browser) configuration
  • The key value is written my.estab.count
  • After adding the item, go to "Monitoring"  "Latest Data" to see if there is any data for the newly added item
  • Once you have the data, you can add graphics
  • "Configuration"  "Host"  "Graphics"  "Create Graph"

Add custom monitoring items

  • The monitoring items of zabbix are reflected in the template, but sometimes there are some personalized needs: the data of a certain business, such as: operators want to know the number of website member registrations and member activity; in fact, these data can be It is reflected by the numbers of the tables in the database, but it is impossible for the zabbix template to provide such detailed monitoring items; in this case, you need to use programming tools to write scripts to display the numbers, and then monitor the items through zabbix to form Chart, you can realize the analysis of the daily data;
  • need
    • Monitor the number of connections on port 80 of a web site and plot the graph
  • Solution:
    • 1) The zabbix monitoring center creates a monitoring project;
    • 2) Graphically display the monitoring item;
  1. First you need to define the script on the client (B machine)
[root@hf-02 ~]# vim /usr/local/sbin/estab.sh

#!/bin/bash
##获取80端口并发连接数
netstat -ant |grep ':80 ' |grep -c ESTABLISHED

保存退出 
  1. Configure a permission that allows the zabbix user to execute
  • chmod 755 /usr/local/sbin/estab.sh
B机器
[root@hf-02 ~]# chmod 755 /usr/local/sbin/estab.sh
[root@hf-02 ~]# 
  1. Edit the zabbix configuration file on the client
B机器
[root@hf-02 ~]# vim /etc/zabbix/zabbix_agentd.conf

搜索 /UnsafeUserParameters

将# UnsafeUserParameters=0
改为UnsafeUserParameters=1  
#目的是使用自定义脚本

搜索 /UserParameter
将# UserParameter=
改为UserParameter=my.estab.count[*],/usr/local/sbin/estab.sh 
#表示自定义监控项的key为my.estab.count,后面的[*]里面写脚本的参数,如果没有参数则可以省略,如果有参数写入到 [ ] 里面,用逗号分隔,脚本为/usr/local/sbin/estab.sh

保存退出
  • my.estab.count This is the key value in the zabbix monitoring item
  1. Client (B machine), restart the zabbix-agent service
B机器
[root@hf-02 ~]# systemctl restart zabbix-agent
[root@hf-02 ~]# 
  1. Check if the custom script is recognized
  2. Go to the server (machine A) to verify, execute the command, the server cannot get the data
  • zabbix_get -s 192.168.202.131 -p 10050 -k 'my.estab.count'
    • -s specifies the IP address of the client
    • -p specifies the port, without -p, the default is port 10050
    • -k means key value
A机器
[root@hf-01 ~]# zabbix_get -s 192.168.202.131 -p 10050 -k 'my.estab.count'
0
[root@hf-01 ~]#
  • If the script permission previously written on the client (machine B) is not set to 755, the server here will prompt that the permission is insufficient when it obtains data
  1. When acquiring data, it is necessary to ensure that the data is normal, firewall and selinux need to be set, otherwise it may not be possible to acquire data
  2. Add monitoring items in the zabbix monitoring center (browser) configuration, in Configuration -> Host -> Create Monitoring Items
  • The name is written as: number of concurrent links
  • Type: select zabbix client
  • The key value is written: my.estab.count --> the key value can be customized and is not fixed
  • Units: keep the default
  • Historical data retention time (unit day): 90 days by default
  • Application set: can not be selected, because no application set is defined

Enter image description

  1. After adding the project, you also need to add graphics, configure -> host -> graphics -> create graphics

Enter image description

  • The name is the number of concurrent links created -> then add the number of concurrent links to the monitoring item -> click Add

Enter image description Enter image description

  1. In the graph, view the newly created graph and select Preview
  • If the website has a lot of traffic, you can see a number every 30 seconds. After a long time, you can see the trend of website traffic.

Enter image description

  1. To make a trigger, select Trigger -> Create Trigger
  • When the concurrency is greater than 200, do the warning level

Enter image description Enter image description

  1. This triggers the configuration successfully

Summarize:

  • Get the value on the server (through a script), give the server permission to access the script, and add the corresponding monitoring item (input key value) in the zabbix monitoring center.

19.13/19.14 Configuring Email Alerts

Configure Email Alerts

  • Use 163 or QQ mailbox to send alert emails
  • First log in to your 163 mailbox, set to open POP3, IMAP, SMTP services
  • Turn on and record the authorization code
  • Then go to the monitoring center to set up email alerts
  • "Manage", "Alarm Media Types", "Create Media Types"
  • {ALERT.SENDTO} ,{ALERT.SUBJECT},{ALERT.MESSAGE}

Enter image description

  • Create the alarm script mail.py
  • vim /usr/lib/zabbix/alertscripts/mail.py//content reference
  • chmod 755 /usr/lib/zabbix/alertscripts/mail.py
  • Create a user to receive alarm emails, "Administration", "User", "Create User", "Alarm Media", select "baojing" for the type, pay attention to the user's permissions, if not, you need to go to the user group to set permissions
  • Set the action, "Configure", "Action", "Create Action", write the name "sendmail" (custom), "Operation" page, the content is as follows
HOST:{HOST.NAME} {HOST.IP}
TIME:{EVENT.DATE}  {EVENT.TIME} 
LEVEL:{TRIGGER.SEVERITY} 
NAME:{TRIGGER.NAME}
messages:{ITEM.NAME}:{ITEM.VALUE}
ID:{EVENT.ID}
  • "New trigger condition", A maintenance status is not under maintenance, B trigger warning degree >= uncategorized
  • "Operation", select the user to be sent is the newly created user, only select "baojing"
  • Switch to "Restore Operation" and change the 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}
  • Click "New", "Operation", select the user to be sent to the newly created user, and only select "baojing"
  • Test alert

Configure Email Alerts

  1. First, you need a mailbox, use the mailbox to send emails (163 mailboxes)
  2. To access 163 mailboxes and call 163 mailboxes to send emails on third-party platforms, you need to set up POP3, IMAP, and SMTP services.
  • Settings - POP3/IMAP/SMTP
    • The first time you open the POP3/IMAP/SMTP service, there will be SMS verification
    • After verification, you also need to set the client authorization password (you need to keep it in mind), and the password for sending emails is the authorization password.
  1. Before writing the script, enter the web interface of zabbix --> management --> alarm media type --> create media type
  • The built-in Email in the alarm media type is inconvenient to use, so write your own script for sending emails in python

Enter image description

  • Name: refers to the name of the alarm media type, custom, here is called baojing
  • Type: Script
  • Script name: custom, here called mail.py
  • Under the py script parameters, the three parameters are distributed in the py script
    • {ALERT.SENDTO} represents the recipient
    • {ALERT.SUBJECT} represents the subject of the email
    • {ALERT.MESSAGE} represents the message content
  1. At this time, you will see that the baojing alarm media type has been successfully added.

Enter image description

  1. To achieve the alarm, first the py script can send out an email
  2. Create an alarm script mail.py on the server (machine A) and check the location of the alarm script
A机器
[root@hf-01 ~]# vim /etc/zabbix/zabbix_server.conf

搜索 /alert
AlertScriptsPath=/usr/lib/zabbix/alertscripts
#报警脚本所在路径

  1. Create the alarm script mail.py, which is empty in the /usr/lib/zabbix/alertscripts/ directory, to create, the script content reference
A机器
[root@hf-01 ~]# vim /usr/lib/zabbix/alertscripts/mail.py

#!/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.163.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('163邮箱','密码','163邮箱',to,subject,content)

if __name__ == "__main__":
    main()
    
    
#####脚本使用说明######
#1. 首先定义好脚本中的邮箱账号和密码
#2. 脚本执行命令为:python mail.py 目标邮箱 "邮件主题" "邮件内容"

保存退出
  1. Change the permissions of the mail.py script (important!!!), otherwise the alert will not work
  • chmod 755 /usr/lib/zabbix/alertscripts/mail.py
[root@hf-01 ~]# chmod 755 /usr/lib/zabbix/alertscripts/mail.py
[root@hf-01 ~]# 
  1. Test whether mail.py can send emails, define three parameters, if you send emails to QQ mailboxes, you may not receive them
[root@hf-01 ~]# cd /usr/lib/zabbix/alertscripts/
[root@hf-01 alertscripts]# python mail.py [email protected] "ssss" "dddd"
[root@hf-01 alertscripts]#
  • If there is no prompt, it means that the email was sent successfully
  1. Then open the 163 mailbox, you will see the mail

Enter image description

  1. If you write Chinese in the email, it will display garbled characters in the mailbox. This is because the utf-8 character set is used in zabbix, and the GBK character set is used in the mailbox, so it will be garbled.
[root@hf-01 alertscripts]# python mail.py [email protected] "邮件" "dddd"
[root@hf-01 alertscripts]# 

Enter image description

  1. Create a user to receive alarm emails (the user is used to receive emails). The premise of creating a user is to create a user group. The user group can use the existing one, so create a user directly here, Manage --> User --> create user

Enter image description

  1. Then set the mailbox for the user just now, Manage-->Users-->Alarm Media-->Add

Enter image description

  1. Also pay attention to the user's permissions. If you don't have permissions, you can't send emails. You have to temporarily go to the user group (Management --> User Group)
  • The default permission is none, you need to modify the group and give permission

Enter image description

Enter image description

  1. Set the action, we have configured the trigger, then what should be done after the trigger, configure --> action --> create action
  • action
    • Name: Both Chinese and English are available, here is called sending an email
    • Condition: The maintenance state is not under maintenance, it means that the maintenance state machine does not send emails
      • The new trigger condition, when the trigger warning degree (that is, information, warning, general serious...) is greater than or equal to uncategorized (uncategorized means all states, will send information)

Enter image description

  • Configuration --> Actions --> Actions
    • Default information: directly delete the original (more messy), paste the following content
    • Suspend the operation during maintenance: select a check (if you don't check it, it is also possible, because it is defined in the conditions of the action)
默认信息定义当发生报警时,zabbix会向你的邮箱发送什么内容
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)

Enter image description

  • Configuration-->Actions-->Restore Actions
    • Default information: directly delete the original (more messy), paste the following content (the pasted content and the copied information in the operation are the same)
HOST:{HOST.NAME} {HOST.IP}
TIME:{EVENT.DATE}  {EVENT.TIME} 
LEVEL:{TRIGGER.SEVERITY} 
NAME:{TRIGGER.NAME}
messages:{ITEM.NAME}:{ITEM.VALUE}
ID:{EVENT.ID}

Enter image description Enter image description

  • Finally click Add to confirm

Enter image description

19.15 Test Alarms

Test alert

  1. Create triggers to implement alarms, configure --> host --> hf-02 host --> create trigger
  • Name: System Load
  • Severity: Warning
  • Expression: as follows

Enter image description

  • select add

Enter image description

  • Finally see the following

Enter image description

  1. Then go back to the monitoring center, home page --> last 20 questions
  • If the prompt is enabled, it means that a problem has been found, the alarm is being enabled, and the question is completed, it means that an email alarm has been sent; as shown in the figure, our experiment is successful

Enter image description

  • Check the mailbox, you will see the mail sent

Enter image description

  1. This means that the test email alert was successful
  2. At this time, if you want to solve this problem, you only need to adjust the value of the trigger system load condition to > 1, and then check the monitoring center, you will find that the problem has disappeared, and an OK email will be sent to the mailbox

19.16 Handling the problem of not sending emails

Troubleshooting the problem of not sending emails

  • Because of the virtual machine, there may be some bugs. When it is configured for the first time, zabbix often finds problems and makes email alerts, but the mailbox does not receive emails;
  • After restoring the snapshot, do it again, and you can send and receive emails normally again.

Configure Email Alert Summary

  1. Go to 163 mailbox or QQ mailbox to open the SMTP service and record the authorization code. If it is not turned on, the script cannot write the password.
  2. Set up email alerts
  • First set an alarm medium;
    • The three parameters must be filled in correctly, and the order must not be wrong
  1. Edit email alert script, script address
  • /usr/lib/zabbix/alertscripts directory
  • The path cannot be wrong, you must add a script under this path
  1. After creating it, remember to give the script 755 permissions, otherwise the zabbix user cannot call the script
  2. Configure users
  • Before configuring users, change the permissions of the user group so that the group has a read and write permission
  • Add the user to the specified group
  • Alarm media, add your own email address and send it to yourself, the success rate will be higher
  1. configure action
  • The operation modifies the default information (the format of the sent mail), and the operation needs to add a "new" to whom to send the mail; the recovery operation is the same
  1. Test to see if the alert can send emails successfully
  • Create a trigger artificially, and set a trigger that will trigger the alarm when the system is running at the lowest level
  • It is also possible to disconnect on the client (stop the client's zabbix service)
  1. Change active and passive
  • PS: Client active mode and passive mode, in the monitoring item, select a monitoring item to open, the selection type is client active, client; client active is active mode, client is passive mode
  1. The trigger cannot be named in Chinese, otherwise the email will display garbled characters

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324973770&siteId=291194637