1.1 版本修改:rpmfind函数、wgetsoft函数和-all选项使用数组
这个脚本是自己的生产服务器上的一个脚本,安装系统都为:CentOS,相关说明如下
1. 使用
- ./nrpe_install.bash --help 或者 ./nrpe_install.bash -h
- 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. 基本环境、安装文件存放路径
- declare -x PATH=usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin
- declare softpath=/usr/local/src
PATH:环境变量声明
softpath:安装软件的存放路径,解压路径也是这
b. 帮助函数:usage
- function usage()
- {
- echo "usage: nrpe_install.bash [--rpmfind|-p] [--wgetsoft|-w] [--adduser|-u] [--nagios-plugin|-p] [ --nrpe|-r] [-all|-a] [--help|-h]"
- }
输出选项和帮助
c. 删除解压软件函数:clear
- function clear()
- {
- if [[ -d $softpath/$software ]]; then
- rm -rf $softpath/$software
- fi
- }
用于安装软件失败时删除相应的解压包
d. 安装依赖的rpm包:rpmfind
- function rpmfind()
- {
- local -a rpmpackages=(wget gcc make openssl openssl-devel perl)
- for rpmpackage in "${rpmpackages[@]}"
- do
- if rpm -qa | grep "^$rpmpackage"; then
- :
- else
- if yum install $rpmpackage -y; then
- :
- else
- return 1
- fi
- fi
- done
- return 0
- }
需要的软件包
wget:下载相关的安装包
gcc、make:编译软件nagios-plugin和nrpe需要
openssl、openssl、perl:库依赖或者环境需要
e. 下载安装包函数:wgetsoft
- function wgetsoft()
- {
- 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/attachment/download/Check-Plugins/Software/check_nginx-sh/check_nginx.sh")
- for i in "${software[@]}"
- do
- declare soft=$(basename $i)
- if [[ -f $softpath/$soft ]];then
- continue
- fi
- if wget $i -P /usr/local/src; then
- :
- else
- return 1
- fi
- done
- return 0
- }
需要的软件有:nagios-plugin、nrpe、check_nginx.sh(用于监控nginx状态)
f. 添加nagios用户函数:adduser
- function adduser()
- {
- if grep '^nagios' /etc/passwd; then
- return 0
- else
- if useradd -r nagios -M -s /bin/bash -d /usr/local/nagios; then
- return 0
- else
- return 1
- fi
- fi
- }
检查是否存在nagios用户,若不存在则添加
g. 安装nagios-plugin函数:nagios-plugin
- function nagios-plugin()
- {
- cd $softpath
- declare software="nagios-plugins-1.4.15"
- if tar zxvf nagios-plugins-1.4.15.tar.gz; then
- if cd nagios-plugins-1.4.15 && ./configure --with-mysql=/usr/local/mysql; then
- if make && make install; then
- :
- else
- clear
- return 1
- fi
- else
- clear
- return 1
- fi
- else
- clear
- return 1
- fi
- if chown -R nagios:nagios /usr/local/nagios; then
- return 0
- else
- return 1
- fi
- }
注意:在编译的时候--with-mysql指定为你的mysql的安装目录
h. 安装nrpe函数
- function nrpe()
- {
- cd $softpath
- declare software="nrpe-2.12"
- if tar zxvf nrpe-2.12.tar.gz; then
- if cd nrpe-2.12 && ./configure; then
- if make all && make install-plugin && make install-daemon && make install-daemon-config; then
- return 0
- else
- clear
- return 1
- fi
- else
- clear
- return 1
- fi
- else
- clear
- return 1
- fi
- }
安装nrpe,如果失败,则删除解压的软件包
i. 设置长选项
- ARGV=($(getopt -o :wuprah -l rpmfind,wgetsoft,adduser,nagios-plugin,nrpe,all,help -- "$@"))
- for((i = 0; i < ${#ARGV[@]}; i++)) {
- eval opt=${ARGV[$i]}
- case $opt in
- --rpmfind|-p)
- if rpmfind; then
- echo "rpmfind success"
- else
- echo "rpmfind failed"
- fi
- ;;
- --wgetsoft|-w)
- if wgetsoft; then
- echo "wgetsoft success"
- else
- echo "wgetsoft failed"
- exit 1
- fi
- ;;
- --adduser|-u)
- if adduser; then
- echo "adduser success"
- else
- echo "adduser failed"
- exit 1
- fi
- ;;
- --nagios-plugin|-p)
- if nagios-plugin; then
- echo "nagios-plugin success"
- else
- echo "nagios-plugin failed"
- exit 1
- fi
- ;;
- --nrpe|-r)
- if nrpe; then
- echo "nrpe success"
- else
- echo "nrpe failed"
- exit 1
- fi
- ;;
- --all|-a)
- declare -a functions=(rpmfind wgetsoft adduser nagios-plugin nrpe)
- for i in "${functions[@]}"
- do
- if $i; then
- echo "$i success"
- else
- echo "$i failed"
- exit 1
- fi
- done
- ;;
- --help|-h)
- usage
- ;;
- --)
- break
- ;;
- esac
- }
设置长选项见:http://linuxjcq.blog.51cto.com/3042600/720996
3. 完整脚本见附件