本文是小弟整合几位大神的文章,见笑@@

Linux利用sendmail和fetion发送报警通知

nagios监控windows主机 && linux主机

CentOS 5/6.X 使用 EPEL YUM源:


实验环境:

两台64位的centos-6.6

一台window-server-2003


安装网络yum源:

centos5.x--32位[root@centos-server~]#wgethttp://mirrors.yun-idc.com/epel/5/i386/epel-release-5-4.noarch.rpmcentos5.x--64位[root@centos-server~]#wgethttp://mirrors.hust.edu.cn/epel//5/x86_64/epel-release-5-4.noarch.rpmcentos6.x--32位[root@centos-server~]#wgethttp://download.fedoraproject.org/pub/epel/6/i386/epel-release-6-8.noarch.rpmcentos6.x--64位[root@centos-server~]#wgethttp://mirrors.hust.edu.cn/epel//6/x86_64/epel-release-6-8.noarch.rpm[root@centos-server~]#rpm-ivhepel-release-6-8.noarch.rpm


搭建nagios监控服务器:

yuminstallhttpdphp*opensslopenssl-develnagiosnagios-plugins*setenforce0iptables-IINPUT-ptcp--dport80-jACCEPT增加httpd的访问验证[root@centos-server~]#htpasswd-c/etc/nagios/htpasswdnagiosadminNewpassword:Re-typenewpassword:Addingpasswordforusernagiosadmin注意nagios的默认用户就是nagiosadmin,如果你使用的不是此用户,需要修改以下项目[root@centos-serverobjects]#cat/etc/nagios/cgi.cfg|grepnagiosadminauthorized_for_system_information=nagiosadminauthorized_for_configuration_information=nagiosadminauthorized_for_system_commands=nagiosadminauthorized_for_all_services=nagiosadminauthorized_for_all_hosts=nagiosadminauthorized_for_all_service_commands=nagiosadminauthorized_for_all_host_commands=nagiosadmin快速修改命令[root@centos-server~]#sed-i's/nagiosadmin/dragon/g'/etc/nagios/cgi.cfg通过httpd访问nagios的web文件[root@centos-serverobjects]#cat>>/etc/httpd/conf/httpd.conf<<ENDscriptAlias/nagios/cgi-bin"/usr/lib64/nagios/cgi-bin/"<Directory"/usr/lib64/nagios/cgi-bin/">AllowOverrideAuthConfigOptionsExecCGIOrderallow,denyAllowfromallAuthUserFile/etc/nagios/htpasswd</Directory>Alias/nagios/usr/share/nagios/html/<Directory"/usr/share/nagios/html/">OptionsNoneAllowOverrideAuthConfigOrderallow,denyAllowfromallAuthUserFile/etc/nagios/htpasswd</Directory>END


定义监控LINUX文件:

[root@centos-serverobjects]#mkdir/etc/nagios/objects/server[root@centos-serverobjects]#vim/etc/nagios/nagios.cfgcfg_dir=/usr/local/nagios/etc/servers[root@centos-serverobjects]#vim/etc/nagios/objects/server/192.168.150.143.cfgdefinehost{uselinux-serverhost_name192.168.150.143aliaslinuxaddress192.168.150.143}defineservice{usegeneric-servicehost_name192.168.150.143service_descriptionCheckSwapcheck_commandcheck_nrpe!check_swap}defineservice{usegeneric-servicehost_name192.168.150.143service_descriptiontotalcheck_commandcheck_nrpe!check_total_procs}defineservice{usegeneric-servicehost_name192.168.150.143service_descriptioncheck_loadcheck_commandcheck_nrpe!check_load}defineservice{usegeneric-servicehost_name192.168.150.143service_descriptioncheck_tcp_3306check_commandcheck_tcp!3306}defineservice{usegeneric-servicehost_name192.168.150.143service_descriptioncheck_userscheck_commandcheck_nrpe!check_users}defineservice{usegeneric-servicehost_name192.168.150.143service_descriptioncheck_mysqlcheck_commandcheck_nrpe!check_mysql}defineservice{usegeneric-servicehost_name192.168.150.143service_descriptioncheck_http192.168.150.143/index.htmlcheck_commandcheck_http!'-u/index.html'}defineservice{usegeneric-servicehost_name192.168.150.143service_descriptioncheck_diskcheck_commandcheck_nrpe!check_sda1}在commonds.cfg中添加check_nrpe的命令[root@centos-serverobjects]#vim/etc/nagios/objects/commands.cfgdefinecommand{command_namecheck_nrpecommand_line$USER1$/check_nrpe-H$HOSTADDRESS$-c$ARG1$}


配置linux客户端:

[root@centos-server~]#yuminstallnagios-plugins*openssl-developensslmysql-server[root@centos-server~]#cat/etc/nrpe.d/nrpeonly_from=127.0.0.1192.168.150.139[root@centos-serverobjects]#vim/etc/nagios/nrpe.cfg添加nagios监控端的ipallowed_hosts=127.0.0.1,192.168.150.139添加以下内容command[check_users]=/usr/lib64/nagios/plugins/check_users-w5-c10command[check_load]=/usr/lib64/nagios/plugins/check_load-w15,10,5-c30,25,20command[check_sda1]=/usr/lib64/nagios/plugins/check_disk-w20%-c10%-p/dev/sda1command[check_zombie_procs]=/usr/lib64/nagios/plugins/check_procs-w5-c10-sZcommand[check_total_procs]=/usr/lib64/nagios/plugins/check_procs-w150-c200command[check_mysql]=/usr/lib64/nagios/plugins/check_mysql-H127.0.0.1-ubbq-pqwe123#此用户需要在mysql中添加command[check_swap]=/usr/lib64/nagios/plugins/check_swap-w20%-c10%

通过yum安装的nagios监控插件还有很多,有需要用到的可以研究下:


两端重启服务:

servicenagiosrestartservicehttpdrestartservicenrperestartservicexinetdrestart[root@centos-server~]#netstat-anl|grep5666tcp000.0.0.0:56660.0.0.0:*LISTENtcp00:::5666:::*LISTEN在nagios监控端测试被监控端nrpe联通:[root@centos-server~]#/usr/lib64/nagios/plugins/check_nrpe-H192.168.150.143NRPEv2.15



监控window-server:

[root@centos-serverobjects]#vim/etc/nagios/objects/server/192.168.150.146.cfgdefinehost{usewindows-serverhost_name192.168.150.146aliasmycomputeraddress192.168.150.146}defineservice{usegeneric-servicehost_name192.168.150.146service_descriptionNSClient++Versioncheck_commandcheck_win!CLIENTVERSION}defineservice{usegeneric-servicehost_name192.168.150.146service_descriptionUptimecheck_commandcheck_win!UPTIME}defineservice{usegeneric-servicehost_name192.168.150.146service_descriptionCPULoadcheck_commandcheck_win!CPULOAD!-l5,80,90}defineservice{usegeneric-servicehost_name192.168.150.146service_descriptionMemoryUsagecheck_commandcheck_win!MEMUSE!-w80-c90}defineservice{usegeneric-servicehost_name192.168.150.146service_descriptionC:\DriveSpacecheck_commandcheck_win!USEDDISKSPACE!-lc-w80-c90}defineservice{usegeneric-servicehost_name192.168.150.146service_descriptionD:\DriveSpacecheck_commandcheck_win!USEDDISKSPACE!-ld-w80-c90}defineservice{usegeneric-servicehost_name192.168.150.146service_descriptionE:\DriveSpacecheck_commandcheck_win!USEDDISKSPACE!-le-w80-c90}defineservice{usegeneric-servicehost_name192.168.150.146service_descriptionExplorercheck_commandcheck_win!PROCSTATE!-dSHOWALL-lExplorer.exe}[root@centos-serverobjects]#vim/etc/nagios/objects/commands.cfgdefinecommand{command_namecheck_wincommand_line$USER1$/check_nt-H"$HOSTADDRESS$"-p12489-sdianyi123-v$ARG1$$ARG2$}


测试配置文件:

[root@centos-serverobjects]#/usr/sbin/nagios-v/etc/nagios/nagios.cfgError:Couldnotfindanyhostgroupmatching'windows-servers'(configfile'/etc/nagios/objects/server/192.168.150.146.cfg',startingonline1)#这里我遇到这个错误了,把/etc/nagios/objects/templates.cfg里window-server模版主机中以下这项注释就可以了#hostgroupswindows-servers;重启以下服务[root@centos-serverobjects]#servicenagiosrestart


配置window安装NSClient++:

1.

2.

3.


看到监控结果:


邮件报警设置:

引用郑彦生的原文1、停止系统自带的sendmail服务#/etc/init.d/postfixstop2、安装sendmail和mailx#yum-yinstallsendmailmailx3、修改sendmail配置文件#cp/etc/mail.rc/etc/mail.rc.bak#cat>>/etc/mail.rc<<EOFsetfrom=13260071987@139.comsmtp=smtp.139.comsetsmtp-auth-user=13260071987@139.comsmtp-auth-password=yi15093547036smtp-auth=loginEOF4、重新启动sendmail服务#servicesendmailrestart5.修改联系人[root@centos-serverobjects]#vim/etc/nagios/objects/contacts.cfgdefinecontact{contact_namenagiosadminusegeneric-contactaliasNagiosAdminemail18507656225@139.com}


查看邮件: