zabbix自定义监控项

前段时间需要自己写zabbix自定义监控项,脚本,,就简单的写上几个,分享给大家.

#!/bin/bash
#监控cpu的负载率

function avage ( ){
    fir=`uptime | awk -F ':'  '{print $NF }' | awk "{print $load}" | tr ',' ' '`

    sleep 3
    fir1=`uptime | awk -F ':'  '{print $NF }' | awk "{print $load}" | tr ',' ' '`

    avage=`echo  "($fir1 - $fir)*100" | bc -l`
    echo $avage
}


load=$1
# 根据这个参数,判断去哪一列的值
avage $load
############
# 监控防火墙的状态()
#iptables
#!/bin/bash
# 判断防火墙规则状态,如果没有规则,则默认是没有启用,开始告警.
function Status ( ) {
    rules=`iptables -nvL| grep -vE "Chain|target" | grep -v '^$'| wc -l`
    if [[ $rules = 0 ]]
    then
        echo 1
        # 1表示防火墙关闭,或异常没有规则了;
    else
        echo 0
    fi
}
# 判断防火墙是否被更新
function Edit ( ) {
    # 通过比对id值
    first1=`iptables -L -n|md5sum | awk '{print $1}'` > ./first1.txt
    sleep 5s
    first2=`iptables -L -n|md5sum | awk '{print $1}'` > ./first2.txt
    num=`diff -c ./first1.txt ./first2.txt | wc -l`
    if [[ $num = 0 ]]
    then
        echo 1 
        

    else
        echo 0
        #防火墙被修改,开始告警
    fi
}
#获取参数

case $1 in 
Edit)
        Edit
        ;;
Status)
        Status
        ;;
*)
        echo "fail"
        ;;
esac

#监控messages日志:

num=`tail -n  100  /var/log/messages |grep -i 'error' |wc -l`
echo $num 


##监控http进程

#监控进程状态
function Status ( ){
    num=`netstat -anlp | grep 80|grep  httpd |wc -l `
    if [[  $num = 0 ]]
    then
        echo 0 
        #表示进程不存在告警

    else
        echo 1
    fi

}
#监控http日志中GET访问次数
function GET_num( ) {

        num=`cat /var/log/httpd/access_log | grep GET | wc -l`
        echo $num
}

#监控http错误日志


#监控单个ip的访问次数
function ip_num ( ){
    num=`cat   /var/log/httpd/access_log | awk '{print $1}' | sort -n  | uniq -c| awk '{print $1}' | sort -n | tail -n 1 `
    echo $num

}
case $1 in 
Status)
        Status
        ;;
GET_num)
        GET_num
        ;;
ip_num )
        ip_num 
;;
*)
        echo "fail"
        ;;
esac





####################################
#监控单个ip链接ssh次数,大于10次就告警
    
    link_num=$(tail -n 100  /var/log/secure | grep 'Failed password'|  awk '{print $11}' | sort -n | uniq -c | awk '{print $1}' | sort -n | tail -n 1 )  
   if [ $link_num  -gt 10 ]
   then
        echo 1  #d但
    else
        echo 0 
   fi
#暴力破解10次后,就触发脚本,通过防火墙,将ip禁用
# 优化脚本


    link_ip=$(cat /var/log/secure | grep 'Failed password'|  awk '{print $11}' | sort -n | uniq -c | awk '{print $2}')
     for i in $link_ip
    do
        # echo  "$i"
        #过滤出其中ip对应的次数
        ssh_num=`cat /var/log/secure | grep 'Failed password'|  awk '{print $11}' | sort -n | uniq -c  | grep "$i" |  awk '{print $1}'`
        if [ $ssh_num  -gt  10 ]
        then
                #超过10次,直接告警,然后执行命令,将ip拉黑,加入ssh黑名单中,这里设置触发一次脚本就可以,脚本只触发一次,然后将ip写入
                echo  1
                deny=`cat   /etc/hosts.deny | grep $i| wc -l`
                if [[  $deny = 0 ]]
                then
                    
                        echo "sshd:$i:deny" >> /etc/hosts.deny
                        systemctl restart sshd

                fi
            

        fi




    done

##检测磁盘cpu中io等待率
#!/bin/bash
num=`iostat -c 5 1   | grep -v "avg-cpu" | awk -F '  ' '{print $12}' |grep -v '^$'`
# if [[ $num > 0.05  ]] 
then
    echo 1

else
    num1=`iostat -c 5 1   | grep -v "avg-cpu" | awk -F '  ' '{print $12}' |grep -v '^$'`
    cha=`echo "$num1-$num" | bc `
    # if [[  $cha > 0.03  ]]
    then
        echo  1

    else
        echo 0
        
    fi


fi


#监控磁盘剩余
#!/bin/bash
size=`df -h | awk '{print $5}'| tail -n 7 | tr '%' ' '| sort -n  | tail -n 1`
if [[  $size  > 80  ]]
then
        echo 1
else
        echo  0 
fi
#
#监测CPU、内存、虚拟内存使用超过80%报警 ration.sh
#/bin/bash
cpu=`iostat -c 5 1   | grep -v "avg-cpu" | awk -F '  ' '{print $6}' |grep -v '^$' `
cpu_use= `echo  "$cpu * 100" | bc`
use=`free -m | grep  Mem | awk '{print $3}'| tr 'M' ' '`
total=`free -m | grep  Mem | awk '{print $2}'| tr 'M' ' ' `
ratio=`awk 'BEGIN{printf "%.2f\n",('$use'/'$total')}'`
mem=`echo "$ratio*100" | bc` 
Total=`cat /proc/meminfo | grep -i "VmallocTotal" | awk '{print $2}'`
Use=`cat /proc/meminfo | grep -i "VmallocUsed" | awk '{print $2}' `
ratio1=`awk 'BEGIN{printf "%.4f\n",('$Use'/'$Total')}'`
vmal=`echo "$ratio1*100" |bc`
if [[  $cpu_use > 80   $vmal  > 80 -o   $mem  > 80 ]]
then
    echo 1
else
    echo 0
fi
#########监控	crontab服务监控,是否正常执行
#/bin/bash
last_mofidy_timestamps="$(stat -c %Y /var/log/cron)"
echo $last_mofidy_timestamps
sleep 3600
current_timestamps="$(stat -c %Y /var/log/cron)"
echo $current_timestamps

if (($current_timestamps - $last_mofidy_timestamps > 3600)); then
    echo 1 #如果时间差大于1小时,就告警
else
    echo 0
fi

########

###监控home分区,读写
mkdir -p /home/test
echo "text" >> /home/test
if [[ $? = 0  ]]
then
    echo 0
else
    echo 1 

fi

######服务器SSH服务新增加222监听端口
num=`netstat -anltp | grep 222 | wc -l `
if [[  $num >0  ]]
then
    echo 0
else
    echo 1
fi

######ssh客户端与ssh服务端,连通性监控
#!/bin/bash
num=`telnet  192.168.136.3 22 | grep  -o "Escape character" | wc -l`
if [[  $num != 1 ]]
then
    echo 1 #告警
fi

# 磁盘io监控,当磁盘的IOPS大于1000进行报警

# 网卡流量监控,当网卡流量超过100M/s进行报警;
#!/bin/bash
RX=`cat /sys/class/net/ens33/statistics/rx_bytes`  #收到的数据包数据
TX=`cat /sys/class/net/ens33/statistics/tx_bytes `   #传输的数据包数量
RMBPS=` echo "$RX/1048576" | bc `
TMBPS=` echo "$TX/1048576" | bc`
if [[  $RMBPS -ge 100 || $TMBPS -ge 100 ]]
then
    echo 1
else
    echo 0

fi
# sar使用,每个5秒抓一次,抓5次,磁盘IO和CPU使用情况报警
#!/bin/bash
cpu=`sar -p 5 5   | awk '{print $4}'| tail -n  +5 | sort -n | tail -n 1 `
io=`sar -d 5 5   | awk '{print $4}'| tail -n  +5 | sort -n | tail -n 1`
cpu_status=`awk 'BEGIN{printf($cpu * 100)}' `
io_status=`awk 'BEGIN{printf($io * 100)}' `
if [[  $cpu_status -ge 80 || $io_status -ge 80  ]]
then
    echo 1
else
    echo 0
fi
# iostat,每个5秒抓一次,抓5次,磁盘IO和CPU使用情况报警
#!/bin/bash
cpu=`iostat -c 5 5   | grep -v "avg-cpu"  | awk '{print $1}' | grep '[0-9]' |sort -n |tail -n 1 `
io=`iostat -d 5 5  | grep sda | awk '{print $2}' | sort -n |tail -n 1`
cpu_status=`awk 'BEGIN{printf($cpu * 100)}' `
io_status=`awk 'BEGIN{printf($io * 100)}' `
if [[  $cpu_status -ge 80 || $io_status -ge 80  ]]
then
    echo 1
else
    echo 0
fi
# vmstat,每个5秒抓一次,抓5次,磁盘IO和CPU使用情况报警
io=`vmstat 5 5  |awk '{print $10}'| grep [0-9]|sort -n| tail -n 1`
cpu=`vmstat 5 5  |awk '{print $13}'| grep [0-9]|sort -n| tail -n 1 `
cpu_status=`awk 'BEGIN{printf($cpu * 100)}' `
io_status=`awk 'BEGIN{printf($io * 100)}' `
if [[  $cpu_status -ge 80 || $io_status -ge 80  ]]
then
    echo 1
    else
    echo 0
fi

#监控网卡流量图
#!/bin/bash
RX=`cat /sys/class/net/ens33/statistics/rx_bytes`  #收到的数据包数据
RMBPS=` echo "$RX/1048576" | bc `
echo $RMBPS

TX=`cat /sys/class/net/ens33/statistics/tx_bytes `   #传输的数据包数量
TMBPS=` echo "$TX/1048576" | bc`
echo  $TMBPS


###监控失败登录次数大于5次就告警
#!/bin/bash
num=`lastb | awk '{print $3}' | sort -n  | uniq -c |awk '{print $1}'`
for i in $num
do 
    if [[  $i  -gt 5 ]]
    then
        ip=`lastb | awk '{print $3}' | sort -n  | uniq -c |grep  -w  "$i" | awk '{print $2}'`
        cat /etc/hosts.allow | grep -q $ip
        if [[  $? -eq 0 ]]
        then
            echo 0
        else
            echo 1
        fi
    fi


done


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

吴爃

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值