1.1 版本修改:rpmfind函数、wgetsoft函数和-all选项使用数组   

这个脚本是自己的生产服务器上的一个脚本,安装系统都为:CentOS,相关说明如下

1. 使用

 
  
  1. ./nrpe_install.bash --help 或者 ./nrpe_install.bash -h
  2. usage: nrpe_install.bash [--rpmfind|-p] [--wgetsoft|-w] [--adduser|-u] [--nagios-plugin|-p] [ --nrpe|-r] [-all|-a] [--help|-h]

选项说明:

--rpmfind|-p:检查所依赖的rpm包是否安装,如果没有安装,则自动安装

--wgetsoft|-w:下载安装nrpe所需的相关软件,如果文件已经存在,则跳过

--adduser|-u:添加nagios用户

--nagios-plugin|-p:安装nagios-plugin

--nrep|-r:安装nrpe

--all|-a:执行以上所有步骤

--help|-h:获取帮助

2. 脚本说明(各个函数说明)

a. 基本环境、安装文件存放路径

 
  
  1. declare -x PATH=usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin  
  2. declare softpath=/usr/local/src 

PATH:环境变量声明

softpath:安装软件的存放路径,解压路径也是这

b. 帮助函数:usage

 
  
  1. function usage()  
  2. {  
  3.     echo "usage: nrpe_install.bash [--rpmfind|-p] [--wgetsoft|-w] [--adduser|-u] [--nagios-plugin|-p] [ --nrpe|-r] [-all|-a] [--help|-h]"  

输出选项和帮助

c. 删除解压软件函数:clear

 
  
  1. function clear()  
  2. {  
  3.     if [[ -d $softpath/$software ]]; then  
  4.      rm -rf $softpath/$software  
  5.     fi  

用于安装软件失败时删除相应的解压包

d. 安装依赖的rpm包:rpmfind

 
  
  1. function rpmfind()  
  2. {  
  3.     local -a rpmpackages=(wget gcc make openssl openssl-devel perl)  
  4.     for rpmpackage in "${rpmpackages[@]}"  
  5.     do    
  6.         if rpm -qa | grep "^$rpmpackage"; then  
  7.             :  
  8.         else  
  9.             if  yum install $rpmpackage -y; then  
  10.                 :  
  11.             else  
  12.                 return 1  
  13.             fi    
  14.         fi    
  15.     done  
  16.  
  17.     return 0  

需要的软件包

wget:下载相关的安装包

gcc、make:编译软件nagios-plugin和nrpe需要

openssl、openssl、perl:库依赖或者环境需要

e. 下载安装包函数:wgetsoft

 
  
  1. function wgetsoft()  
  2. {  
  3. local -a software=("http://nchc.dl.sourceforge.net/project/nagiosplug/nagiosplug/1.4.15/nagios-plugins-1.4.15.tar.gz" "http://nchc.dl.sourceforge.net/project/nagios/nrpe-2.x/nrpe-2.12/nrpe-2.12.tar.gz" "http://www.monitoringexchange.org/p_w_upload/download/Check-Plugins/Software/check_nginx-sh/check_nginx.sh")  
  4.  
  5. for i in "${software[@]}"  
  6. do  
  7.     declare soft=$(basename $i)  
  8.     if [[ -f $softpath/$soft ]];then  
  9.         continue  
  10.     fi  
  11.     if wget $i -P /usr/local/src; then  
  12.         :  
  13.     else  
  14.         return 1  
  15.     fi  
  16. done  
  17.  
  18. return 0  

需要的软件有:nagios-plugin、nrpe、check_nginx.sh(用于监控nginx状态)

f. 添加nagios用户函数:adduser

 
  
  1. function adduser()
  2. {
  3. if grep '^nagios' /etc/passwd; then
  4. return 0
  5. else
  6. if useradd -r nagios -M -s /bin/bash -d /usr/local/nagios; then
  7. return 0
  8. else
  9. return 1
  10. fi
  11. fi
  12. }

检查是否存在nagios用户,若不存在则添加

g. 安装nagios-plugin函数:nagios-plugin

 
  
  1. function nagios-plugin()  
  2. {  
  3. cd $softpath  
  4. declare software="nagios-plugins-1.4.15" 
  5. if tar zxvf nagios-plugins-1.4.15.tar.gz; then  
  6.     if cd nagios-plugins-1.4.15 && ./configure --with-mysql=/usr/local/mysql; then  
  7.         if make && make install; then  
  8.             :  
  9.         else  
  10.             clear  
  11.             return 1  
  12.         fi  
  13.     else  
  14.         clear  
  15.         return 1  
  16.     fi  
  17. else  
  18.     clear  
  19.     return 1  
  20. fi  
  21.  
  22. if chown -R nagios:nagios /usr/local/nagios; then  
  23.     return 0  
  24. else  
  25.     return 1  
  26. fi  

注意:在编译的时候--with-mysql指定为你的mysql的安装目录

h. 安装nrpe函数

 
  
  1. function nrpe()  
  2. {  
  3. cd $softpath  
  4. declare software="nrpe-2.12" 
  5. if tar zxvf nrpe-2.12.tar.gz; then  
  6.     if cd nrpe-2.12 && ./configure; then  
  7.         if make all && make install-plugin && make install-daemon && make install-daemon-config; then  
  8.             return 0  
  9.         else  
  10.             clear  
  11.             return 1  
  12.         fi  
  13.     else  
  14.         clear  
  15.         return 1  
  16.     fi  
  17. else  
  18.     clear  
  19.     return 1  
  20. fi  

安装nrpe,如果失败,则删除解压的软件包

i. 设置长选项

 
  
  1. ARGV=($(getopt -o :wuprah -l rpmfind,wgetsoft,adduser,nagios-plugin,nrpe,all,help -- "$@"))  
  2. for((i = 0; i < ${#ARGV[@]}; i++)) {  
  3.     eval opt=${ARGV[$i]}  
  4.     case $opt in  
  5.     --rpmfind|-p)  
  6.         if rpmfind; then  
  7.             echo "rpmfind success"  
  8.         else  
  9.             echo "rpmfind failed"  
  10.         fi  
  11.         ;;  
  12.     --wgetsoft|-w)  
  13.         if wgetsoft; then  
  14.             echo "wgetsoft success"  
  15.         else  
  16.             echo "wgetsoft failed"  
  17.             exit 1  
  18.         fi  
  19.         ;;  
  20.     --adduser|-u)  
  21.         if adduser; then  
  22.             echo "adduser success"  
  23.         else  
  24.             echo "adduser failed"  
  25.             exit 1  
  26.         fi  
  27.         ;;  
  28.     --nagios-plugin|-p)  
  29.         if nagios-plugin; then  
  30.             echo "nagios-plugin success"  
  31.         else  
  32.             echo "nagios-plugin failed"  
  33.             exit 1  
  34.         fi  
  35.         ;;  
  36.     --nrpe|-r)  
  37.         if nrpe; then  
  38.             echo "nrpe success"  
  39.         else  
  40.             echo "nrpe failed"  
  41.             exit 1  
  42.         fi  
  43.         ;;  
  44.     --all|-a)  
  45.         declare -a functions=(rpmfind wgetsoft adduser nagios-plugin nrpe)  
  46.         for i in "${functions[@]}"  
  47.         do  
  48.             if $i; then  
  49.                 echo "$i success"  
  50.             else  
  51.                 echo "$i failed"  
  52.                 exit 1  
  53.             fi  
  54.         done  
  55.         ;;  
  56.     --help|-h)  
  57.         usage  
  58.         ;;  
  59.     --)  
  60.         break  
  61.         ;;  
  62.     esac  

设置长选项见:http://linuxjcq.blog.51cto.com/3042600/720996

3. 完整脚本见附件