zabbix 腳本監控示例

手寫磁盤百分比監控腳本

# cat percent_free_disk.sh
#!/bin/bash

function root {
    DISK_ROOT_USE=`df|grep /dev/vda1|awk -F'[ |%]' '{print $(NF-2)}'`
    echo $((100-$DISK_ROOT_USE))
}
function apps {
    DISK_APPS_USE=`df|grep /apps|awk -F'[ |%]' '{print $(NF-2)}'`
    echo $((100-$DISK_APPS_USE))
}
function jenkins {
    DISK_JENKINS_USE=`df|grep jenkins |awk -F'[ |%]' '{print $(NF-2)}'`
    echo $((100-$DISK_JENKINS_USE))
}

$1

配置文件

# cat userparams.conf
UserParameter=disk.root,/etc/zabbix/zabbix_agentd.d/percent_free_disk.sh root
UserParameter=disk.apps,/etc/zabbix/zabbix_agentd.d/percent_free_disk.sh apps
#UserParameter=disk.jenkins,/etc/zabbix/zabbix_agentd.d/percent_free_disk.sh jenkins

 

nginx狀態獲取

# cat get-nginx-stat.sh
#!/bin/bash

# Set Variables
URL="localhost/nginx_stat"

# Functions to return nginx stats

function active {
/usr/bin/curl "$URL" 2> /dev/null| grep 'Active' | awk '{print $NF}'
}

function reading {
/usr/bin/curl "$URL" 2> /dev/null| grep 'Reading' | awk '{print $2}'
}

function writing {
/usr/bin/curl "$URL" 2>/dev/null| grep 'Writing' | awk '{print $4}'
}

function waiting {
/usr/bin/curl "$URL" 2> /dev/null| grep 'Waiting' | awk '{print $6}'
}

function accepts {
/usr/bin/curl "$URL" 2> /dev/null| awk NR==3 | awk '{print $1}'
}

function handled {
/usr/bin/curl "$URL" 2> /dev/null| awk NR==3 | awk '{print $2}'
}

function requests {
/usr/bin/curl "$URL" 2> /dev/null| awk NR==3 | awk '{print $3}'
}

# Run the requested function
$1

tcp狀態監控

# cat get-nginx-stat.sh
#!/bin/bash

# Set Variables
URL="localhost/nginx_stat"

# Functions to return nginx stats

function active {
/usr/bin/curl "$URL" 2> /dev/null| grep 'Active' | awk '{print $NF}'
}

function reading {
/usr/bin/curl "$URL" 2> /dev/null| grep 'Reading' | awk '{print $2}'
}

function writing {
/usr/bin/curl "$URL" 2>/dev/null| grep 'Writing' | awk '{print $4}'
}

function waiting {
/usr/bin/curl "$URL" 2> /dev/null| grep 'Waiting' | awk '{print $6}'
}

function accepts {
/usr/bin/curl "$URL" 2> /dev/null| awk NR==3 | awk '{print $1}'
}

function handled {
/usr/bin/curl "$URL" 2> /dev/null| awk NR==3 | awk '{print $2}'
}

function requests {
/usr/bin/curl "$URL" 2> /dev/null| awk NR==3 | awk '{print $3}'
}

# Run the requested function
$1
[root@prod-service08 zabbix_agentd.d]# cat get-tcp-stat.py
#!/usr/bin/python
'''
get tcp stat for zabbix
yangxiangyu
20141210
v1
'''

import commands
import sys
out = commands.getoutput('ss -ant')
print int(out.split().count(sys.argv[1]))

fpm狀態監控

# cat get-fpm-stat.sh
#!/bin/bash
URL="localhost/status"

function startSince(){
    curl http://$URL 2>/dev/null | grep "start since" | awk -F: '{print $2}' | sed s/[[:space:]]//g
}
function acceptdConn(){
    curl http://$URL 2>/dev/null | grep "accepted conn" | awk -F: '{print $2}' | sed s/[[:space:]]//g
}
function listenQueue(){
    curl http://$URL 2>/dev/null | grep "listen queue" | awk -F: '{print $2}' | sed s/[[:space:]]//g
}
function maxListenQueue(){
    curl http://$URL 2>/dev/null | grep "max listen queue" | awk -F: '{print $2}' | sed s/[[:space:]]//g
}
function listenQueueLen(){
    curl http://$URL 2>/dev/null | grep "listen queue len" | awk -F: '{print $2}' | sed s/[[:space:]]//g
}
function idleProcesses(){
    curl http://$URL 2>/dev/null | grep "idle processes" | awk -F: '{print $2}' | sed s/[[:space:]]//g
}
function activeProcess(){
    curl http://$URL 2>/dev/null | grep "active processes" | awk -F: '{print $2}' | sed s/[[:space:]]//g | sed -n '1p'
}
function totalProcesses(){
    curl http://$URL 2>/dev/null | grep "total processes" | awk -F: '{print $2}' | sed s/[[:space:]]//g
}
function maxActiveProcesses(){
    curl http://$URL 2>/dev/null | grep "max active processes" | awk -F: '{print $2}' | sed s/[[:space:]]//g
}
function maxChildrenReached(){
    curl http://$URL 2>/dev/null | grep "max children reached" | awk -F: '{print $2}' | sed s/[[:space:]]//g
}
function slowRequests(){
    curl http://$URL 2>/dev/null | grep "slow requests" | awk -F: '{print $2}' | sed s/[[:space:]]//g
}
$1

jvm-cpu監控

# cat jvm-cpu.sh
#!/bin/bash

process=$1
jvmcpu=0
cpunum=`cat /proc/cpuinfo| grep "initial apicid"| sort| uniq| wc -l`

pid=`ps -ef | grep "${process}" | grep -vE "grep|bash" | awk '{print $2}'`
if [ $(echo ${pid}|wc -w) -ne 1 ];then
    echo "Error! pid is ${pid}"
    exit 1
fi

CPU(){
    jvmcpu=`ps aux|grep ${pid}|grep -vE "grep|bash"|awk '{print $3}'`
    echo $jvmcpu $cpunum | awk '{print $1/$2}'
}

CPU

 

jvm-mem監控

# cat jvm-mem.sh
#!/bin/bash

process=$1
jvmmem=0

pid=`ps -ef | grep "${process}" | grep -vE "grep|bash" | awk '{print $2}'`
if [ $(echo ${pid}|wc -w) -ne 1 ];then
    echo "Error! pid is ${pid}"
    exit 1
fi

MEM(){
    jvmmem=`cat /proc/${pid}/status | grep "VmRSS" | awk '{print $2}'`
    echo $jvmmem
}

MEM

jvm-task監控

# cat jvm-task.sh
#!/bin/bash

process=$1
jvmTasks=0

pid=`ps -ef | grep "${process}" | grep -vE "grep|bash" | awk '{print $2}'`
if [ $(echo ${pid}|wc -w) -ne 1 ];then
    echo "Error! pid is ${pid}"
    exit 1
fi

Tasks(){
    jvmTasks=`ls /proc/${pid}/task/|wc -w`
    echo $jvmTasks
}

Tasks

 

redis-alive監控

# cat redis_alive.sh
#!/bin/bash
#written for check whether we can connect KVstore.

#check input parameters.
PARAM_COUNT=$#
if [ ${PARAM_COUNT} -ne 1 ];then
    echo "[Error]Wrong input parameter numbers."
    exit 1
fi

PARAM_CHECK=$1

#predefine functions.
function final_result(){
    if [ "${1}" == "PONG" ];then
        echo 0
    else
        echo 1
    fi
}

function check_statistics(){
    result=`redis-cli -h 52a34c024547489a.m.cnbja.kvstore.aliyuncs.com -p 6379 -a 52a34c024547489a:0s9j09sHSj1sdf1oL ping 2>/dev/null`

    final_result ${result}
}

function check_yunying(){
    result=`redis-cli -h b0d5cb45f85b4884.m.cnbja.kvstore.aliyuncs.com -p 6379 -a b0d5cb45f85b4884:b0d5cb45f85b4884MhxzKhl8887 ping 2>/dev/null`

    final_result ${result}
}

function check_hermes_redis(){
    result=`redis-cli -h f203a3bb4d1f4ddf.m.cnbja.kvstore.aliyuncs.com -p 6379 -a f203a3bb4d1f4ddf:MhxzKhl8888 ping 2>/dev/null`

    final_result ${result}
}

function check_redis_session(){
    #mkdir -p /tmp/zabbix 2>/dev/null
    #redis-cli -h af1d975705a611e5.m.cnbja.kvstore.aliyuncs.com -p 6379 -a af1d975705a611e5:BaiJiaHuLian0922 ping 2>/dev/null 1>/tmp/zabbix/check_redis_session &
    #command_pid=$!
    #( sleep 3 ; kill -9 $command_pid >/dev/null 2>&1 ) &
    #watchdog=$!
    #sleep_pid=$PPID
    #wait $command_pid >/dev/null 2>&1
    #kill $sleep_pid >/dev/null 2>&1


    result=`redis-cli -h af1d975705a611e5.m.cnbja.kvstore.aliyuncs.com -p 6379 -a af1d975705a611e5:BaiJiaHuLian0922 ping 2>/dev/null`
    #result=`cat /tmp/zabbix/check_redis_session`
    final_result ${result}
}

function check_prod(){
    result=`redis-cli -h 1a10cea4e72111e4.m.cnbja.kvstore.aliyuncs.com -p 6379 -a 1a10cea4e72111e4:BaiJiaHuLian0922 ping 2>/dev/null`
    final_result ${result}
}

${PARAM_CHECK}

exit 0

system-status監控

# cat system-status.sh
#!/bin/bash
netstatus="`sar -n DEV 2 5 | grep eth0 | tail -1`"
case $1 in
    rxpack)
        echo ${netstatus} | awk '{print $3}'
        ;;
    txpack)
        echo ${netstatus} | awk '{print $4}'
        ;;
    *)
        echo "100000"
        ;;
esac

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章