nagios 安装脚本

研究了一段时间nagios,其实安装很简单,但头懒得做,要求脚本啊。。。
#! /usr/bin/env bash

# Author: Lawrency Meng
# Date: 2013-3-1
# Version: v1.0.2
# Description: the nagios install sh

# dirs cont
TOP_DIR=`cd $(dirname "$0") && pwd`

SRC_DIR="${TOP_DIR}/src"
DEPEND="${TOP_DIR}/apt"
PLUGIN_DIR="${TOP_DIR}/plugin"
ETC_DIR="${TOP_DIR}/etc"
NAGIOS_DIR="/usr/local/nagios/"
UNTAR_TMP="/tmp/nagios/"
PLUGIN_DIR="$NAGIOS_DIR/libexec/"

# srcs cont
CORE_SRC="nagios-3.4.4.tar.gz"
NRPE_SRC="nrpe-2.14.tar.gz"
PLUGIN_SRC="nagios-plugins-1.4.16.tar.gz"
NDOUTILS_SRC="ndoutils-1.5.2.tar.gz"

# check the depends in apt file
function check_depends(){
	#apt-get update
        while read -r line
        do
            dpkg -l $line > /dev/null || apt-get install -y $line
        done < $DEPEND
}

# check the use input
function check_input(){
        if [ "x${CHOOSE}" == "x" ]; then
                return "ok"
        fi
        if [[ "${CHOOSE}" =~ (c|C|n|N|p|P|d|D) ]]; then
                return "err"
        else
                return "err"
        fi
}

# check the untar tmp dir
function check_dir(){
        if [ ! -z ${UNTAR_TMP} ]; then
                sudo mkdir -p ${UNTAR_TMP}
                echo "[Info] Create the untar temp dir ${UNTAR_TMP}"
        fi
}


# create users and groups 
function create_UG(){
        if [ ! `getent passwd nagios` > /dev/null ]; then
                echo "[Info] Creating a user called nagios..."
                useradd -U -G sudo -s /bin/bash -m nagios 
                groupadd nagcmd
                usermod -a -G nagcmd www-data
                get_sudo
		cp -r $TOP_DIR /home/nagios/
		chown -R nagios:nagios /home/nagios/
        else
                echo "[Info] user nagios is exist..."
        fi
}

# give sudo priv
function get_sudo(){
        echo "[Info] Giving nagios user passwordless sudo priviledges"
        grep -q "^#includedir.*/etc/sudoers.d" /etc/sudoers ||
                echo "#includedir /etc/sudoers.d" >> /etc/sudoers
            ( umask 226 && echo "nagios ALL=(ALL) NOPASSWD:ALL" \
                     > /etc/sudoers.d/50_nagios_sh )
}

#  switch user
function switch_user(){
	chown -R nagios:nagios /usr/local/nagios/
}

# clean install files
function clean(){
	if [ $1 = 'PLUGIN' ]; then
		cd ${UNTAR_TMP}/${PLUGIN_SRC/.tar.gz/}
		make clean
	fi
	
	if [ $1 = 'NRPE' ]; then
		cd ${UNTAR_TMP}/${NRPE_SRC/.tar.gz/}		
		make clean
		[ `iptables --list | grep 'nrpe' > /dev/null` ] && sudo iptables -D INPUT -p tcp -m tcp --dport 5666 -j ACCEPT
		[ `grep -i 'nrpe' /etc/services > /dev/null` ] && sed -i "/nrpe/ d" /etc/services
        	[ `grep -i "iptables-restore" /etc/network/interfaces > /dev/null` ] && sed -i "/iptables-restore/ d" /etc/network/interfaces	
		
	fi
	if [ $1 = 'CORE' ]; then
		cd ${UNTAR_TMP}/${CORE_SRC%%-*}
		make clean
	fi
}

# core install func
function core_install(){
        switch_user nagios
        cd ${SRC_DIR}
        check_dir
        sudo tar -zxvf ${CORE_SRC} -C ${UNTAR_TMP}  && cd ${UNTAR_TMP}/${CORE_SRC%%-*}
        ./configure --with-command-group=nagcmd
	make clean
        make all
        make install && make install-init && make install-config && make install-commandmode
        echo "[Info] Install nagios core successfully..."
        switch_user root
}

function nrpe_install(){
        switch_user nagios
	`clean "NRPE"` 
        cd ${SRC_DIR}
        check_dir
        sudo tar -xzvf ${NRPE_SRC} -C ${UNTAR_TMP} && cd ${UNTAR_TMP}/${NRPE_SRC/.tar.gz/}
        ./configure --with-ssl=/usr/lib/openssl --with-ssl-lib=/usr/lib/x86_64-linux-gnu/ --enable-command-args
        make all
        make install-plugin && make install-daemon && make install-daemon-config && make install-xinetd
        cp -r ${ETC_DIR} ${NAGIOS_DIR}/etc/
        sed -i "s/dont_blame_nrpe=0/dont_blame_nrpe=1/g" /usr/local/nagios/etc/nrpe.cfg
	[ ! `grep -i 'nrpe' /etc/services > /dev/null` ] && echo -e "nrpe\t5666/tcp\t#NRPE" >> /etc/services
	service xinetd restart
	[ ! `iptables --list | grep 'nrpe' > /dev/null` ] && sudo iptables -A INPUT -p tcp -m tcp --dport 5666 -j ACCEPT
	iptables-save
	[ ! `grep -i "iptables-restore" /etc/network/interfaces > /dev/null` ] && sed -i "/iface eth0/a\\pre-up iptables-restore < /etc/iptables.up.rules" /etc/network/interfaces 
        [ ! "`netstat -at | grep nrpe`" ] && echo "[info] Install nagios nrpe successfully..."
        switch_user root
}

function plugin_install(){
        switch_user
	`clean "PLUGIN"`
        cd ${SRC_DIR}
        check_dir
        tar -xzvf ${PLUGIN_SRC} -C ${UNTAR_TMP}  && cd ${UNTAR_TMP}/${PLUGIN_SRC/.tar.gz/}
        ./configure --with-nagios-user=nagios --with-nagios-group=nagios
        make && make install
	cp ${PLUGIN_DIR}/* /usr/local/nagios/libexec/ 
        [ -d "/usr/local/nagios/libexec" ] && echo "[Info] Install nagios plugin successfully..."
        echo "[Info] Nagios plugin default install in dir: /usr/local/nagios/libexec/"
        switch_user 
}

function main(){
        
        check_depends
        create_UG
        while true
        do
                echo "[Info] Please choose to install nagios modules: "
                echo "[c] Nagios core" 
                echo "[n] Nagios NRPE"
                echo "[p] Nagios plugin"
#                echo "[d] Nagios NDOUtils"
                echo "[e] exit..."
		
                read -p ">>>>" CHOOSE
		
                case "${CHOOSE}" in
                        [cC]) core_install ;;
                        [nN]) nrpe_install ;;
                        [pP]) plugin_install ;;
#                        [dD]) ndoutil_install ;;
                        [eE]) exit 0;;
                        *) echo "[Err] only accept input [ cC | nN | pP | dD | eE ]" ;;
                esac
        done
}

main $@

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值