一键搭建nagios监控系统之二 脚本解释篇
由于发布文章字数有限制。脚本解释将在这一篇文章里讲解。如下:
#!/bin/bash## QQ:410018348##################nagiosserverconfigure########################installserver(){#定义安装nagios函数read-p"Pleaseinputthealarmemailaddress:"email#报警邮箱read-p"Pleaseinputtheloginnagiosuser:"loginuser#网页登录时帐户read-p"Pleaseinputtheloginnagiospass:"loginpass#网页登录时的密码echo"###############Installnagiosserver#################"yum-yinstallhttpdphpphp-gdphp-xmlphp-mbstringphp-ldapphp-pearphp-xmlrpchttpd-manualmod_sslmod_perlzliblibpngfreetypelibart_lgpllibart_lgpl-devellibxml*pango*#安装apachephp及其他wgethttp://prdownloads.sourceforge.net/sourceforge/nagios/nagios-3.2.3.tar.gzuseraddnagios-s/sbin/nologintar-xvfnagios-3.2.3.tar.gzcdnagios-3.2.3./configure--prefix=/usr/local/nagiosmakeallmakeinstallmakeinstall-initmakeinstall-configmakeinstall-commandmodemakeinstall-webconfcd.. #安装nagios软件wgethttp://prdownloads.sourceforge.net/sourceforge/nagiosplug/nagios-plugins-1.4.15.tar.gztar-xvfnagios-plugins-1.4.15.tar.gzcdnagios-plugins-1.4.15./configuremakemakeinstallcd.. #安装nagios监控插件wgethttp://nchc.dl.sourceforge.net/sourceforge/nagios/nrpe-2.12.tar.gztar-xvfnrpe-2.12.tar.gzcdnrpe-2.12./configuremakeallmakeinstallmakeinstall-pluginmakeinstall-daemonmakeinstall-daemon-configcd.. #安装nrpe插件wgethttp://oss.oetiker.ch/rrdtool/pub/rrdtool-1.4.7.tar.gztar-xvfrrdtool-1.4.7.tar.gzcdrrdtool-1.4.7./configure--prefix=/usr/local/rrdtool/&&make&&makeinstallcd.. #安装绘图工具wgethttp://pkgs.fedoraproject.org/repo/pkgs/pnp4nagios/pnp-0.4.12.tar.gz/eb833a4769a5b58aad0ac53cae3e3e9f/pnp-0.4.12.tar.gztar-xvfpnp-0.4.12.tar.gzcdpnp-0.4.12./configure--with-nagios-user=nagios--with-nagios-group-nagios--with-rrdtool=/usr/local/rrdtool/bin/rrdtoolmakemakeallmakeinstallmakeinstall-configmakeinstall-initcd.. #安装绘图工具##以上为安装nagios服务的一些软件。wgethttp://218.6.16.170/somenagios.tar #这里的我个人找的一些cpu,内存,及mysql的插件。打包在这。tar-xvfsomenagios.tar-C/usr/local/nagios/libexec/echo"################installnagiosservercompleted#############"#nagios安装完成。echo"#################configurenagiosserverstart###############"#下面是常规的一些配置。htpasswd-bc/usr/local/nagios/etc/htpasswd.users$loginuser$loginpass#创建用户密码,就是上面要求输入的内容。#cgi.cfgconfigure#sed-i"s#use_authentication=1#use_authentication=0#g"/usr/local/nagios/etc/cgi.cfg#更改use_authentication参数#nagios.cfgconfigure#sed-i'35i\cfg_file=/usr/local/nagios/etc/objects/hosts.cfg'/usr/local/nagios/etc/nagios.cfg#在nagios.cfg里添加host.cfgsed-i'35i\cfg_file=/usr/local/nagios/etc/objects/services.cfg'/usr/local/nagios/etc/nagios.cfg#在nagios.cfg里添加services.cfgsed-i'35i\cfg_file=/usr/local/nagios/etc/objects/hostgroup.cfg'/usr/local/nagios/etc/nagios.cfg#在nagios.cfg里添加hostgroup.cfgsed-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#更改process_performance_data为1sed-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#command.cfgconfigure#line1=`sed-n'/process-host-perfdata$/='/usr/local/nagios/etc/objects/commands.cfg`#查找commands.cfg里process-host-perfdata的行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'definecommand{command_namecheck_nrpecommand_line$USER1$/check_nrpe-H$HOSTADDRESS$-c$ARG1$-t30}definecommand{command_namecheck_mysqlcommand_line$USER1$/check_mysql-H$HOSTADDRESS$-unagdb-pnagdb-dnagdb}'>>/usr/local/nagios/etc/objects/commands.cfg#在commands.cfg里添加两条命令。nrpe及监控mysql#templates.cfgconfigure#echo'definehost{namehosts-pnpregister0action_url/nagios/pnp/index.php?host=$HOSTNAME$process_perf_data1}defineservice{nameservices-pnpregister0action_url/nagios/pnp/index.php?host=$HOSTNAME$&srv=$SERVICEDESC$process_perf_data1}'>>/usr/local/nagios/etc/objects/templates.cfg#上面是在templates.cfg里添加性能分析图表的功能#pnpconfigure#cp/usr/local/nagios/etc/pnp/process_perfdata.cfg-sample/usr/local/nagios/etc/pnp/process_perfdata.cfgcp/usr/local/nagios/etc/pnp/npcd.cfg-sample/usr/local/nagios/etc/pnp/npcd.cfgcp/usr/local/nagios/etc/pnp/rra.cfg-sample/usr/local/nagios/etc/pnp/rra.cfgchown-Rnagios:nagios/usr/local/nagios/etc/pnp/*#上面把pnp的模板文件改成真的配置文件并授权sed-i's#LOG_LEVEL=0#LOG_LEVEL=2#g'/usr/local/nagios/etc/pnp/process_perfdata.cfg#更改LOG_LEVEL为2#contacts.cfgconfigure#sed-i"s#nagios@localhost#$email#g"/usr/local/nagios/etc/objects/contacts.cfgclear#更改contacts.cfg里面的联系人为所输入的联系人echo"###################nagiosserverinstallandconfigurecompleted,plaseaddhostsandservicesthenstartit##################"}#以上nagios安装及配置完成。restartnagios(){#这里是定义nagios,nrpe,及apache重启的函数echo"stopping......"servicenagiosstop#nagiosstopkillall-9nrpe#nrpestopservicehttpdstop#apachestopecho"starting......"servicenagiosstart#nagiosstart/usr/local/nagios/bin/nrpe-c/usr/local/nagios/etc/nrpe.cfg-d#nrpestartservicehttpdstart#apachestart}addservices(){#这里定义了在nagios端添加客户机的函数read-p"pleaseinputthealiasname:"host#输入添加机器别名read-p"pleaseinputtheclintip:"ip#输入添加机器的ipecho"definehost{host_name$host uselinux-server,hosts-pnp aliasnagiosserver address$ip contact_groupsadmins check_commandcheck-host-alivemax_check_attempts5 notification_interval10 notification_period24x7 notification_optionsd,u,r }">>/usr/local/nagios/etc/objects/hosts.cfg#在hosts.cfg里添加一个主机(参数的意思可以在网上看下)echo"definehostgroup{hostgroup_namemyserveraliasmyservermembers$host}">>/usr/local/nagios/etc/objects/hostgroup.cfg#在hostgroup.cfg里添中一个主机组,$host就是添加的别名是添加监控的具体服务。echo"defineservice{host_name$hostuselocal-service,services-pnpservice_descriptionCPUcheck_period24x7max_check_attempts4normal_check_interval1retry_check_interval1contact_groupsadminsnotification_interval10notification_period24x7notification_optionsw,u,c,rcheck_commandcheck_nrpe!check_cpu}#监控客户机CPUdefineservice{host_name$hostuselocal-service,services-pnpservice_descriptionCPU_loadcheck_period24x7max_check_attempts4normal_check_interval1retry_check_interval1contact_groupsadminsnotification_interval10notification_period24x7notification_optionsw,u,c,rcheck_commandcheck_nrpe!check_load}#监控客户机负载defineservice{host_name$hostuselocal-service,services-pnpservice_descriptionHTTPcheck_period24x7max_check_attempts4normal_check_interval1retry_check_interval1contact_groupsadminsnotification_interval10notification_period24x7notification_optionsw,u,c,rcheck_commandcheck_nrpe!check_http}#监控客户机http服务defineservice{host_name$hostuselocal-service,services-pnpservice_descriptionMEMORYcheck_period24x7max_check_attempts4normal_check_interval1retry_check_interval1contact_groupsadminsnotification_interval10notification_period24x7notification_optionsw,u,c,rcheck_commandcheck_nrpe!check_mem}#监控客户机的内存使用情况defineservice{host_name$hostservice_descriptionconnect-countcheck_period24x7max_check_attempts4normal_check_interval1retry_check_interval1contact_groupsadminsnotification_interval10notification_period24x7notification_optionsw,u,c,rcheck_commandcheck_nrpe!check_ips}#监控客户机的连接情况defineservice{host_name$hostservice_descriptionDISKcheck_period24x7max_check_attempts4normal_check_interval1retry_check_interval1contact_groupsadminsnotification_interval10notification_period24x7notification_optionsw,u,c,rcheck_commandcheck_nrpe!check_disk}#监控客户机的硬盘分区使用情况defineservice{host_name$hostservice_descriptionmysqlcheck_period24x7max_check_attempts4normal_check_interval1retry_check_interval1contact_groupsadminsnotification_interval10notification_period24x7notification_optionsw,u,c,rcheck_commandcheck_mysql}#监控客户机的mysql监控状态defineservice{host_name$hostservice_descriptionPINGuselocal-service,services-pnpcheck_period24x7max_check_attempts4normal_check_interval1retry_check_interval1contact_groupsadminsnotification_interval10notification_period24x7notification_optionsw,u,c,rcheck_commandcheck_ping!100.0,20%!500.0,60%}#监控客户机的ping是否正常defineservice{host_name$hostuselocal-service,services-pnpservice_descriptionFTPcheck_period24x7max_check_attempts4normal_check_interval60retry_check_interval10contact_groupsadminsnotification_interval10notification_period24x7notification_optionsw,u,c,rcheck_commandcheck_nrpe!check_ftp}#监控客户机的ftp服务">>/usr/local/nagios/etc/objects/services.cfg#把上面内容添加到services.cfg里面 (参数的意思可以在网上看下)echo"###############Addservicescompleted########"}#########################nagiosconfigurecompleted#########################以上为nagios服务端配置。#######################clientserverconfigure#########################以下为被监控端配置clientnagios(){#定义客户机的安装配置###installsoft###read-p"Pleaseinputthenagiosserverip:"nagiosip#输入naiogs服务端ipread-p"Pleaseinputthemysqlpassword:"mysqlpass#输入本机的mysql密码。useraddnagiosyuminstallzliblibpngfreetypelibart_lgpllibart_lgpl-devellibxml*pango*#安装所需要的包wgethttp://prdownloads.sourceforge.net/sourceforge/nagiosplug/nagios-plugins-1.4.15.tar.gztar-xvfnagios-plugins-1.4.15.tar.gzcdnagios-plugins-1.4.15./configuremakemakeinstallcd..wgethttp://nchc.dl.sourceforge.net/sourceforge/nagios/nrpe-2.12.tar.gztar-xvfnrpe-2.12.tar.gzcdnrpe-2.12./configuremakeallmakeinstallmakeinstall-pluginmakeinstall-daemonmakeinstall-daemon-configcd..#以上为安装被监控机所需要的软件 wgethttp://218.6.16.170/somenagios.tar tar-xvfsomenagios.tar-C/usr/local/nagios/libexec/#这里是个人找的几个监控插件###installcompleted######configure####rm/usr/local/nagios/etc/nrpe.cfg#删除安装的nrpe.cfg配置文件cat>>/usr/local/nagios/etc/nrpe.cfg<<EOF#添加以下内容到nrpe.cfg配置文件里。主要就是一些监控本机的命令,供nagios服务端调用log_facility=daemonpid_file=/var/run/nrpe.pidserver_port=5666nrpe_user=nagiosnrpe_group=nagiosallowed_hosts=$nagiosipdont_blame_nrpe=1debug=1command_timeout=30connection_timeout=300command[check_http]=/usr/local/nagios/libexec/check_http-H-u127.0.0.1/index.php-t60command[check_disk]=/usr/local/nagios/libexec/check_disk-w15%-c10%-A-i'/dev/shm'command[check_cpu]=/usr/local/nagios/libexec/check_cpu.shcommand[check_mem]=/usr/local/nagios/libexec/check_mem.shcommand[check_ips]=/usr/local/nagios/libexec/ip_conn.sh400600command[check_load]=/usr/local/nagios/libexec/check_load-w14,12,10-c18,15,12command[check_ftp]=/usr/local/nagios/libexec/check_ftp127.0.0.1-w10-c60EOFmysql-uroot-p"$mysqlpass"<<EOFcreatedatabasenagdb;grantallonnagdb.*tonagdb@"$nagiosip"identifiedby'nagdb';flushprivileges;\qEOF#这里是创建一个只能是nagios服务端访问的库。监控mysql状态是需有的。killall-9nrpe/usr/local/nagios/bin/nrpe-c/usr/local/nagios/etc/nrpe.cfg-d#重启被监控机的nrpe服务############configurecompleted############}#客户机配置完成real=`grep-l'\^H'/root/.bash_profile`if[$?-eq1];thenecho'sttyerase^H'>>/root/.bash_profilesource/root/.bash_profile#这几行主要就是让在使用read键时能使用回删键。写错了,回删了,重启写。不用这段的话,回删键会变成乱码。fiecho-e'\033[0;33;1m#################nagios##################\033[0m'#让echo能弄点颜色出来好看点。。。echo"nagiosserverinstallandconfigureoraddserverpleaseinput:1"echo"clintserverinstallorconfigurepleaseinput:2"echo"exitpleaseinput:3"echo-e'\033[0;33;1m#########################################\033[0m'read-p"pleasechose:"frist#定义输入的值if[$frist-eq1];then#值等于1时执行nagios服务器配置echo-e'\033[0;33;1m#################nagios##################\033[0m'echo"nagiosserverinstallandconfiguepleaseinput:1"echo"Addserverandservicespleaseinput:2"echo"exitpleaseinput:3"echo-e'\033[0;33;1m#########################################\033[0m'read-p"pleasechose:"nagiosfrist#定义nagiosfrist输入的值if[$nagiosfrist-eq1];then#当nagiosfrist等于1时,执行下面安装,添加服务入重启服务的函数installserveraddservicesrestartnagioselif[$nagiosfrist-eq2];then#当nagiosfrist等于2时,执行添加服务,重启服务函数addservicesrestartnagioselif[$nagiosfrist-eq3];then#nagiosfrist等于3,就退出clearexitelseecho"plasechose[1|2|3]"#都不等于的话,打印这个fielif[$frist-eq2];then#当frist等于2时执行被监控端的函数clientnagioselif[$frist-eq3];then#等于3时退出clearexitelseecho"plasechose[1|2|3]"#都不等的话,就打印这个fi
脚本解析完成。本人也做了一系列的测试。看脚本是否能正常运行。答案是可以的。在测试当中,本人也装一些图抓了下来,在下篇博客 《一键搭建nagios监控系统之三 测试篇》中我们来看下。
QQ:410018348
声明:本站所有文章资源内容,如无特殊说明或标注,均为采集网络资源。如若本站内容侵犯了原著者的合法权益,可联系本站删除。