把这个脚本和nagios安装源码包以及插件源码包放到一起,执行脚本就可以自动部署了,现在分为2部分,第一部分安装nagios ,第二部分安装nagios-plugins,安装之前会判断需要的软件包是否存在,如果有未安装的会提示你安装,yum安装就可以,关于yum仓库配置,我之前有发过一个脚本,执行那个脚本就可以,在第一部分安装的最后,会提示你输入密码,那个密码就是web访问的密码,登陆用户为nagiosadmin,安装完成后直接访问即可,后续会加入nrpe以及nsca 部分配置内容。

 

 
  
  1.  
  2. #install nagios  
  3. #chenjian   
  4. #2010-009-01  
  5.  
  6. export LANG=en_US.UTF-8  
  7.  
  8. NAGIOS=./nagios-[0-9]*.tar.gz  
  9. NAGIOS_PLIUGINS=./nagios-plugins-*.tar.gz  
  10. NEED_PACKAGE="httpd gcc glibc glibc-common gd gd-devel php" 
  11. MYDATE=`date +%Y-%m-%d`  
  12.  
  13.  
  14. if [ $(id -u) -ne 0 ]  
  15.         then  echo error -please use super user.  
  16.         exit 2  
  17. fi  
  18.  
  19.  
  20. check_status () {  
  21. if [ $? -ne 0 ]  
  22. then clear  
  23.      echo $1 [fail]  
  24.      exit 2  
  25. else   
  26.      echo $1 [OK]  
  27. fi   
  28. }  
  29.  
  30.  
  31. check_need_package () {  
  32. echo -------- Step1 : Check the relevant package installation situation.  
  33. echo `rpm -qa $NEED_PACKAGE |sed 's/-[0-9]*\..*//g'` > /tmp/install_nagios  
  34.  
  35. for i in  $NEED_PACKAGE  
  36.        do `grep $i /tmp/install_nagios >& /dev/null`  
  37.            if [ $? -eq 0 ]  
  38.               then   
  39.                    :   
  40.               else 
  41.                    echo $i is not install.  
  42.                    SITUATION=OK  
  43.               fi  
  44.  
  45. done  
  46.  
  47. if [ $SITUATION ]  
  48.    then exit 2  
  49. fi  
  50. }  
  51.  
  52. server () {  
  53. cat <<eof  
  54.                                                  nagios install  
  55. -----------------------------------------------------------------------------------------------------------------  
  56.                         User:$USER     Select the choice you want install      Date:$MYDATE  
  57. -----------------------------------------------------------------------------------------------------------------  
  58.  
  59.                                         1) install nagios  
  60.  
  61.                                         2) install nagios-plugins  
  62.  
  63.                                         q)  exit programe  
  64. -----------------------------------------------------------------------------------------------------------------  
  65. eof  
  66. }  
  67.  
  68. portal () {  
  69. clear  
  70. server  
  71. read info  
  72. case $info in 
  73. 1)  
  74. clear  
  75.  
  76. check_need_package  
  77.           
  78. if ! id nagios >& /dev/null 
  79.         then 
  80.                 useradd -m nagios  
  81.                 groupadd nagcmd  
  82.                 usermod -a -G nagcmd nagios  
  83.                 usermod -a -G nagcmd apache  
  84. fi  
  85.  
  86. if [ ! -f $NAGIOS ]  
  87.         then echo "Nagios source package not found." 
  88.         exit 2  
  89. else 
  90.  
  91.              tar -zxf $NAGIOS -C /tmp  
  92.              cd /tmp/nagios-[0-9]*  
  93.              clear  
  94.              echo Step2 : Configure and compile the source code for your system ,please wait.....  
  95.              sleep 5  
  96.              ./configure --with-command-group=nagcmd  
  97. fi  
  98.  
  99. if [ $? -eq 0 ]  
  100. then echo -------- Configure Successful.  
  101. fi  
  102.         clear   
  103.         echo Step3 : Make and install everything ,please wait.....  
  104.         sleep 5  
  105.         make all   
  106.         check_status "make all" 
  107.         make install   
  108.         check_status "make install" 
  109.         make install-init   
  110.         check_status "make install-init" 
  111.         make install-config   
  112.         check_status "make install-config" 
  113.         make install-commandmode   
  114.         check_status "make install-commandmode" 
  115.         make install-webconf   
  116.         check_status "make install-webconf" 
  117.  
  118.         if [ $? -eq 0 ]  
  119.            then   
  120.                 clear   
  121.                 echo  Nagios make install Successful.   
  122.         fi  
  123.  
  124.  
  125.  
  126. clear  
  127. echo set nagios web access password,username is nagiosadmin.  
  128.  
  129. htpasswd -c /usr/local/nagios/etc/htpasswd.users nagiosadmin  
  130.         cd ~  
  131.         rm -rf /tmp/install_nagios  
  132.         rm -rf /tmp/nagios-[0-9]*  
  133.  
  134. clear  
  135.  
  136.  
  137. if ! sestatus |awk '{print $3}' |grep disabled >& /dev/null 
  138.         then   
  139.         chcon -R -t httpd_sys_content_t /usr/local/nagios/sbin/  
  140.         chcon -R -t httpd_sys_content_t /usr/local/nagios/share/  
  141. fi  
  142.  
  143. echo Set apache and nagios start at boot time.  
  144.  
  145. chkconfig httpd on && echo apache set [OK]  
  146. chkconfig nagios on && echo nagios set [OK]  
  147.  
  148.  
  149. service httpd start  
  150. service nagios start  
  151.  
  152. echo -e "  
  153. -------------------------------------------------------------------\n  
  154. install nagios successful ,please use http://yourip/nagios access\n  
  155.       user: nagiosadmin              password: ***(just set)  
  156. -------------------------------------------------------------------"  
  157. exit 0  
  158.  
  159. ;;  
  160.  
  161.  
  162. 2)  
  163. clear  
  164. check_need_package  
  165.  
  166. if [ ! -f $NAGIOS_PLIUGINS ]  
  167.         then echo "Nagios-pliugins source package not found." 
  168.         exit 2  
  169. else 
  170.              tar -zxf $NAGIOS_PLIUGINS -C /tmp  
  171.              cd /tmp/nagios-plugins*  
  172.              clear  
  173.              echo Step2 : Configure and compile the source code for your system ,please wait.....  
  174.              sleep 5  
  175.              ./configure --with-nagios-user=nagios --with-nagios-group=nagios  
  176. fi  
  177.  
  178. if [ $? -eq 0 ]  
  179. then echo -------- Configure Successful.   
  180.  
  181. fi  
  182.  
  183. clear  
  184.         echo Step3 : Make and install everything ,please wait.....  
  185.         sleep 5  
  186.         make   
  187.         check_status "make" 
  188.         make install  
  189.         check_status "make install" 
  190. echo -e "  
  191. -------------------------------------------------------------------\n  
  192. install nagios plugins successful.\n  
  193. -------------------------------------------------------------------"  
  194. cd ~  
  195.  
  196. rm -rf /tmp/nagios-plugins*  
  197. rm -rf /tmp/install_nagios  
  198. ;;  
  199.  
  200. q)  
  201. exit 0  
  202. ;;  
  203. *)  
  204. portal  
  205. ;;  
  206. esac  
  207. }  
  208.  
  209. main () {  
  210. clear  
  211. portal  
  212. exit 0  
  213. }  
  214.  
  215. main