一键搭建nagios监控系统之二 脚本解释篇

  由于发布文章字数有限制。脚本解释将在这一篇文章里讲解。如下:

  1. #! /bin/bash   
  2. ##  QQ:410018348
  3.  
  4. ##################  nagios server configure ########################   
  5.  
  6. installserver() {   #定义安装nagios函数 
  7. read -p " Please input the alarm  email address : " email   #报警邮箱 
  8. read -p " Please input the login nagios user: " loginuser   #网页登录时帐户 
  9. read -p " Please input the login nagios pass: " loginpass   #网页登录时的密码 
  10.    
  11. echo  "###############  Install nagios server #################"   
  12. 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*   
  13. #安装 apache php 及其他 
  14.    
  15. wget http://prdownloads.sourceforge.net/sourceforge/nagios/nagios-3.2.3.tar.gz   
  16. useradd nagios -s /sbin/nologin   
  17. tar -xvf  nagios-3.2.3.tar.gz   
  18. cd nagios-3.2.3   
  19. ./configure --prefix=/usr/local/nagios   
  20. make all   
  21. make install   
  22. make install-init   
  23. make install-config   
  24. make install-commandmode   
  25. make install-webconf   
  26. cd  ..    #安装nagios软件
  27.    
  28. wget http://prdownloads.sourceforge.net/sourceforge/nagiosplug/nagios-plugins-1.4.15.tar.gz   
  29. tar -xvf  nagios-plugins-1.4.15.tar.gz   
  30. cd nagios-plugins-1.4.15   
  31. ./configure   
  32. make   
  33. make install   
  34. cd ..    #安装nagios监控插件
  35.    
  36. wget http://nchc.dl.sourceforge.net/sourceforge/nagios/nrpe-2.12.tar.gz   
  37. tar -xvf  nrpe-2.12.tar.gz   
  38. cd nrpe-2.12   
  39. ./configure   
  40. make all   
  41. make install   
  42. make install-plugin   
  43. make install-daemon   
  44. make install-daemon-config   
  45. cd ..    #安装nrpe插件
  46.    
  47. wget http://oss.oetiker.ch/rrdtool/pub/rrdtool-1.4.7.tar.gz   
  48. tar -xvf  rrdtool-1.4.7.tar.gz   
  49. cd rrdtool-1.4.7   
  50. ./configure --prefix=/usr/local/rrdtool/ &&make &&make install   
  51. cd ..    #安装绘图工具
  52.    
  53. wget http://pkgs.fedoraproject.org/repo/pkgs/pnp4nagios/pnp-0.4.12.tar.gz/eb833a4769a5b58aad0ac53cae3e3e9f/pnp-0.4.12.tar.gz   
  54. tar -xvf  pnp-0.4.12.tar.gz   
  55. cd pnp-0.4.12   
  56. ./configure --with-nagios-user=nagios --with-nagios-group-nagios --with-rrdtool=/usr/local/rrdtool/bin/rrdtool   
  57. make    
  58. make all    
  59. make install    
  60. make install-config    
  61. make install-init   
  62. cd ..    #安装绘图工具
  63. ## 以上为安装nagios服务的一些软件。 
  64.    
  65. wget http://218.6.16.170/somenagios.tar      #这里的我个人找的一些 cpu ,内存,及mysql的插件。 打包在这。 
  66. tar -xvf somenagios.tar -C /usr/local/nagios/libexec/   
  67.    
  68. echo  "################install nagios server  completed#############"   #nagios安装完成。 
  69.    
  70. echo "################# configure nagios server start###############"   #下面是常规的一些配置。 
  71. htpasswd -bc /usr/local/nagios/etc/htpasswd.users  $loginuser $loginpass   #创建用户密码,就是上面要求输入的内容。 
  72.    
  73. # cgi.cfg configure#   
  74. sed -i "s#use_authentication=1#use_authentication=0#g" /usr/local/nagios/etc/cgi.cfg   #更改use_authentication参数 
  75.    
  76. #nagios.cfg configure#   
  77. sed -i '35i\cfg_file=/usr/local/nagios/etc/objects/hosts.cfg' /usr/local/nagios/etc/nagios.cfg   #在nagios.cfg里添加 host.cfg 
  78. sed -i '35i\cfg_file=/usr/local/nagios/etc/objects/services.cfg' /usr/local/nagios/etc/nagios.cfg  #在nagios.cfg里添加 services.cfg 
  79. sed -i '35i\cfg_file=/usr/local/nagios/etc/objects/hostgroup.cfg' /usr/local/nagios/etc/nagios.cfg  #在nagios.cfg里添加 hostgroup.cfg 
  80. 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  #禁用监控本地的配置文件     
  81. sed -i "s#process_performance_data=0#process_performance_data=1#g" /usr/local/nagios/etc/nagios.cfg  #更改 process_performance_data 为1 
  82. sed -i "s/#host_perfdata_command=process-host-perfdata/host_perfdata_command=process-host-perfdata/g" /usr/local/nagios/etc/nagios.cfg #开启 host_perfdata_commandsed -i "s/#service_perfdata_command=process-service-perfdata/service_perfdata_command=process-service-perfdata/g" /usr/local/nagios/etc/nagios.cfg   #开启service_perfdata_command 
  83.    
  84. #command.cfg configure#   
  85.   
  86. line1=`sed -n '/process-host-perfdata$/=' /usr/local/nagios/etc/objects/commands.cfg`  #查找commands.cfg里 process-host-perfdata的行 
  87. line2=`sed -n '/process-service-perfdata$/=' /usr/local/nagios/etc/objects/commands.cfg`  #同上 
  88.    
  89. sed -i "$(($line1+1))"d /usr/local/nagios/etc/objects/commands.cfg  #删除上面找的行的下一行 
  90. sed -i "$(($line1+1))i\        command_line /usr/local/nagios/libexec/process_perfdata.pl"  /usr/local/nagios/etc/objects/commands.cfg  #插入新的命令 
  91. sed -i "$(($line2+1))"d /usr/local/nagios/etc/objects/commands.cfg  #删除上面找的行的下一行 
  92. sed -i "$(($line2+1))i\        command_line /usr/local/nagios/libexec/process_perfdata.pl"  /usr/local/nagios/etc/objects/commands.cfg  #插入新的命令 
  93.    
  94. echo '   
  95. define command{   
  96.         command_name    check_nrpe   
  97.         command_line    $USER1$/check_nrpe -H $HOSTADDRESS$ -c $ARG1$ -t 30   
  98.         }   
  99.    
  100. define command {   
  101.        command_name  check_mysql   
  102.        command_line    $USER1$/check_mysql -H $HOSTADDRESS$ -unagdb -pnagdb -d nagdb   
  103.    
  104.        }   
  105. '>>/usr/local/nagios/etc/objects/commands.cfg  # 在commands.cfg里添加两条命令。 nrpe及监控mysql 
  106.    
  107. #templates.cfg configure#   
  108.    
  109. echo '   
  110. define host {   
  111.         name       hosts-pnp   
  112.         register   0   
  113.         action_url /nagios/pnp/index.php?host=$HOSTNAME$   
  114.         process_perf_data               1   
  115.    
  116. }   
  117. define service {   
  118.         name       services-pnp   
  119.         register   0   
  120.         action_url /nagios/pnp/index.php?host=$HOSTNAME$&srv=$SERVICEDESC$   
  121.         process_perf_data               1   
  122.    
  123. }'>>/usr/local/nagios/etc/objects/templates.cfg   
  124. # 上面是在templates.cfg里添加性能分析图表的功能 
  125.    
  126. #pnp configure#   
  127.    
  128. cp  /usr/local/nagios/etc/pnp/process_perfdata.cfg-sample  /usr/local/nagios/etc/pnp/process_perfdata.cfg    
  129. cp  /usr/local/nagios/etc/pnp/npcd.cfg-sample  /usr/local/nagios/etc/pnp/npcd.cfg   
  130. cp  /usr/local/nagios/etc/pnp/rra.cfg-sample  /usr/local/nagios/etc/pnp/rra.cfg    
  131. chown -R nagios:nagios  /usr/local/nagios/etc/pnp/*  
  132. #上面把pnp的模板文件改成真的配置文件 并授权    
  133. sed -i 's#LOG_LEVEL = 0#LOG_LEVEL = 2#g'  /usr/local/nagios/etc/pnp/process_perfdata.cfg   
  134. #更改 LOG_LEVEL 为2 
  135.  
  136. #contacts.cfg configure#     
  137. sed -i  "s#nagios@localhost#$email#g" /usr/local/nagios/etc/objects/contacts.cfg    
  138. clear   
  139. #更改contacts.cfg里面的联系人为所输入的联系人 
  140.    
  141. echo "################### nagios server install and  configure completed,plase add hosts and services  then start it ##################"   
  142.    
  143. } #以上nagios安装及配置完成。   
  144.   
  145. restartnagios(){   # 这里是定义nagios,nrpe,及apache重启的函数 
  146.     echo "stopping ......"   
  147.     service nagios stop   #nagios stop 
  148.     killall -9 nrpe   #nrpe stop 
  149.     service httpd stop   #apache stop 
  150.     echo "starting ......"    
  151.     service nagios start   #nagios start 
  152.     /usr/local/nagios/bin/nrpe -c /usr/local/nagios/etc/nrpe.cfg -d  # nrpe start 
  153.     service httpd start   #apache start 
  154. }   
  155.  
  156. addservices(){   #这里定义了在nagios端添加客户机的函数 
  157.    
  158. read -p "please input the alias name : "  host   #输入添加机器别名 
  159. read -p "please input the clint ip : "  ip   #输入添加机器的ip 
  160. echo "define host {   
  161.        host_name                 $host   
  162.         use            linux-server,hosts-pnp   
  163.        alias                      nagios server   
  164.        address                   $ip   
  165.        contact_groups             admins   
  166.        check_command              check-host-alive   
  167.        max_check_attempts         5   
  168.        notification_interval      10   
  169.        notification_period        24x7   
  170.        notification_options        d,u,r   
  171.        }" >> /usr/local/nagios/etc/objects/hosts.cfg   #在hosts.cfg里添加一个主机 (参数的意思可以在网上看下)
  172.    
  173.    
  174. echo "define hostgroup {   
  175.         hostgroup_name myserver   
  176.         alias          myserver   
  177.         members        $host   
  178. }" >>/usr/local/nagios/etc/objects/hostgroup.cfg   #在hostgroup.cfg里添中一个主机组,$host就是添加的别名
  179.  
  180. 是添加监控的具体服务。 
  181. echo "define service {    
  182.         host_name     $host   
  183.         use local-service,services-pnp   
  184.         service_description   CPU   
  185.         check_period          24x7   
  186.         max_check_attempts    4   
  187.         normal_check_interval 1   
  188.         retry_check_interval 1   
  189.         contact_groups        admins   
  190.         notification_interval   10   
  191.         notification_period     24x7   
  192.         notification_options    w,u,c,r   
  193.         check_command    check_nrpe!check_cpu   
  194.        }   #监控客户机CPU 
  195. define service {   
  196.         host_name     $host   
  197.         use local-service,services-pnp   
  198.         service_description   CPU_load   
  199.         check_period          24x7   
  200.         max_check_attempts    4   
  201.         normal_check_interval 1   
  202.         retry_check_interval 1   
  203.         contact_groups        admins   
  204.         notification_interval   10   
  205.         notification_period     24x7   
  206.         notification_options    w,u,c,r   
  207.         check_command    check_nrpe!check_load   
  208.        }  #监控客户机负载 
  209. define service {   
  210.         host_name     $host   
  211.         use local-service,services-pnp   
  212.         service_description   HTTP   
  213.         check_period          24x7   
  214.         max_check_attempts    4   
  215.         normal_check_interval 1   
  216.         retry_check_interval 1   
  217.         contact_groups        admins   
  218.         notification_interval   10   
  219.         notification_period     24x7   
  220.         notification_options    w,u,c,r   
  221.         check_command    check_nrpe!check_http   
  222.        }  #监控客户机http服务 
  223. define service {   
  224.         host_name     $host   
  225.         use local-service,services-pnp   
  226.         service_description   MEMORY   
  227.         check_period          24x7   
  228.         max_check_attempts    4   
  229.         normal_check_interval 1   
  230.         retry_check_interval 1   
  231.         contact_groups        admins   
  232.         notification_interval   10   
  233.         notification_period     24x7   
  234.         notification_options    w,u,c,r   
  235.         check_command    check_nrpe!check_mem   
  236.        }  #监控客户机的内存使用情况 
  237. define service {   
  238.         host_name     $host   
  239.         service_description   connect-count   
  240.         check_period          24x7   
  241.         max_check_attempts    4   
  242.         normal_check_interval 1   
  243.         retry_check_interval 1   
  244.         contact_groups        admins   
  245.         notification_interval   10   
  246.         notification_period     24x7   
  247.         notification_options    w,u,c,r   
  248.         check_command    check_nrpe!check_ips   
  249.        }  #监控客户机的连接情况 
  250. define service {   
  251.         host_name     $host   
  252.         service_description   DISK   
  253.         check_period          24x7   
  254.         max_check_attempts    4   
  255.         normal_check_interval 1   
  256.         retry_check_interval 1   
  257.         contact_groups        admins   
  258.         notification_interval   10   
  259.         notification_period     24x7   
  260.         notification_options    w,u,c,r   
  261.         check_command    check_nrpe!check_disk   
  262.        }  #监控客户机的硬盘分区使用情况 
  263. define service {   
  264.         host_name     $host   
  265.         service_description   mysql   
  266.         check_period          24x7   
  267.         max_check_attempts    4   
  268.         normal_check_interval 1   
  269.         retry_check_interval 1   
  270.         contact_groups        admins   
  271.         notification_interval   10   
  272.         notification_period     24x7   
  273.         notification_options    w,u,c,r   
  274.         check_command   check_mysql   
  275.        }  #监控客户机的mysql监控状态 
  276. define service {   
  277.         host_name     $host   
  278.         service_description   PING   
  279.         use local-service,services-pnp   
  280.         check_period          24x7   
  281.         max_check_attempts    4   
  282.         normal_check_interval 1   
  283.         retry_check_interval 1   
  284.         contact_groups        admins   
  285.         notification_interval   10   
  286.         notification_period     24x7   
  287.         notification_options    w,u,c,r   
  288.         check_command    check_ping!100.0,20%!500.0,60%   
  289.        }  #监控客户机的ping是否正常 
  290. define service {   
  291.         host_name     $host   
  292.         use local-service,services-pnp   
  293.         service_description  FTP   
  294.         check_period          24x7   
  295.         max_check_attempts    4   
  296.         normal_check_interval 60   
  297.         retry_check_interval 10   
  298.         contact_groups        admins   
  299.         notification_interval   10   
  300.         notification_period     24x7   
  301.         notification_options    w,u,c,r   
  302.         check_command    check_nrpe!check_ftp   
  303.        }  #监控客户机的ftp服务 
  304.    
  305. >>/usr/local/nagios/etc/objects/services.cfg   #把上面内容添加到services.cfg里面 (参数的意思可以在网上看下)
  306. echo "###############  Add services completed ########"   
  307. }   
  308. ######################### nagios  configure  completed ######################## 
  309. #以上为nagios服务端配置。   
  310.    
  311. ####################### client  server configure ########################   
  312.  #以下为被监控端配置 
  313. clientnagios() {   #定义客户机的安装配置 
  314.    
  315. ### install soft###   
  316. read -p "Please input the nagios server ip : " nagiosip   #输入naiogs服务端ip 
  317.    
  318. read -p "Please input the mysql password : " mysqlpass   #输入本机的mysql密码。 
  319.    
  320. useradd nagios   
  321. yum install zlib libpng  freetype  libart_lgpl libart_lgpl-devel libxml*   pango*  
  322. #安装所需要的包   
  323.    
  324. wget http://prdownloads.sourceforge.net/sourceforge/nagiosplug/nagios-plugins-1.4.15.tar.gz   
  325. tar -xvf  nagios-plugins-1.4.15.tar.gz   
  326. cd nagios-plugins-1.4.15   
  327. ./configure   
  328. make   
  329. make install   
  330. cd ..  
  331.    
  332. wget http://nchc.dl.sourceforge.net/sourceforge/nagios/nrpe-2.12.tar.gz   
  333. tar -xvf  nrpe-2.12.tar.gz   
  334. cd nrpe-2.12   
  335. ./configure   
  336. make all   
  337. make install   
  338. make install-plugin   
  339. make install-daemon   
  340. make install-daemon-config   
  341. cd ..   
  342. #以上为安装被监控机所需要的软件 
  343. wget http://218.6.16.170/somenagios.tar  
  344. tar -xvf somenagios.tar -C /usr/local/nagios/libexec/  #这里是个人找的几个监控插件 
  345. ### install completed ###  
  346. ### configure ####   
  347.    
  348. rm /usr/local/nagios/etc/nrpe.cfg   #删除安装的nrpe.cfg配置文件 
  349.    
  350. cat >>/usr/local/nagios/etc/nrpe.cfg<<EOF  #添加以下内容到nrpe.cfg配置文件里。主要就是一些监控本机的命令,供nagios服务端调用 
  351. log_facility=daemon   
  352. pid_file=/var/run/nrpe.pid   
  353. server_port=5666   
  354. nrpe_user=nagios   
  355. nrpe_group=nagios   
  356. allowed_hosts=$nagiosip   
  357. dont_blame_nrpe=1   
  358. debug=1   
  359. command_timeout=30   
  360. connection_timeout=300   
  361. command[check_http]=/usr/local/nagios/libexec/check_http -H  -u 127.0.0.1 /index.php -t 60   
  362. command[check_disk]=/usr/local/nagios/libexec/check_disk -w 15% -c 10% -A -i '/dev/shm'   
  363. command[check_cpu]=/usr/local/nagios/libexec/check_cpu.sh   
  364. command[check_mem]=/usr/local/nagios/libexec/check_mem.sh   
  365. command[check_ips]=/usr/local/nagios/libexec/ip_conn.sh  400 600   
  366. command[check_load]=/usr/local/nagios/libexec/check_load -w 14,12,10 -c 18,15,12   
  367. command[check_ftp]=/usr/local/nagios/libexec/check_ftp 127.0.0.1 -w 10 -c 60   
  368. EOF   
  369.    
  370. mysql -uroot -p"$mysqlpass" <<EOF   
  371. create database nagdb;   
  372. grant all on nagdb.* to nagdb@"$nagiosip" identified by 'nagdb';   
  373. flush privileges;   
  374. \q   
  375. EOF #这里是创建一个只能是nagios服务端访问的库。监控mysql状态是需有的。   
  376.    
  377. killall -9 nrpe   
  378. /usr/local/nagios/bin/nrpe -c /usr/local/nagios/etc/nrpe.cfg -d   #重启被监控机的nrpe服务 
  379.    
  380. ############ configure completed ############   
  381.  } 
  382. #客户机配置完成   
  383.    
  384. real=`grep -l '\^H' /root/.bash_profile`   
  385. if [ $? -eq 1 ];then   
  386. echo  'stty erase ^H' >> /root/.bash_profile   
  387. source /root/.bash_profile   #这几行主要就是让在使用read键时能使用回删键。写错了,回删了,重启写。不用这段的话,回删键会变成乱码。 
  388. fi   
  389. echo -e '\033[0;33;1m #################nagios################## \033[0m' #让echo能弄点颜色出来好看点。。。     
  390. echo "nagios server install and configure or addserver          please input : 1"   
  391. echo "  clint server install or configure               please input : 2"   
  392. echo "      exit                            please input : 3"   
  393. echo -e '\033[0;33;1m ######################################### \033[0m'   
  394.    
  395. read -p "please chose : " frist   #定义输入的值 
  396.    
  397. if [ $frist -eq 1 ];then   #值等于1时执行nagios服务器配置 
  398.    
  399. echo -e '\033[0;33;1m #################nagios################## \033[0m'   
  400. echo "nagios server install and configue                    please input : 1"   
  401. echo "  Add server and services                     please input : 2"   
  402. echo "      exit                            please input : 3"   
  403. echo -e '\033[0;33;1m ######################################### \033[0m'   
  404. read -p "please chose : " nagiosfrist  #定义nagiosfrist输入的值 
  405.     if [ $nagiosfrist -eq 1 ];then   #当nagiosfrist等于1时,执行下面 安装,添加服务 入重启服务的函数 
  406.                     installserver   
  407.                     addservices   
  408.                     restartnagios   
  409.     elif [ $nagiosfrist -eq 2 ];then   #当nagiosfrist等于2时,执行添加服务,重启服务函数 
  410.                     addservices   
  411.                     restartnagios   
  412.     elif [  $nagiosfrist -eq 3 ];then  #nagiosfrist等于3,就退出 
  413.                     clear   
  414.                     exit   
  415.     else   
  416.                     echo "plase chose [1|2|3]"   #都不等于的话,打印这个 
  417.     fi                 
  418.        
  419. elif [ $frist -eq 2 ];then   # 当frist等于2时 执行被监控端的函数 
  420. clientnagios   
  421.   
  422. elif [ $frist -eq 3 ];then  #等于3时退出 
  423. clear   
  424. exit   
  425. else    
  426. echo "plase chose [1|2|3]"   #都不等的话,就打印这个 
  427. fi   

脚本解析完成。本人也做了一系列的测试。看脚本是否能正常运行。答案是可以的。在测试当中,本人也装一些图抓了下来,在下篇博客 《一键搭建nagios监控系统之三 测试篇 》中我们来看下。

QQ:410018348