Zabbix安装配置及邮件报警

Posted 还行少年

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Zabbix安装配置及邮件报警相关的知识,希望对你有一定的参考价值。

目录

环境

zabbix   192.168.30.7/24  centos7.6
linux    192.168.30.8/24  centos7.6
windows  192.168.30.100/24

一、zabbix服务器的安装

1.部署LAMP架构

1.1 安装依赖包

[root@localhost ~]# yum install -y httpd mariadb-server mariadb php php-mysql php-gd libjpeg* php-ldap php-odbc php-pear php-xml php-xmlrpc php-mhash
已加载插件:fastestmirror, langpacks
Loading mirror speeds from cached hostfile
 * base: mirrors.aliyun.com
 * extras: mirrors.aliyun.com
 * updates: mirrors.aliyun.com
软件包 httpd-2.4.6-97.el7.centos.x86_64 已安装并且是最新版本
软件包 1:mariadb-server-5.5.68-1.el7.x86_64 已安装并且是最新版本
软件包 1:mariadb-5.5.68-1.el7.x86_64 已安装并且是最新版本
软件包 php-5.4.16-48.el7.x86_64 已安装并且是最新版本
软件包 php-mysql-5.4.16-48.el7.x86_64 已安装并且是最新版本
软件包 php-gd-5.4.16-48.el7.x86_64 已安装并且是最新版本
软件包 libjpeg-turbo-1.2.90-8.el7.x86_64 已安装并且是最新版本
软件包 libjpeg-turbo-devel-1.2.90-8.el7.x86_64 已安装并且是最新版本
软件包 libjpeg-turbo-static-1.2.90-8.el7.x86_64 已安装并且是最新版本
软件包 libjpeg-turbo-utils-1.2.90-8.el7.x86_64 已安装并且是最新版本
软件包 php-ldap-5.4.16-48.el7.x86_64 已安装并且是最新版本
软件包 php-odbc-5.4.16-48.el7.x86_64 已安装并且是最新版本
软件包 1:php-pear-1.9.4-22.el7.noarch 已安装并且是最新版本
软件包 php-xml-5.4.16-48.el7.x86_64 已安装并且是最新版本
软件包 php-xmlrpc-5.4.16-48.el7.x86_64 已安装并且是最新版本
软件包 php-common-5.4.16-48.el7.x86_64 已安装并且是最新版本
无须任何处理
[root@localhost ~]# 

1.2 修改配置文件

[root@localhost ~]# vim /etc/httpd/conf/httpd.conf 
ServerName www.aa.com:80
<IfModule dir_module>
    DirectoryIndex index.html index.php
</IfModule>
[root@localhost ~]# vim /etc/php.ini
date.timezone = PRC

1.3 启动apache和mariadb

[root@localhost ~]# systemctl start httpd.service
[root@localhost ~]# systemctl start mariadb.service
[root@localhost ~]# netstat -antp | grep 80
tcp6       0      0 :::80                   :::*                    LISTEN      22334/httpd         
[root@localhost ~]# netstat -antp | grep 3306
tcp        0      0 0.0.0.0:3306            0.0.0.0:*               LISTEN      22596/mysqld        
[root@localhost ~]# 

1.4 初始化数据库

[root@localhost ~]# 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.

Set root password? [Y/n] n    //是否设置密码
 ... skipping.

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] n    //是否删除匿名用户
 ... skipping.

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] y     //是否禁止root用户远程登录
 ... Success!

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] n    //是否删除test库
 ... skipping.

Reloading the privilege tables will ensure that all changes made so far
will take effect immediately.

Reload privilege tables now? [Y/n] y          //是否重载权限表
 ... Success!

Cleaning up...

All done!  If you've completed all of the above steps, your MariaDB
installation should now be secure.


1.5 验证php网页是否可以打开

[root@localhost ~]# vi /var/www/html/index.php
<?php
phpinfo();
?>

1.6 对zabbix用户授权

[root@localhost ~]# mysql
Welcome to the MariaDB monitor.  Commands end with ; or \\g.
Your MariaDB connection id is 13
Server version: 5.5.68-MariaDB MariaDB Server

Copyright (c) 2000, 2018, Oracle, MariaDB Corporation Ab and others.

Type 'help;' or '\\h' for help. Type '\\c' to clear the current input statement.

MariaDB [(none)]> CREATE DATABASE zabbix character set utf8 collate utf8_bin;
Query OK, 1 row affected (0.00 sec)

MariaDB [(none)]> GRANT all ON zabbix.* TO 'zabbix'@'%' IDENTIFIED BY 'admin123';
Query OK, 0 rows affected (0.00 sec)

MariaDB [(none)]> flush privileges;
Query OK, 0 rows affected (0.00 sec)

MariaDB [(none)]> 

1.7 验证php和mysql的联动性

[root@localhost ~]# vi /var/www/html/index.php

<?php
$link=mysql_connect('192.168.30.7','zabbix','admin123');
if($link) echo "<h1>Success!!</h1>";
else echo "Fail!!";
mysql_close();
?>
~     

2.部署zabbix服务器

2.1 添加zabbix源

[root@localhost ~]# vi /etc/yum.repos.d/CentOS-Base.repo 

[Ali]
name=CentOS-$releasever - Base
baseurl=http://mirrors.aliyun.com/centos/$releasever/os/$basearch/
gpgcheck=1
gpgkey=http://mirrors.aliyun.com/centos/RPM-GPG-KEY-CentOS-7

[root@localhost ~]# vi /etc/yum.repos.d/zabbix.repo

[zabbix]
name=Zabbix Official Repository - \\$basearch
baseurl=https://mirrors.aliyun.com/zabbix/zabbix/4.0/rhel/7/\\$basearch/
enabled=1
gpgcheck=0

[zabbix-non-supported]
name=Zabbix Official Repository non-supported - \\$basearch
baseurl=https://mirrors.aliyun.com/zabbix/non-supported/rhel/7/\\$basearch/
enabled=1
gpgcheck=0
[root@localhost ~]# yum clean all    //清理缓存
已加载插件:fastestmirror, langpacks
正在清理软件源: Ali epel zabbix zabbix-non-supported
Cleaning up list of fastest mirrors
Other repos take up 1.0 G of disk space (use --verbose for details)
[root@localhost ~]# yum makecache   //添加缓存
已加载插件:fastestmirror, langpacks
Determining fastest mirrors
Ali                                                                                                       | 3.6 kB  00:00:00     
epel                                                                                                      | 4.7 kB  00:00:00     
zabbix                                                                                                    | 2.9 kB  00:00:00     
zabbix-non-supported                                                                                      | 2.9 kB  00:00:00     
(1/16): Ali/7/x86_64/group_gz                                                                             | 153 kB  00:00:00     
(2/16): Ali/7/x86_64/primary_db                                                                           | 6.1 MB  00:00:01     
(3/16): Ali/7/x86_64/filelists_db                                                                         | 7.2 MB  00:00:01     
(4/16): Ali/7/x86_64/other_db                                                                             | 2.6 MB  00:00:00     
(5/16): epel/x86_64/group_gz                                                                              |  96 kB  00:00:00     
(6/16): epel/x86_64/updateinfo                                                                            | 1.0 MB  00:00:00     
(7/16): epel/x86_64/prestodelta                                                                           | 1.2 kB  00:00:00     
(8/16): epel/x86_64/filelists_db                                                                          |  12 MB  00:00:02     
(9/16): epel/x86_64/primary_db                                                                            | 6.9 MB  00:00:02     
(10/16): epel/x86_64/other_db                                                                             | 3.4 MB  00:00:00     
(11/16): zabbix/x86_64/primary_db                                                                         | 231 kB  00:00:00     
(12/16): zabbix/x86_64/other_db                                                                           | 313 kB  00:00:00     
(13/16): zabbix-non-supported/x86_64/primary_db                                                           | 3.7 kB  00:00:00     
(14/16): zabbix/x86_64/filelists_db                                                                       | 176 kB  00:00:00     
(15/16): zabbix-non-supported/x86_64/filelists_db                                                         | 1.7 kB  00:00:00     
(16/16): zabbix-non-supported/x86_64/other_db                                                             | 3.3 kB  00:00:00     
元数据缓存已建立
[root@localhost ~]# 

2.2 安装zabbix服务端软件

[root@localhost ~]# yum install -y php-mbstring php-bcmath   //添加必要插件
已加载插件:fastestmirror, langpacks
Loading mirror speeds from cached hostfile
软件包 php-mbstring-5.4.16-48.el7.x86_64 已安装并且是最新版本
软件包 php-bcmath-5.4.16-48.el7.x86_64 已安装并且是最新版本
无须任何处理

[root@localhost ~]# rpm -ivh http://repo.zabbix.com/zabbix/4.0/rhel/7/x86_64/zabbix-release-3.5-1.el7.noarch.rpm
获取http://repo.zabbix.com/zabbix/4.0/rhel/7/x86_64/zabbix-release-3.5-1.el7.noarch.rpm
警告:/var/tmp/rpm-tmp.dz2aRz: 头V4 RSA/SHA512 Signature, 密钥 ID a14fe591: NOKEY
准备中...                          ################################# [100%]
正在升级/安装...
   1:zabbix-release-3.5-1.el7         ################################# [100%]

[root@localhost ~]# yum install zabbix-server-mysql zabbix-web-mysql  -y  --nogpgcheck
已加载插件:fastestmirror, langpacks
Loading mirror speeds from cached hostfile
软件包 zabbix-server-mysql-4.0.33-1.el7.x86_64 已安装并且是最新版本
软件包 zabbix-web-mysql-4.0.33-1.el7.noarch 已安装并且是最新版本
无须任何处理
[root@localhost ~]# 

2.3 导入zabbix监控数据库

[root@localhost ~]# zcat /usr/share/doc/zabbix-server-mysql-4.0.33/create.sql.gz | mysql -uzabbix -padmin123 zabbix  //导入zabbix监控数据库
[root@localhost ~]# vim /etc/zabbix/zabbix_server.conf   //查看zabbix未注释的生效行
[root@localhost ~]# grep -n '^'[a-Z] /etc/zabbix/zabbix_server.conf  
38:LogFile=/var/log/zabbix/zabbix_server.log
49:LogFileSize=0
72:PidFile=/var/run/zabbix/zabbix_server.pid
82:SocketDir=/var/run/zabbix
100:DBName=zabbix
116:DBUser=zabbix
124:DBPassword=admin123
356:SNMPTrapperFile=/var/log/snmptrap/snmptrap.log
471:Timeout=4
514:AlertScriptsPath=/usr/lib/zabbix/alertscripts
525:ExternalScripts=/usr/lib/zabbix/externalscripts
561:LogSlowQueries=3000
[root@localhost ~]# 

2.4 解决乱码问题

[root@localhost fonts]# vim /usr/share/zabbix/include/defines.inc.php
:%s /graphfont/kaiti/g 
[root@localhost ~]# cd /usr/share/zabbix/assets/fonts/
[root@localhost fonts]# ls
graphfont.ttf
[root@localhost fonts]# rz -E
rz waiting to receive.
[root@localhost fonts]# rz -E
rz waiting to receive.
[root@localhost fonts]# ls
 COUR.TTF  graphfont.ttf
[root@localhost fonts]# mv COUR.TTF simkai.ttf
[root@localhost fonts]# ls
graphfont.ttf  simkai.ttf

2.5 启动zabbix

[root@localhost fonts]# systemctl enable zabbix-server
Created symlink from /etc/systemd/system/multi-user.target.wants/zabbix-server.service to /usr/lib/systemd/system/zabbix-server.service.
[root@localhost fonts]# systemctl start zabbix-server
[root@localhost fonts]# netstat -anpt | grep zabbix 
tcp        0      0 0.0.0.0:10051           0.0.0.0:*               LISTEN      25764/zabbix_server 
tcp6       0      0 :::10051                :::*                    LISTEN      25764/zabbix_server 
[root@localhost fonts]# systemctl restart httpd.service
[root@localhost fonts]# netstat -antp | grep 80
tcp6       0      0 :::80                   :::*                    LISTEN      25847/httpd         
[root@localhost fonts]# 

3.zabnix网页配置

健康检查

连接数据库

zabbix服务器名称

信息总览

4.设置linux版zabbix客户端

[root@localhost ~]# yum install -y zabbix-agent
已加载插件:fastestmirror, langpacks
Loading mirror speeds from cached hostfile
软件包 zabbix-agent-4.0.33-1.el7.x86_64 已安装并且是最新版本
无须任何处理
[root@localhost ~]# 

4.1 修改配置

[root@localhost ~]# vim /etc/zabbix/zabbix_agentd.conf 
[root@localhost ~]# grep -n '^'[a-Z] /etc/zabbix/zabbix_agentd.conf 
13:PidFile=/var/run/zabbix/zabbix_agentd.pid
32:LogFile=/var/log/zabbix/zabbix_agentd.log
43:LogFileSize=0
98:Server=192.168.30.7
139:ServerActive=192.168.30.7
150:Hostname=client
268:Include=/etc/zabbix/zabbix_agentd.d/*.conf

4.2 启动服务

[root@localhost ~]# systemctl enable zabbix-agent.service
Created symlink from /etc/systemd/system/multi-user.target.wants/zabbix-agent.service to /usr/lib/systemd/system/zabbix-agent.service.
[root@localhost ~]# systemctl restart zabbix-agent.service
[root@localhost ~]# netstat -anpt | grep zabbix
tcp        0      0 0.0.0.0:10050           0.0.0.0:*               LISTEN      19600/zabbix_agentd 
tcp6       0      0 :::10050                :::*                    LISTEN      19600/zabbix_agentd 

4.3 web创建被控主机


4.4 安装邮件服务

[root@localhost fonts]# yum -y install mailx
已加载插件:fastestmirror, langpacks
Loading mirror speeds from cached hostfile
软件包 mailx-12.5-19.el7.x86_64 已安装并且是最新版本
无须任何处理

[root@localhost alertscripts]# vim /etc/mail.rc  //这里添加个人邮箱

测试

[root@localhost assets]# cd /usr/lib/zabbix/alertscripts/
[root@localhost alertscripts]# ls
mailx.sh
[root@localhost alertscripts]# cat mailx.sh 
#!/bin/bash
#send mail

messages=`echo $3 | tr '\\r\\n' '\\n'`
subject=`echo $2 | tr '\\r\\n' '\\n'`
echo "$messages" | mail -s "$subject" $1 >>/tmp/mailx.log 2>&1
[root@localhost alertscripts]# vim /etc/mail.rc
[root@localhost alertscripts]# vi mailx.sh 
[root@localhost alertscripts]# touch /tmp/mailx.log 
[root@localhost alertscripts]# chown -R zabbix.zabbix  /tmp/mailx.log 
[root@localhost alertscripts]# chmod +x /usr/lib/zabbix/alertscripts/mailx.sh
[root@localhost alertscripts]# chown -R zabbix.zabbix /usr/lib/zabbix/

[root@localhost alertscripts]# ./mailx.sh 1111111@qq.com "123""123"

5.添加规则

5.1 报警媒介

5.2 创建动作



5.3 配置主机


5.4 停止客户端服务,测试

root@localhost ~]# systemctl stop zabbix-agent.service
//客户端停止
[root@localhost zabbix]# tail -f /var/log/zabbix/zabbix_server.log    //服务器日志
 34013:20210903:000022.790 Zabbix agent item "agent.ping" on host "client" failed: first network error, wait for 15 seconds
 34015:20210903:000037.751 Zabbix agent item "proc.num[,,run]" on host "client" failed: another network error, wait for 15 seconds
 34015:20210903:000052.758 Zabbix agent item "system.cpu.load[percpu,avg5]" on host "client" failed: another network error, wait for 15 seconds
 34015:20210903:000107.766 temporarily disabling Zabbix agent checks on host "client": host unavailable

以上是关于Zabbix安装配置及邮件报警的主要内容,如果未能解决你的问题,请参考以下文章

Zabbix实战之运维篇Zabbix监控平台的邮件报警配置

zabbix邮件报警之完整总结篇

zabbix邮件报警接入,微信报警接入

通过脚本发送zabbix邮件报警

zabbix设置邮件发送报警

Zabbix实战之故障处理篇Zabbix的告警邮件发送失败问题处理