nagios自动安装脚本
#!/bin/bash
##nagios监控软件一件使用
##date : 2016-08-23
##做者:liuwenzhi
#定义变量
YUMNUM=`yum repolist 2>/dev/null|grep repolist: |sed 's/[^0-9]//g'`
NAGIOSzip=nagios.zip
NAGIOS=nagios
#定义yum源是否可用脚本
YUMREPO (){
echo -ne "3[34m正在检测yum源3[0m"
sleep 3
if [ $YUMNUM -eq 0 ];then
echo -e "3[32myum源不可用,请先配置yum源3[0m"
exit 10
else
echo -e "3[34myum源检测经过!3[0m"
fi
}
#定义菜单
menu (){
echo " ##############----一键安装菜单----##############"
echo "# 1. 安装nagios 监控端"
echo "# 2. 安装远程被监控端"
echo "# 3. 退出 "
read -p "请输入菜单【1-3】" select
}
#安装nagios依赖环境
yilai_install (){
echo "----------正在安装依赖包"
case $select in
1)
yum install -y httpd php gcc glibc unzip zip
glibc-common gd gd-devel libpng libjpeg zlib openssl-devel
&>/dev/null
;;
2)
yum install -y gcc openssl-devel &>/dev/null
;;
esac
echo "----------依赖安装完成"
}
#定义configure时是否出错
configure_err(){
if [ $? -ne 0 ];then
echo "cofigure失败"
exit 11
fi
}
#定义make时是否出错
make_err(){
if [ $? -ne 0 ];then
echo "make失败"
exit 12
fi
}
#定义make install 安装时是否出错
make_install_err(){
if [ $? -ne 0 ];then
echo "make install失败"
exit 13
fi
}
#修改httpd文件支持php
xiugai_httpd (){
sed -i '/^DirectoryIndex/s/$/& index.php/g' /etc/httpd/conf/httpd.conf
service httpd start &>/dev/null
chkconfig httpd on
}
#nagios安装
nagios_install (){
echo "----------安装nagios"
groupadd nagcmd &>/dev/null
useradd -s /sbin/nologin nagios &>/dev/null
usermod -G nagcmd apache
if [ -f $NAGIOS ];then
rm -rf $NAGIOS
fi
unzip $NAGIOSzip &>/dev/null
cd $NAGIOS
tar -zxf nagios-3.2.1.tar.gz -C /usr/src &>/dev/null
cd /usr/src/nagios-3.2.1/
echo "----------编译中"
./configure --with-command-group=nagcmd &>/dev/null
configure_err
make all &>/dev/null
make_err
make install &>/dev/null
make_install_err
make install-init &>/dev/null
make install-config &>/dev/null
make install-commandmode &>/dev/null
make install-webconf &>/dev/null
}
#nagios-plugins安装
plugins(){
cd - &>/dev/null
tar -zxf nagios-plugins-1.4.14.tar.gz
cd nagios-plugins-1.4.14
./configure --with-nagios-user=nagios \
--with-nagios-group=nagcmd &>/dev/null
configure_err
make &>/dev/null
make_err
make install &>/dev/null
make_install_err
#监控脚本资源在ls /usr/local/nagios/libexec/ 下
}
#安装nrpe
nrpe_install (){
cd - &>/dev/null
tar -zxf nrpe-2.12.tar.gz
cd nrpe-2.12
./configure &>/dev/null
configure_err
make &>/dev/null
make_err
make install &>/dev/null
make_install_err
make install-plugin &>/dev/null
sed -i '$a\define command {' /usr/local/nagios/etc/objects/commands.cfg
sed -i '$a\ command_name check_nrpe' /usr/local/nagios/etc/objects/commands.cfg
sed -i '$a\ command_line $USER1$/check_nrpe -H
$HOSTADDRESS$ -c $ARG1$ ' /usr/local/nagios/etc/objects/commands.cfg
sed -i '$a\ }' /usr/local/nagios/etc/objects/commands.cfg
#cd /usr/local/nagios/etc/objects/
#read -p "请输入须要监控的用户名" u
#cp localhost.cfg $u.cfg
#sed -i '/host_name/s/localhost/$u/g' /usr/local/nagios/etc/objects/localhost.cfg
#sed -i '$a\cfg_file=/usr/local/nagios/etc/objects/$u.cfg
}
#修改配置文件
abc (){
[ -f /usr/local/nagios/etc/objects/commands.cfg ] || \
cp /usr/src/nagios-3.2.1/sample-config/template-object/commands.cfg /usr/local/nagios/etc/objects/
[ -f /usr/local/nagios/etc/objects/localhost.cfg ] || \
cp /usr/src/nagios-3.2.1/sample-config/template-object/localhost.cfg /usr/local/nagios/etc/objects/
[ -f /usr/local/nagios/etc/nagios.cfg ] || \
cp /usr/src/nagios-3.2.1/sample-config/nagios.cfg /usr/local/nagios/etc/
cp /usr/src/nagios-3.2.1/sample-config/httpd.conf /etc/httpd/conf.d/nagios.conf
echo "请设置页面登入密码,按回车设置"
read
htpasswd -c /usr/local/nagios/etc/htpasswd.users nagiosadmin
/etc/init.d/nagios start &>/dev/null
service httpd restart &>/dev/null
}
#安装被监控端主机
ggg (){
echo "----------安装中"
useradd nagios &>/dev/null
groupadd nagcmd &>/dev/null
usermod -G nagcmd nagios &>/dev/null
unzip $NAGIOSzip &>/dev/dull
cd $NAGIOS
tar -zxf nagios-plugins-1.4.14.tar.gz
cd nagios-plugins-1.4.14
./configure &>/dev/null
configure_err
make &>/dev/null
make_err
make install &>/dev/null
make_install_err
cd ..
tar -zxf nrpe-2.12.tar.gz
cd nrpe-2.12
./configure &>/dev/null
configure_err
make &>/dev/null
make_err
make install &>/dev/null
make_install_err
make install-plugin &>/dev/null
make install-daemon &>/dev/null
make install-daemon-config &>/dev/null
make install-xinetd &>/dev/null
yum install -y telnet-server &>/dev/null
read -p "-----请输入监控端的ip----- : " ip
sed -i '/only_from/s/$/& "$ip"/' /etc/xinetd.d/nrpe
grep -q nrpe /etc/services || \
sed -i '$a\nrpe 5666/tcp ' /etc/services
/etc/init.d/xinetd restart &>/dev/null
}
while :
do
clear
menu
case $select in
1)
YUMREPO
yilai_install
xiugai_httpd
nagios_install
plugins
abc
nrpe_install
echo -e "3[32mOK,按回车继续3[0m"
read
;;
2)
ggg
echo -e "3[32mOK,按回车继续3[0m"
read
;;
3)
exit 0
;;
*)
echo "输入有误"
;;
esac
done
1.此脚本在多台机器上测试成功。
2.nagios版本是nagios-3.2.1版本跑在httpd上
3.访问时:
输入: ip/nagios
4.页面用httpd的密码工具生成用户密码,用户是nagiosadmin(脚本里设置的,可本身改)。
5.弹出本身输入密码
6.脚本可能还有瑕疵,不知道依赖包考虑的是否是 彻底.
7. naios.zip文件10M ,传不上来。上传到百度云里。有兴趣的能够下载,并在本身虚拟机上运行一次看下效果
下载地址:http://pan.baidu.com/s/1slJ5GIT 密码nexl8.脚本运行使用
当须要远程监控一台机器时,先在被监控端运行2,而后再监控端运行3.
这是运行1大概 的过程------------
##############----一键安装菜单----##############
# 1. 安装nagios 监控端
# 2. 安装远程被监控端
#3.监控端修改被监控端文件
#4. 退出
请输入菜单【1-4】1
正在检测yum源yum源检测经过!
----------正在安装依赖包
----------依赖安装完成
----------安装nagios
----------编译中
请设置页面登入密码,按回车设置
New password:
Re-type new password:
Adding password for user nagiosadmin
OK,按回车继续