Zabbix 监控 nginx
配置 Nginx 状态页
server {
listen 80;
server_name localhost;
root /usr/share/nginx/html;
location /nginx_status {
stub_status;
}
}
[root@web01 ~]# systemctl restart nginx
Nginx 状态页
[root@web01 ~]# curl 10.0.0.7/nginx_status
Active connections: 3
server accepts handled requests
3 3 3
Reading: 0 Writing: 1 Waiting: 2
监控脚本
[root@web01 ~]# mkdir /scripts
[root@web01 ~]# cd /scripts/
[root@web01 scripts]# wget https://www.itsky.tech/download/zabbix/template_sh/zabbix%E7%9B%91%E6%8E%A7/nginx_status.sh
[root@web01 scripts]# chmod +x nginx_status.sh
配置监控项
[root@web01 ~]# vim /etc/zabbix/zabbix_agentd.d/nginx_status.conf
UserParameter=nginx_status[*],/bin/bash /scripts/nginx_status.sh $1
[root@web01 ~]# systemctl restart zabbix-agent.service
测试监控项
# 客户端测试
[root@web01 ~]# zabbix_agentd -t nginx_status[active]
nginx_status[active] [t|1]
# 服务端测试
[root@zabbix plugins]# zabbix_get -s 172.16.1.7 -k nginx_status[active]
1
导入模板
# 导入的模板,通过 Web 页面导入即可
wget https://www.itsky.tech/download/zabbix/template_sh/zabbix%E7%9B%91%E6%8E%A7/zbx_export_hosts.xml
查看监控项
PS:也可以自己添加监控到此模板下,如监控 80 端口
Zabbix 监控 PHP
安装 PHP
[root@web01 ~]# wget https://www.itsky.tech/download/zabbix/template_sh/zabbix%E7%9B%91%E6%8E%A7/php72.tar.gz
[root@web01 ~]# tar xf php72.tar.gz
[root@web01 package]# yum localinstall -y opt/php/packages/*.rpm
配置 PHP 状态页
[root@web01 package]# vim /etc/php-fpm.d/www.conf
pm.status_path = /php_status
启动 PHP
[root@web01 package]# systemctl start php-fpm
配置 Nginx 访问 PHP 状态页
server {
listen 80;
server_name localhost;
root /usr/share/nginx/html;
location /nginx_status {
stub_status;
}
location ~* ^/(php_status)$ {
fastcgi_pass 127.0.0.1:9000;
fastcgi_param SCRIPT_FILENAME $fastcgi_script_name;
include fastcgi_params;
}
}
[root@web01 package]# systemctl restart nginx
PHP 状态页
[root@web01 package]# curl http://127.0.0.1/php_status
pool: www # 连接池
process manager: dynamic # 管理方式
start time: 11/Sep/2020:16:05:50 +0800 # 启动时间
start since: 417 # 启动了多久
accepted conn: 9 # 连接数
listen queue: 0 # 等待的请求队列
max listen queue: 0 # 最大等待的请求队列
listen queue len: 128 # 等待的请求队列长度
idle processes: 4 # 空闲的进程数
active processes: 1 # 活跃的进程数
total processes: 5 # 总的进程数
max active processes: 1 # 最大的活跃的进程数
max children reached: 0 # 进程启动数量
slow requests: 0 # 慢请求
下载脚本
[root@web01 scripts]# wget https://www.itsky.tech/download/zabbix/template_sh/zabbix%E7%9B%91%E6%8E%A7/php_status.sh
[root@web01 scripts]# chmod +x php_status.sh
配置监控项
[root@web01 ~]# vim /etc/zabbix/zabbix_agentd.d/php_status.conf
UserParameter=phpfpm_status[*],/bin/bash /scripts/php_status.sh $1
[root@web01 ~]# systemctl restart zabbix-agent.service
验证监控项
[root@web01 scripts]# zabbix_agentd -t phpfpm_status[max_active_processes]
php_status[max_active_processes] [t|1]
[root@zabbix plugins]# zabbix_get -s 172.16.1.7 -k phpfpm_status[slow_requests]
0
导入模板
# 导入的模板,通过 Web 页面导入即可
wget https://www.itsky.tech/download/zabbix/template_sh/zabbix%E7%9B%91%E6%8E%A7/php_status.xml
Zabbix 监控 MySQL
如果想监控 MySQL,需要安装 Percona Monitor Plugins 插件,同时还需要 PHP 的环境!!!
安装数据库
[root@web01 ~]# yum install -y mariadb-server
启动数据库
[root@web01 ~]# systemctl start mariadb
创建监控用户
MariaDB [(none)]> grant all on *.* to zabbix@'localhost' identified by '123';
Query OK, 0 rows affected (0.00 sec)
安装 Percona-zabbix-templates
[root@web01 ~]# yum install -y http://www.percona.com/downloads/percona-release/redhat/0.1-3/percona-release-0.1-3.noarch.rpm
#安装插件
[root@web01 ~]# yum install -y percona-zabbix-templates
查看安装的内容
[root@web01 ~]# tree /var/lib/zabbix/percona/
/var/lib/zabbix/percona/
├── scripts
# 需要配置一下 mysql 用户和密码
│ ├── get_mysql_stats_wrapper.sh
# php 脚本,需要 php 环境
│ └── ss_get_mysql_stats.php
└── templates
├── userparameter_percona_mysql.conf
# 该模板不可用在 4.0 版本的 zabbix server 上,版本不兼容
└── zabbix_agent_template_percona_mysql_server_ht_2.0.9-sver1.1.8.xml
2 directories, 4 files
[root@web01 ~]#
配置监控项
# 将 userparameter_percona_mysql.conf 拷贝到 zabbix 的配置文件目录
[root@web01 ~]# cp /var/lib/zabbix/percona/templates/userparameter_percona_mysql.conf /etc/zabbix/zabbix_agentd.d/
# 重启服务
[root@web01 ~]# systemctl restart zabbix-agent.service
修改连接数据库信息
# 修改 php 连接
[root@web01 ~]# vim /var/lib/zabbix/percona/scripts/ss_get_mysql_stats.php
$mysql_user = 'zabbix';
$mysql_pass = '123';
$mysql_port = 3306;
验证监控项
[root@web01 ~]# zabbix_agentd -t MySQL.running-slave
MySQL.running-slave [t|0]
# 服务端验证,报错
[root@zabbix plugins]# zabbix_get -s 172.16.1.7 -k MySQL.running-slave
ERROR 1045 (28000): Access denied for user 'zabbix'@'localhost' (using password: NO)
0
# 因为脚本中没有使用数据库密码和用户
[root@web01 ~]# vim /var/lib/zabbix/percona/scripts/get_mysql_stats_wrapper.sh
# 把 -uzabbix -p123 加进去
RES=`HOME=~zabbix mysql -uzabbix -p123 -e 'SHOW SLAVE STATUSG' | egrep '(Slave_IO_Running|Slave_SQL_Running):' | awk -F: '{print $2}' | tr '
' ','`
导入监控模板
wget https://www.itsky.tech/download/zabbix/template_sh/zabbix%E7%9B%91%E6%8E%A7/zbx_percona_mysql_template.xml
# 用 percona 提供的默认版本会出错,因为版本不兼容
Zabbix 监控 Redis
搭建 Discuz 论坛
# 1.配置nginx
[root@web01 ~]# vim /etc/nginx/conf.d/discuz.conf
server {
listen 80;
server_name linux.discuz.com;
location / {
root /code/upload;
index index.php;
}
location ~* .php$ {
root /code/upload;
fastcgi_pass 127.0.0.1:9000;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}
}
[root@web01 ~]# systemctl restart nginx
# 2.配置站点
[root@web01 ~]# mkdir /code
[root@web01 ~]# rz Discuz_X3.3_SC_GBK.zip
[root@web01 ~]# unzip Discuz_X3.3_SC_GBK.zip -d /code/
# 3.配置hosts
10.0.0.7 linux.discuz.com
# 4.统一用户
[root@web01 ~]# vim /etc/php-fpm.d/www.conf
[root@web01 ~]# vim /etc/nginx/nginx.conf
[root@web01 ~]# groupadd www -g 666
[root@web01 ~]# useradd www -u 666 -g 666
[root@web01 ~]# systemctl restart nginx
[root@web01 ~]# systemctl restart php-fpm
[root@web01 ~]# chown -R www.www /code/
安装 Redis
[root@web01 ~]# yum install -y redis
[root@web01 ~]# vim /etc/redis.conf
requirepass 123
[root@web01 ~]# systemctl start redis
配置 Discuz 连接 Redis
[root@web01 ~]# vim /code/upload/config/config_global.php
// -------------------------- CONFIG MEMORY --------------------------- //
$_config['memory']['prefix'] = 'kRZWnw_';
$_config['memory']['redis']['server'] = '127.0.0.1';
$_config['memory']['redis']['port'] = 6379;
$_config['memory']['redis']['pconnect'] = 1;
$_config['memory']['redis']['timeout'] = '0';
$_config['memory']['redis']['requirepass'] = '123';
Redis 状态监控
[root@web01 ~]# redis-cli info
上传脚本
[root@web01 scripts]# wget https://www.itsky.tech/download/zabbix/template_sh/zabbix%E7%9B%91%E6%8E%A7/redis_status.sh
# 根据 Redis 实际配置修改脚本内容
[root@web01 ~]# cat redis_status.sh
#!/bin/bash
R_COMMAND="$1"
R_PORT="6379" # 根据实际情况调整端口
R_SERVER="127.0.0.1" # 根据具体情况调整 IP 地址
PASSWD="123456" # 如果没有设置 Redis 密码,为空即可
[root@web01 scripts]# chmod +x redis_status.sh
配置监控项
[root@web01 scripts]# vim /etc/zabbix/zabbix_agentd.d/redis_status.conf
UserParameter=redis_status[*],/bin/bash /scripts/redis_status.sh $1
[root@web01 scripts]# systemctl restart zabbix-agent.service
检查监控项
[root@web01 scripts]# zabbix_agentd -t redis_status[used_memory]
redis_status[used_memory]
[root@zabbix plugins]# zabbix_get -s 172.16.1.7 -k redis_status[used_memory]
/scripts/redis_status.sh: line 7: /tmp/redis_6379.tmp: Permission denied
924680
# 权限不足,将文件删除即可
[root@web01 scripts]# rm -rf /tmp/redis_6379.tmp
导入模板
# 该模板内有 Value 为字符串类型,不能作图,在 Web 端将作图勾掉
wget https://www.itsky.tech/download/zabbix/template_sh/zabbix%E7%9B%91%E6%8E%A7/zbx_redis_templates.xml