1 #!/bin/sh 2 #********************************************************** 3 #*Author: abner 4 #*Email: 517465591@qq.com 5 #*Blog http://www.cnblogs.com/lord-dang/ 6 #*Last modified: 2016-09-19 18:28 7 #*Filename: install_nagios_server.sh 8 #*Description: auto install nagios server 9 #*Version: v1.0 10 #********************************************************* 11 . /etc/init.d/functions 12 NAGIOS_TOOLS_DIR=/tmp/nagios 13 admin='abner' 14 PASSWD='123456' 15 #------ step 1: config yum ------ 16 cd /etc/yum.repos.d/ 17 wget -O /etc/yum.repos.d/CentOS-Base.repo http://mirrors.aliyun.com/repo/Centos-6.repo >/dev/null 2>&1 18 echo "[1/10]:config yum OK" 19 20 #------ step 2: config profile------ 21 echo 'export LC_ALL=C'>> /etc/profile 22 source /etc/profile 23 echo "[2/10]:config profile OK" 24 #------ step 3: stop iptables and selinux------ 25 /etc/init.d/iptables stop>/dev/null 2>&1 26 chkconfig iptables off 27 setenforce 0 28 if [ -f /etc/selinux/config ]; then 29 sed -i 's#SELINUX=enforcing#SELINUX=disabled#g' /etc/selinux/config 30 setenforce 0 31 fi 32 echo "[3/10]:Stop iptables and selinux OK" 33 34 #------ step 4: config time sync------ 35 /usr/sbin/ntpdate pool.ntp.org 36 echo '#time sync at 2016/9/25 by abner'>>/var/spool/cron/root 37 echo '*/10 * * * * /usr/sbin/ntpdate pool.ntp.org >/dev/null 2>&1'>>/var/spool/cron/root >/dev/null 2>&1 38 echo "[4/10]:Config time sync OK" 39 40 #------ step 5: install gcc and lamp env etc------ 41 yum install gcc glibc glibc-common openssl-devel -y >/dev/null 2>&1 42 yum install gd gd-devel -y >/dev/null 2>&1 43 yum install httpd php php-gd -y >/dev/null 2>&1 44 yum install mysql* -y >/dev/null 2>&1 45 echo "[5/10]:Install gcc and lamp env etc OK" 46 #------ step 6: add nagios user and group------ 47 /usr/sbin/useradd -m nagios -s /sbin/nologin 48 /usr/sbin/groupadd nagcmd 49 /usr/sbin/usermod -a -G nagcmd nagios 50 /usr/sbin/usermod -a -G nagcmd apache 51 echo "[6/10]:Add nagios user and group OK" 52 53 #------ step 7: download and install nagios------ 54 cd $NAGIOS_TOOLS_DIR 55 [ ! -f nagios_package.zip ] && { 56 echo "Not found nagios_package.zip ,please tansfer it in $NAGIOS_TOOLS_DIR" 57 exit 1 58 } 59 unzip nagios_package.zip >/dev/null 2>&1 60 tar -xf nagios-3.5.1.tar.gz 61 cd nagios 62 ./configure --with-command-group=nagcmd >/dev/null 2>&1 63 make all >/dev/null 2>&1 64 make install >/dev/null 2>&1 65 make install-init >/dev/null 2>&1 66 make install-config >/dev/null 2>&1 67 make install-commandmode >/dev/null 2>&1 68 make install-webconf >/dev/null 2>&1 69 [ $? -eq 0 ]&&{ 70 echo "[7/10]:Install nagios OK" 71 }||{ 72 echo "[7/10]:Install nagios Filed"&&exit 1 73 } 74 cd ../ 75 #------ step 8: config web auth------" 76 htpasswd -cb /usr/local/nagios/etc/htpasswd.users $admin $PASSWD >/dev/null 2>&1 77 echo "[8/10]:Config web auth OK" 78 #------ step 9: install nagios-plugins------" 79 yum -y install perl-devel >/dev/null 2>&1 80 tar -xf nagios-plugins-1.4.16.tar.gz 81 cd nagios-plugins-1.4.16 82 ./configure --with-nagios-user=nagios --with-nagios-group=nagios --enable-perl-modules >/dev/null 2>&1 83 `make >/dev/null 2>&1` && `make install >/dev/null 2>&1` 84 [ $? -eq 0 ]&&{ 85 echo "[9/10]:Install nagios-plugins OK" 86 }||{ 87 echo "[9/10]:Install nagios-plugins Filed"&&exit 1 88 } 89 cd ../ 90 91 #------ step 10: install nrpe------" 92 tar -xf nrpe-2.12.tar.gz 93 cd nrpe-2.12 94 ./configure >/dev/null 2>&1 95 make all >/dev/null 2>&1 96 make install-plugin >/dev/null 2>&1 97 make install-daemon >/dev/null 2>&1 98 make install-daemon-config >/dev/null 2>&1 99 [ $? -eq 0 ]&&{ 100 echo "[10/10]:Install nrpe OK" 101 }||{ 102 echo "[10/10]:Install nrpe Filed"&&exit 1 103 } 104 cd .. 105 106 107 #------ step 11: startup service and check------" 108 /etc/init.d/nagios start >/dev/null 2>&1 109 /etc/init.d/httpd start >/dev/null 2>&1 110 rm -fr $NAGIOS_TOOLS_DIR/* 111 httpstat=`lsof -i :80|wc -l` 112 nagiosinfo=`ps -ef|grep nagios|wc -l` 113 [ $httpstat -ne 0 -a $nagiosinfo -ne 0 ]&&action "Install over and service startup" /bin/true ||{ 114 action "Install over and service startup" /bin/false 115 exit 1 116 }
安裝步驟:php
下載文件安裝包,將文件放入腳本中自定義的路徑(默認爲/tmp/nagios)執行安裝腳本mysql
安裝完成後會自動刪除/tmp/nagios目錄linux