#!/bin/sh
mkdir -p /etc/zabbix/etc/script
cp zabbix_linux_plugin.sh /etc/zabbix/etc/script/
chown zabbix.zabbix /etc/zabbix/etc/script/zabbix_linux_plugin.sh
chmod +x /etc/zabbix/etc/script/zabbix_linux_plugin.sh

##检查脚本可用性
sh /etc/zabbix/etc/script/zabbix_linux_plugin.sh nginx_status accepts 
sh /etc/zabbix/etc/script/zabbix_linux_plugin.sh nginx_status handled 
sh /etc/zabbix/etc/script/zabbix_linux_plugin.sh nginx_status requests 
sh /etc/zabbix/etc/script/zabbix_linux_plugin.sh nginx_status active 
sh /etc/zabbix/etc/script/zabbix_linux_plugin.sh nginx_status reading 
 
##修改zabbix配置文件
cp /etc/zabbix/etc/zabbix_agentd.conf /etc/zabbix/etc/zabbix_agentd.conf.`date +%F`
cat >> /etc/zabbix/etc/zabbix_agentd.conf << 'EOF'
UserParameter=nginx_status[*],/etc/zabbix/etc/script/zabbix_linux_plugin.sh nginx_status $1
EOF
cat /etc/zabbix/etc/zabbix_agentd.conf


##重启zabbix客户端
/etc/init.d/salt-minion restart


##服务端测试
#zabbix_get -s 10.x.x.x -k  nginx_status[accepts]
#zabbix_get -s 10.x.x.x -k  nginx_status[handled]
#zabbix_get -s 10.x.x.x -k  nginx_status[requests]
#zabbix_get -s 10.x.x.x -k  nginx_status[active]
#zabbix_get -s 10.x.x.x -k  nginx_status[reading]
#zabbix_get -s 10.x.x.x -k  nginx_status[writing]
#zabbix_get -s 10.x.x.x -k  nginx_status[waiting]

#!/bin/bash
############################################################
# $Name:         zabbix_linux_plugins.sh
# $Version:      v1.0
# $Description:  Monitor Linux Service Status
############################################################

nginx_status_fun(){
    NGINX_PORT=80
    NGINX_COMMAND=$1
    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 
}


main(){
    case $1 in
        nginx_status)
            nginx_status_fun $2 $3;
            ;;
        *)
            echo $"Usage: $0 {nginx_status key}"
    esac
}

main $1 $2