|
一、准备工作:1、更换Yum源163cd /etc/yum.repos.d/mv CentOS-Base.repo{,.bak}wget http://mirrors.163.com/.help/CentOS-Base-163.repoyum makecache2、LAMP环境优化编译安装,都加了简单优化选项,可以直接当LAMP环境的网站使用apache安装cd /usr/local/srcwget http://mirrors.sohu.com/apache/httpd-2.2.21.tar.bz2yum -y install openssl openssl-develtar jxvf httpd-2.2.21.tar.bz2cd httpd-2.2.21./configure --prefix=/usr/local/apache2 --enable-so --enable-ssl --enable-rewrite --enable-deflate --enable-expiresmake && make installcd ..为了方便以后安装其它需要sql的监控服务,装上mysqlwget http://mirrors.sohu.com/mysql/MySQL-5.1/mysql-5.1.57.tar.gzgroupadd mysqluseradd mysql -g mysql -s /sbin/nologintar zxvf mysql-5.1.57.tar.gzyum -y install ncurses ncurses-devel./configure --prefix=/usr/local/mysql --localstatedir=/var/lib/mysql --sysconfdir=/etc --enable-thread-safe-client --with-client-ldflags=-all-static --with-mysqld-ldflags=-all-static --with-unix-socket-path=/tmp/mysql.sock --enable-assembler --without-debug --with-plugins=utf8,gbk --with-plugins=innobasemake && make installcp my-small.cnf /etc/my.cnfcp mysql.server /etc/init.d/mysql.serverchmod 755 /etc/init.d/mysql.servervi /etc/init.d/mysql.server46行 basedir=/usr/local/mysql47行 datadir=/usr/local/mysql/varcd /usr/local/mysql/chown -R mysql.mysql .bin/mysql_install_db --user=mysql --basedir=/usr/local/mysql --datadir=/usr/local/mysql/varchown -R root .chown -R mysql /usr/local/mysql/varchkconfig --add mysql.serverchkconfig mysql.server on/etc/init.d/mysql.server startecho "export PATH=$PATH:/usr/local/mysql/bin" >>/etc/profilesource /etc/profilenagios3以后版本需要php的支持PHP的安装wget http://mirrors.sohu.com/php/php-5.3.6.tar.bz2因为需要有图片产生,所以先需要php的gd库支持wget ftp://217.146.241.3/pub/linux/lib/gd-2.0.33.tar.gzyum -y install libjpeg libjpeg-devel libpng libpng-devel freetype freetype-devel libxml2 libxml2-devel libXpm libXpm-develtar zxvf gd-2.0.33.tar.gzcd gd-2.0.33./configure --prefix=/usr/local/gd2 --with-png --with-freetype --with-jpegmake && make install编译phptar jxvf php-5.3.6.tar.bz2cd php-5.3.6./configure --prefix=/usr/local/php5 --with-apxs2=/usr/local/apache2/bin/apxs --with-mysql=/usr/local/mysql --with-mysql-sock=/tmp/mysql.sock --enable-mbstring=cn --enable-force-cgi-redirect --enable-ftp -with-gd -with-jpeg -with-zlib -with-png -with-freetype --disable-debug --enable-inline-optimization --enable-sockets --enable-bcmathmake && make install增加php与apache的关联vi /usr/local/apache2/conf/httpd.conf确认有这样一行LoadModule php5_module modules/libphp5.so添加:AddType application/x-httpd-php .php修改apache的运行用户和组为nagiosphpinfo测试下是否成功nagios的安装wget http://cdnetworks-kr-2.dl.sourceforge.net/project/nagios/nagios-3.x/nagios-3.2.3/nagios-3.2.3.tar.gzwget http://cdnetworks-kr-2.dl.sourceforge.net/project/nagiosplug/nagiosplug/1.4.15/nagios-plugins-1.4.15.tar.gzwget http://cdnetworks-kr-1.dl.sourceforge.net/project/nagios/nrpe-2.x/nrpe-2.12/nrpe-2.12.tar.gzgroupadd nagiosuseradd nagios -g nagiostar zxvf nagios-3.2.3.tar.gzcd nagios-3.2.3./configure --prefix=/usr/local/nagiosmkdir -p /etc/httpd/conf.d/make all //编译make install //安装nagios主要程序,cgi和html文件等make install-init //安装nagios启动脚本make install-config //将配置文件的例子复制到nagios配置文件里make install-commandmode //配置nagios目录权限。make install-webconf 因为这个命令执行的时候,会在/etc/httpd/conf.d/创建 nagios.conf文件cat /etc/httpd/conf.d/nagios.conf >>/usr/local/apache2/conf/httpd.conf安装nagios插件tar zxvf nagios-plugins-1.4.15.tar.gzcd nagios-plugins-1.4.15./configure --prefix=/usr/local/nagios/make && make install安装完成后,会在/usr/local/nagios/ 目录下面生成libexe插件目录,nagios所有的插件都会在这个目录里面。chown -R nagios.nagios /usr/local/nagios/安装nrpetar zxvf nrpe-2.12.tar.gzcd nrpe-2.12./configure && make allmake install-plugin //安装check_nrpe插件make install-daemonmake install-daemon-configmake install-xinetd //安装xinetd脚本yum -y install xinetdvi /etc/xinetd.d/nrpeonly_from = 127.0.0.1 注释掉 allowshosts =192.168.1.221 添加监控主机,一般为nagios服务器的Ipvi /etc/services添加两行nrpe 5666/tcpnrpe 5666/udp启动xinetd/etc/init.d/xinetd start 看看5666端口是否开启添加apache授权用户 -c选项为创建文件,创建第二个选项不需要/usr/local/apache2/bin/htpasswd -c /usr/local/nagios/etc/htpasswd.users itv1修改cgi.cfg #vi /usr/local/nagios/etc/cgi.cfg找到这几行,后面添加 erbin用户,可以自己定义用户,使用“,”隔开authorized_for_system_information=nagiosadmin,itv1authorized_for_configuration_information=nagiosadmin,itv1authorized_for_system_commands=nagiosadmin,itv1authorized_for_all_services=nagiosadmin,itv1authorized_for_all_hosts=nagiosadmin,itv1authorized_for_all_service_commands=nagiosadmin,itv1authorized_for_all_host_commands=nagiosadmin,itv1/usr/local/nagios/bin/nagios -v /usr/local/nagios/etc/nagios.cfg测试下,如果没有eror就可以启动了,/usr/local/nagios/bin/nagios -d /usr/local/nagios/etc/nagios.cfg启动就行了重新启动下apache,然后通过http://IP/nagios访问,输入用户名密码如果成功登陆,到此安装完成。三、使用现在跟着我的步骤走,不要怀疑,坚决执行吧哈哈cd /usr/local/nagios/etc/objects/ls看一下,看下就可以哈,然后rm -f contacts.cfg printer.cfg switch.cfg templates.cfg windows.cfg###如果你是头次安装,建议看下这些文件内容contacts.cfg ------默认联系人配置文件printer.cfg ------默认监控打印机的配置文件switch.cfg ------默认监控交换机的配置问及爱你templates.cfg------默认的模板文件windows.cfg ------默认监控windows的配置文件commands.cfg ------命令的配置文件timeperiods.cfg----时间配置文件进入正题:首先介绍下nagios的几个重要的项,就这么叫吧,哈哈host(主机)------hostgroup(主机组)service(服务)------servicegroup(服务组)contact(联系人) ------contactgroup(联系人组)address(监控主机的ip)service_description(定义检查服务的名称,在nagios页面上显示的)check_command(检查命令) 好了,了解这些足够了,来一点点建立你自己的nagios1、建立个模板文件,名字随意.cfg,就叫moban.cfg吧通俗易懂1 define contact{2 name generic-contact---模板名称3 #联系组名称4 service_notification_period 24x7---和下面一项是在timeperiods.cfg定义的,自己看下就明白5 #服务报警周期6 host_notification_period 24x77 #主机报警周期8 service_notification_options w,u,c,r,s9 #服务通知选项10 host_notification_options d,u,r,s11 #主机通知选项12 service_notification_commands notify-service-by-sendEmail ----command.cfg中定义,下面说13 #服务通知方式14 host_notification_commands notify-host-by-sendEmail15 #主机通知方式16 register 017 }1819 define host{20 name generic-host----足迹模板名称21 #服务器名称22 notifications_enabled 123 #开启主机通知24 event_handler_enabled 125 #开启时间处理26 flap_detection_enabled 127 #启用状态抖动检测28 failure_prediction_enabled 129 #启用故障预测30 process_perf_data 131 #启用进程性能数据记录32 retain_status_information 133 #启用状态信息保存功能34 retain_nonstatus_information 135 #启用非状态信息保存功能36 notification_period 24x737 #报警的时间周期 timeperiods.cfg配置38 register 039 check_interval 140 #正常检测的时间间隔(单位:分)41 retry_interval 242 #重试检测间隔时间43 max_check_attempts 344 #最大尝试次数45 check_command check-host-alive----在command.cfg中定义的46 #检查主机存货(command.cfg中定义)47 notification_period 24x748 #监控周期49 notification_interval 12050 #同一问题的报警间隔51 notification_options d,u,r52 #主机通知报警选项 d--down, u--unknown, r--recover53 contact_groups SMC----自己定义的联系人组,下面定义54 }5556 define service{57 name generic-service58 active_checks_enabled 159 passive_checks_enabled 160 parallelize_check 161 obsess_over_service 162 check_freshness 063 notifications_enabled 164 event_handler_enabled 165 flap_detection_enabled 166 failure_prediction_enabled 167 process_perf_data 168 retain_status_information 169 retain_nonstatus_information 170 is_volatile 071 check_period 24x772 max_check_attempts 373 normal_check_interval 274 retry_check_interval 175 notification_options w,u,c,r76 notification_interval 6077 notification_period 24x778 register 079 contact_groups SMC80 } 2、建立个联系人文件,还叫contact.cfg吧define contact{contact_name duty1---联系人名称use generic-contact---关联generic-contact,在上面定义了alias duty1---别名email 138xxxxxxxx7@139.com ---邮件地址} define contact{contact_name duty2use generic-contactalias duty2email 138xxxxxxxx7@139.com}define contact{contact_name duty3use generic-contactalias duty1email 138xxxxxxxx7@139.com} define contact{contact_name duty4use generic-contactalias duty2email 138xxxxxxxx7@139.com} define contactgroup {---定义联系组contactgroup_name SMC---组名alias SMC---组别名members duty1,duty2---成员,必须为已经定义的联系人} define contactgroup {contactgroup_name SMC2alias SMC2members duty3,duty4} 两个小问题解释下:1、use generic-contact的作用,关联此模板的所有已定义内容到此define contact中,省去很多笔墨以duty2为例,完整的就是define contact{contact_name duty2use generic-contactalias duty2email 138xxxxxxxx7@139.comservice_notification_period 24x7host_notification_period 24x7service_notification_options w,u,c,r,shost_notification_options d,u,r,sservice_notification_commands notify-service-by-sendEmail host_notification_commands notify-host-by-sendEmailregister 0}还一点,定义单个{}与模板中相同参数的优先性 :自定义参数 > 模板中相同参数,也就是说模板的参数是缺省值2、报警方式(139短信)现在支持的报警方式很多,后面会详细说,这里就介绍下报警的过程。nagios产生监控状态硬性变化,调用邮件功能,将报警信息发送至mail.139.com,监控人员开通139短信通知,会自动收到139发来的短信 3、command.cfg文件添加nrpe插件的命令# 'check_nrpe' command definitiondefine command{command_name check_nrpecommand_line /usr/local/nagios/libexec/check_nrpe -H $HOSTADDRESS$ -c $ARG1$} 介绍下我的报警发邮件sendEmail,一个perl写的命令行发邮件工具。在我的系统应用了一年多,非常出色下载wget http://caspian.dotconf.net/menu/Software/SendEmail/sendEmail-v1.56.tar.gz解开后,将可执行文件sendEmail复制到/usr/local/bin/目录并且修改为nagios可执行发个邮件测试一下/usr/local/bin/sendEmail -f old_hoodlum@163.com -t 138xxxxxxx -s smtp.163.com -u "test" -m "hello" -xu old_hoodlum -xp password -l /var/log/sendEmail.log-f --from-t --to-s 使用的smtp域名-u 标题-m 内容-xu smtp登陆用户名-xp smtp登陆密码建议,nagios报警尽量不要使用本机sendmail,在大网站注册个免费邮箱发去吧,也减少了很多麻烦,比如sendmail问题导致的报警不能及时到达以下为command中定义的发邮件命令,根据自己的实际情况修改command.cfg中加入define command{command_name notify-host-by-sendEmailcommand_line /usr/bin/printf "%b" "***** Nagios-BJ *****\n\nNotification Type: $NOTIFICATIONTYPE$\nHost: $HOSTNAME$\nState: $HOSTSTATE$\nAddress: $HOSTADDRESS$\nInfo: $HOSTOUTPUT$\n\nDate/Time: $LONGDATETIME$\n" | /usr/local/bin/sendEmail -f old_hoodlum@163.com -t $CONTACTEMAIL$ -s smtp.163.com -u "** $NOTIFICATIONTYPE$ Host Alert: $HOSTNAME$ is $HOSTSTATE$ **" -xu old_hoodlum -xp xxxxxxxx -l /var/log/sendEmail.log} define command{command_name notify-service-by-sendEmailcommand_line /usr/bin/printf "%b" "***** Nagios-BJ *****\n\nNotification Type: $NOTIFICATIONTYPE$\n\nService: $SERVICEDESC$\nHost: $HOSTNAME$\nAddress: $HOSTADDRESS$\nState: $SERVICESTATE$\n\nDate/Time: $LONGDATETIME$\n\nAdditional Info:\n\n$SERVICEOUTPUT$\n" | /usr/local/bin/sendEmail -f old_hoodlum@163.com -t $CONTACTEMAIL$ -s smtp.163.com -u "** $NOTIFICATIONTYPE$ Service Alert: $HOSTNAME$/$SERVICEDESC$ is $SERVICESTATE$ **" -xu old_hoodlum -xp xxxxxxxx -l /var/log/sendEmail.log} 4、现在万事具备了,说最重点的主机、服务的监控策略假如,公司内网n个网段,192.168.x.0/24(x=1-8),每个x运行不同的业务,各有不同的系统,需要监控不同的服务,怎么规划呢?如果只写一个host.cfg和一个service.cfg的话,维护起来是非常困难的,我们可以这样建立1-x.cfg.....8-x.cfg,添加每个网段的主机,根据不同的业务及不同系统对主机分组,也可根据需要监控的内容将类似的主机分组,总而言之,怎么方便你少写字你就怎么来这就是开源的魅力,随心随意不必拘泥于形式,你可以给nagios定义100个配置文件,只要这么做方便你管理就好,写完了配置文件,只要在nagios.cfg文件加入你这个文件名就可以了,cfg_file=/usr/local/nagios/etc/objects/x.cfg定义service(service_group),一样的道理,监控同个服务的组就好了,这样做会省去你很多的时间,举个例子吧##定义主机define host{use generic-hosthost_name BJ-OUTPUT-sanlihealias BJ-OUTPUT-sanliheaddress 120.72.48.70} define host{use generic-hosthost_name BJ-GATEWAY-sanlihealias BJ-GATEWAY-sanliheaddress 120.72.48.1} define host{use generic-hosthost_name NA-GATEWAYalias NA-GATEWAYaddress 218.30.50.1}#定义单个主机服务define service {use generic-servicehost_name NA-GATEWAYmax_check_attempts 3normal_check_interval 1retry_check_interval 2contact_groups SMCservice_description PINGcheck_command check_ping!850.0,90%!1600.0,95%} define host{use generic-hosthost_name GW-dianxintongalias GW-dianxintongaddress 218.247.13.60} define host{use generic-hosthost_name BJ-dianxintongalias BJ-dianxintongaddress 218.247.13.62}#定义主机组define hostgroup{hostgroup_name itvalias itvmembers BJ-OUTPUT-sanlihe,BJ-GATEWAY-sanlihe,GW-dianxintong,BJ-dianxintong}#定义主机组服务define service{use generic-servicehostgroup_name itvmax_check_attempts 3normal_check_interval 1retry_check_interval 2contact_groups SMCservice_description PINGcheck_command check_ping!500.0,90%!1000.0,95%} 假如想用多种方式同时报警怎么办?在moban.cfg的service_notification_commands 和host....中添加多种报警方式就好了,中间用“,”隔开 想让每个人在不同时间以不同方式收到报警?联系人中定义不同报警方式和报警时间就行了 想让QQ给你报警?没戏,哈哈,发到QQ邮箱,如果QQ在线会自动弹出来的 nagios有多少种报警方式?我也不知道有多少种,我用过的有短信猫、email、飞信、qq、msn.... 都想用?当然可以,escalations会帮你完成你的需求 想让nagios帮你处理?可以,event_hander会做到,但是强烈建议在你不是非常精通你所要操作的服务时,请不要使用 怎么能让nagios检测到硬性变化后立刻报警?报警时间跟这几个参数有关,一看就能明白1、interval_length 在nagios.cfg文件731行左右,默认为60,单位s2、check_interval3、retry_interval4、max_check_attempts nagios分布式??假如你有100个nagios server,你想看看总况,总不能打开100个web页面吧?nagios分布式玩玩可以,个人建议不用 我想监控...用我们经理的一句话:“你能想到它就能做到”,知道nagios监控插件的0、1、2就行了,http://exchange.nagios.org/ 插件最多的地方 个人一些建议:(1)、nagios服务器强烈建议放在最安全的地方,在战争年代,它是一个哨兵,也是你的最后一道防线,如果监控挂了,服务器再挂了,你如何知道?(2)、报警方式,个人意见:短信猫>email(socket方式)>msn || qq || fetion,以稳为主(3)、每天定点在nagios server上给自己发个消息是个很好的习惯(4)、对于服务的检查次数,设置为3-4此比较合理,如果太短你可能会经历一次狼来了的故事,太长的话,你可能都不会知道问题的存在(5)、报警方式,如果有条件最好不要选择单种,自己设置好策略(6)、nagios本机的系统信息也很重要,可能一些时候的误报是因为主机系统资源紧张造成的,比如ping(7)、尽量不去监控无所谓的程序或者应用,尽量不去使用snmp收集数据(8)、总之、不要把nagios只当个辅助来对待,它是运维的尖锐利 本文出自 “搁浅” 博客,请务必保留此出处http://hoodlum.blog.运维网.com/4075497/743722
|
|
|