部署Zabbix集中监控系统
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了部署Zabbix集中监控系统相关的知识,希望对你有一定的参考价值。
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 ‘
‘ ‘
‘`
subject=`echo $2 | tr ‘
‘ ‘
‘`
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服务
以上是关于部署Zabbix集中监控系统的主要内容,如果未能解决你的问题,请参考以下文章