共计 3716 个字符,预计需要花费 10 分钟才能阅读完成。
#!/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 “%.2f\n”,cpuNumber*warn/100}’ `;
criticalNumber=`awk -v cpuNumber=$allCpuNumber -v critial=$CRITICAL ‘BEGIN{printf “%.2f\n”,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
}
Nagios 的详细介绍 :请点这里
Nagios 的下载地址 :请点这里
相关阅读 :
网络监控器 Nagios 全攻略 http://www.linuxidc.com/Linux/2013-07/87067.htm
Nagios 搭建与配置详解 http://www.linuxidc.com/Linux/2013-05/84848.htm
Nginx 环境下构建 Nagios 监控平台 http://www.linuxidc.com/Linux/2011-07/38112.htm
在 RHEL5.3 上配置基本的 Nagios 系统 (使用 Nagios-3.1.2) http://www.linuxidc.com/Linux/2011-07/38129.htm
CentOS 5.5+Nginx+Nagios 监控端和被控端安装配置指南 http://www.linuxidc.com/Linux/2011-09/44018.htm
Ubuntu 13.10 Server 安装 Nagios Core 网络监控运用 http://www.linuxidc.com/Linux/2013-11/93047.htm