基于nagios网络监控----脚本

一:写bash脚本时候经常用到的颜色代码

创新互联是网站建设技术企业,为成都企业提供专业的成都做网站、网站制作,网站设计,网站制作,网站改版等技术服务。拥有十年丰富建站经验和众多成功案例,为您定制适合企业的网站。十年品质,值得信赖!

COLOR_SUCCESS=$(echo -en "[url=file://\\033[1;32m]\\033[1;32m[/url]" )
COLOR_FAILURE=$(echo -en "[url=file://\\033[1;31m]\\033[1;31m[/url]" )
COLOR_WARNING=$(echo -en "[url=file://\\033[1;33m]\\033[1;33m[/url]")
COLOR_NORMAL=$(echo -en "[url=file://\\033[0;39m]\\033[0;39m[/url]")

二:自动化部署简介

   由于服务器数量的不断增加,面对数以千计的需要做Nagios 监控的客户端,自动化部署就会被提上日程了,自动化部署最简单的是把安装时的命令通过脚本组合在一起,再加上一些成功与否的判断

Nagios server 端Nagios服务器自动安装脚本部署脚本

#!/bin/sh
#====================================
# $Name:         nagios-server-install.sh
# $Revision:     1.0
# $Function:     install the nagios monitor
# $Author:       zangyunchao
# $organization: 洛阳理工&zzu
# $Create Date:  2013.05
#====================================
#颜色设置
COLOR_SUCCESS=$(echo -en "[url=file://\\033[1;32m]\\033[1;32m[/url]" )
COLOR_FAILURE=$(echo -en "[url=file://\\033[1;31m]\\033[1;31m[/url]" )
COLOR_WARNING=$(echo -en "[url=file://\\033[1;33m]\\033[1;33m[/url]")
COLOR_NORMAL=$(echo -en "[url=file://\\033[0;39m]\\033[0;39m[/url]")
yum -y install httpd gcc gcc-c++ glibc glibc-common gd gd-devel
useradd nagios
groupadd nagcmd
usermod -G nagcmd nagios
usermod -G nagcmd apache
cd /usr/local/src
wget http://nchc.dl.sourceforge.net/sourceforge/nagios/nagios-3.0.6.tar.gz
wget http://nchc.dl.sourceforge.net/sourceforge/nagiosplug/nagios-plugins-1.4.13.tar.gz
wget http://prdownloads.sourceforge.net/sourceforge/nagios/nrpe-2.13.tar.gz
tar zxvf nagios-3.0.6.tar.gz
cd nagios-3.0.6
./configure --with-command-group=nagcmd --prefix=/usr/local/nagios
make all
make install
make install-init
make install-config
make install-commandmode
make install-webconf
cd ../
tar zxvf nagios-plugins-1.4.13.tar.gz
cd nagios-plugins-1.4.13
./configure --prefix=/usr/local/nagios --with-nagios-user=nagios --with-nagios-group=nagios
make && make install
cd ../
tar zxvf nrpe-2.13.tar.gz
cd nrpe-2.13
./configure
make all
make install-plugin
make install-daemon
make install-daemon-config
htpasswd -bc /usr/local/nagios/etc/htpasswd.users xiaozang333
xiaozang333
#为nagios登录名和密码要牢记
echo "alias nagioscheck='/usr/local/nagios/bin/nagios -v /usr/local/nagios/etc/nagios.cfg' " >> /root/.bashrc
source /root/.bashrc
chkconfig nagios on
chkconfig httpd on
service httpd start
service nagios start
service sendmail start

 

Nagios Client 端《linux系统》部署脚本

脚本一

 

#!/bin/sh
#====================================
# $Name:         nagios-server-install.sh
# $Revision:     1.0
# $Function:     install the nagios monitor
# $Author:       zangyunchao
# $organization: 洛阳理工&zzu
# $Create Date:  2013.05
#====================================
useradd nagios
cd /usr/local/src
wget http://nchc.dl.sourceforge.net/sourceforge/nagiosplug/nagios-plugins-1.4.13.tar.gz
wget http://prdownloads.sourceforge.net/sourceforge/nagios/nrpe-2.13.tar.gz
tar zxvf nagios-plugins-1.4.13.tar.gz
cd nagios-plugins-1.4.13
./configure
make
make install
chown nagios:nagios /usr/local/nagios
chown -R nagios:nagios /usr/local/nagios/libexec
cd ../
tar zxvf nrpe-2.13.tar.gz
cd nrpe-2.13
./configure
make all
make install-plugin
make install-daemon
make install-daemon-config
sed -i 's@allowed_hosts=127.0.0.1@allowed_hosts=114.112.11.11@' /usr/local/nagios/etc/nrpe.cfg
/usr/local/nagios/bin/nrpe -c /usr/local/nagios/etc/nrpe.cfg -d
echo "/usr/local/nagios/bin/nrpe -c /usr/local/nagios/etc/nrpe.cfg -d" >> /etc/rc.local

nagios服务监控端增加window主机脚本(windows系统)

## script begins here
#! /bin/bash
echo "Enter Host Name"
read name
echo "Enter IP Address of $name"
read ip_add
echo "
define host{
use windows-server //使用的主机组名
host_name $name //主机名
alias $name //主机别名
}

nagios监控系统内存

#!/bin/sh
#====================================
# $Name:         sysmem_check.sh
# $Revision:     1.0
# $Function:     This Nagios plugin can be check linux system memory status
# $Author:       zangyunchao cp from Shundong Zhao
# $organization: UnixHot
# $Create Date:  2010-06-10
#====================================
USAGE_Method="$(basename $0) [-w|--warning]  [-c|--critical] "
USAGE_Value="WARNING value must be large than CRITICAL value: `basename $0` $*"
STATE_OK=0
STATE_WARNING=1
STATE_CRITICAL=2
STATE_UNKNOWN=3
#设置Nagios要求的状态
if [ $# -lt 4 ];then
echo
echo "Usage: $USAGE_Method"
echo
exit 0
fi
while [ $# -gt 0 ];do
case "$1" in
-w|--warning)
shift
WARNING=$1
;;
-c|--critical)
shift
CRITICAL=$1
;;
esac
shift
done
if [[ $WARNING -eq $CRITICAL || $WARNING -lt $CRITICAL ]]
then
echo
echo "$USAGE_Value"
echo
echo "Usage: $USAGE_Method"
echo
exit 0
fi
FREE_MEM=$(free -m | grep - | awk -F ' ' '{print $4}')  #取当前未使用的内存,注意是未使用!
TOTAL_MEM=$(free -m | grep Mem | awk -F ' ' '{print $2}') #取当前系统总内存
PERCENT=$(bc <<< "scale=2;$FREE_MEM/$TOTAL_MEM" | tr '^.' ' ') #使用bc输出百分比
if [ "$PERCENT" -le "$CRITICAL" ] #如果监测百分比小于等于用户设置的严重错误值,返回2
then
echo "CRITICAL - $FREE_MEM MB ($PERCENT%) Free Memory"
exit 2
fi
if [ "$PERCENT" -le  "$WARNING" ]  #如果百分比小于等于用户设置的警告值就警告并返回1
then
echo "WARNING - $FREE_MEM MB ($PERCENT%) Free Memory"
exit 1
fi
if [ "$PERCENT" -gt "$WARNING" ] #如果百分比大于用户设置的警告值就输出OK返回0
then
echo "OK - $FREE_MEM MB ($PERCENT%) Free Memory"
exit 0
fi

 

nagios client 被监控端-----for linux系统

#/bin/bash
clear
echo "========================================================================="
echo "Welcome to UnixHot Linux Monitor"
echo "zangyunchao graduation created"
echo ""
echo "UHLM v1.0 client by UnixHot "
echo "========================================================================="
echo ""
echo "For more information please visit http://www.unixhot.com/"
echo ""
SOFTWARE_PATH=/usr/local/src
PACKAGE1=gcc
PACKAGE2=glibc
PACKAGE3=xinetd
#PACKAGE4=gd
download(){
echo "==================Start download Nagios Client package==========="
cd $SOFTWARE_PATH
wget
http://prdownloads.sourceforge.net/sourceforge/nagiosplug/nagios-plugins-1.4.14.tar.gz
wget http://prdownloads.sourceforge.net/sourceforge/nagios/nrpe-2.12.tar.gz
chmod +x nagios-plugins-1.4.14.tar.gz
chmod +x nrpe-2.12.tar.gz
echo "Download packages finished!"
echo "================================================================="
}
nagios-plugins(){
echo "=======================install Nagios-plugins====================="
cd $SOFTWARE_PATH
useradd -s /sbin/nologin nagios
tar zxvf nagios-plugins-1.4.14.tar.gz
cd nagios-plugins-1.4.14
./configure
make && make install
}
nrpe(){
echo "=======================install nrpe================================"
cd $SOFTWARE_PATH
tar zxvf nrpe-2.12.tar.gz
cd nrpe-2.12
./configure && make all
make install-plugin
make install-daemon
make install-daemon-config
make install-xinetd
}
config(){
echo "=======================Config======================================"
sed -i 's/yes/no/g' /etc/xinetd.d/nrpe
sed -i 's/127.0.0.1/127.0.0.1 '$Nagios_Server'/g' /etc/xinetd.d/nrpe
echo "nrpe 5666/tcp #nrpe" >> /etc/services
chown -R nagios:nagios /usr/local/nagios
/etc/init.d/xinetd restart
sleep 5
netstat -na | grep 5666
echo "************************************************"
echo "If you look the LISTEN 5666 port,Congratulations, Nagios-Client Install are
successful"
echo "************************************************"
}
main(){
rpm -q $PACKAGE1 $PACKAGE2 $PACKAGE3 > /tmp/rpm.log
SOFTWARE_STATUS=`grep 'not installed' /tmp/rpm.log`
if [ -z "$SOFTWARE_STATUS" ];then
echo "Please input the Nagios Server IP Address"
read
echo "The Nagios server ip address is $REPLY"
Nagios_Server=$REPLY
download
nagios-plugins
nrpe
config
else
echo "Please install the following software first!"
echo ""
echo ""
grep 'not installed' /tmp/rpm.log
echo ""
fi
}

 


当前文章:基于nagios网络监控----脚本
文章起源:http://pwwzsj.com/article/gjgpce.html