《nagios安装监控报警配置详解byHF讲课讲稿.doc》由会员分享,可在线阅读,更多相关《nagios安装监控报警配置详解byHF讲课讲稿.doc(55页珍藏版)》请在taowenge.com淘文阁网|工程机械CAD图纸|机械工程制图|CAD装配图下载|SolidWorks_CaTia_CAD_UG_PROE_设计图分享下载上搜索。
1、Good is good, but better carries it.精益求精,善益求善。nagios安装监控报警配置详解byHF-Nagios安装与配置详解内容简介本文档介绍nagios监控linux服务器,因为监控都是依靠插件去完成的,而监控linux主要使用NRPE插件,本文首先简单介绍一下NRPE监控基础,及监控过程,然后一步步的配置一个实例实现监控linux服务器1.NRPE监控插件基础NRPE总共由两部分组成:check_nrpe插件,运行在监控主机上。NRPEdaemon,运行在远程的linux主机上(通常就是被监控机)整个的监控过程:(如下图)当Nagios需要监控某个远程l
2、inux主机的服务或者资源情况时:1).nagios运行check_nrpe插件,我们要在nagios配置文件中告诉它要检查什么.2).check_nrpe插件会通过SSL连接到远程的NRPEdaemon.3).NRPEdaemon会运行相应的nagios插件来执行检查本地资源或服务.4).NRPEdaemon将检查的结果返回给check_nrpe插件,插件将其递交给nagios做处理.注意:NRPEdaemon需要nagios插件安装在远程被监控linux主机上,否则,daemon不能做任何的监控.别外因为它们间的通信是加密的SSL,所以在编译安装时都要加上选项,./configure-en
3、able-ssl-with-ssl-lib=/lib/,否则也会出错.安装Nagios一、准备软件包在做安装之前确认要对该机器拥有root权限。确认你安装好的linux系统上已经安装如下软件包再继续。ApacheGCC编译器GD库与开发库可以用yum命令来安装这些软件包,键入命令:yumyinstallhttpdgccglibcglibccommongdgddevelrootlocalhost#rpm-qa|grepgdgd-devel-2.0.33-9.3.fc6gdb-6.5-16.el5gd-2.0.33-9.3.fc6gdbm-1.8.0-26.2.1sysklogd-1.4.1-39
4、.2gdm-2.16.0-30.el5rootlocalhost#rpm-qa|grepglibglibc-headers-2.5-12dbus-glib-0.70-5NetworkManager-glib-0.6.4-6.el5avahi-glib-0.6.16-1.el5glibc-2.5-12glibc-common-2.5-12glib-java-0.2.6-3.fc6compat-glibc-headers-2.3.4-2.26glib2-2.12.3-2.fc6glibc-devel-2.5-12compat-glibc-2.3.4-2.2rootlocalhostnagios#r
5、pm-qa|grepgcccompat-gcc-34-3.4.6-4gcc-4.1.1-52.el5gcc-c+-4.1.1-52.el5compat-gcc-34-g77-3.4.6-4libgcc-4.1.1-52.el5compat-gcc-34-c+-3.4.6-4gcc-gfortran-4.1.1-52.el5下面安装需要用到的软件包下载地址:1nagioshttp:/cdnetworks-kr-2nagios-plugins-1.4.14.tar.gzhttp:/cdnetworks-kr-3nrpe-2.12.tar.gzhttp:/cdnetworks-kr-二、安装Nagi
6、os操作过程2.1建立nagios账号/usr/sbin/useraddnagios&passwdnagios2.2创建一个用户组名为nagioscmd用于从Web接口执行外部命令。将nagios用户和apache用户都加到这个组中。rootlocalhostnagios#groupaddnagioscmdrootlocalhostnagios#usermodGnagioscmdnagiosrootlocalhostnagios#usermodGnagioscmdapache(没装apache可以先不配置)rootlocalhost#less/etc/groupnagios:x:500:nag
7、ioscmd:x:501:nagios,apache2.3编译与安装Nagiosrootlocalhost#tarzxvfnagios3.0.6.tar.gzrootlocalhost#cdnagios3.0.6rootlocalhosts#./configure-withcommandgroup=nagioscmd-prefix=/usr/local/nagios提示如下就进行下步Reviewtheoptionsaboveforaccuracy.Iftheylookokay,typemakealltocompilethemainprogramandCGIs.rootlocalhosts#.#
8、makeall*Compilefinished*rootlocalhosts#.#makeinstall*Mainprogram,CGIsandHTMLfilesinstalled*rootlocalhosts#.#makeinstallinitrootlocalhosts#.#makeinstallconfigrootlocalhosts#.#makeinstallcommandmode*Configfilesinstalled*Remember,theseare*SAMPLE*configfiles.Youllneedtoreadthedocumentationformoreinforma
9、tiononhowtoactuallydefineservices,hosts,etc.tofityourparticularneeds.安装nagios与别的软件安装稍有不同,nagios的安装要好几步才能完成。第一步执行makeinstall安装主要的程序、CGI及HTML文件第二步执行makeinstall-commandmode给外部命令访问nagios配置文件的权限第三步执行makeinstall-config把配置文件的例子复制到nagios的安装目录第三步执行makeinstall-init它的作用是把nagios做成一个运行脚本,使nagios随系统开机启动,这是一个很方便的措
10、施rootlocalhostnagios-3.2.1#ll/usr/local/nagios/total24drwxrwxr-x2nagiosnagios4096May1600:14bindrwxrwxr-x3nagiosnagios4096May1600:15etcdrwxrwxr-x2nagiosnagios4096May1600:14libexecdrwxrwxr-x2nagiosnagios4096May1600:14sbindrwxrwxr-x9nagiosnagios4096May1600:14sharedrwxrwxr-x5nagiosnagios4096May1600:15va
11、r各文件夹的作用如下:binNagios执行程序所在目录,这个目录只有一个文件nagiosetcNagios配置文件位置,初始安装完后,只有几个*.cfg-sample文件sbinNagiosCgi文件所在目录,也就是执行外部命令所需文件所在的目录ShareNagios网页文件所在的目录VarNagios日志文件、spid等文件所在的目录此时Libexec文件夹里没有任何文件,下面安装插件2.4编译并安装nagios插件nagiospluginsroot#tarzxvfnagiosplugins1.4.13.tar.gzroot#cdnagiosplugins1.4.13root#./conf
12、igurewithnagiosuser=nagioswithnagiosgroup=nagiosperfix=/usr/local/nagiosroot#make&makeinstall验证:root#ls/usr/local/nagios/libexec会显示安装的插件文件,即所有的插件都安装在libexec这个目录下三.安装配置web服务器3.1安装apacheWeb服务不是nagios所必须的,但是如果nagios没有web,查看监控对象的对象将非常费事和没有趣味的事情(只有通过查看nagios的日志来判断状态)。我不愿干特无聊的事,所以就花少许时间把web安装一下。在unix/linu
13、x世界,apache是web服务器的首选对象,其下载网站为www.apache.org。建议下载源码。因为我们不需要很复杂的web功能,因此简单的执行一下几个步骤就可以正确的把apache安装到系统:rootlocalhost#tarzxvfhttpd-2.2.0.tar.gzrootlocalhost#cdhttpd-2.2.0rootlocalhost#./configure-prefix=/usr/local/apache-enable-modules=all-enable-sorootlocalhost#make&makeinstall。安装完成后,执行/usr/local/apach
14、e/bin/apachectl-t检查一下apache是否正确安装。rootlocalhost#/usr/local/apache/bin/apachectl-tSyntaxOKrootlocalhosthttpd-2.2.15#usermod-Gnagioscmdapache添加到管理组里3.2启动、停止以及重启服务器启动服务器,在安装目录的子目录下,如/usr/local/apache/bin,输入以下命令即可:rootlocalhosthttpd-2.2.15#./apachectlstart此时,你服务器端窗口应显示如下:apachectlstart:httpdstarted此时,可以
15、在客户端用浏览器输入服务器的IP地址,IE应该显示如下画面:Itworks!停止服务器,只需在相同的目录下输入以下命令:rootlocalhosthttpd-2.2.15#./apachectlstop重新启动服务器,可输入以下命令:rootlocalhost#./apachectlgracefulapachectlgraceful:httpdgracefullyrestarted或输入如下命令:rootlocalhost#./apachectlrestartedapachectlrestart:httpdrestartedrootlocalhost#cp/usr/local/apache/b
16、in/apachectl/etc/rc.d/init.d/httpd这样就可以实用servicehttpdStart了3.3配置WEB接口方法一:直接在安装nagios时makeinstallwebconf创建一个nagiosadmin的用户用于Nagios的WEB接口登录。记下你所设置的登录令,一会儿你会用到它。htpasswdc/usr/local/nagios/etc/htpasswd.usersnagiosadmin重启Apache服务以使设置生效。servicehttpdrestart方法二:在httpd.conf最后添加如下内容:#settingfornagios20090325#
17、settingbybbs.linuxtone.orgScriptAlias/nagios/cgi-bin/usr/local/nagios/sbinOptionsExecCGIAllowOverrideNoneOrderallow,denyAllowfromallAuthNameNagiosAccessAuthTypeBasicAuthUserFile/usr/local/nagios/etc/htpasswdRequirevalid-userAlias/nagios/usr/local/nagios/shareOptionsNoneAllowOverrideNoneOrderallow,de
18、nyAllowfromallAuthNameNagiosAccessAuthTypeBasicAuthUserFile/usr/local/nagios/etc/htpasswdRequirevalid-user#root#/usr/local/apache/bin/htpasswd-c/usr/local/nagios/etc/htpasswdhuangfNewpassword:Re-typenewpassword:Addingpasswordforuserhuangf注:htpasswdc表示追加,否者是覆盖写入。查看认证文件的内容rootlocalhost#less/usr/local/
19、nagios/etc/htpasswdhuangf:aDQkEaFyJwpb6本例添加的是huangf用户名,需要改cgi.cfg配置文件,允许test用户在网页添加机器authorized_for_system_information=huangfauthorized_for_configuration_information=huangfauthorized_for_system_commands=huangfauthorized_for_all_services=huangfauthorized_for_all_hosts=nagiosadmin,huangfauthorized_for
20、_all_service_commands=huangfauthorized_for_all_host_commands=huangf如果有多用户,请用逗号格开3.4安装PHP如果不支持PHP还要安装php安装php5,php5必须有libxml2支持!安装libxml2#tar-zvxflibxml2-(version).tar.gz#cdlibxml2-(version)#./configure-prefix=/usr/local/libxml2#make#makeinstall安装PHP5#tarzvxfphp-4.3.4.tar.gz#cdphp-4.3.4#./configure-w
21、ith-apxs2=/usr/local/apache/bin/apxs-prefix=/usr/local/php+-+|License:|ThissoftwareissubjecttothePHPLicense,availableinthis|distributioninthefileLICENSE.Bycontinuingthisinstallation|process,youareboundbythetermsofthislicenseagreement.|Ifyoudonotagreewiththetermsofthislicense,youmustabort|theinstalla
22、tionprocessatthispoint.|+-+#make#maketest#makeinstall3.5重新配置apache2让他支持php#cd/usr/local/apache2/conf#vimhttpd.conf#添加以下LoadModulephp5_modulemodules/libphp5.soAddTypeapplication/x-httpd-php.phpAddTypeapplication/x-httpd-php.htmlAddTypeapplication/x-httpd-php-source.phps没有这个可能会报错OK,基本的安装已经完成.通过网址访问以下h
23、ttp:/203.212.5.19/nagios,没有问题就OK3.6启动nagios测试apache把Nagios加入到服务列表中以使之在系统启动时自动启动chkconfigaddnagioschkconfignagioson验证Nagios的样例配置文件/usr/local/nagios/bin/nagiosv/usr/local/nagios/etc/nagios.cfg如果没有报错,可以启动Nagios服务servicenagiosstart测试登录http:/localhost/nagios/输入用户名和密码就可以正常登录了四、安装客户端NRPE4.1安装客户端nagios插件tar
24、zxvfnagiosplugins*.tar.gzcdnagiosplugins*./configure-prefix=/usr/local/nagiosmakemakeinstalluseraddnagioschownnagios.nagios/usr/local/nagioschownRnagios.nagios/usr/local/nagios/libexec4.2给客户端安装nrperootlocalhost#tarzxvfnrpe*.tar.gzrootlocalhost#cdnrpe2.8.1rootlocalhost#./configure(会自动加载SSL)#如果后面make报
25、错,加如下参数rootlocalhostnrpe-2.12#rpm-qa|grepopensslopenssl-devel-0.9.8b-8.3.el5openssl-0.9.8b-8.3.el5rootlocalhostnrpe-2.12#rpm-qlopenssl-devel-0.9.7a-43.17.el4_6.1|morerootlocalhostnrpe-2.12#./configure-enable-ssl-with-ssl-lib=/usr/lib/(当然前提要有openssl)rootlocalhost#makeallrootlocalhost#makeinstallplugi
26、nrootlocalhost#makeinstalldaemonrootlocalhost#makeinstalldaemonconfigroot203nrpe-2.12#/usr/local/nagios/bin/nrpe-c/usr/local/nagios/etc/nrpe.cfg-droot203nrpe-2.12#/usr/local/nagios/libexec/check_nrpe-H127.0.0.1NRPEv2.124.3配置nrpe客户端添加nrpe服务器地址,允许服务器对其读取信息allowed_hosts=127.0.0.1,203.212.5.19增加command字
27、段来添加要监控的服务commandcheck_users=/usr/local/nagios/libexec/check_users-w1-c3commandcheck_load=/usr/local/nagios/libexec/check_load-w15,10,5-c30,25,20commandcheck_disk=/usr/local/nagios/libexec/check_disk-w20%-c10%-p/commandcheck_zombie_procs=/usr/local/nagios/libexec/check_procs-w5-c10-sZcommandcheck_to
28、tal_procs=/usr/local/nagios/libexec/check_procs-w150-c200commandcheck_ping=/usr/local/nagios/libexec/check_ping-H203.212.6.17-w3,2%-c10,6%-p30重启nrperootlocalhostnagios#killall-9nrperootlocalhostnagios#/usr/local/nagios/bin/nrpe-c/usr/local/nagios/etc/nrpe.cfg-d五、安装服务器端NRPE5.1检查是否有openssl,没有装要安装rootl
29、ocalhostnrpe-2.12#rpm-qa|grepopensslopenssl-0.9.8b-8.3.el5从上看没有装openssl-devel包,所以要装一个,装openssl-devel-0.9.8b-8.3.el5.i386.rpm是需要关联另外2个rpm包(e2fsprogs-devel-1.39-8.el5.i386.rpmkrb5-devel-1.5-17.i386.rpm)rootlocalhost#rpm-ivhe2fsprogs-devel-1.39-8.el5.i386.rpmwarning:e2fsprogs-devel-1.39-8.el5.i386.rpm:
30、HeaderV3DSAsignature:NOKEY,keyID37017186Preparing.#100%1:e2fsprogs-devel#100%root#rpmivhkrb5-devel-1.5-17.i386.rpmwarning:krb5-devel-1.5-17.i386.rpm:HeaderV3DSAsignature:NOKEY,keyID37017186Preparing.#100%1:krb5-devel#100%rootlocalhost#rpm-ivhopenssl-devel-0.9.8b-8.3.el5.i386.rpmwarning:openssl-devel
31、-0.9.8b-8.3.el5.i386.rpm:HeaderV3DSAsignature:NOKEY,keyID37017186Preparing.#100%1:openssl-devel#100%rootlocalhost#rpm-qlopenssl-devel-0.9.7a-43.17.el4_6.1|more(查找lib目录)5.2安装NRPErootlocalhost#./configure-prefix=/usr/local/nagios-enable-ssl-with-ssl-lib=/usr/lib/rootlocalhost#makeallrootlocalhost#make
32、installpluginrootlocalhost#makeinstalldaemonrootlocalhost#makeinstalldaemonconfig5.3commands.cfg定义外部构件nrperootlocalhostnrpe-2.12#vi/usr/local/nagios/etc/objects/commands.cfg#添加checknrpedefinecommandcommand_namecheck_nrpecommand_line$USER1$/check_nrpe-H$HOSTADDRESS$-c$ARG1$5.4配置要监控的linux主机rootlocalho
33、stnrpe-2.12#vi/usr/local/nagios/etc/nagios.cfg#修改成如下#cfg_file=/usr/local/nagios/etc/objects/localhost.cfgcfg_dir=/usr/local/nagios/etc/serversrootlocalhostnrpe-2.12#mkdir/usr/local/nagios/etc/servers/rootlocalhostnrpe-2.12#chown-Rnagios.nagios/usr/local/nagios/etc/servers/5.5新建6-20.cfg设置监控内容rootloca
34、lhostnrpe-2.12#vi/usr/local/nagios/etc/servers/6-20.cfg1definehost2uselinux-server3host_name6.204alias6.205address203.212.6.2067defineservice8usegeneric-service9host_name6.2010service_descriptioncheck-disk11check_commandcheck_nrpe!check_disk121314defineservice15usegeneric-service16host_name6.2017ser
35、vice_descriptioncheck-users18check_commandcheck_nrpe!check_users19rootlocalhostnrpe-2.12#/usr/local/nagios/bin/nagios-v/usr/local/nagios/etc/nagios.cfg如果没有错误就可以启动nagios了,服务端不用启用NRPEdaemon进程。5.6启动与停止nagios为验证你配置,运行Nagios带命令行参数v,象这样:/usr/local/nagios/bin/nagios-v/usr/local/nagios/etc/nagios.cfg初始化脚本:最
36、简单的启动Nagios守护进程的方式是使用初始化脚本,象这样:/etc/rc.d/init.d/nagiosstart手工方式:你可以手动地启动Nagios守护进程,用命令参数d,象这样:/usr/local/nagios/bin/nagiosd/usr/local/nagios/etc/nagios.cfg重启动Nagios当你修改了配置文件并想使之生效的话,重启动或重载入动作是必须的。初始化脚本:最简单地重启动Nagios守护进程的方式是使用初始化脚本,象这样:/etc/rc.d/init.d/nagiosreload评估启动时间/usr/local/nagios/bin/nagioss/
37、usr/local/nagios/etc/nagios.cfg六、通过WEB管理nagios也可以通过nagios的web页面重启服务和添加监控,设置如下:1、apache的运行用户添加到nagioscmd组里2、nagios.cfg里command_file=/usr/local/nagios/var/rw/nagios.cmd3、nagios.cfg里的nagiosgroup设置成nagioscmd3、chmod777-R/usr/local/nagios/var/rw即可用web配置nagios七、Nagios报警设置7.1网页报警1、修改cgi.cfg去掉下面几行注释host_unre
38、achable_sound=hostdown.wavhost_down_sound=hostdown.wavservice_critical_sound=critical.wavservice_warning_sound=warning.wavservice_unknown_sound=warning.wavnormal_sound=noproblem.wav2、在/usr/local/nagios/share/media/应该有相应的声音文件(这个默认没有的,需要自己添加)7.2通过sendmail发送报警一、安装sendmail#wget#tarzxvfsendEmail-v1.55.ta
39、r.gz#cdsendEmail-v1.56#cpsendEmail/usr/local/bin#chmod0755/usr/local/bin/sendEmail#./usr/local/bin/sendEmailfnagiostmailu“fromnagios”xunagiosxpscomdmnagiostest-f表示发送者的邮箱-t表示接收者的邮箱-s表示SMTP服务器的域名或者ip-u表示邮件的主题-xu表示SMTP验证的用户名-xp表示SMTP验证的密码(注意,这个密码貌似有限制,例如我用d!5neyland就不能被正确识别)-m表示邮件的内容二、测试这里使用系统自带的sendma
40、il测试rootlocalhost#mail-sThisissersnagiostestmaifei_20018install.log如果能收到邮件则设置正常三、修改commands.cfgdefinecommandcommand_namenotify-host-by-emailcommand_line/usr/bin/printf%b*Nagios*nnNotificationType:$NOTIFICATIONTYPE$nHost:$HOSTNAME$nState:$HOSTSTATE$nAddress:$HOSTADDRESS$nInfo:$HOSTOUTPUT$nnDate/Time:
41、$LONGDATETIME$n|/bin/mail-s*$NOTIFICATIONTYPE$HostAlert:$HOSTNAME$is$HOSTSTATE$*$CONTACTEMAIL$#notify-service-by-emailcommanddefinitiondefinecommandcommand_namenotify-service-by-emailcommand_line/usr/bin/printf%b*Nagios*nnNotificationType:$NOTIFICATIONTYPE$nnService:$SERVICEDESC$nHost:$HOSTALIAS$nAddress:$HOSTADDRESS$nState:$SERVICESTATE$nnDate/Time:$LONGDATETIME$nnAdditionalInfo:nn$SERVICEOUTPUT$|/bin/mail-s*$NOTIFICATIONTYPE$ServiceAlert:$HOSTALIAS$/$SERVICEDESC$is$SERVICESTATE$*$CONTACTEMAIL$四、编辑contact.cfg,添加邮件通知definecontactcontact_namenagiosadminuse