Nagios基于客户端环境自动修改nrpe.cfg脚本

2022-06-29 19:07:41 浏览数 (1)

#!/bin/bash

######################################

#基于客户端系统环境自动生成告警的临界值

#包含网卡、CPU、磁盘、等等

#mail:jianye_jia@163.com

#version 1.0

##############

################

#加载基础算术函数

#math.sh

#################

source ./math.sh

########################

#is_runing

#判断一个进程是否存在

#######################

is_runing() {

#  for pid in $(pidof "$1");

    pidof $1 >/dev/null;

    res=$?;

    if [ $res -eq 0 ];then

        echo 0;

    else

        echo 1;

    fi

}

WARNING=95

CRITICAL=105

DISKCAL=10240

scriptPath="/usr/local/nagios/libexec";

##############################

#提取网卡工作模式,

#生成网卡告警信息

#目前对kvm虚拟机网卡信息不友好

##############################

function getNic()

{

    allNicName=`ifconfig | grep HWaddr| awk '{print $1}'`;

    for nicName in `echo $allNicName`

    do

    nicSpeed=`ethtool $nicName| grep Speed | grep -o  '[0-9] '`;

    if [ -z $nicSpeed ]; then

        continue;

    else

        sed -i "/check_$nicName/d" /usr/local/nagios/etc/nrpe.cfg;

        echo "command[check_$nicName]=$scriptPath/check_iftraffic.pl -i $nicName -w $WARNING -c $CRITICAL -b $nicSpeed -u m" >>  /usr/local/nagios/etc/nrpe.cfg;

    fi

    done

}

####################

#提取CPU个数

#生成LOAD临界值

####################

function getLoad()

{

    WARNING=90

    CRITICAL=100

    allCpuNumber=`cat /proc/cpuinfo | grep processor | wc -l`;

    warningNumber=`awk -v cpuNumber=$allCpuNumber -v warn=$WARNING 'BEGIN{printf "%.2fn",cpuNumber*warn/100}' `;

    criticalNumber=`awk -v cpuNumber=$allCpuNumber -v critial=$CRITICAL 'BEGIN{printf "%.2fn",cpuNumber*critial/100}' `;

    sed -i '/check_load/d' /usr/local/nagios/etc/nrpe.cfg;

    echo "command[check_load]=$scriptPath/check_load -w $warningNumber -c $criticalNumber" >>  /usr/local/nagios/etc/nrpe.cfg;

}

####################

#提取磁盘个数

#生成每个DISK临界值

####################

function getDisk()

{

    WARNING=8%;

    CRITICAL=5%;

    #去掉第一行,去掉挂载的文件系统,去掉tmpfs

    partionName=`df -m | grep -v "Filesystem "|grep -v "文件系统"|grep -v tmpfs| grep -v ":" | awk -v partionDISKCAL=$DISKCAL '{if ( $2 > partionDISKCAL )  {print $6} }'` ;

    #将所有大于10G的分区加入监控项中

    sed -i '/check_disk/d' /usr/local/nagios/etc/nrpe.cfg;

    for pN in `echo $partionName`

    do

        echo "command[check_disk_$pN]=$scriptPath/check_disk -w $WARNING -c $CRITICAL -p $pN " >>  /usr/local/nagios/etc/nrpe.cfg;

    done

}

###################

#提取进程信息

#生成服务监控项

###################

##NGINX

function getProc_nginx()

{

    WARNING="3:5";

    CRITICAL="1:10";

    is_Nginx=`is_runing nginx`;

    if [ $is_Nginx  -eq 1 ];then

        return;

    else

        sed -i '/check_nginx_proc/d' /usr/local/nagios/etc/nrpe.cfg;

        echo "command[check_nginx_proc]=$scriptPath/check_procs -w $WARNING -c $CRITICAL -a nginx" >>  /usr/local/nagios/etc/nrpe.cfg;

    fi

}

#################################

#HTTPD

#根据内存总量,单个httpd进程占用内存量

#来计算该服务器中可以承载多少httpd进程

#如果httpd进程占用CPU过多,也要告警

####################################

function getProc_httpd()

{

    getMemory=`free -k | awk '/Mem/{print $2}'`;

    getSingleHttpdMemory=` ps aux | grep http | awk '$1~/apache|www|/{httpdTotalMemory =$6}END{print httpdTotalMemory/NR}'`;

    getCriticalNumber=`awk -v tM=$getMemory -v sM=$getSingleHttpdMemory 'BEGIN{printf "%d",tM/sM/100*90}'`;

    getWarningNumber=`awk -v tM=$getMemory -v sM=$getSingleHttpdMemory  'BEGIN{printf "%d",tM/sM/100*82}'`;

    WARNING="10:$getWarningNumber";

    CRITICAL="6:$getCriticalNumber";

    is_Httpd=`is_runing  httpd`;

    if [ $is_Httpd  -eq 1 ];then

        return;

    else

        sed -i '/check_httpd_proc/d' /usr/local/nagios/etc/nrpe.cfg;

        echo "command[check_httpd_proc]=$scriptPath/check_procs -w $WARNING -c $CRITICAL -a httpd" >>  /usr/local/nagios/etc/nrpe.cfg;

    fi

}

0 人点赞