<repo_name>summerbuild/zabbix-conf<file_sep>/script/tcp_status
#!/bin/bash  
#this script is used to get tcp and udp connetion status  
#tcp status
  
case $1 in  
   closed) 
		  output=`ss -ant|awk '{++S[$1]}END{for(a in S) print a,S[a]}'|awk '/CLOSED/{print $2}'`
          if [ "$output" == "" ];then  
             echo 0  
          else  
             echo $output  
          fi  
        ;;  
   listen)  
		  output=`ss -ant|awk '{++S[$1]}END{for(a in S) print a,S[a]}'|awk '/LISTEN/{print $2}'`
          if [ "$output" == "" ];then  
             echo 0  
          else  
             echo $output  
          fi  
        ;;  
   synrecv)  
		  output=`ss -ant|awk '{++S[$1]}END{for(a in S) print a,S[a]}'|awk '/SYN_RECV/{print $2}'`
          if [ "$output" == "" ];then  
             echo 0  
          else  
             echo $output  
          fi  
        ;;  
   synsent)
          output=`ss -ant|awk '{++S[$1]}END{for(a in S) print a,S[a]}'|awk '/SYN_SENT/{print $2}'`		  
          if [ "$output" == "" ];then  
             echo 0  
          else  
             echo $output  
          fi  
        ;;  
   established)  
          output=`ss -ant|awk '{++S[$1]}END{for(a in S) print a,S[a]}'|awk '/ESTAB/{print $2}'`		  
          if [ "$output" == "" ];then  
             echo 0  
          else  
             echo $output  
          fi  
        ;;  
   timewait) 
          output=`ss -ant|awk '{++S[$1]}END{for(a in S) print a,S[a]}'|awk '/TIME_WAIT/{print $2}'`		  
          if [ "$output" == "" ];then  
             echo 0  
          else  
             echo $output  
          fi  
        ;;  
   closing)  
          output=`ss -ant|awk '{++S[$1]}END{for(a in S) print a,S[a]}'|awk '/CLOSING/{print $2}'`		  
          if [ "$output" == "" ];then  
             echo 0  
          else  
             echo $output  
          fi  
        ;;  
   closewait) 
          output=`ss -ant|awk '{++S[$1]}END{for(a in S) print a,S[a]}'|awk '/CLOSE_WAIT/{print $2}'`		  
          if [ "$output" == "" ];then  
             echo 0  
          else  
             echo $output  
          fi  
        ;;  
   lastack)    
		  output=`ss -ant|awk '{++S[$1]}END{for(a in S) print a,S[a]}'|awk '/LAST_ACK/{print $2}'`
          if [ "$output" == "" ];then  
             echo 0  
          else  
             echo $output  
          fi  
         ;;  
   finwait1)  
          output=`ss -ant|awk '{++S[$1]}END{for(a in S) print a,S[a]}'|awk '/FIN_WAIT1/{print $2}'`		  
          if [ "$output" == "" ];then  
             echo 0  
          else  
             echo $output  
          fi  
         ;;  
   finwait2)   
          output=`ss -ant|awk '{++S[$1]}END{for(a in S) print a,S[a]}'|awk '/FIN_WAIT2/{print $2}'`		  
          if [ "$output" == "" ];then  
             echo 0  
          else  
             echo $output  
          fi  
         ;;  
         *)  
          echo -e "\e[033mUsage: sh  $0 [closed|closing|closewait|synrecv|synsent|finwait1|finwait2|listen|established|lastack|timewait]\e[0m"  
esac
<file_sep>/script/check_mem
#!/bin/bash
# check memory script
# sunny 2008.2.15

if [ -f /etc/redhat-release ];then
    OS=`cat /etc/redhat-release |awk '{print $4}'|awk -F. '{print $1}'`
    if [ $OS == 7 ];then
        total_value=`free|grep Mem|awk '{print $2}'`
	Free=`free|grep Mem|awk '{print $NF}'`
	#cached_value=`free|grep Mem|awk '{print $6}'`
	share_value=`free|grep Mem|awk '{print $5}'`
	used_value=$[$total_value - $Free]
    else
	# Total memory 
	total_value=`cat /proc/meminfo |grep "^MemTotal"|awk '{print $2}'`
	# Free memory 
	free_value=`cat /proc/meminfo |grep "^MemFree"|awk '{print $2}'`
	buffers_value=`cat /proc/meminfo |grep "^Buffers"|awk '{print $2}'`
	cached_value=`cat /proc/meminfo |grep "^Cached"|awk '{print $2}'`
	Free=$[$free_value + $buffers_value + $cached_value]
	# Used memory 
	used_value=$[$total_value - $Free]
    fi
else
    total_value=`free|grep Mem|awk '{print $2}'`
    Free=`free|grep Mem|awk '{print $NF}'`
    #cached_value=`free|grep Mem|awk '{print $6}'`
    share_value=`free|grep Mem|awk '{print $5}'`
    used_value=`expr $total_value - $Free`
fi
# to calculate used percent
# use the expression  used * 100 / total
usedtmp=`expr $used_value \* 100`
usedent=`expr $usedtmp / $total_value`
#############################################################################################
total () {
#如果指定单位为M,则换算为M.默认是G为单位
if [ "$1" ];then
	total=$[$total_value/1024]
	if [ "$total" -le 1 ];then
            total=`awk 'BEGIN{printf "%.2f\n",'$total_value/1024'}'|awk '{printf("%0.1f\n",$0)}'`
	fi
else
        #前一个awk保留两位小数,后一个awk4舍5入
        total=`awk 'BEGIN{printf "%.2f\n",'$total_value/1024/1024'}'|awk '{printf("%0.1f\n",$0)}'`
fi
	echo $total
}
#############################################################################################
free(){
if [ "$1" ];then
        free=$[$Free/1024]
	if [ "$free" -le 1 ];then
            free=`awk 'BEGIN{printf "%.2f\n",'$Free/1024'}'`
	fi
else 
        free=`awk 'BEGIN{printf "%.2f\n",'$Free/1024/1024'}'`
fi
	echo $free
}
#############################################################################################
used (){
if [ "$1" ];then
        used=$[$used_value/1024]
	if [ "$used" -le 1 ];then
            used=`awk 'BEGIN{printf "%.2f\n",'$used_value/1024'}'`
	fi
else 
        used=`awk 'BEGIN{printf "%.2f\n",'$used_value/1024/1024'}'`
fi
	echo $used
}
#############################################################################################
case $1 in
	total)
		total $2
	;;
	used)
		used $2
	;;
	free)
		free $2
	;;
	usedent)
		echo $usedent
	;;
	--help|help)
		echo "        $1 [total|used|usedent];$2 [m|default is G]"
		exit 0
esac

<file_sep>/script/zabbixDiscovery
#!/bin/bash
serviceDiscovery() {
    project_dir="/home/deploy/projects/"
    serviceList=($(ls $project_dir|grep "service"))
    printf '{\n'
    printf '\t"data":[\n'
    for key in ${!serviceList[@]}
    do
        if [[ "$key" -lt "((${#serviceList[@]}-1))" ]];then
	    printf "\t\t{\n"
            printf "\t\t\t\"{#SERVICENAME}\":\"${serviceList[$key]}\"},\n"
	else
	    printf "\t\t{\n"
            printf "\t\t\t\"{#SERVICENAME}\":\"${serviceList[$key]}\"}\n"
        fi
    done
    printf '\t ]\n'
    printf '}\n'
}

webDiscovery(){
    webarray=(`ls /data/web-server/|grep -E "^tomcat.*"`)
    length=${#webarray[@]}
    printf "{\n"
    printf  '\t'"\"data\":["
    for ((i=0;i<$length;i++))
    do
        printf '\n\t\t{'
        printf "\"{#WEBNAME}\":\"${webarray[$i]}\"}"
        if [ $i -lt $[$length-1] ];then
                printf ','
        fi
    done
    printf  "\n\t]\n"
    printf "}\n"
}
diskDiscovery(){
    diskarray=(`cat /proc/diskstats |grep -E "\bsd[abcdefg]\b|\bxvd[abcdefg]\b|vd[abcdef]\b"|grep -i "\b$1\b"|awk '{print $3}'|sort|uniq 2>/dev/null`)
    length=${#diskarray[@]}
    printf "{\n"
    printf  '\t'"\"data\":["
    for ((i=0;i<$length;i++))
    do
        printf '\n\t\t{'
        printf "\"{#DISKNAME}\":\"${diskarray[$i]}\"}"
        if [ $i -lt $[$length-1] ];then
                printf ','
        fi
    done
    printf  "\n\t]\n"
    printf "}\n"
}
zkDiscovery(){
    process=$(ps -ef|grep "zookeeper"|grep -c "zoo.cfg")
    if [ $process != 0 ];then
        list=($(ps -ef|grep "zookeeper"|grep "zoo.cfg"|awk '{print $NF}'|sed 's#/bin/..##g'))
        ipaddress=$(ifconfig|grep "inet addr"|grep -v "127.0.0.1"|awk -F: '{print $2}'|awk '{print $1}'|grep -E "^192|^10")
        printf "{\n"
        printf  '\t'"\"data\":["
        for key in ${!list[@]}
        do
            port=$(awk -F= '/clientPort/{print $2}' ${list[$key]})
            printf '\n\t\t{'
            printf "\"{#ZKNODES}\":\"$ipaddress:$port\"}"
            if [[ "$key" -lt "((${#list[@]}-1))" ]];then
                printf ","
            fi
        done
        printf  "\n\t]\n"
        printf "}\n"
    fi
}
redisDiscovery(){
    redisarray=($(ps -ef|grep redis-server|grep -v grep|awk '{print $9}'))
    length=${#redisarray[@]}
    printf "{\n"
    printf  '\t'"\"data\":["
    for ((i=0;i<$length;i++))
    do
        printf '\n\t\t{'
        printf "\"{#REDISNODE}\":\"${redisarray[$i]}\"}"
        if [ $i -lt $[$length-1] ];then
                printf ','
        fi
    done
    printf  "\n\t]\n"
    printf "}\n"
}
processDiscovery(){
    if [ -f "/etc/zabbix/tmp/process" ];then
        processarray=($(cat /etc/zabbix/tmp/process))
        length=${#processarray[@]}
        printf "{\n"
        printf  '\t'"\"data\":["
        for ((i=0;i<$length;i++))
        do
            printf '\n\t\t{'
            printf "\"{#PROCESS}\":\"${processarray[$i]}\"}"
            if [ $i -lt $[$length-1] ];then
                    printf ','
            fi
        done
        printf  "\n\t]\n"
        printf "}\n"
    fi
}

case $1 in
    service)
        serviceDiscovery
        ;;
    web)
        webDiscovery
        ;;
    disk)
        diskDiscovery
        ;;
    redis)
        redisDiscovery
        ;;
    process)
        processDiscovery
        ;;
esac
<file_sep>/script/disk_io
#!/bin/bash
#
#
file="/tmp/disk_io"
if [ $1 ] && [ $2 ];then
	`iostat -x  -t 1 -d 2 > /tmp/disk_io`
fi
case $2 in
        read)
                data=`cat $file|grep $1|tail -1|awk '{print $6}'`
                echo $data
        ;;
        write)
                data=`cat $file|grep $1|tail -1|awk '{print $7}'`
                echo $data
        ;;
        avgqu)
                data=`cat $file|grep $1|tail -1|awk '{print $9}'`
                echo $data
        ;;
        wait)
                data=`cat $file|grep $1|tail -1|awk '{print $10}'`
                echo $data
        ;;
        svctm)
                data=`cat $file|grep $1|tail -1|awk '{print $11}'`
                echo $data
        ;;
        util)
                if [ -f /etc/redhat-release ];then
                        data=`cat $file|grep $1|tail -1|awk '{print $12}'`
                else
                        version=`cat /etc/issue|awk '{print $1}'|head -1`
                        if [ "$version" = "Ubuntu" ];then
                        data=`cat $file|grep $1|tail -1|awk '{print $14}'`
                        fi
                fi
                echo $data
        ;;
esac
