nagios 被监控端一键配置脚本

nagios

脚本是基于我自己公司的环境来编写的,我们主要用的aws云服务器,如果编译有问题的根据错误自己yum添加相应的缺失的包.
脚本的安装过程没有隐藏,便于发现错误。可以自己把不想看的扔到垃圾桶里 > /dev/null 2>&1

#! /bin/bash
PATH=/bin:/sbin:/usr/bin:/usr/sbin:/usr/local/bin:/usr/local/sbin:~/bin
export PATH

####################################
clear
echo ""
echo "#############################################################"
echo "#          One click Install nagios(被监控端配置)           #"
echo "#############################################################"
echo ""


####################################
function rootness(){
    if [[ $EUID -ne 0 ]]; then
       echo "Error:This script must be run as root!" 1>&2
       exit 1
    fi
}

function gcc_check(){
    rpm -q gcc
    if [[ $? -ne 0 ]]; then
        yum install gcc -y
    fi
    rpm -q openssl-devel
    if [[ $? -ne 0 ]]; then
                yum install openssl-devel -y
        fi
}

function xinetd_check(){
    rpm -q xinetd
    if [[ $? -ne 0 ]]; then
        yum install xinetd -y
    fi
}

function create_user_nagios(){
    echo "Please input password for nagios:"
    read -p "(Default password: nagios):" nagiospwd
    [ -z "$nagiospwd" ] && nagiospwd="nagios"
    echo ""
    echo "---------------------------"
    echo "password = $nagiospwd"
    echo "---------------------------"
    echo ""
    id nagios > /dev/null 2>&1
    if [[ $? -ne 0 ]]; then
        useradd nagios
        echo "$nagiospwd" | passwd --stdin "nagios" > /dev/null 2>&1
    else 
        echo "$nagiospwd" | passwd --stdin "nagios" > /dev/null 2>&1
    fi
}

function nagios_install(){
    cd /opt/
    wget http://nagios-plugins.org/download/nagios-plugins-2.1.1.tar.gz
    tar -vxf nagios-plugins-2.1.1.tar.gz
    cd nagios-plugins-2.1.1
    gcc_check
    ./configure --prefix=/usr/local/nagios
    if [[ $? -eq 0 ]]; then
        make && make install
        if [[ $? -eq 0 ]]; then
            echo "install nagios success."
        else
            echo "install nagios failed! Please check Error."
            exit 1
        fi
    else
        echo "please check the error and retry!"
        exit 1
    fi
    chown nagios.nagios /usr/local/nagios
    chown -R nagios.nagios /usr/local/nagios/libexec    
}

function nrpe_install(){
    cd /opt/
    wget http://prdownloads.sourceforge.net/sourceforge/nagios/nrpe-2.13.tar.gz
    tar -xvf nrpe-2.13.tar.gz
    cd nrpe-2.13
    gcc_check
    ./configure
    if [[ $? -eq 0 ]]; then
        make all
        if [[ $? -eq 0 ]]; then
            echo "install nrpe success."
        else
            echo "install nrpe failed! Please check Error."
            exit 1
        fi
    else
        echo "please check the error and retry!"
        exit 1
    fi
    make install-plugin && make install-daemon && make install-daemon-config 
    xinetd_check
    make install-xinetd
    if [[ $? -eq 0 ]]; then
         sed -i '/\<only_from\>/d' /etc/xinetd.d/nrpe
        echo "nrpe      5666/tcp        #nrpe" >> /etc/services
    fi
    service xinetd restart
    sleep 10
    netstat -an|grep 5666
    if [[ $? -eq 0 ]]; then
        /usr/local/nagios/libexec/check_nrpe -H localhost
        if [[ $? -eq 0 ]]; then
            echo 'install xinetd success. '
        else
            echo "install nrpe failed! Please check Error."
                exit 1  
        fi
    else
        echo 'xinetd configure error,please check'
        exit 1
    fi
}

function nrpe_configure(){
    panfu=$(df -h | sed -n '2p' | awk '{print $1}' | cut -d "/" -f 3)
    cd /usr/local/nagios/etc
    if [[ -f nrpe.cfg ]]; then
        sed -i "s/hda1/$panfu/g" nrpe.cfg
    else 
        echo "nrpe is wrong"
        exit 1
    fi
}

function install(){
    rootness
    create_user_nagios
    nagios_install
    nrpe_install
    nrpe_configure
    echo ""
    echo "Congratulations, nagios install completed!"
    echo -e "Your nagios password: \033[41;37m ${nagiospwd} \033[0m"
    echo -e "please open Port: \033[41;37m 6555 \033[0m"
    echo ""
    exit 0
}

#############################
#  结构留着以后添加卸载部分
#############################
action=$1
[  -z $1 ] && action=install
case "$action" in
install)
    install
    ;;
*)
    echo "Arguments error! [${action} ]"
    echo "Usage: `basename $0` {install}"
    ;;
esac

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值