一:写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-develuseradd nagiosgroupadd nagcmdusermod -G nagcmd nagiosusermod -G nagcmd apachecd /usr/local/srcwget http://nchc.dl.sourceforge.net/sourceforge/nagios/nagios-3.0.6.tar.gzwget http://nchc.dl.sourceforge.net/sourceforge/nagiosplug/nagios-plugins-1.4.13.tar.gzwget http://prdownloads.sourceforge.net/sourceforge/nagios/nrpe-2.13.tar.gztar zxvf nagios-3.0.6.tar.gzcd nagios-3.0.6./configure --with-command-group=nagcmd --prefix=/usr/local/nagiosmake allmake installmake install-initmake install-configmake install-commandmodemake install-webconfcd ../tar zxvf nagios-plugins-1.4.13.tar.gzcd nagios-plugins-1.4.13./configure --prefix=/usr/local/nagios --with-nagios-user=nagios --with-nagios-group=nagiosmake && make installcd ../tar zxvf nrpe-2.13.tar.gzcd nrpe-2.13./configuremake allmake install-pluginmake install-daemonmake install-daemon-confightpasswd -bc /usr/local/nagios/etc/htpasswd.users xiaozang333xiaozang333#为nagios登录名和密码要牢记echo "alias nagioscheck='/usr/local/nagios/bin/nagios -v /usr/local/nagios/etc/nagios.cfg' " >> /root/.bashrcsource /root/.bashrcchkconfig nagios onchkconfig httpd onservice httpd startservice nagios startservice 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 nagioscd /usr/local/srcwget http://nchc.dl.sourceforge.net/sourceforge/nagiosplug/nagios-plugins-1.4.13.tar.gzwget http://prdownloads.sourceforge.net/sourceforge/nagios/nrpe-2.13.tar.gztar zxvf nagios-plugins-1.4.13.tar.gzcd nagios-plugins-1.4.13./configuremakemake installchown nagios:nagios /usr/local/nagioschown -R nagios:nagios /usr/local/nagios/libexeccd ../tar zxvf nrpe-2.13.tar.gzcd nrpe-2.13./configuremake allmake install-pluginmake install-daemonmake install-daemon-configsed -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 -decho "/usr/local/nagios/bin/nrpe -c /usr/local/nagios/etc/nrpe.cfg -d" >> /etc/rc.local
nagios服务监控端增加window主机脚本(windows系统)
## script begins here#! /bin/bashecho "Enter Host Name"read nameecho "Enter IP Address of $name"read ip_addecho "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=0STATE_WARNING=1STATE_CRITICAL=2STATE_UNKNOWN=3#设置Nagios要求的状态if [ $# -lt 4 ];thenechoecho "Usage: $USAGE_Method"echoexit 0fiwhile [ $# -gt 0 ];docase "$1" in-w|--warning)shiftWARNING=$1;;-c|--critical)shiftCRITICAL=$1;;esacshiftdoneif [[ $WARNING -eq $CRITICAL || $WARNING -lt $CRITICAL ]]thenechoecho "$USAGE_Value"echoecho "Usage: $USAGE_Method"echoexit 0fiFREE_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" ] #如果监测百分比小于等于用户设置的严重错误值,返回2thenecho "CRITICAL - $FREE_MEM MB ($PERCENT%) Free Memory"exit 2fiif [ "$PERCENT" -le "$WARNING" ] #如果百分比小于等于用户设置的警告值就警告并返回1thenecho "WARNING - $FREE_MEM MB ($PERCENT%) Free Memory"exit 1fiif [ "$PERCENT" -gt "$WARNING" ] #如果百分比大于用户设置的警告值就输出OK返回0thenecho "OK - $FREE_MEM MB ($PERCENT%) Free Memory"exit 0fi
nagios client 被监控端-----for linux系统
#/bin/bashclearecho "========================================================================="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/srcPACKAGE1=gccPACKAGE2=glibcPACKAGE3=xinetd#PACKAGE4=gddownload(){echo "==================Start download Nagios Client package==========="cd $SOFTWARE_PATHwgethttp://prdownloads.sourceforge.net/sourceforge/nagiosplug/nagios-plugins-1.4.14.tar.gzwget http://prdownloads.sourceforge.net/sourceforge/nagios/nrpe-2.12.tar.gzchmod +x nagios-plugins-1.4.14.tar.gzchmod +x nrpe-2.12.tar.gzecho "Download packages finished!"echo "================================================================="}nagios-plugins(){echo "=======================install Nagios-plugins====================="cd $SOFTWARE_PATHuseradd -s /sbin/nologin nagiostar zxvf nagios-plugins-1.4.14.tar.gzcd nagios-plugins-1.4.14./configuremake && make install}nrpe(){echo "=======================install nrpe================================"cd $SOFTWARE_PATHtar zxvf nrpe-2.12.tar.gzcd nrpe-2.12./configure && make allmake install-pluginmake install-daemonmake install-daemon-configmake install-xinetd}config(){echo "=======================Config======================================"sed -i 's/yes/no/g' /etc/xinetd.d/nrpesed -i 's/127.0.0.1/127.0.0.1 '$Nagios_Server'/g' /etc/xinetd.d/nrpeecho "nrpe 5666/tcp #nrpe" >> /etc/serviceschown -R nagios:nagios /usr/local/nagios/etc/init.d/xinetd restartsleep 5netstat -na | grep 5666echo "************************************************"echo "If you look the LISTEN 5666 port,Congratulations, Nagios-Client Install aresuccessful"echo "************************************************"}main(){rpm -q $PACKAGE1 $PACKAGE2 $PACKAGE3 > /tmp/rpm.logSOFTWARE_STATUS=`grep 'not installed' /tmp/rpm.log`if [ -z "$SOFTWARE_STATUS" ];thenecho "Please input the Nagios Server IP Address"readecho "The Nagios server ip address is $REPLY"Nagios_Server=$REPLYdownloadnagios-pluginsnrpeconfigelseecho "Please install the following software first!"echo ""echo ""grep 'not installed' /tmp/rpm.logecho ""fi}