一、zabbix监控nginx运行状态:
1、安装nginx、php-fpm、mariadb-server服务:
1.1 安装nginx:
先安装最新yum源:
[root@web01 yum.repos.d]# cat nginx.repo
[nginx]
name=nginx repo
baseurl=http://nginx.org/packages/centos/7/$basearch/
gpgcheck=0
enabled=1
安装nginx:
[root@web01 ~]# yum install -y nginx
[root@web01 ~]# rpm -qa nginx
nginx-1.16.0-1.el7.ngx.x86_64
[root@web01 ~]# systemctl start nginx
[root@web01 ~]# systemctl enable nginx
1.2 安装php-fpm 、 php-mysql 、php-gd:
[root@web01 html]# yum install -y php-mysql php-gd
[root@web01 ~]# rpm -qa php-fpm
php-fpm-5.4.16-46.el7.x86_64
1.3 安装mariadb-server:
[root@web01 ~]# yum install -y mariadb-server
[root@web01 ~]# rpm -qa mariadb-server
mariadb-server-5.5.60-1.el7_5.x86_64
1.4 下载zabbix-agent压缩包
[root@web01]# wget
https://mirrors.tuna.tsinghua.edu.cn/zabbix/zabbix/4.0/rhel/7/x86_64/zabbix-agent-4.0.9-3.el7.x86_64.rpm
安装zabbix-agent:
[root@web01]#rpm -ivh zabbix-agent-4.0.9-3.el7.x86_64.rpm
在客户端配置zabbix-agent检测服务的配置文件(路径:/etc/zabbix/zabbix_agentd.conf):
2、配置nginx主配置文件conf.d下的主文件,在文件中加入nginx_status项:
[root@web01 ~]# cat /etc/nginx/conf.d/01-www.conf
server {
listen 80;
server_name bbs.oldboy.com;
access_log /var/log/nginx/access_blog.log main;
root /usr/share/nginx/html/bbs;
location / {
index index.php index.html index.htm;
}
location /nginx_status {
stub_status;
}
location ~* \.(php|php5)$ {
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}
}
注: location /nginx_status {
stub_status;
}
需要做的nginx状态监控项目就是status在起作用
3、导入模板:
3.1 先把官方nginx状态监测的zabbix服务模板,导入zabbix的web界面(数据库):
3.2 检查模板是否加入,以及把模板添加到服务主机:
3.3 在监控项里面检查nginx状态监测服务是否已经添加:
4、添加zabbix客户端的监测配置文件:
4.1 检查一下主配置文件需要放在的路径:
[root@web01 ~]# vim /etc/zabbix/zabbix_agentd.conf
Include=/etc/zabbix/zabbix_agentd.d/*.conf
此配置文件的路径,就类似于我们nginx服务里面的/etc/nginx/conf.d/* 一样,就是以后主配置文件存放的路径。
4.2 把我们已经做好的自定义nginx状态监测项,放在次路径下面:
[root@web01 zabbix_agentd.d]# vim nginx.conf
UserParameter=nginx_status[*],/bin/bash /server/script/nginx_monitor.sh $1
4.3 配置文件里面需要用到脚本监测,所以还需要把官方监测脚本放在指定目录下(官方监测文件脚本):
[root@web01 ]# mkdir -p /server/script/
[root@web01 script]# cat nginx_monitor.sh
#!/bin/bash
NGINX_COMMAND=$1
NGINX_PORT=80
CACHEFILE="/tmp/nginx_status.txt"
CMD="/usr/bin/curl http://127.0.0.1:"$NGINX_PORT"/nginx_status/"
if [ ! -f $CACHEFILE ];then
$CMD >$CACHEFILE 2>/dev/null
fi
# Check and run the script
TIMEFLM=`stat -c %Y $CACHEFILE`
TIMENOW=`date +%s`
if [ `expr $TIMENOW - $TIMEFLM` -gt 60 ]; then
rm -f $CACHEFILE
fi
if [ ! -f $CACHEFILE ];then
$CMD >$CACHEFILE 2>/dev/null
fi
nginx_active(){
grep 'Active' $CACHEFILE| awk '{print $NF}'
exit 0;
}
nginx_reading(){
grep 'Reading' $CACHEFILE| awk '{print $2}'
exit 0;
}
nginx_writing(){
grep 'Writing' $CACHEFILE | awk '{print $4}'
exit 0;
}
nginx_waiting(){
grep 'Waiting' $CACHEFILE| awk '{print $6}'
exit 0;
}
nginx_accepts(){
awk NR==3 $CACHEFILE| awk '{print $1}'
exit 0;
}
nginx_handled(){
awk NR==3 $CACHEFILE| awk '{print $2}'
exit 0;
}
nginx_requests(){
awk NR==3 $CACHEFILE| awk '{print $3}'
exit 0;
}
case $NGINX_COMMAND in
active)
nginx_active;
;;
reading)
nginx_reading;
;;
writing)
nginx_writing;
;;
waiting)
nginx_waiting;
;;
accepts)
nginx_accepts;
;;
handled)
nginx_handled;
;;
requests)
nginx_requests;
;;
*)
echo 'Invalid credentials';
exit 2;
esac
[root@web01 ~]# systemctl restart zabbix-agent.service
5、上面文件配置完成后,进入此页面进行观察,查看是否正常:
6、检查自己添加的nginx状态数据是否正常:
二、zabbix监控php运行状态:
1、需要安装nginx、php-fpm、zabbix-agent服务,安装方法与第一节讲的安装nginx状态监控一样:
2、搭建客户端所有配置文件:
2.1 配置nginx主配置文件conf.d下的主文件,在文件中加入php_status项:
[root@web01 conf.d]# cat 01-www.conf
server {
listen 80;
server_name bbs.oldboy.com;
access_log /var/log/nginx/access_blog.log main;
root /usr/share/nginx/html/bbs;
location / {
index index.php index.html index.htm;
}
location /nginx_status {
stub_status;
}
location /php_status{
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}
location ~* \.(php|php5)$ {
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}
}
注: location /php_status 文件中此配置在起作用
重启服务:[root@web01 ]#systemctl restart nginx
2.2、在客户端配置zabbix-agent检测服务的配置文件(路径:/etc/zabbix/zabbix_agentd.conf):
重启服务:[root@web01 ]#systemctl restart zabbix-agent.service
2.3、把官方监控php服务状态配置文件,放在此路径下
[root@web01 zabbix_agentd.d]# cat fpm.conf
UserParameter=php-fpm[*],/bin/bash /server/script/fpm.sh "$1" "$2"
[root@web01 ]#systemctl restart zabbix-agent.service
2.4、 存放官方配置脚本(脚本存放位置在2.3步骤里面的配置文件中):
[root@web01 script]# cat fpm.sh
#!/bin/bash
##################################
# Zabbix monitoring script
#
# php-fpm:
# - anything available via FPM status page
#
##################################
# Contact:
# vincent.viallet@gmail.com
##################################
# ChangeLog:
# 20100922 VV initial creation
##################################
# Zabbix requested parameter
ZBX_REQ_DATA="$1"
ZBX_REQ_DATA_URL="$2"
# Nginx defaults
NGINX_STATUS_DEFAULT_URL="http://localhost/fpm/status"
WGET_BIN="/usr/bin/wget"
#
# Error handling:
# - need to be displayable in Zabbix (avoid NOT_SUPPORTED)
# - items need to be of type "float" (allow negative + float)
#
ERROR_NO_ACCESS_FILE="-0.91"
ERROR_NO_ACCESS="-0.92"
ERROR_WRONG_PARAM="-0.93"
ERROR_DATA="-0.94" # either can not connect / bad host / bad port
# Handle host and port if non-default
if [ ! -z "$ZBX_REQ_DATA_URL" ]; then
URL="$ZBX_REQ_DATA_URL"
else
URL="$NGINX_STATUS_DEFAULT_URL"
fi
# save the nginx stats in a variable for future parsing
NGINX_STATS=$($WGET_BIN -q $URL -O - 2>/dev/null)
# error during retrieve
if [ $? -ne 0 -o -z "$NGINX_STATS" ]; then
echo $ERROR_DATA
exit 1
fi
#
# Extract data from nginx stats
#
#RESULT=$(echo "$NGINX_STATS" | awk 'print $0;match($0, "^'"$ZBX_REQ_DATA"':[[:space:]]+(.*)", a) { print a[1] }')
#RESULT=$(echo "$NGINX_STATS" | grep "$ZBX_REQ_DATA" | awk -F : '{print $2}')
RESULT=$(echo "$NGINX_STATS" | awk -F : "{if(\$1==\"$ZBX_REQ_DATA\") print \$2}")
if [ $? -ne 0 -o -z "$RESULT" ]; then
echo $ERROR_WRONG_PARAM
exit 1
fi
echo $RESULT
exit 0
2.5 重启zabbix-agent服务,并curl命令检测:
[root@web01 ]#systemctl restart zabbix-agent.service
[root@web01 ~]# curl 10.0.0.7/php_status
pool: www
process manager: dynamic
start time: 10/Jul/2019:17:09:09 +0800
start since: 11781
accepted conn: 1427
listen queue: 0
max listen queue: 0
listen queue len: 128
idle processes: 6
active processes: 1
total processes: 7
max active processes: 3
max children reached: 0
slow requests: 0
注:出现上面的内容后,说明配置文件全部生效,可以进行zabbix的web界面配置了
3、zabbix界面添加php监控项:
3.1 添加官方php状态监控模板:
3.2 给导入模板修改宏:
3.3 在所需要监控的主机里面加入已经修改好的模板:
3.4 加入监控项目后,检查项目是否已启用(报错的话,会出现红色图示)
3.5 在最新数据里面查看所监测项目数值是否正常:
三、zabbix监控redis运行状态:
1、需要安装nginx、redis、php-fpm、zabbix-agent服务,除redis软件外,其余安装方法与第一节讲的安装nginx状态监控一样:
1.1 安装redis:
[root@web01 ~]# yum install -y redis
Loaded plugins: fastestmirror
Loading mirror speeds from cached hostfile
* base: mirrors.aliyun.com
* extras: mirrors.163.com
* updates: mirrors.163.com
base
epel
extras
nginx
updates
Resolving Dependencies
--> Running transaction check
---> Package redis.x86_64 0:3.2.12-2.el7 will be installed
--> Processing Dependency: libjemalloc.so.1()(64bit) for package: redis-3.2.12-2.e
--> Running transaction check
---> Package jemalloc.x86_64 0:3.6.0-1.el7 will be installed
--> Finished Dependency Resolution
Dependencies Resolved
==================================================================================
Package Arch Version
==================================================================================
Installing:
redis x86_64 3.2.12-2.el7
Installing for dependencies:
jemalloc x86_64 3.6.0-1.el7
Transaction Summary
==================================================================================
Install 1 Package (+1 Dependent package)
Total download size: 648 k
Installed size: 1.7 M
Downloading packages:
(1/2): jemalloc-3.6.0-1.el7.x86_64.rpm
(2/2): redis-3.2.12-2.el7.x86_64.rpm
----------------------------------------------------------------------------------
Total
Running transaction check
Running transaction test
Transaction test succeeded
Running transaction
Installing : jemalloc-3.6.0-1.el7.x86_64
Installing : redis-3.2.12-2.el7.x86_64
Verifying : redis-3.2.12-2.el7.x86_64
Verifying : jemalloc-3.6.0-1.el7.x86_64
Installed:
redis.x86_64 0:3.2.12-2.el7
Dependency Installed:
jemalloc.x86_64 0:3.6.0-1.el7
Complete!
1.2 安装php-pecl-redis:
[root@web01 ~]# yum install -y php-pecl-redis
Loaded plugins: fastestmirror
Loading mirror speeds from cached hostfile
* base: mirrors.aliyun.com
* extras: mirrors.163.com
* updates: mirrors.163.com
Resolving Dependencies
--> Running transaction check
---> Package php-pecl-redis.x86_64 0:2.2.8-1.el7 will be installed
--> Processing Dependency: /usr/bin/pecl for package: php-pecl-redis-2.2.8-1.el7.x
--> Processing Dependency: /usr/bin/pecl for package: php-pecl-redis-2.2.8-1.el7.x
--> Processing Dependency: php-pecl-igbinary(x86-64) for package: php-pecl-redis-2
--> Running transaction check
---> Package php-pear.noarch 1:1.9.4-21.el7 will be installed
--> Processing Dependency: php-xml for package: 1:php-pear-1.9.4-21.el7.noarch
--> Processing Dependency: php-posix for package: 1:php-pear-1.9.4-21.el7.noarch
---> Package php-pecl-igbinary.x86_64 0:1.2.1-1.el7 will be installed
--> Running transaction check
---> Package php-process.x86_64 0:5.4.16-46.el7 will be installed
---> Package php-xml.x86_64 0:5.4.16-46.el7 will be installed
--> Finished Dependency Resolution
Dependencies Resolved
==================================================================================
Package Arch Version
==================================================================================
Installing:
php-pecl-redis x86_64 2.2.8-1.el7
Installing for dependencies:
php-pear noarch 1:1.9.4-21.el7
php-pecl-igbinary x86_64 1.2.1-1.el7
php-process x86_64 5.4.16-46.el7
php-xml x86_64 5.4.16-46.el7
Transaction Summary
==================================================================================
Install 1 Package (+4 Dependent packages)
Total download size: 732 k
Installed size: 3.2 M
Downloading packages:
(1/5): php-pecl-igbinary-1.2.1-1.el7.x86_64.rpm
(2/5): php-pecl-redis-2.2.8-1.el7.x86_64.rpm
(3/5): php-pear-1.9.4-21.el7.noarch.rpm
(4/5): php-xml-5.4.16-46.el7.x86_64.rpm
(5/5): php-process-5.4.16-46.el7.x86_64.rpm
----------------------------------------------------------------------------------
Total
Running transaction check
Running transaction test
Transaction test succeeded
Running transaction
Installing : php-xml-5.4.16-46.el7.x86_64
Installing : php-process-5.4.16-46.el7.x86_64
Installing : 1:php-pear-1.9.4-21.el7.noarch
Installing : php-pecl-igbinary-1.2.1-1.el7.x86_64
Installing : php-pecl-redis-2.2.8-1.el7.x86_64
Verifying : php-process-5.4.16-46.el7.x86_64
Verifying : php-pecl-igbinary-1.2.1-1.el7.x86_64
Verifying : php-pecl-redis-2.2.8-1.el7.x86_64
Verifying : php-xml-5.4.16-46.el7.x86_64
Verifying : 1:php-pear-1.9.4-21.el7.noarch
Installed:
php-pecl-redis.x86_64 0:2.2.8-1.el7
Dependency Installed:
php-pear.noarch 1:1.9.4-21.el7 php-pecl-igbinary.x86_64 0:1.2.1-1.el7
php-xml.x86_64 0:5.4.16-46.el7
Complete!
1.3 重启redis服务和php-fpm服务:
[root@web01 ~]# systemctl start redis
[root@web01 ~]# systemctl enable redis
Created symlink from /etc/systemd/system/multi-user.target.wants/redis.service to
[root@web01 ~]# systemctl enable php-fpm
[root@web01 ~]# systemctl start php-fpm
2、搭建客户端所有配置文件:
1.1 监控redis服务不需要配置nginx服务,和前面2个监控项的唯一区别就是这里:
2.2、在客户端配置zabbix-agent检测服务的配置文件(路径:/etc/zabbix/zabbix_agentd.conf):
重启服务:[root@web01 ]#systemctl restart zabbix-agent.service
2.3、把官方监控redis服务状态配置文件,放在此路径下
[root@web01 zabbix_agentd.d]# cat redis.conf
UserParameter=redis.discovery,/bin/bash /server/script/redis.sh localhost list_key_space_db
UserParameter=redis[*],/bin/bash /server/script/redis.sh $1 $2 $3
[root@web01 ]#systemctl restart zabbix-agent.service
2.4、 存放官方配置脚本(脚本存放位置在2.3步骤里面的配置文件中):
[root@web01 script]# cat redis.sh
#!/bin/bash
#Redis status
METRIC="$2"
SERV="$1"
DB="$3"
PORT="6379"
if [[ -z "$1" ]]; then
#echo "Please set server"
#exit 1
SERV=127.0.0.1
fi
CACHETTL="55" # Время действия кеша в секундах (чуть меньше чем период опроса элементов)
CACHE="/tmp/redis-status-`echo $SERV | md5sum | cut -d" " -f1`.cache"
if [ -s "$CACHE" ]; then
TIMECACHE=`stat -c"%Z" "$CACHE"`
else
TIMECACHE=0
fi
TIMENOW=`date '+%s'`
if [ "$(($TIMENOW - $TIMECACHE))" -gt "$CACHETTL" ]; then
redis-cli -a 123456 -h $SERV -p $PORT info > $CACHE
#(echo -en "INFO\r\n"; sleep 1;) | nc $SERV $PORT > $CACHE || exit 1
fi
FIRST_ELEMENT=1
function json_head {
printf "{";
printf "\"data\":[";
}
function json_end {
printf "]";
printf "}";
}
function check_first_element {
if [[ $FIRST_ELEMENT -ne 1 ]]; then
printf ","
fi
FIRST_ELEMENT=0
}
function databse_detect {
json_head
for dbname in $LIST_DATABSE
do
local dbname_t=$(echo $dbname| sed 's!\n!!g')
check_first_element
printf "{"
printf "\"{#DBNAME}\":\"$dbname_t\""
printf "}"
done
json_end
}
case $METRIC in
'redis_version')
cat $CACHE | grep "redis_version:" | cut -d':' -f2
;;
'redis_git_sha1')
cat $CACHE | grep "redis_git_sha1:" | cut -d':' -f2
;;
'redis_git_dirty')
cat $CACHE | grep "redis_git_dirty:" | cut -d':' -f2
;;
'redis_mode')
cat $CACHE | grep "redis_mode:" | cut -d':' -f2
;;
'arch_bits')
cat $CACHE | grep "arch_bits:" | cut -d':' -f2
;;
'multiplexing_api')
cat $CACHE | grep "multiplexing_api:" | cut -d':' -f2
;;
'gcc_version')
cat $CACHE | grep "gcc_version:" | cut -d':' -f2
;;
'uptime_in_seconds')
cat $CACHE | grep "uptime_in_seconds:" | cut -d':' -f2
;;
'lru_clock')
cat $CACHE | grep "lru_clock:" | cut -d':' -f2
;;
'connected_clients')
cat $CACHE | grep "connected_clients:" | cut -d':' -f2
;;
'client_longest_output_list')
cat $CACHE | grep "client_longest_output_list:" | cut -d':' -f2
;;
'client_biggest_input_buf')
cat $CACHE | grep "client_biggest_input_buf:" | cut -d':' -f2
;;
'used_memory')
cat $CACHE | grep "used_memory:" | cut -d':' -f2
;;
'used_memory_peak')
cat $CACHE | grep "used_memory_peak:" | cut -d':' -f2
;;
'mem_fragmentation_ratio')
cat $CACHE | grep "mem_fragmentation_ratio:" | cut -d':' -f2
;;
'loading')
cat $CACHE | grep "loading:" | cut -d':' -f2
;;
'rdb_changes_since_last_save')
cat $CACHE | grep "rdb_changes_since_last_save:" | cut -d':' -f2
;;
'rdb_bgsave_in_progress')
cat $CACHE | grep "rdb_bgsave_in_progress:" | cut -d':' -f2
;;
'aof_rewrite_in_progress')
cat $CACHE | grep "aof_rewrite_in_progress:" | cut -d':' -f2
;;
'aof_enabled')
cat $CACHE | grep "aof_enabled:" | cut -d':' -f2
;;
'aof_rewrite_scheduled')
cat $CACHE | grep "aof_rewrite_scheduled:" | cut -d':' -f2
;;
'total_connections_received')
cat $CACHE | grep "total_connections_received:" | cut -d':' -f2
;;
'total_commands_processed')
cat $CACHE | grep "total_commands_processed:" | cut -d':' -f2
;;
'instantaneous_ops_per_sec')
cat $CACHE | grep "instantaneous_ops_per_sec:" | cut -d':' -f2
;;
'rejected_connections')
cat $CACHE | grep "rejected_connections:" | cut -d':' -f2
;;
'expired_keys')
cat $CACHE | grep "expired_keys:" | cut -d':' -f2
;;
'evicted_keys')
cat $CACHE | grep "evicted_keys:" | cut -d':' -f2
;;
'keyspace_hits')
cat $CACHE | grep "keyspace_hits:" | cut -d':' -f2
;;
'keyspace_misses')
cat $CACHE | grep "keyspace_misses:" | cut -d':' -f2
;;
'pubsub_channels')
cat $CACHE | grep "pubsub_channels:" | cut -d':' -f2
;;
'pubsub_patterns')
cat $CACHE | grep "pubsub_patterns:" | cut -d':' -f2
;;
'latest_fork_usec')
cat $CACHE | grep "latest_fork_usec:" | cut -d':' -f2
;;
'role')
cat $CACHE | grep "role:" | cut -d':' -f2
;;
'connected_slaves')
cat $CACHE | grep "connected_slaves:" | cut -d':' -f2
;;
'used_cpu_sys')
cat $CACHE | grep "used_cpu_sys:" | cut -d':' -f2
;;
'used_cpu_user')
cat $CACHE | grep "used_cpu_user:" | cut -d':' -f2
;;
'used_cpu_sys_children')
cat $CACHE | grep "used_cpu_sys_children:" | cut -d':' -f2
;;
'used_cpu_user_children')
cat $CACHE | grep "used_cpu_user_children:" | cut -d':' -f2
;;
'key_space_db_keys')
cat $CACHE | grep $DB:|cut -d':' -f2|awk -F, '{print $1}'|cut -d'=' -f2
;;
'key_space_db_expires')
cat $CACHE | grep $DB:|cut -d':' -f2|awk -F, '{print $2}'|cut -d'=' -f2
;;
'list_key_space_db')
LIST_DATABSE=`cat $CACHE | grep '^db[0-9]\+:'|cut -d: -f1`
databse_detect
;;
*)
echo "Not selected metric"
exit 0
;;
esac
2.5 重启zabbix-agent服务:
[root@web01 ]#systemctl restart zabbix-agent.service
2.6 命令行检测键值得时候,需要先在zabbix服务器上面安装zabbix-get服务(纯命令,无需启动服务):
[root@zabbix ~]# yum install -y zabbix-get.x86_64
2.7 在命令行检测数值:
3、zabbix界面添加redis监控项:
3.1 添加官方redis状态监控模板:
3.2 给导入模板修改宏:
3.3 在所需要监控的主机里面加入已经修改好的模板:
3.4 加入监控项目后,检查项目是否已启用(报错的话,会出现红色图示)
3.5 在最新数据里面查看所监测项目数值是否正常: