最近接了个项目,要求帮客户的6台服务器做一个监控系统,主要就是监控 cpuload;cpu使用率;硬盘分区使用情况;ftp是否健康;http是否健康;内存使用率;ping值;正常连接数以及mysql健康的监控. 而且要做到警告时,检测失败时以及恢复时要发送邮件到客户邮箱进行报警。(本来想是用飞信的,客户是电信189用户,不支持飞信,将邮件发送到189邮箱,然后邮箱邮件会自动发送到手机。) 基于这样的需求,决定来搭建一个nagios监控系统。可是正在准备着手搭建的时候,完了,时间长了忘记了怎么去搭建及配置了。有点杯具了。不过经过半天的摸索,终于上道了,花点时间搭建好了。也交差了。不过后来想想,始终靠记忆去记东西,靠不住啊。 决定把nagios服务器及客户端的配置,写到一个脚本里。只要在服务端和客户端执行 这个脚本,选择相应的操作,就能自动的将nagios监控系统完整的搭建完成。以后只要拿着这个脚本在手,就再不也怕啥"难够死"了。

经过一段时间的编写,这个脚本终于面试了。

1,脚本内容如下:

  1. #! /bin/bash  
  2. ##################  nagios server configure ########################  
  3. installserver() {  
  4. read -p " Please input the alarm  email address : " email  
  5. read -p " Please input the login nagios user: " loginuser  
  6. read -p " Please input the login nagios pass: " loginpass  
  7.   
  8. echo  "###############  Install nagios server #################"  
  9. yum -y install httpd php php-gd php-xml php-mbstring php-ldap php-pear php-xmlrpc httpd-manual mod_ssl mod_perl  zlib libpng  freetype  libart_lgpl libart_lgpl-devel libxml*   pango*  
  10.   
  11. wget http://prdownloads.sourceforge.net/sourceforge/nagios/nagios-3.2.3.tar.gz  
  12. useradd nagios -s /sbin/nologin  
  13. tar -xvf  nagios-3.2.3.tar.gz  
  14. cd nagios-3.2.3  
  15. ./configure --prefix=/usr/local/nagios  
  16. make all  
  17. make install  
  18. make install-init  
  19. make install-config  
  20. make install-commandmode  
  21. make install-webconf  
  22. cd  ..  
  23.   
  24. wget http://prdownloads.sourceforge.net/sourceforge/nagiosplug/nagios-plugins-1.4.15.tar.gz  
  25. tar -xvf  nagios-plugins-1.4.15.tar.gz  
  26. cd nagios-plugins-1.4.15  
  27. ./configure  
  28. make  
  29. make install  
  30. cd ..  
  31.   
  32. wget http://nchc.dl.sourceforge.net/sourceforge/nagios/nrpe-2.12.tar.gz  
  33. tar -xvf  nrpe-2.12.tar.gz  
  34. cd nrpe-2.12  
  35. ./configure  
  36. make all  
  37. make install  
  38. make install-plugin  
  39. make install-daemon  
  40. make install-daemon-config  
  41. cd ..  
  42.   
  43. wget http://oss.oetiker.ch/rrdtool/pub/rrdtool-1.4.7.tar.gz  
  44. tar -xvf  rrdtool-1.4.7.tar.gz  
  45. cd rrdtool-1.4.7  
  46. ./configure --prefix=/usr/local/rrdtool/ &&make &&make install  
  47. cd ..  
  48.   
  49. wget http://pkgs.fedoraproject.org/repo/pkgs/pnp4nagios/pnp-0.4.12.tar.gz/eb833a4769a5b58aad0ac53cae3e3e9f/pnp-0.4.12.tar.gz  
  50. tar -xvf  pnp-0.4.12.tar.gz  
  51. cd pnp-0.4.12  
  52. ./configure --with-nagios-user=nagios --with-nagios-group-nagios --with-rrdtool=/usr/local/rrdtool/bin/rrdtool  
  53. make   
  54. make all   
  55. make install   
  56. make install-config   
  57. make install-init  
  58. cd ..  
  59.   
  60. wget http://218.6.16.170/somenagios.tar  
  61. tar -xvf somenagios.tar -C /usr/local/nagios/libexec/  
  62.   
  63. echo  "################install nagios server  completed#############"  
  64.   
  65. echo "################# configure nagios server start###############"  
  66. htpasswd -bc /usr/local/nagios/etc/htpasswd.users  $loginuser $loginpass  
  67.   
  68. # cgi.cfg configure#  
  69. sed -i "s#use_authentication=1#use_authentication=0#g" /usr/local/nagios/etc/cgi.cfg  
  70.   
  71. #nagios.cfg configure#  
  72. sed -i '35i\cfg_file=/usr/local/nagios/etc/objects/hosts.cfg' /usr/local/nagios/etc/nagios.cfg  
  73. sed -i '35i\cfg_file=/usr/local/nagios/etc/objects/services.cfg' /usr/local/nagios/etc/nagios.cfg  
  74. sed -i '35i\cfg_file=/usr/local/nagios/etc/objects/hostgroup.cfg' /usr/local/nagios/etc/nagios.cfg  
  75. sed -i "s#cfg_file=/usr/local/nagios/etc/objects/localhost.cfg#\#cfg_file=/usr/local/nagios/etc/objects/localhost.cfg#g"  /usr/local/nagios/etc/nagios.cfg      
  76. sed -i "s#process_performance_data=0#process_performance_data=1#g" /usr/local/nagios/etc/nagios.cfg  
  77. sed -i "s/#host_perfdata_command=process-host-perfdata/host_perfdata_command=process-host-perfdata/g" /usr/local/nagios/etc/nagios.cfg  
  78. sed -i "s/#service_perfdata_command=process-service-perfdata/service_perfdata_command=process-service-perfdata/g" /usr/local/nagios/etc/nagios.cfg  
  79.   
  80. #command.cfg configure#  
  81.  
  82. line1=`sed -n '/process-host-perfdata$/=' /usr/local/nagios/etc/objects/commands.cfg`  
  83. line2=`sed -n '/process-service-perfdata$/=' /usr/local/nagios/etc/objects/commands.cfg`  
  84.   
  85. sed -i "$(($line1+1))"d /usr/local/nagios/etc/objects/commands.cfg  
  86. sed -i "$(($line1+1))i\        command_line /usr/local/nagios/libexec/process_perfdata.pl"  /usr/local/nagios/etc/objects/commands.cfg  
  87. sed -i "$(($line2+1))"d /usr/local/nagios/etc/objects/commands.cfg  
  88. sed -i "$(($line2+1))i\        command_line /usr/local/nagios/libexec/process_perfdata.pl"  /usr/local/nagios/etc/objects/commands.cfg  
  89.   
  90. echo '  
  91. define command{  
  92.         command_name    check_nrpe  
  93.         command_line    $USER1$/check_nrpe -H $HOSTADDRESS$ -c $ARG1$ -t 30  
  94.         }  
  95.   
  96. define command {  
  97.        command_name  check_mysql  
  98.        command_line    $USER1$/check_mysql -H $HOSTADDRESS$ -unagdb -pnagdb -d nagdb  
  99.   
  100.        }  
  101. '>>/usr/local/nagios/etc/objects/commands.cfg  
  102.   
  103. #templates.cfg configure#  
  104.   
  105. echo '  
  106. define host {  
  107.         name       hosts-pnp  
  108.         register   0  
  109.         action_url /nagios/pnp/index.php?host=$HOSTNAME$  
  110.         process_perf_data               1  
  111.   
  112. }  
  113. define service {  
  114.         name       services-pnp  
  115.         register   0  
  116.         action_url /nagios/pnp/index.php?host=$HOSTNAME$&srv=$SERVICEDESC$  
  117.         process_perf_data               1  
  118.   
  119. }'>>/usr/local/nagios/etc/objects/templates.cfg  
  120.   
  121. #pnp configure#  
  122.   
  123. cp  /usr/local/nagios/etc/pnp/process_perfdata.cfg-sample  /usr/local/nagios/etc/pnp/process_perfdata.cfg   
  124. cp  /usr/local/nagios/etc/pnp/npcd.cfg-sample  /usr/local/nagios/etc/pnp/npcd.cfg  
  125. cp  /usr/local/nagios/etc/pnp/rra.cfg-sample  /usr/local/nagios/etc/pnp/rra.cfg   
  126. chown -R nagios:nagios  /usr/local/nagios/etc/pnp/*  
  127.   
  128. sed -i 's#LOG_LEVEL = 0#LOG_LEVEL = 2#g'  /usr/local/nagios/etc/pnp/process_perfdata.cfg  
  129.   
  130. #contacts.cfg configure#  
  131.   
  132. sed -i  "s#nagios@localhost#$email#g" /usr/local/nagios/etc/objects/contacts.cfg   
  133. clear  
  134.   
  135. echo "################### nagios server install and  configure completed,plase add hosts and services  then start it ##################"  
  136.   
  137. }  
  138.  
  139. restartnagios(){  
  140.     echo "stopping ......"  
  141.     service nagios stop  
  142.     killall -9 nrpe  
  143.     service httpd stop  
  144.     echo "starting ......"  
  145.     service nagios start  
  146.     /usr/local/nagios/bin/nrpe -c /usr/local/nagios/etc/nrpe.cfg -d  
  147.     service httpd start  
  148. }  
  149. addservices(){  
  150.   
  151. read -p "please input the alias name : "  host  
  152. read -p "please input the clint ip : "  ip  
  153. echo "define host {  
  154.        host_name                 $host  
  155.         use            linux-server,hosts-pnp  
  156.        alias                      nagios server  
  157.        address                   $ip  
  158.        contact_groups             admins  
  159.        check_command              check-host-alive  
  160.        max_check_attempts         5  
  161.        notification_interval      10  
  162.        notification_period        24x7  
  163.        notification_options        d,u,r  
  164.        }" >> /usr/local/nagios/etc/objects/hosts.cfg  
  165.   
  166.   
  167. echo "define hostgroup {  
  168.         hostgroup_name myserver  
  169.         alias          myserver  
  170.         members        $host  
  171. }" >>/usr/local/nagios/etc/objects/hostgroup.cfg  
  172.  
  173. echo "define service {  
  174.         host_name     $host  
  175.         use local-service,services-pnp  
  176.         service_description   CPU  
  177.         check_period          24x7  
  178.         max_check_attempts    4  
  179.         normal_check_interval 1  
  180.         retry_check_interval 1  
  181.         contact_groups        admins  
  182.         notification_interval   10  
  183.         notification_period     24x7  
  184.         notification_options    w,u,c,r  
  185.         check_command    check_nrpe!check_cpu  
  186.        }  
  187. define service {  
  188.         host_name     $host  
  189.         use local-service,services-pnp  
  190.         service_description   CPU_load  
  191.         check_period          24x7  
  192.         max_check_attempts    4  
  193.         normal_check_interval 1  
  194.         retry_check_interval 1  
  195.         contact_groups        admins  
  196.         notification_interval   10  
  197.         notification_period     24x7  
  198.         notification_options    w,u,c,r  
  199.         check_command    check_nrpe!check_load  
  200.        }  
  201. define service {  
  202.         host_name     $host  
  203.         use local-service,services-pnp  
  204.         service_description   HTTP  
  205.         check_period          24x7  
  206.         max_check_attempts    4  
  207.         normal_check_interval 1  
  208.         retry_check_interval 1  
  209.         contact_groups        admins  
  210.         notification_interval   10  
  211.         notification_period     24x7  
  212.         notification_options    w,u,c,r  
  213.         check_command    check_nrpe!check_http  
  214.        }  
  215. define service {  
  216.         host_name     $host  
  217.         use local-service,services-pnp  
  218.         service_description   MEMORY  
  219.         check_period          24x7  
  220.         max_check_attempts    4  
  221.         normal_check_interval 1  
  222.         retry_check_interval 1  
  223.         contact_groups        admins  
  224.         notification_interval   10  
  225.         notification_period     24x7  
  226.         notification_options    w,u,c,r  
  227.         check_command    check_nrpe!check_mem  
  228.        }  
  229. define service {  
  230.         host_name     $host  
  231.         service_description   connect-count  
  232.         check_period          24x7  
  233.         max_check_attempts    4  
  234.         normal_check_interval 1  
  235.         retry_check_interval 1  
  236.         contact_groups        admins  
  237.         notification_interval   10  
  238.         notification_period     24x7  
  239.         notification_options    w,u,c,r  
  240.         check_command    check_nrpe!check_ips  
  241.        }  
  242. define service {  
  243.         host_name     $host  
  244.         service_description   DISK  
  245.         check_period          24x7  
  246.         max_check_attempts    4  
  247.         normal_check_interval 1  
  248.         retry_check_interval 1  
  249.         contact_groups        admins  
  250.         notification_interval   10  
  251.         notification_period     24x7  
  252.         notification_options    w,u,c,r  
  253.         check_command    check_nrpe!check_disk  
  254.        }  
  255. define service {  
  256.         host_name     $host  
  257.         service_description   mysql  
  258.         check_period          24x7  
  259.         max_check_attempts    4  
  260.         normal_check_interval 1  
  261.         retry_check_interval 1  
  262.         contact_groups        admins  
  263.         notification_interval   10  
  264.         notification_period     24x7  
  265.         notification_options    w,u,c,r  
  266.         check_command   check_mysql  
  267.        }  
  268. define service {  
  269.         host_name     $host  
  270.         service_description   PING  
  271.         use local-service,services-pnp  
  272.         check_period          24x7  
  273.         max_check_attempts    4  
  274.         normal_check_interval 1  
  275.         retry_check_interval 1  
  276.         contact_groups        admins  
  277.         notification_interval   10  
  278.         notification_period     24x7  
  279.         notification_options    w,u,c,r  
  280.         check_command    check_ping!100.0,20%!500.0,60%  
  281.        }  
  282. define service {  
  283.         host_name     $host  
  284.         use local-service,services-pnp  
  285.         service_description  FTP  
  286.         check_period          24x7  
  287.         max_check_attempts    4  
  288.         normal_check_interval 60  
  289.         retry_check_interval 10  
  290.         contact_groups        admins  
  291.         notification_interval   10  
  292.         notification_period     24x7  
  293.         notification_options    w,u,c,r  
  294.         check_command    check_nrpe!check_ftp  
  295.        }  
  296.   
  297. >>/usr/local/nagios/etc/objects/services.cfg  
  298. echo "###############  Add services completed ########"  
  299. }  
  300. ######################### nagios  configure  completed ########################  
  301.   
  302. ####################### client  server configure ########################  
  303.   
  304. clientnagios() {  
  305.   
  306. ### install soft###  
  307. read -p "Please input the nagios server ip : " nagiosip  
  308.   
  309. read -p "Please input the mysql password : " mysqlpass  
  310.   
  311. useradd nagios  
  312. yum -y install zlib libpng  freetype  libart_lgpl libart_lgpl-devel libxml*   pango*  
  313.   
  314. wget http://prdownloads.sourceforge.net/sourceforge/nagiosplug/nagios-plugins-1.4.15.tar.gz  
  315. tar -xvf  nagios-plugins-1.4.15.tar.gz  
  316. cd nagios-plugins-1.4.15  
  317. ./configure  
  318. make  
  319. make install  
  320. cd ..  
  321.   
  322. wget http://nchc.dl.sourceforge.net/sourceforge/nagios/nrpe-2.12.tar.gz  
  323. tar -xvf  nrpe-2.12.tar.gz  
  324. cd nrpe-2.12  
  325. ./configure  
  326. make all  
  327. make install  
  328. make install-plugin  
  329. make install-daemon  
  330. make install-daemon-config  
  331. cd ..  
  332.   
  333. wget http://218.6.16.170/somenagios.tar  
  334. tar -xvf somenagios.tar -C /usr/local/nagios/libexec/  
  335. ### install completed ### 
  336. ### configure ####  
  337.   
  338. rm /usr/local/nagios/etc/nrpe.cfg  
  339.   
  340. cat >>/usr/local/nagios/etc/nrpe.cfg<<EOF
  341. log_facility=daemon  
  342. pid_file=/var/run/nrpe.pid  
  343. server_port=5666  
  344. nrpe_user=nagios  
  345. nrpe_group=nagios  
  346. allowed_hosts=$nagiosip  
  347. dont_blame_nrpe=1  
  348. debug=1  
  349. command_timeout=30  
  350. connection_timeout=300  
  351. command[check_http]=/usr/local/nagios/libexec/check_http -H  -u 127.0.0.1 /index.php -t 60  
  352. command[check_disk]=/usr/local/nagios/libexec/check_disk -w 15% -c 10% -A -i '/dev/shm'  
  353. command[check_cpu]=/usr/local/nagios/libexec/check_cpu.sh  
  354. command[check_mem]=/usr/local/nagios/libexec/check_mem.sh  
  355. command[check_ips]=/usr/local/nagios/libexec/ip_conn.sh  400 600  
  356. command[check_load]=/usr/local/nagios/libexec/check_load -w 14,12,10 -c 18,15,12  
  357. command[check_ftp]=/usr/local/nagios/libexec/check_ftp 127.0.0.1 -w 10 -c 60  
  358. EOF
  359.   
  360. mysql -uroot -p"$mysqlpass" <<EOF
  361. create database nagdb;  
  362. grant all on nagdb.* to nagdb@"$nagiosip" identified by 'nagdb';  
  363. flush privileges;  
  364. \q  
  365. EOF
  366.   
  367. killall -9 nrpe  
  368. /usr/local/nagios/bin/nrpe -c /usr/local/nagios/etc/nrpe.cfg -d  
  369.   
  370. ############ configure completed ############  
  371.   
  372. }  
  373.   
  374. real=`grep -l '\^H' /root/.bash_profile`  
  375. if [ $? -eq 1 ];then  
  376. echo  'stty erase ^H' >> /root/.bash_profile  
  377. source /root/.bash_profile  
  378. fi  
  379. echo -e '\033[0;33;1m #################nagios################## \033[0m'    
  380. echo "nagios server install and configure or addserver          please input : 1"  
  381. echo "  clint server install or configure                please input : 2"  
  382. echo "      exit                             please input : 3"  
  383. echo -e '\033[0;33;1m ######################################### \033[0m'  
  384.   
  385. read -p "please chose : " frist  
  386.   
  387. if [ $frist -eq 1 ];then  
  388.   
  389. echo -e '\033[0;33;1m #################nagios################## \033[0m'  
  390. echo "nagios server install and configue                    please input : 1"  
  391. echo "  Add server and services                      please input : 2"  
  392. echo "      exit                             please input : 3"  
  393. echo -e '\033[0;33;1m ######################################### \033[0m'  
  394. read -p "please chose : " nagiosfrist  
  395.     if [ $nagiosfrist -eq 1 ];then  
  396.                     installserver  
  397.                     addservices  
  398.                     restartnagios  
  399.     elif [ $nagiosfrist -eq 2 ];then  
  400.                     addservices  
  401.                     restartnagios  
  402.     elif [  $nagiosfrist -eq 3 ];then  
  403.                     clear  
  404.                     exit  
  405.     else  
  406.                     echo "plase chose [1|2|3]"  
  407.     fi                
  408.       
  409. elif [ $frist -eq 2 ];then  
  410. clientnagios  
  411.  
  412. elif [ $frist -eq 3 ];then  
  413. clear  
  414. exit  
  415. else   
  416. echo "plase chose [1|2|3]"  
  417. fi  

由于字数有限,脚本解释将在另一篇博客《一键搭建nagios监控系统之二 脚本解释篇》中讲解

QQ:410018348