以下是LNMP+Nagios Server端的安装脚本,脚本预计需要执行半小时,当脚本执行完毕后你的基于LNMP的Nagios Server端就装好啦j_0058.gif

 

下面是效果图:

wKioL1VqzAXA1DygAASjPqzi-HE335.jpg


wKiom1VqzYzDi8CMAAKVzHThL1E064.jpg


wKioL1VqzyPikzcvAAHe_cjOWwE831.jpg


wKioL1VQBhbzVzRIAAZP-tQBpx4099.jpg


wKiom1VQBJ_AS5g0AANlKtEpfyM878.jpg

 

下面是脚本代码:写的不太好,求大神们指点

#!/bin/bash
#date:2015-5-27
#author:yfshare
#mail:838554604@qq.com
echo ''
echo -e "\t###########################################################"
echo -e "\t###\t\t\t\t\t\t\t###"
echo -e "\t###\twelcome to use Nagios&Nginx Install Scripts\t###"
echo -e "\t###\tRequirement:\t\t\t\t\t###"
echo -e "\t###\tSystem:\t\tCentOS 6.x\t\t\t###"
echo -e "\t###\tprocessor:\tx86_64\t\t\t\t###"
echo -e "\t###\t\t\t\t\t\t\t###"
echo -e "\t###########################################################"
function check_service() {
network=`netstat -tunlp |grep ${1} |wc -l`
Process=`ps -ef|grep ${1}|grep -v grep|wc -l`
while true
do
	if [ ${network} -ge 1 ] || [ ${Process} -ge 1 ];then
		echo -e "${1} started successfully"
		break
	else
		echo -e "${1} failed to start"
		/bin/sh $2
		sleep 2
	fi
done
}
function Content() {
echo ''
read -p 'please input software path:' path
mkdir ${path}/log
log="${path}/log/install.log"
echo ''
echo -e 'Please input the password for user admin:'
yum -y install httpd-tools >>${log} 2>&1
declare -i count=3
while true
do
	htpasswd -cm $path/conf/htpasswd.users admin
	result=`echo $?`
	((--count))
	if [[ $result != 0 ]];then
	        if [[ $count -ge 1 ]];then
        	        echo ''
	        else
			exit 1
        	fi
	else
        	break
	fi
done
cd $path
yum -y install gcc gcc-c++ perl perl-devel mysql mysql-devel libxml2 libxml2-devel unzip >>${log} 2>&1
#install php
tar -zxvf zlib-1.2.7.tar.gz -C $path >>${log} 2>&1
cd $path/`tar -zxvf ${path}/zlib-1.2.7.tar.gz|head -n 1|awk -F/ '{print $1}'`
./configure -prefix=/usr/local/zlib >>${log} 2>&1 &&make >>${log} 2>&1 &&make install >>${log} 2>&1
tar -zxvf ${path}/libmcrypt-2.5.7.tar.gz -C $path >>${log} 2>&1
cd $path/`tar -zxvf ${path}/libmcrypt-2.5.7.tar.gz|head -n 1|awk -F/ '{print $1}'`
./configure >>${log} 2>&1 &&make >>${log} 2>&1 &&make install >>${log} 2>&1
useradd -s /sbin/nologin www >>${log} 2>&1
groupadd nagcmd &&useradd -G nagcmd nagios &&usermod -G nagcmd www
tar -jxvf $path/php-5.3.8.tar.bz2 -C $path >>${log} 2>&1
yum install libpng.x86_64 freetype.x86_64 libjpeg-turbo.x86_64 libjpeg-turbo-devel.x86_64 libpng-devel.x86_64 freetype-devel.x86_64 -y >>${log} 2>&1
cd $path/`tar -jxvf ${path}/php-5.3.8.tar.bz2|head -n 1|awk -F/ '{print $1}'`
./configure --prefix=/usr/local/php --enable-mbstring --enable-soap --enable-wddx=shared --enable-xml --with-gd --with-gettext --with-regex=system --with-zlib-dir=/usr/local/zlib --enable-fpm --with-jpeg-dir --with-fpm-user=www --with-fpm-group=www --enable-shmop --with-mcrypt --with-mysqli=mysqlnd >>${log} 2>&1 &&make >>${log} 2>&1 &&make install >>${log} 2>&1
cp /usr/local/php/etc/php-fpm.conf.default /usr/local/php/etc/php-fpm.conf
sed -i '/run\/php-fpm.pid/s/^;//g' /usr/local/php/etc/php-fpm.conf
sed -i '/^;pm.min_spare_servers/s/^;//g' /usr/local/php/etc/php-fpm.conf
sed -i '/^;pm.max_spare_servers/s/^;//g' /usr/local/php/etc/php-fpm.conf
sed -i '/^;pm.start_servers/s/^;//g' /usr/local/php/etc/php-fpm.conf
/usr/local/php/sbin/php-fpm
echo '/usr/local/php/sbin/php-fpm'>>/etc/rc.local
check_service php "/usr/local/php/sbin/php-fpm"
#install nginx
tar -jxvf ${path}/pcre-8.12.tar.bz2 -C ${path} >>${log} 2>&1
cd ${path}/`tar -jxvf ${path}/pcre-8.12.tar.bz2|head -n 1|awk -F/ '{print $1}'`
./configure >>${log} 2>&1 &&make >>${log} 2>&1 &&make install >>${log} 2>&1
tar -zxvf $path/openssl-1.0.1c.tar.gz -C $path >>${log} 2>&1
cd $path/`tar -zxvf $path/openssl-1.0.1c.tar.gz|head -n 1|awk -F/ '{print $1}'`
./config >>${log} 2>&1 &&make >>${log} 2>&1 &&make install >>${log} 2>&1
tar -zxvf $path/nginx-1.6.2.tar.gz -C $path >>${log} 2>&1
cd $path/`tar -zxvf $path/nginx-1.6.2.tar.gz|head -n 1|awk -F/ '{print $1}'`
./configure --prefix=/usr/local/nginx --user=nginx --group=nginx --with-http_ssl_module --with-pcre=${path}/pcre-8.12 --with-zlib=${path}/zlib-1.2.7 --with-openssl=${path}/openssl-1.0.1c --with-http_stub_status_module >>${log} 2>&1 &&make >>${log} 2>&1 &&make install >>${log} 2>&1
cp $path/conf/etc_init.d_nginx /etc/init.d/nginx >>${log} 2>&1
chown www:www /usr/local/nginx -R
chmod 775 /etc/init.d/nginx &&/etc/init.d/nginx start >>${log} 2>&1
chkconfig --add nginx && chkconfig nginx on
check_service nginx "/etc/init.d/nginx start"
cat $path/conf/nginx.conf >/usr/local/nginx/conf/nginx.conf
cat $path/conf/fastcgi.conf >/usr/local/nginx/conf/fastcgi.conf
cat $path/conf/fcgi.conf >/usr/local/nginx/conf/fcgi.conf
cat $path/conf/start_perl_cgi.sh > /usr/local/nginx/start_perl_cgi.sh
chmod 775 /usr/local/nginx/start_perl_cgi.sh
#install FCGI
tar -zxvf $path/FCGI-0.73.tar.gz -C $path >>${log} 2>&1
cd $path/`tar -zxvf $path/FCGI-0.73.tar.gz|head -n 1|awk -F/ '{print $1}'`
perl Makefile.PL >>${log} 2>&1 &&make >>${log} 2>&1 &&make install >>${log} 2>&1
tar -zxvf $path/FCGI-ProcManager-0.25.tar.gz -C $path >>${log} 2>&1
cd $path/`tar -zxvf $path/FCGI-ProcManager-0.25.tar.gz|head -n 1|awk -F/ '{print $1}'`
perl Makefile.PL >>${log} 2>&1 &&make >>${log} 2>&1 &&make install >>${log} 2>&1
tar -zxvf $path/IO-1.25.tar.gz -C $path >>${log} 2>&1
cd $path/`tar -zxvf $path/IO-1.25.tar.gz|head -n 1|awk -F/ '{print $1}'`
perl Makefile.PL >>${log} 2>&1 &&make >>${log} 2>&1 &&make install >>${log} 2>&1
tar -zxvf $path/IO-All-0.39.tar.gz -C $path >>${log} 2>&1
cd $path/`tar -zxvf $path/IO-All-0.39.tar.gz|head -n 1|awk -F/ '{print $1}'`
perl Makefile.PL >>${log} 2>&1 &&make >>${log} 2>&1 &&make install >>${log} 2>&1
tar -zxvf $path/spawn-fcgi-1.6.3.tar.gz -C $path >>${log} 2>&1
cd $path/`tar -zxvf $path/spawn-fcgi-1.6.3.tar.gz|head -n 1|awk -F/ '{print $1}'`
./configure >>${log} 2>&1 &&make >>${log} 2>&1 &&make install >>${log} 2>&1
cd $path &&unzip $path/perl-fcgi.zip >>${log} 2>&1
cp $path/perl-fcgi.pl /usr/local/nginx/
chmod 755 /usr/local/nginx/perl-fcgi.pl
/usr/local/nginx/start_perl_cgi.sh start
echo "/bin/sh /usr/local/nginx/start_perl_cgi.sh start" >>/etc/rc.local
IP=`ls /usr/local/nginx/logs/|grep "perl-fcgi.sock"|wc -l`
while true
do
	if [ ${IP} -eq 1 ];then
		echo -e 'start perl-fcgi done'
		break
	else
		echo -e 'perl-fcgi failed to start'
		/usr/local/nginx/start_perl_cgi.sh start
	fi
done
#install Nagios
yum -y install php-gd >>${log} 2>&1
tar -zxvf $path/nagios-3.3.1.tar.gz -C $path >>${log} 2>&1
cd $path/`tar -zxvf $path/nagios-3.3.1.tar.gz|head -n 1|awk -F/ '{print $1}'`
./configure --with-command-group=nagcmd --prefix=/usr/local/nagios --with-gd-lib=/usr/lib --with-gd-nc=/usr/include >>${log} 2>&1 &&make all >>${log} 2>&1 &&make install >>${log} 2>&1 &&make install-init >>${log} 2>&1 &&make install-config >>${log} 2>&1 &&make install-commandmode >>${log} 2>&1
sed -i '/authorized\_for\_/s/nagiosadmin/&\,admin/g' /usr/local/nagios/etc/cgi.cfg
mv $path/conf/htpasswd.users /usr/local/nagios/etc/
mkdir /usr/local/nagios/logs -p
sed -i '/use_authentication/s/1/0/g' /usr/local/nagios/etc/cgi.cfg
sed -i '/requiretty$/s/^Defaults/#&/g' /etc/sudoers
chown nagios:nagcmd /usr/local/nagios -R
chkconfig --add nagios &&chkconfig nagios on
/usr/local/nagios/bin/nagios -v /usr/local/nagios/etc/nagios.cfg |grep ^Total
/etc/init.d/nagios start
check_service nagios "/etc/init.d/nagios start"
#install Nagios-plugin
tar -zxvf $path/nagios-plugins-1.4.15.tar.gz -C $path >>${log} 2>&1
cd $path/`tar -zxvf $path/nagios-plugins-1.4.15.tar.gz|head -n 1|awk -F/ '{print $1}'`
./configure --with-nagios-user=nagios --with-nagios-group=nagios --enable-perl-modules >>${log} 2>&1
make >>${log} 2>&1 &&make install >>${log} 2>&1
libexec=`ls /usr/local/nagios/libexec/|wc -l`
echo -e "nagios/libexec:${libexec}"
#install NRPE
yum -y install openssl-devel >>${log} 2>&1
tar -zxvf ${path}/nrpe-2.13.tar.gz -C $path >>${log} 2>&1
cd $path/`tar -zxvf ${path}/nrpe-2.13.tar.gz|head -n 1|awk -F/ '{print $1}'`
./configure >>${log} 2>&1 &&make all >>${log} 2>&1 &&make install-plugin >>${log} 2>&1 &&make install-daemon >>${log} 2>&1 &&make install-daemon-config >>${log} 2>&1
e=`ifconfig eth0|grep "inet addr"|awk '{print $2}'|awk -F: '{print $2}'`
sed -i "/allowed_host/s/127.0.0.1/&,${e}/g" /usr/local/nagios/etc/nrpe.cfg
echo "/usr/local/nagios/bin/nrpe -c /usr/local/nagios/etc/nrpe.cfg -d" >> /etc/rc.local
chown nagios:nagcmd /usr/local/nagios -R
ln -s /usr/local/nagios/share /usr/local/nginx/html/nagios
/usr/local/nagios/libexec/check_nrpe -H localhost >>${log} 2>&1
sed -i '/^:OUTPUT/a\-A INPUT \-m state \-\-state NEW \-m tcp \-p tcp \-\-dport 80 \-j ACCEPT' /etc/sysconfig/iptables
sed -i '/^:OUTPUT/a\-A INPUT \-m state \-\-state NEW \-m tcp \-p tcp \-\-dport 5666 \-j ACCEPT' /etc/sysconfig/iptables
sed -i '/^:OUTPUT/a\-A INPUT \-m state \-\-state NEW \-m tcp \-p tcp \-\-dport 9000 \-j ACCEPT' /etc/sysconfig/iptables
sed -i '/WorldWideWeb/s/http/#&/g' /etc/services
sed -i '/HyperText/s/http/#&/g' /etc/services
sed -i '/cslistener/s/cslistener/#&/g' /etc/services
sed -i "/WorldWideWeb/anginx\t 80\/tcp\nnginx\t80\/udp\nnginx\t80\/sctp" /etc/services
sed -i "9770 a php\-fpm\t9000\/tcp\nphp\-fpm\t9000\/udp" /etc/services
sed -i "/sfm-db-server/anrpe\t5666\/tcp\nnrpe\t5666\/udp" /etc/services
/etc/init.d/iptables restart >>${log} 2>&1 &&chkconfig iptables on
/etc/init.d/nagios restart
echo -e ''
/etc/init.d/nginx reload
echo ''
echo "Script installation log in ${path}/log/install.log"
}
#
ret_code=`curl -I -s --connect-timeout 15 www.baidu.com -w %{http_code}|tail -n 1`
System=`cat /proc/version |grep -io Centos`
ID=`cat /proc/version |awk -F'(' '{print $1}'`
processor=`uname -p`
if [[ ${ret_code} != '200' ]];then
	echo ''
	echo -e "\033[31;49;1m[Error]Network exception, please try again\033[39;49;0m"
	exit 1
else
	if [[ ${System} != 'centos' ]] || [[ ${processor} != 'x86_64' ]];then
echo 'Warn message:'
echo -e "The current operation system:${ID} is not in the system support list\nSystem environment check fails, it may affect the program to run"
read -p "Ignored the warning messages [Y/N]?  " key
		if [[ ${key} != [Yy] ]];then
			echo -e "\033[31;49;1mThanks for using\033[39;49;0m"
			echo ''
		else
			echo ''
			echo -e "\033[31;49;1mStart install...\033[39;49;0m"
			Content
			exit 1
		fi
	else
		echo ''
		echo -e "\033[31;49;1mStart install...\033[39;49;0m"
		Content
		exit 1
	fi
fi