menny 发表于 2019-1-15 12:45:20

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

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



[*]#! /bin/bash
[*]##################nagios server configure ########################
[*]installserver() {
[*]read -p " Please input the alarmemail address : " email
[*]read -p " Please input the login nagios user: " loginuser
[*]read -p " Please input the login nagios pass: " loginpass
[*]
[*]echo"###############Install nagios server #################"
[*]yum -y install httpd php php-gd php-xml php-mbstring php-ldap php-pear php-xmlrpc httpd-manual mod_ssl mod_perlzlib libpngfreetypelibart_lgpl libart_lgpl-devel libxml*   pango*
[*]
[*]wget http://prdownloads.sourceforge.net/sourceforge/nagios/nagios-3.2.3.tar.gz
[*]useradd nagios -s /sbin/nologin
[*]tar -xvfnagios-3.2.3.tar.gz
[*]cd nagios-3.2.3
[*]./configure --prefix=/usr/local/nagios
[*]make all
[*]make install
[*]make install-init
[*]make install-config
[*]make install-commandmode
[*]make install-webconf
[*]cd..
[*]
[*]wget http://prdownloads.sourceforge.net/sourceforge/nagiosplug/nagios-plugins-1.4.15.tar.gz
[*]tar -xvfnagios-plugins-1.4.15.tar.gz
[*]cd nagios-plugins-1.4.15
[*]./configure
[*]make
[*]make install
[*]cd ..
[*]
[*]wget http://nchc.dl.sourceforge.net/sourceforge/nagios/nrpe-2.12.tar.gz
[*]tar -xvfnrpe-2.12.tar.gz
[*]cd nrpe-2.12
[*]./configure
[*]make all
[*]make install
[*]make install-plugin
[*]make install-daemon
[*]make install-daemon-config
[*]cd ..
[*]
[*]wget http://oss.oetiker.ch/rrdtool/pub/rrdtool-1.4.7.tar.gz
[*]tar -xvfrrdtool-1.4.7.tar.gz
[*]cd rrdtool-1.4.7
[*]./configure --prefix=/usr/local/rrdtool/ &&make &&make install
[*]cd ..
[*]
[*]wget http://pkgs.fedoraproject.org/repo/pkgs/pnp4nagios/pnp-0.4.12.tar.gz/eb833a4769a5b58aad0ac53cae3e3e9f/pnp-0.4.12.tar.gz
[*]tar -xvfpnp-0.4.12.tar.gz
[*]cd pnp-0.4.12
[*]./configure --with-nagios-user=nagios --with-nagios-group-nagios --with-rrdtool=/usr/local/rrdtool/bin/rrdtool
[*]make   
[*]make all   
[*]make install   
[*]make install-config   
[*]make install-init
[*]cd ..
[*]
[*]wget http://218.6.16.170/somenagios.tar
[*]tar -xvf somenagios.tar -C /usr/local/nagios/libexec/
[*]
[*]echo"################install nagios servercompleted#############"
[*]
[*]echo "################# configure nagios server start###############"
[*]htpasswd -bc /usr/local/nagios/etc/htpasswd.users$loginuser $loginpass
[*]
[*]# cgi.cfg configure#
[*]sed -i "s#use_authentication=1#use_authentication=0#g" /usr/local/nagios/etc/cgi.cfg
[*]
[*]#nagios.cfg configure#
[*]sed -i '35i\cfg_file=/usr/local/nagios/etc/objects/hosts.cfg' /usr/local/nagios/etc/nagios.cfg
[*]sed -i '35i\cfg_file=/usr/local/nagios/etc/objects/services.cfg' /usr/local/nagios/etc/nagios.cfg
[*]sed -i '35i\cfg_file=/usr/local/nagios/etc/objects/hostgroup.cfg' /usr/local/nagios/etc/nagios.cfg
[*]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      
[*]sed -i "s#process_performance_data=0#process_performance_data=1#g" /usr/local/nagios/etc/nagios.cfg
[*]sed -i "s/#host_perfdata_command=process-host-perfdata/host_perfdata_command=process-host-perfdata/g" /usr/local/nagios/etc/nagios.cfg
[*]sed -i "s/#service_perfdata_command=process-service-perfdata/service_perfdata_command=process-service-perfdata/g" /usr/local/nagios/etc/nagios.cfg
[*]
[*]#command.cfg configure#
[*]
[*]line1=`sed -n '/process-host-perfdata$/=' /usr/local/nagios/etc/objects/commands.cfg`
[*]line2=`sed -n '/process-service-perfdata$/=' /usr/local/nagios/etc/objects/commands.cfg`
[*]
[*]sed -i "$(($line1+1))"d /usr/local/nagios/etc/objects/commands.cfg
[*]sed -i "$(($line1+1))i\      command_line /usr/local/nagios/libexec/process_perfdata.pl"/usr/local/nagios/etc/objects/commands.cfg
[*]sed -i "$(($line2+1))"d /usr/local/nagios/etc/objects/commands.cfg
[*]sed -i "$(($line2+1))i\      command_line /usr/local/nagios/libexec/process_perfdata.pl"/usr/local/nagios/etc/objects/commands.cfg
[*]
[*]echo '
[*]define command{
[*]      command_name    check_nrpe
[*]      command_line    $USER1$/check_nrpe -H $HOSTADDRESS$ -c $ARG1$ -t 30
[*]      }
[*]
[*]define command {
[*]       command_namecheck_mysql
[*]       command_line    $USER1$/check_mysql -H $HOSTADDRESS$ -unagdb -pnagdb -d nagdb
[*]
[*]       }
[*]'>>/usr/local/nagios/etc/objects/commands.cfg
[*]
[*]#templates.cfg configure#
[*]
[*]echo '
[*]define host {
[*]      name       hosts-pnp
[*]      register   0
[*]      action_url /nagios/pnp/index.php?host=$HOSTNAME$
[*]      process_perf_data               1
[*]
[*]}
[*]define service {
[*]      name       services-pnp
[*]      register   0
[*]      action_url /nagios/pnp/index.php?host=$HOSTNAME$&srv=$SERVICEDESC$
[*]      process_perf_data               1
[*]
[*]}'>>/usr/local/nagios/etc/objects/templates.cfg
[*]
[*]#pnp configure#
[*]
[*]cp/usr/local/nagios/etc/pnp/process_perfdata.cfg-sample/usr/local/nagios/etc/pnp/process_perfdata.cfg   
[*]cp/usr/local/nagios/etc/pnp/npcd.cfg-sample/usr/local/nagios/etc/pnp/npcd.cfg
[*]cp/usr/local/nagios/etc/pnp/rra.cfg-sample/usr/local/nagios/etc/pnp/rra.cfg   
[*]chown -R nagios:nagios/usr/local/nagios/etc/pnp/*
[*]
[*]sed -i 's#LOG_LEVEL = 0#LOG_LEVEL = 2#g'/usr/local/nagios/etc/pnp/process_perfdata.cfg
[*]
[*]#contacts.cfg configure#
[*]
[*]sed -i"s#nagios@localhost#$email#g" /usr/local/nagios/etc/objects/contacts.cfg   
[*]clear
[*]
[*]echo "################### nagios server install andconfigure completed,plase add hosts and servicesthen start it ##################"
[*]
[*]}
[*]
[*]restartnagios(){
[*]    echo "stopping ......"
[*]    service nagios stop
[*]    killall -9 nrpe
[*]    service httpd stop
[*]    echo "starting ......"
[*]    service nagios start
[*]    /usr/local/nagios/bin/nrpe -c /usr/local/nagios/etc/nrpe.cfg -d
[*]    service httpd start
[*]}
[*]addservices(){
[*]
[*]read -p "please input the alias name : "host
[*]read -p "please input the clint ip : "ip
[*]echo "define host {
[*]       host_name               $host
[*]      use            linux-server,hosts-pnp
[*]       alias                      nagios server
[*]       address                   $ip
[*]       contact_groups             admins
[*]       check_command            check-host-alive
[*]       max_check_attempts         5
[*]       notification_interval      10
[*]       notification_period      24x7
[*]       notification_options      d,u,r
[*]       }" >> /usr/local/nagios/etc/objects/hosts.cfg
[*]
[*]
[*]echo "define hostgroup {
[*]      hostgroup_name myserver
[*]      alias          myserver
[*]      members      $host
[*]}" >>/usr/local/nagios/etc/objects/hostgroup.cfg
[*]
[*]echo "define service {
[*]      host_name   $host
[*]      use local-service,services-pnp
[*]      service_description   CPU
[*]      check_period          24x7
[*]      max_check_attempts    4
[*]      normal_check_interval 1
[*]      retry_check_interval 1
[*]      contact_groups      admins
[*]      notification_interval   10
[*]      notification_period   24x7
[*]      notification_options    w,u,c,r
[*]      check_command    check_nrpe!check_cpu
[*]       }
[*]define service {
[*]      host_name   $host
[*]      use local-service,services-pnp
[*]      service_description   CPU_load
[*]      check_period          24x7
[*]      max_check_attempts    4
[*]      normal_check_interval 1
[*]      retry_check_interval 1
[*]      contact_groups      admins
[*]      notification_interval   10
[*]      notification_period   24x7
[*]      notification_options    w,u,c,r
[*]      check_command    check_nrpe!check_load
[*]       }
[*]define service {
[*]      host_name   $host
[*]      use local-service,services-pnp
[*]      service_description   HTTP
[*]      check_period          24x7
[*]      max_check_attempts    4
[*]      normal_check_interval 1
[*]      retry_check_interval 1
[*]      contact_groups      admins
[*]      notification_interval   10
[*]      notification_period   24x7
[*]      notification_options    w,u,c,r
[*]      check_command    check_nrpe!check_http
[*]       }
[*]define service {
[*]      host_name   $host
[*]      use local-service,services-pnp
[*]      service_description   MEMORY
[*]      check_period          24x7
[*]      max_check_attempts    4
[*]      normal_check_interval 1
[*]      retry_check_interval 1
[*]      contact_groups      admins
[*]      notification_interval   10
[*]      notification_period   24x7
[*]      notification_options    w,u,c,r
[*]      check_command    check_nrpe!check_mem
[*]       }
[*]define service {
[*]      host_name   $host
[*]      service_description   connect-count
[*]      check_period          24x7
[*]      max_check_attempts    4
[*]      normal_check_interval 1
[*]      retry_check_interval 1
[*]      contact_groups      admins
[*]      notification_interval   10
[*]      notification_period   24x7
[*]      notification_options    w,u,c,r
[*]      check_command    check_nrpe!check_ips
[*]       }
[*]define service {
[*]      host_name   $host
[*]      service_description   DISK
[*]      check_period          24x7
[*]      max_check_attempts    4
[*]      normal_check_interval 1
[*]      retry_check_interval 1
[*]      contact_groups      admins
[*]      notification_interval   10
[*]      notification_period   24x7
[*]      notification_options    w,u,c,r
[*]      check_command    check_nrpe!check_disk
[*]       }
[*]define service {
[*]      host_name   $host
[*]      service_description   mysql
[*]      check_period          24x7
[*]      max_check_attempts    4
[*]      normal_check_interval 1
[*]      retry_check_interval 1
[*]      contact_groups      admins
[*]      notification_interval   10
[*]      notification_period   24x7
[*]      notification_options    w,u,c,r
[*]      check_command   check_mysql
[*]       }
[*]define service {
[*]      host_name   $host
[*]      service_description   PING
[*]      use local-service,services-pnp
[*]      check_period          24x7
[*]      max_check_attempts    4
[*]      normal_check_interval 1
[*]      retry_check_interval 1
[*]      contact_groups      admins
[*]      notification_interval   10
[*]      notification_period   24x7
[*]      notification_options    w,u,c,r
[*]      check_command    check_ping!100.0,20%!500.0,60%
[*]       }
[*]define service {
[*]      host_name   $host
[*]      use local-service,services-pnp
[*]      service_descriptionFTP
[*]      check_period          24x7
[*]      max_check_attempts    4
[*]      normal_check_interval 60
[*]      retry_check_interval 10
[*]      contact_groups      admins
[*]      notification_interval   10
[*]      notification_period   24x7
[*]      notification_options    w,u,c,r
[*]      check_command    check_nrpe!check_ftp
[*]       }
[*]
[*]" >>/usr/local/nagios/etc/objects/services.cfg
[*]echo "###############Add services completed ########"
[*]}
[*]######################### nagiosconfigurecompleted ########################
[*]
[*]####################### clientserver configure ########################
[*]
[*]clientnagios() {
[*]
[*]### install soft###
[*]read -p "Please input the nagios server ip : " nagiosip
[*]
[*]read -p "Please input the mysql password : " mysqlpass
[*]
[*]useradd nagios
[*]yum -y install zlib libpngfreetypelibart_lgpl libart_lgpl-devel libxml*   pango*
[*]
[*]wget http://prdownloads.sourceforge.net/sourceforge/nagiosplug/nagios-plugins-1.4.15.tar.gz
[*]tar -xvfnagios-plugins-1.4.15.tar.gz
[*]cd nagios-plugins-1.4.15
[*]./configure
[*]make
[*]make install
[*]cd ..
[*]
[*]wget http://nchc.dl.sourceforge.net/sourceforge/nagios/nrpe-2.12.tar.gz
[*]tar -xvfnrpe-2.12.tar.gz
[*]cd nrpe-2.12
[*]./configure
[*]make all
[*]make install
[*]make install-plugin
[*]make install-daemon
[*]make install-daemon-config
[*]cd ..
[*]
[*]wget http://218.6.16.170/somenagios.tar
[*]tar -xvf somenagios.tar -C /usr/local/nagios/libexec/
[*]### install completed ###
[*]### configure ####
[*]
[*]rm /usr/local/nagios/etc/nrpe.cfg
[*]
[*]cat >>/usr/local/nagios/etc/nrpe.cfg
页: [1]
查看完整版本: 一键搭建nagios监控系统之一 脚本篇