注意需要自行下载nagios,nagios-plugin,nrpe
脚本只完成nagios,nagios-plugin,apache,nrpe的安装集成。
#!/bin/bash
#########################
#
#install nagios on centos7
#
#########################
#依赖系统库
g_sys_lib=(
gcc glibc glibc-common gcc-c++
gd gd-devel
openssl openssl-devel
unzip bzip2
libxml2 libxml2-devel
)
g_src_dir=`pwd`/src
g_log=`pwd`/install_nagios.log
g_base_dir=/usr/local
g_apr=apr-1.4.5
g_pcre=pcre-8.33
g_apr_util=apr-util-1.3.12
g_httpd=httpd-2.4.28
g_php=php-5.6.25
g_nagios=nagios-4.3.2
g_nplugin=nagios-plugins-2.2.1
g_nrpe=nrpe-2.12
#下载软件
function download_src_code(){
[ ! -d $g_src_dir ] && mkdir -p $g_src_dir
cd $g_src_dir
wget http://archive.apache.org/dist/apr/apr-1.4.5.tar.gz
wget http://archive.apache.org/dist/apr/apr-util-1.3.12.tar.gz
wget http://jaist.dl.sourceforge.net/project/pcre/pcre/8.33/pcre-8.33.tar.gz
wget http://mirror.bit.edu.cn/apache//httpd/httpd-2.4.28.tar.gz
wget http://cn2.php.net/distributions/php-5.6.25.tar.gz
}
#通用安装
function common_install_soft(){
echo ">>>>start install $1"
local soft_name=$1
local configure=$2
cd $g_src_dir && \
tar -xf ${soft_name}.tar.gz && \
cd ${soft_name} && \
./configure $configure && \
make && make install
echo ">>>>end install soft $1 with $?" >>$g_log
}
#安装系统依赖
function install_sys_lib(){
for lib in $g_sys_lib
do
rpm -q $lib
[ $? -ne 0 ] && yum install -y $?
done
echo ">>>>end $FUNCNAME" >> $g_log
}
#添加用户和组
function add_user_group(){
/usr/sbin/groupadd nagios
/usr/sbin/groupadd nagcmd
/usr/sbin/groupadd apache
/usr/sbin/useradd nagios -g nagios -s /sbin/nologin
/usr/sbin/useradd apache -g apache -s /sbin/nologin
/usr/sbin/usermod -a -G nagcmd nagios
/usr/sbin/usermod -a -G nagcmd apache
echo ">>>>end $FUNCNAME" >> $g_log
}
#安装apache
function install_httpd_php(){
echo ">>>>start $FUNCNAME"
yum remove -y apr-util-devel apr apr-util-mysql \
apr-docs apr-devel apr-util apr-util-docs
common_install_soft ${g_apr} "--prefix=$g_base_dir/apr"
common_install_soft ${g_apr_util} "--prefix=$g_base_dir/apr-util \ --with-apr=$g_base_dir/apr/bin/apr-1-config"
common_install_soft ${g_pcre} "--prefix=$g_base_dir/pcre"
common_install_soft ${g_httpd} "--prefix=$g_base_dir/apache2 \
--with-apr=$g_base_dir/apr \
--with-apr-util=$g_base_dir/apr-util \
--with-pcre=$g_base_dir/pcre"
common_install_soft ${g_php} "--prefix=$g_base_dir/php \
--with-apxs2=$g_base_dir/apache2/bin/apxs"
echo ">>>>end $FUNCNAME" >> $g_log
}
#安装nagios
function install_nagios_core(){
echo ">>>>start install nagios core" >>$g_log
cd $g_src_dir && tar -xf $g_nagios.tar.gz && \
cd $g_nagios && \
./configure --with-command-group=nagcmd && \
make all && \
make install && \
make install-init && \
make install-config && \
make install-commandmode && \
/usr/bin/install -c -m 644 $g_src_dir/$g_nagios/sample-config/httpd.conf \
$g_base_dir/apache2/conf/extra/httpd-nagios.conf && \
$g_base_dir/apache2/bin/htpasswd -bc \
$g_base_dir/nagios/etc/htpasswd.users nagiosadmin admin && \
chkconfig --add nagios && \
chkconfig nagios on
echo ">>>>end $FUNCNAME with $?" >> $g_log
}
#安装nagios插件
function install_nagios_plugin(){
common_install_soft ${g_nplugin} "--with-nagios-user=nagios \ --with-nagios-group=nagios"
}
#安装nrpe
function install_nrpe(){
cd $g_src_dir
tar -xf nrpe-2.12.tar.gz
cd nrpe-2.12
./configure
make all
make install-plugin
}
#集成apache和nagios
function init_config(){
local conf=$g_base_dir/apache2/conf/httpd.conf
echo "1.开启apache cgi模块,解决点击nagios出现下载cgi文件问题"
sed -i "s+^#LoadModule cgid_module+LoadModule cgid_module+g" $conf
echo "2.开启apache php模块,解决403禁止访问问题"
sed -i "s/DirectoryIndex index.html/& index.php/g" $conf
sed -i "/application\/x-gzip/aAddType application\/x-httpd-php .php" $conf
echo "3.添加apache nagios配置文件"
echo "Include conf/extra/httpd-nagios.conf" >> $conf
echo "4.设置apache 运行用户和组为apache"
sed -i "s/^User.*$/User apache/g" $conf
sed -i "s/^Group.*$/Group apache/g" $conf
}
function main(){
echo ">>>>start time `date`" >> $g_log
download_src_code
install_sys_lib
add_user_group
install_httpd_php
install_nagios_core
install_nagios_plugin
install_nrpe
init_config
echo ">>>>end time `date`" >> $g_log
}
main