1. 搭建zabbix
zabbix是基于lnmp架构的,所以在搭建zabbix之前需要先搭建lnmp架构,前面文章有写到如何搭建,可以参考文章LNMP架构
安装依赖包
[root@node2 ~]# yum -y install net-snmp-devel libevent-devel
下载zabbix
[root@node2 ~]# wget https://cdn.zabbix.com/zabbix/sources/stable/5.0/zabbix-5.0.2.tar.gz
[root@node2 ~]# ls
zabbix-5.0.1.tar.gz
[root@node2 ~]# tar xf zabbix-5.0.1.tar.gz
创建zabbix用户和组
[root@node2 ~]# useradd -r -M -s /sbin/nologin zabbix
[root@node2 ~]# id zabbix
uid=304(zabbix) gid=304(zabbix) 组=304(zabbix)
配置zabbix数据库
[root@node2 ~]# mysql -uroot -p123
mysql> create database zabbix character set utf8 collate utf8_bin;
Query OK, 1 row affected (0.00 sec)
mysql> grant all privileges on zabbix.* to zabbix@localhost identified by '123';
Query OK, 0 rows affected, 2 warnings (0.00 sec)
mysql> flush privileges;
Query OK, 0 rows affected (0.01 sec)
mysql> quit
Bye
[root@node2 ~]# cd zabbix-5.0.1/database/mysql/
[root@node2 mysql]# ls
data.sql images.sql Makefile.in
double.sql Makefile.am schema.sql
[root@node2 mysql]# mysql -uroot -p123 zabbix < schema.sql
[root@node2 mysql]# mysql -uroot -p123 zabbix < images.sql
[root@node2 mysql]# mysql -uroot -p123 zabbix < data.sql
编译安装zabbix
[root@node2 ~]# cd zabbix-5.0.1
[root@node2 zabbix-5.0.1]# ./configure --enable-server \
--enable-agent \
--with-mysql \
--with-net-snmp \
--with-libcurl \
--with-libxml2
[root@node2 zabbix-5.0.1]# make install
配置zabbix服务端
[root@node2 ~]# cd /usr/local/etc/
[root@node2 etc]# ls
zabbix_agentd.conf zabbix_server.conf
zabbix_agentd.conf.d zabbix_server.conf.d
[root@node2 etc]# vim zabbix_server.conf
...
### Option: DBPassword
# Database password.
# Comment this line if no password is used.
#
# Mandatory: no
# Default:
DBPassword=123 //将此行注释取消,并改为DBPassword=你的数据库登录密码,我这里为123
//启动服务
[root@node2 etc]# zabbix_server
[root@node2 etc]# zabbix_agentd
[root@node2 etc]# ss -antl |grep -E '10051|10050'
LISTEN 0 128 *:10050 *:*
LISTEN 0 128 *:10051 *:*
配置zabbix服务web界面
//这里如果不修改后面访问web界面安装的时候会报错
[root@node2 ~]# sed -ri 's/(post_max_size =).*/\1 16M/g' /etc/php.ini
[root@node2 ~]# sed -ri 's/(max_execution_time =).*/\1 300/g' /etc/php.ini
[root@node2 ~]# sed -ri 's/(max_input_time =).*/\1 300/g' /etc/php.ini
[root@node2 ~]# sed -i '/;date.timezone/a date.timezone = Asia/Shanghai' /etc/php.ini
[root@node2 ~]# service php-fpm restart
Gracefully shutting down php-fpm . done
Starting php-fpm done
//配置web界面
[root@node2 ~]# cd zabbix-5.0.1
[root@node2 zabbix-5.0.1]# cp -a ui /usr/local/nginx/html/zabbix
[root@node2 zabbix-5.0.1]# chown -R nginx.nginx /usr/local/nginx/html/zabbix/
//配置nginx虚拟主机
[root@node2 ~]# vim /usr/local/nginx/conf/nginx.conf
...
location / {
root html/zabbix; //这里修改为zabbix的web界面目录
index index.php index.html index.htm;
}
...
location ~ \.php$ {
root html/zabbix; //这里修改为zabbix的web界面目录
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}
...
//设置zabbix/conf目录的权限,让zabbix有权限生成配置文件zabbix.conf.php
[root@node2 ~]# chmod 777 /usr/local/nginx/html/zabbix/conf
//重载配置文件
[root@node2 ~]# nginx -s reload
访问web界面
2. 开启状态页面
[root@node2 ~]# vim /usr/local/nginx/conf/nginx.conf
...
location /status {
stub_status on;
allow 192.168.207.0/24;
deny all;
}
...
[root@node2 ~]# nginx -s reload
访问
3. 配置监控脚本
[root@node2 ~]# ls /scripts/
check_nginx.sh
[root@node2 ~]# cd /scripts/
[root@node2 scripts]# cat check_nginx.sh
#!/bin/bash
function handled()
{
status=$(curl -s http://192.168.207.130/status |awk 'NR==3{print $3}')
echo $status
}
function Reading()
{
status=$(curl -s http://192.168.207.130/status |awk 'NR==4{print $2}')
echo $status
}
function Writing()
{
status=$(curl -s http://192.168.207.130/status |awk 'NR==4{print $4}')
echo $status
}
case $1 in
Writing)
Writing
;;
handled)
handled
;;
Reading)
Reading
;;
*)
echo "$0 handled|Writing|Reading"
;;
esac
[root@node2 scripts]# chmod +x check_nginx.sh
4. 编辑zabbix_agent配置文件
[root@node2 ~]# vim /usr/local/etc/zabbix_agentd.conf
...
# Default:
UnsafeUserParameters=1 //将此行取消注释,并将0改为1
...
# Default:
UserParameter=check_nginx[*],/bin/bash /scripts/check_nginx.sh $1
[root@node2 ~]# pkill zabbix
[root@node2 ~]# zabbix_server
[root@node2 ~]# zabbix_agentd
//测试key是否可用
[root@node2 ~]# zabbix_get -s 127.0.0.1 -k check_nginx[handled]
107
[root@node2 ~]# zabbix_get -s 127.0.0.1 -k check_nginx[Reading]
0
[root@node2 ~]# zabbix_get -s 127.0.0.1 -k check_nginx[Writing]
1
5. web界面配置
配置监控项
6. 查看最新数据
版权声明:本文为qq_45948194原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接和本声明。