Zabbix监控插件-Nginx、TCP、Redis、Memcached

戚阳
2023-12-01

2014年写的Zabbix监控插件,主要监控TCP的11种状态、Nginx状态、Redis和Memcached状态。
 
监控模板请查看附件。直接导入到Zabbix中即可。
 
Nginx监控状态配置默认使用127.0.0.1获取。
 
Nginx监控配置:
 
1.为Nginx增加状态配置文件。

[root@linux-node2 ~]# yum install -y nginx
[root@linux-node2 ~]# vim /etc/nginx/conf.d/nginx-status.conf
server {
       server_name 127.0.0.1;
       location /nginx_status {
                stub_status on;
                access_log off;
                allow 127.0.0.1;
                deny all;
                }
}

2.测试Nginx配置并启动

[root@linux-node2 ~]# nginx -t
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful
[root@linux-node2 ~]# systemctl start nginx

3.测试Nginx状态接口

[root@linux-node2 ~]# curl http://127.0.0.1/nginx_status
Active connections: 1 
server accepts handled requests
 7 7 7 
Reading: 0 Writing: 1 Waiting: 0
#!/bin/bash
############################################################
# $Name:         zabbix_linux_plugins.sh
# $Version:      v1.0
# $Function:     zabbix plugins
# $Author:       Jason Zhao
# $organization: www.unixhot.com
# $Create Date:  2014-08-10
# $Description:  Monitor Linux Service Status
############################################################
tcp_status_fun(){
	TCP_STAT=$1
	#netstat -n | awk '/^tcp/ {  state[$NF]} END {for(key in state) print key,state[key]}' > /tmp/netstat.tmp
	ss -ant | awk 'NR>1 {  s[$1]} END {for(k in s) print k,s[k]}' > /tmp/netstat.tmp
	TCP_STAT_VALUE=$(grep "$TCP_STAT" /tmp/netstat.tmp | cut -d ' ' -f2)
	if [ -z $TCP_STAT_VALUE ];then
		TCP_STAT_VALUE=0
	fi
	echo $TCP_STAT_VALUE
}

nginx_status_fun(){
	NGINX_PORT=$1
	NGINX_COMMAND=$2
	nginx_active(){
        /usr/bin/curl "http://127.0.0.1:"$NGINX_PORT"/nginx_status/" 2>/dev/null| grep 'Active' | awk '{print $NF}'
        }
	nginx_reading(){
        /usr/bin/curl "http://127.0.0.1:"$NGINX_PORT"/nginx_status/" 2>/dev/null| grep 'Reading' | awk '{print $2}'
       }
	nginx_writing(){
        /usr/bin/curl "http://127.0.0.1:"$NGINX_PORT"/nginx_status/" 2>/dev/null| grep 'Writing' | awk '{print $4}'
       }
	nginx_waiting(){
        /usr/bin/curl "http://127.0.0.1:"$NGINX_PORT"/nginx_status/" 2>/dev/null| grep 'Waiting' | awk '{print $6}'
       }
	nginx_accepts(){
        /usr/bin/curl "http://127.0.0.1:"$NGINX_PORT"/nginx_status/" 2>/dev/null| awk NR==3 | awk '{print $1}'
       }
	nginx_handled(){
        /usr/bin/curl "http://127.0.0.1:"$NGINX_PORT"/nginx_status/" 2>/dev/null| awk NR==3 | awk '{print $2}'
       }
	nginx_requests(){
        /usr/bin/curl "http://127.0.0.1:"$NGINX_PORT"/nginx_status/" 2>/dev/null| awk NR==3 | awk '{print $3}'
       }
  	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;
		esac 
}

memcached_status_fun(){
	M_PORT=$1
	M_COMMAND=$2
	echo -e "stats\nquit" | nc 127.0.0.1 "$M_PORT" | grep "STAT $M_COMMAND " | awk '{print $3}'
}

redis_status_fun(){
	R_PORT=$1
	R_COMMAND=$2
	(echo -en "INFO \r\n";sleep 1;) | nc 127.0.0.1 "$R_PORT" > /tmp/redis_"$R_PORT".tmp
	REDIS_STAT_VALUE=$(grep ""$R_COMMAND":" /tmp/redis_"$R_PORT".tmp | cut -d ':' -f2)
 	echo $REDIS_STAT_VALUE	
}

main(){
	case $1 in
		tcp_status)
			tcp_status_fun $2;
			;;
		nginx_status)
			nginx_status_fun $2 $3;
			;;
		memcached_status)
			memcached_status_fun $2 $3;
			;;
		redis_status)
			redis_status_fun $2 $3;
			;;
		*)
			echo $"Usage: $0 {tcp_status key|memcached_status key|redis_status key|nginx_status key}"
	esac
}

main $1 $2 $3

使用方法:

[root@linux-node2 ~]# ./zabbix_linux_plugin.sh 
Usage: ./zabbix_linux_plugin.sh {tcp_status key|memcached_status key|redis_status key|nginx_status key}

获取TCP监控状态

[root@linux-node2 ~]# ./zabbix_linux_plugin.sh tcp_status LISTEN
14

作者:赵班长

地址:https://www.unixhot.com/article/139463

 类似资料: