Linux CentOS-7 system offline installation and deployment of Zabbix--->Customized monitoring--->SMS alarm detailed tutorial

In the internal network environment, we cannot connect to the external network. If we want to use zabbix at this time, we need to install it offline.

There are three ways to install and deploy Zabbix:

The first is to use source code installation , the second is to use RPM package installation , and the third is to use container deployment . What we are talking about here is RPM package installation.

You can download the package online yourself, or you can use the RPM installation package I have prepared.

Table of contents

1. Prepare the installation package required for zabbix on your own computer virtual machine

1. Turn off the firewall and SELINUX safe mode

 2. Install the rpm source of zabbix

3. Download the required installation package for zabbix

        1. Download zabbix service and agent packages

        2. Download Red Hat Software Collections: to facilitate the installation of higher versions of php.

        3. Download the Zabbix front-end web software package.

        4. Download the mysql database:

        5. Create a local yum repository

2. Install zabbix offline in the intranet environment

1. Zabbix server preparation

        1. Configure the host name

        2. Turn off the firewall and SELINUX safe mode

2. Back up system YUM source

3. Local yum warehouse configuration

4. Start installing zabbix

        1. Install zabbix server, agent, Software Collections and mysql database.

        2. Install zabbix front-end web.

         3. Configure database

         4. Initialize the database and set the database login password

        5. Enter the database and create the zabbix database

        6. Import initial schema and data

         7. Modify the zabbix_server.conf configuration file

        8. Modify the php configuration file of zabbix

        9. Start the Zabbix server and agent processes and set them to start automatically at boot 

        10. Configure Zabbix front-end-web interface installation

 5. Add client zabbix-agent

          1. Turn off the firewall and SELINUX safe mode

        2. Upload the zabbix-agent installation package to /tmp

         3. Install zabbix-agent

        4. Configure zabbix-agent

        5. Start zabbix-agent

        6. Front-end-web interface added

3. Customized monitoring

1. Script writing

2. Zabbix front-end interface configuration operation

        1. Create a new template

2. Create a new application set

 3. Create new monitoring items

 4. Create a new trigger

 5. Apply templates

6. Check whether it is successful

 4. Information alarm

1. Script addition

2. Add SMS alarm media to the zabbix operation interface;

3. Add SMS alarm method for designated users;

4. Set when the action will send SMS alarm;

5. Test


1. Prepare the installation package required for zabbix on your own computer virtual machine

1. Turn off the firewall and SELINUX safe mode

#关闭防火墙并设置开机不启动
systemctl stop firewalld

systemctl disable firewalld

#关闭SElinux安全模式
setenforce 0
    
#设置永久关闭selinux

vi /etc/selinux/config

#修改SELINUX:
SELINUX=disabled

#重启生效
reboot

cced805592664616b37f80cfb8a5b90c.pngba2a22441edb4fba925bfdb192866e7b.png

 2. Install the rpm source of zabbix

#使用国内镜像源——阿里云源
rpm -Uvh https://mirrors.aliyun.com/zabbix/zabbix/5.0/rhel/7/x86_64/zabbix-release-5.0-1.el7.noarch.rpm

#更改使用国内镜像源
sed -i 's#http://repo.zabbix.com#https://mirrors.aliyun.com/zabbix#' /etc/yum.repos.d/zabbix.repo

#清除所有缓存
yum clean all
 

4ab5a20e20a942e7a89b3d4869650aee.png

3. Download the required installation package for zabbix

Use the yum --downloadonly command to only download the rpm package without installing it and caching it.

        1. Download zabbix service and agent packages

yum install zabbix-server-mysql zabbix-agent --downloadonly --downloaddir=/tmp/offline_rpm

#如报没有downloadonly功能,下载即可
yum install yum-plugin-downloadonly

        2. Download Red Hat Software Collections: to facilitate the installation of higher versions of php.

yum install centos-release-scl --downloadonly --downloaddir=/tmp/offline_rpm

#安装 Software Collections。
yum install centos-release-scl -y

#将[zabbix-frontend]下的 enabled 改为 1.
vi /etc/yum.repos.d/zabbix.repo

28c0dee9c50544a3a4f943f0971b86a6.png

        3. Download the Zabbix front-end web software package.

yum install zabbix-web-mysql-scl zabbix-apache-conf-scl --downloadonly --downloaddir=/tmp/offline_rpm

        4. Download the mysql database:

yum install mariadb-server.x86_64 --downloadonly --downloaddir=/tmp/offline_rpm

        5. Create a local yum repository

Use createrepo to create the repository warehouse. If there is no such command in the system, you need to install createrepo first.

9f8e4b6d807b4c84b1b9617fba62f048.png

yum install createrepo -y
# 使用createrepo来创建repository仓库
createrepo /tmp/offline_rpm

7eed145577594218a86fe37ab66f7ecc.png

#到存储目录下
cd /tmp/offline_rpm/

#查看是否有repodata这个文件夹,有就是创建成功了
ls

fe75de618b434375af2a023dc0f3993b.png

2. Install zabbix offline in the intranet environment

Copy all the offline_rpm folders of /tmp/offline_rpm into the /tmp directory on the zabbix server prepared on the intranet .

1. Zabbix server preparation

        1. Configure the host name

hostnamectl set-hostname zabbix-server

bash
05a794a705fc45a5953fc4e3cb2fab3c.png

        2. Turn off the firewall and SELINUX safe mode

#关闭防火墙并设置开机不启动
systemctl stop firewalld

systemctl disable firewalld

#关闭SElinux安全模式
setenforce 0
    
#设置永久关闭selinux

vi /etc/selinux/config

#修改SELINUX:
SELINUX=disabled

#重启生效
reboot

2. Back up system YUM source

cd /etc/yum.repos.d

#创建备份文件夹
mkdir bak

#所有的repo文件移动到bak下
mv *.repo bak

e1c6d6d4048440be9fc2f19573ccd388.png

3. Local yum warehouse configuration

        1. Create a new repo file under /etc/yum.repos.d/.

vi /etc/yum.repos.d/new.repo

        2. Write the following content.

[New]
name=New
baseurl=file:///tmp/offline_rpm
gpgcheck=0
enabled=1
        3. Clear all caches
yum clean all

2ea4abe0c27244028d15687b723fa0ae.png

4. Start installing zabbix

        1. Install zabbix server, agent, Software Collections and mysql database.

        Because there are packages in the configured yum warehouse, you can install them directly with the command

yum install zabbix-server-mysql zabbix-agent -y


yum install centos-release-scl -y


yum -y install mariadb-server.x86_64 

        After installing zabbix server, agent and Software Collections, two new .repo files will be generated under /etc/yum.repos.d and moved to bak.

ls

mv CentOS-SCLo-scl.repo CentOS-SCLo-scl-rh.repo bak

ls

df8774c2ffa84bebb7a5865601b99a8e.png

   Note: If you do not move it, the following error will be reported

Cannot find a valid baseurl for repo: centos-sclo-rh/x86_64

        2. Install zabbix front-end web.

yum install zabbix-web-mysql-scl zabbix-apache-conf-scl -y

         3. Configure database

#启动
systemctl start mariadb

#设置开机自启动
systemctl enable mariadb

#查看
systemctl status mariadb

0c4556c38e5d4bd0bb3b576bc64b0137.png

         4. Initialize the database and set the database login password

mysql_secure_installation
[root@zabbix-servre yum.repos.d]# mysql_secure_installation
NOTE: RUNNING ALL PARTS OF THIS SCRIPT IS RECOMMENDED FOR ALL MariaDB
SERVERS IN PRODUCTION USE! PLEASE READ EACH STEP CAREFULLY!
In order to log into MariaDB to secure it, we'll need the current
password for the root user. If you've just installed MariaDB, and
you haven't set the root password yet, the password will be blank,
so you should just press enter here.
Enter current password for root (enter for none): #此处直接回车
OK, successfully used password, moving on...
Setting the root password ensures that nobody can log into the MariaDB
root user without the proper authorisation.
8、创建zabbix数据库。
Set root password? [Y/n] y #此处y
New password: #输入密码
Re-enter new password: #再次输入密码
Password updated successfully!
Reloading privilege tables..
... Success!
By default, a MariaDB installation has an anonymous user, allowing anyone
to log into MariaDB without having to have a user account created for
them. This is intended only for testing, and to make the installation
go a bit smoother. You should remove them before moving into a
production environment.
Remove anonymous users? [Y/n] y #此处y
... Success!
Normally, root should only be allowed to connect from 'localhost'. This
ensures that someone cannot guess at the root password from the network.
Disallow root login remotely? [Y/n] n #此处n
... skipping.
By default, MariaDB comes with a database named 'test' that anyone can
access. This is also intended only for testing, and should be removed
before moving into a production environment.
Remove test database and access to it? [Y/n] y #此处y
- Dropping test database...
... Success!
- Removing privileges on test database...
... Success!
Reloading the privilege tables will ensure that all changes made so far
will take effect immediately.
Reload privilege tables now? [Y/n] y #此处y
... Success!
Cleaning up...
All done! If you've completed all of the above steps, your MariaDB
installation should now be secure.
Thanks for using MariaDB!
[root@zabbix-servre yum.repos.d]#

        5. Enter the database and create the zabbix database

        Note: Set the encoding format to utf-8.

#进入数据库
mysql -u root -p

#创建数据库
create database zabbix character set utf8 collate utf8_bin;

#创建用户(注意修改password,这是之前初始化输入的密码)
create user zabbix@localhost identified by 'password';

#授权
grant all privileges on zabbix.* to zabbix@localhost;

#修改生效
flush privileges;

#退出
quit;

        6. Import initial schema and data

        You will be prompted for your password.

zcat /usr/share/doc/zabbix-server-mysql*/create.sql.gz | mysql -uzabbix -p zabbix

         7. Modify the zabbix_server.conf configuration file

#为zabbix server配置数据库
#修改内容
#DBPassword=password(你之前输的密码)
vi /etc/zabbix/zabbix_server.conf 

218550ffcccf450a858743fecb443796.png

        8. Modify the php configuration file of zabbix

#修改内容
#将注释;去掉,并修改为
#php_value[date.timezone] = Asia/Shanghai
vi /etc/opt/rh/rh-php72/php-fpm.d/zabbix.conf 

e1fca5227c5e46ac83b0e05104f09edd.png

        9. Start the Zabbix server and agent processes and set them to start automatically at boot 

#启动
systemctl start zabbix-server zabbix-agent httpd rh-php72-php-fpm

#设置开机自启动
systemctl enable zabbix-server zabbix-agent httpd rh-php72-php-fpm

#查看
systemctl status zabbix-server zabbix-agent httpd rh-php72-php-fpm

        10. Configure Zabbix front-end-web interface installation

        Connect to Zabbix front end: http://IP/zabbix

b944d0f5743644748dd7c6dee6993e8c.png

         Check all conditions

2881f727ec254d07ad5edc8f6ae18b55.png

         Configure database information

1300719378de414ab7e7b1aaf65daccf.png

        Server basic information 

cedc615fe1a94ca0a504ea8c8b813ed3.png

        Confirm information

ba81d0a9847c4428a26aa51923da025b.png

         The installation is complete

ba211da24ea046afa9f950129c177a99.png

         Log in to the front-end page: system default username and password Admin/zabbix (note case)

18d22bccebab4ab08e7a219ccbd3d682.png

         Modify to Chinese

1f796dcdf9d34e95a15506039e6fa0a3.png

b33e29fda83c446dace1512306f3c9d1.png

 5. Add client zabbix-agent

          1. Turn off the firewall and SELINUX safe mode

#关闭防火墙并设置开机不启动
systemctl stop firewalld

systemctl disable firewalld

#关闭SElinux安全模式
setenforce 0
    
#设置永久关闭selinux

vi /etc/selinux/config

#修改SELINUX:
SELINUX=disabled

#重启生效
reboot

        2. Upload the zabbix-agent installation package to /tmp

        It was previously in the offline_rpm folder

b04995ac698843ea8406c4022ce2f1bd.png

         3. Install zabbix-agent

        cd to the directory where the files are stored to install

cd /tmp

#查看
ls

#安装
yum install zabbix-agent-5.0.32-1.el7.x86_64.rpm -y

0db9f82d1cf041e68ba9046563d88dfc.png

        4. Configure zabbix-agent

vi /etc/zabbix/zabbix_agentd.conf

修改内容如下
# zabbix 服务端地址
Server=192.168.222.23
# zabbix活动服务器地址
ServerActive=192.168.222.23
# 主机名,也就是要监控的那台主机名,在web页面添加主机时需设置相同
Hostname=zabbix-agent

        5. Start zabbix-agent

#启动
systemctl start zabbix-agent

#设置开机自启动
systemctl enable zabbix-agent

#查看
systemctl status zabbix-agent

        6. Front-end-web interface added

f2c70bbefce44789968dbd5984585fc1.png

a287063cb4044870ba079a3effc4b25e.png b1935ea5d1d74802a8c32519b2d406e8.png

         Check whether the monitored host is connected and whether the status is normal

a5c144cd527e4c1c95557e898809ec26.png

 If the following error is reported, just wait 10 minutes. This means that the normal running time of the customer service side is less than 10 minutes.a1d2c287e36f4b3fa9ecdc3552d7da48.png

3. Customized monitoring

1. Script writing

1. Create a new script in the directory where the zabbix-agent of the IP         that needs to be monitored stores the script.

#进入/etc/zabbix/zabbix_agentd.conf查看自定义脚本存放路径
vi /etc/zabbix/zabbix_agentd.conf


#脚本要以.conf 结尾
#Include后面跟着的路径就是自定义脚本存放路径

cd /etc/zabbix/zabbix_agentd.d/

#创建一个新脚本,key很重要(就是命名),要记得,最好取容易看出是什么意思的
#脚本格式:UserParameter=<key>,<shell command>
#df -h|grep '/dev/mapper/centos-root'|awk '{print $5}'|awk -F "%" '{print $1}'  这个是查看内存还有百分之多少的
#样列:UserParameter=memory,df -h|grep '/dev/mapper/centos-root'|awk '{print $5}'|awk -F "%" '{print $1}'
#脚本得出的结果就是要监控的数据

vi memory.conf

#新脚本创建后,要重启zabbix-agent服务才生效

#前往服务端使用命令查看是否成功  命令自行修改
#呢此命令要安装zabbix_get才能使用

yum -y install zabbix-get-3.2.1-1.el7.x86_64.rpm

#如果安装失败,可在外网下载个zabbix-get安装包后上传到内网安装(或用我网盘包里zabbix-get,解压后搜索找到上传安装即可)

zabbix_get -s 客户端ip -p 10050 -k "key值"

Common parameters of zabbix_get command:

-s Specify client hostname or IP
-p Client port, default: 10050
-I Specify source IP
-k The key you want to get

2. Zabbix front-end interface configuration operation

        1. Create a new template

        If you already have the required template, you can skip it.

 If you already have a template, but you still need to add some functional items, you can choose to link the template. I will not demonstrate it here and make no selection.

2. Create a new application set

An application set represents a type of monitoring item. If it already exists, you do not need to create a new one.

 3. Create new monitoring items

Items that require customized monitoring

 

 4. Create a new trigger

If there is no alarm requirement, no trigger can be set

 5. Apply templates

After the template is applied, subsequent application sets, monitoring items, and triggers will be added to the host. If you don't want to use any of them, just go to the host and deactivate them.

6. Check whether it is successful

 4. Information alarm

1. Script addition

#查看zabbix_server配置文件zabbix_server.conf,确认信息报警脚本位置

vi /etc/zabbix/zabbix_server.conf

#AlertScriptsPath参数的路径为脚本路径,如果没有自行创建

I use python to call this, or you can use shell to call it (if there is no SMS interface, you can use DingTalk robot to send alarm information, the tutorial is on Baidu)

#到该路径下
cd /usr/lib/zabbix/alertscripts/


#创建脚本
vi sms.py


#----python告警脚本(post)

#!/usr/bin/python
# -*- coding:utf-8 -*-

import json
import sys
import requests

url = 'api地址信息'

post_headers = {'Content-Type': 'application/json'}

post_data = {

"moban":"gj",
"num":sys.argv[1],
#"first":sys.argv[2],
"keyword1":"信息",
"remark":sys.argv[3],
"people":"key值"

}

ret = requests.post(url, data = json.dumps(post_data), headers = post_headers)

print(ret.text)
#设置脚本和日志文件权限
touch /tmp/SMS.log

chown zabbix:zabbix /tmp/SMS.log

chown zabbix:zabbix /usr/lib/zabbix/alertscripts/sms.py

chmod +x /usr/lib/zabbix/alertscripts/sms.py

2. Add SMS alarm media to the zabbix operation interface;

Call script to send text message

 There are message templates in Zabbix. Pay attention to the format. If a large string of characters is reported, please try typing the SMS template by hand instead of pasting it.

 This provides two templates

##告警
主机名称:{HOST.NAME}
IP地址:{HOST.IP}
告警时间:{EVENT.DATE}{EVENT.TIME}
告警信息:{TRIGGER.NAME}
告警级别:{TRIGGER.SEVERITY}
检查项:{TRIGGER.KEY1}
当前状态:{TRIGGER.STATUS}
值={ITEM.LASTVALUE}
故障已持续{EVENT.AGE}

##恢复
主机名称:{HOST.NAME}
IP地址:{HOST.IP}
告警时间:{EVENT.DATE}{EVENT.TIME}
恢复时间:{EVENT.RECOVERY.DATE}{EVENT.RECOVERY.TIME}
故障时长:{EVENT.AGE}
告警信息:{TRIGGER.NAME}
告警级别:{TRIGGER.SEVERITY}
检查项:{TRIGGER.KEY1}
当前状态:{TRIGGER.STATUS}
值={ITEM.VALUE}

test

3. Add SMS alarm method for designated users;

Take the administrator "admin" user as an example

4. Set when the action will send SMS alarm;

5. Test

Add a new IP login number monitoring and trigger. For each additional remote control interface, there will be one more user number.

Script content

#查看有多少个连接数

UserParameter=user_sum,who  |wc -l

 To add custom monitoring items, just follow the previous tutorial.

Make it update every 10 seconds to facilitate testing

If it is greater than or equal to 6, it will alarm. If it is less than 6, it will not alarm. It will return to normal.

 After doing it, test whether it is successful by yourself (you need to add this alarm in the action)

Guess you like

Origin blog.csdn.net/weixin_68547003/article/details/129922985