研究了一段时间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 $@