共计 6593 个字符,预计需要花费 17 分钟才能阅读完成。
Zabbix 概述
Zabbix 是一个基于 Web 界面的分布式系统监控与网络设备监控系统,它能够监控各种网络参数,并提供灵活的通知机制,以使管理员能快速定位故障并解决由 zabbix server 端与 zabbix agent 组成, 可通过 SNMP、zabbix、agent、ping、端口监视等方法实现对远程主机与网络设备的监控。
zabbix 特点
- 支持自动发现网络设备和服务器
- 支持分布式监控
- 可设置报警阈值
- 可以通过多种方式进行数据收集
- 可定制报警方式
- 实时的绘图功能
- 通过 Web 监控系统与设置
zabbix 官网: https://www.zabbix.com
系统环境
主机 | IP | 服务 |
---|---|---|
监控端 | 192.168.26.161 | lamp、zabbix-server |
被监控端 | 192.168.26.163 | zabbix-agent |
实验步骤
关闭所有主机的防火墙和安全性策略
systemctl stop firewalld.service
setenforce 0
一、Server 端安装配置 LAMP 环境,以满足 zabbix 的代码部署要求
1. 安装 lamp 相关软件包
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
2. 编辑修改 httpd、php 配置文件
vi /etc/httpd/conf/httpd.conf
ServerName www.benet.com
DirectoryIndex index.html index.php
vi /etc/php.ini
date.timezone = PRC // 设置中国时区
3. 启动 http 服务与 mariadb 服务
systemctl start httpd.service
systemctl start mariadb.service
netstat -ntap | egrep ‘(80|3306)’ // 查看端口
4.MySQL 初始化安装
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] 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] n // 是否移除 anonymous 用户
… 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] n // 是否允许 root 用户远程登录
… 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] 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.
Thanks for using MariaDB!
5. 登录 mysql, 创建 zabbix 数据库及创建 zabbix 用户并设置密码
mysql -u root -p
CREATE DATABASE zabbix character set utf8 collate utf8_bin;
GRANT all privileges ON *.* TO ‘zabbix’@’%’ IDENTIFIED BY ‘admin123’;
flush privileges;
6. 创建 mysql 数据库的测试网页文件
vi /var/www/html/index.php
<?php
phpinfo();
$link=mysql_connect(‘192.168.26.161′,’zabbix’,’admin123′);
if($link) echo “<h1>Success!!</h1>”;
else echo “Fail!!”;
mysql_close();
?>
7. 客户端访问 index.php 网页
显示 success 则访问 mysql 正常,fail 则访问失败,检查 index.php 中连接地址、用户名、密码是否正确,若还是有问题请检查 mysql.user 表用户名称是否有空用户名称占用而导致的错误,以下为解决方法
> select user,host from mysql.user; // 用户名称为空占用导致本地无法登录远程可登录
+——–+———–+
| user | host |
+——–+———–+
| zabbix | % |
| root | 127.0.0.1 |
| root | ::1 |
| | localhost |
| root | localhost |
| | zabbix |
| root | zabbix |
+——–+———–+
> drop user ”@localhost;
> drop user ”@zabbix;
> flush privileges;
二、部署 zabbix Server
1. 下载 zabbix 官方 yum 源文件
rpm -i https://repo.zabbix.com/zabbix/2.2/rhel/7/x86_64/zabbix-release-2.2-1.el7.noarch.rpm
2. 安装 zabbix 服务端及被控端软件包
yum install zabbix-server-mysql zabbix-web-mysql zabbix-agent -y
3. 导入 zabbix 数据库
zcat /usr/share/doc/zabbix-server-mysql-4.0.0/create.sql.gz | mysql -uzabbix -p zabbix
4. 编辑 zabbix_service.conf 配置, 结果如下
egrep -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
101:DBName=zabbix
117:DBUser=zabbix
125:DBPassword=admin123 // 修改本行为创建 zabbix 数据库的密码
347:SNMPTrapperFile=/var/log/snmptrap/snmptrap.log
465:Timeout=4
507:AlertScriptsPath=/usr/lib/zabbix/alertscripts
517:ExternalScripts=/usr/lib/zabbix/externalscripts
553:LogSlowQueries=3000
5. 编辑 zabbix 配置文件,指定时区
vim /etc/httpd/conf.d/zabbix.conf
php_value date.timezone Asia/Shanghai
6. 修正 zabbix-web 图表中文乱码
vim /usr/share/zabbix/include/defines.inc.php
# 替换全文中所有 graphfot 为 kaiti
:%s/graphfont/kaiti/g
7. 复制字体文件至 zabbix/fonts/ 目录下
cp kaiti.ttf /usr/share/zabbix/fonts/
8. 启动 zabbix-server 服务
systemctl enable zabbix-server
systemctl start zabbix-server
9. 检查是否已监听 10051 端口
netstat -anpt | grep zabbix
10. 重启 httpd 服务
systemctl restart httpd.service
三、部署 Zabbix-agent
1. 下载 zabbix 官方 yum 源文件
rpm -i https://repo.zabbix.com/zabbix/2.2/rhel/7/x86_64/zabbix-release-2.2-1.el7.noarch.rpm
2. 安装 zabbix 被控端软件包
yum install -y zabbix-agent
3. 编辑 zabbix_agentd.conf 配置,结果如下
egrep -n ‘^'[a-Z] /etc/zabbix/zabbix_agentd.conf
13:PidFile=/var/run/zabbix/zabbix_agentd.pid
23:LogFile=/var/log/zabbix/zabbix_agentd.log
34:LogFileSize=0
85:Server=192.168.96.70
126:ServerActive=192.168.96.70
137:Hostname=zabbix
246:Include=/etc/zabbix/zabbix_agentd.d/
4. 启动 zabbix-agent 服务
systemctl enable zabbix-agent.service
systemctl start zabbix-agent.service
5. 检查是否已监听 10050 端口
netstat -anpt | grep zabbix
四、在 web 界面安装 zabbix
打开浏览器输入:
http://192.168.26.161/zabbix/
安装后登录 用户名 Admin 密码:zabbix
点击用户:设置中文环境方便管理
添加被控主机
配置 > 主机 > 创建主机
主机页面根据需求配置
选中主机 > 点击批量更新(可以添加监控项)> 模板(添加 2 个模板测试下)
五、配置邮件报警功能
1. 首先在 zabbix-server 上配置
yum install mailx -y
vi /etc/mail.rc
—- 文件末尾新增 —
set from=qq 邮箱
set smtp=smtp.qq.com
set smtp-auth-user=qq 邮箱
set smtp-auth-password= 邮箱授权码
set smtp-auth=login
echo “hello world” | mail -s “testmail” qq 邮箱 // 测试发信正常
cd /usr/lib/zabbix/alertscripts
vi mailx.sh // e 编写发邮件脚本
#!/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
——
touch /tmp/mailx.log
chown -R zabbix.zabbix /tmp/mailx.log
chmod +x /usr/lib/zabbix/alertscripts/mailx.sh
chown -R zabbix.zabbix /usr/lib/zabbix/
./mailx.sh qq 邮箱 “ 主题 ” “ 内容 ” // 测试发邮件脚本是否可以正常工作
2. 然后在服务器 WEB 上配置
打开管理 ==》报警媒体类型 ==》创建媒体���型
名称:Mail-Test
类型:脚本
脚本名称:mailx.sh
脚本参数:// 新增以下三个参数
{ALERT.SENDTO}
{ALERT.SUBJECT}
{ALERT.MESSAGE}
管理 ==》用户 ==》点击 Admin=》报警媒介
类型:Mail-Test // 调用上面的脚本 br/> 收件人:qq 邮箱
其它默认 - 保存
配置 -》动作 -》创建动作 -》删除默认标签,修改触发条件
名称:Mailx
条件 A 主机群组 =Linux servers
操作 -》如下配置
默认操作步骤持续时间 60
默认接收人:{TRIGGER.STATUS}:{TRIGGER.NAME}
默认信息:
告警主机:{HOST.NAME}
告警 IP:{HOST.IP}
告警时间:{EVENT.DATE}-{EVENT.TIME}
告警等级:{TRIGGER.SEVERITY}
告警信息:{TRIGGER.NAME}:{ITEM.VALUE}
事件 ID:{EVENT.ID}操作细节:-》
操作类型:发送消息
发送到用户:Admin (Zabbix Administrator)
仅送到:Mail-Test // 一定要配置否则邮件发送不成功
恢复操作:{TRIGGER.STATUS}:{TRIGGER.NAME}
恢复信息:
恢复主机:{HOST.NAME}
恢复 IP:{HOST.IP}
恢复时间:{EVENT.DATE}-{EVENT.TIME}
恢复等级:{TRIGGER.SEVERITY}
恢复信息:{TRIGGER.NAME}:{ITEM.VALUE}
恢复 ID:{EVENT.ID}操作细节:-》
操作类型:发送消息
发送到用户:Admin (Zabbix Administrator)
仅送到:Mail-Test // 一定要配置否则邮件发送不成功
六、测试邮箱报警功能
添加一个监控项(ssh service)
systemctl stop sshd // 在被监控主机上关闭 ssh 服务
重新启动 ssh 服务
:
![post-qrcode](https://www.xgss.net/wp-content/uploads/2023/07/二维码-微信公众号256x256.jpg)