设为首页 收藏本站
查看: 1295|回复: 0

nagios搭建和邮件短信报警设置

[复制链接]

尚未签到

发表于 2015-9-8 12:18:24 | 显示全部楼层 |阅读模式
  Nagios搭建与139邮箱短信报警设置
  
  最近因为工作需要搭建nagios,初次接触 DSC0000.gif ,看了很多网上很多nagios的教程,历尽千辛万苦 DSC0001.gif 最终搭建了一个简单的 DSC0002.gif nagios,结合网上的教程我把自己做的配置贴出来,有错误欢迎指正   DSC0003.gif                      
  一:环境准备
  由于要搭建的nagios主机是一个裸机,所以搭建nagios监控系统前需要搭建一个简单的LAMP平台,
  这里我就直接用yum安装了
  
yum -y install http* mysql* php-*
  对于英语不好的我,每做完一步我就喜欢用echo $? 验证一下 看看返回值是不是0         如果是0说明操作正确,否则错误,这种方法对我很实用,谁让英语差呢!
  [iyunv@localhost ~]# service httpd start
  启动 httpd:                                               [确定]
  [iyunv@localhost ~]# chkconfig httpd on
  [iyunv@localhost ~]# vim  /etc/httpd/conf/httpd.conf
231 User apache
  232 Group apache
  391 DirectoryIndex index.php index.html index.html.var
  992   #setting for nagios
  993    ScriptAlias /nagios/cgi-bin "/usr/local/nagios/sbin"
  994   <Directory "/usr/local/nagios/sbin">
  995    AuthType Basic
  996     Options ExecCGI
  997     AllowOverride None
  998     Order allow,deny
  999     Allow from all
  1000    AuthName "nagios access"
  1001    AuthUserFile /usr/local/nagios/etc/htpasswd
  1002    Require valid-user
  1003    </Directory>
  1004    Alias /nagios "/usr/local/nagios/share"
  1005     <Directory "/usr/local/nagios/share">
  1006   AuthType Basic
  1007     Options ExecCGI
  1008    AllowOverride None
  1009  Order allow,deny
  1010     Allow from all
  1011    AuthName "nagios access"
  1012    AuthUserFile /usr/local/nagios/etc/htpasswd
  1013   Require valid-user
  1014     </Directory>
  创建apache目录的验证文件,mkdir -p /usr/local/nagios/etc/htpasswd
添加用户
  [iyunv@localhost ~]# htpasswd -c /usr/local/nagios/etc/htpasswd cainiao
  
  
  
  
  
二:nagios安装
  (1)创建nagios程序用户、组
[iyunv@localhost ~]# useradd -s /sbin/nologin nagios   
  [iyunv@localhost ~]# mkdir /usr/local/nagios/   
  [iyunv@localhost ~]# chown -R nagios:nagios /usr/local/nagios/
  
( 2) 安装和编译nagios
  搭建nagios所需要的软件已经放在/nagios
  [iyunv@localhost nagios]# tar zxf nagios-3.2.0.tar.gz
  [iyunv@localhost nagios]# cd nagios-3.2.0
  [iyunv@localhost nagios-3.2.0]# ./configure --prefix=/usr/local/nagios/
  [iyunv@localhost nagios-3.2.0]# make all ; make install ; make install-init ; make install-commandmode ; make install-config
  [iyunv@localhost nagios-3.2.0]# chkconfig --add nagios
  [iyunv@localhost nagios-3.2.0]# chkconfig nagios on
  
(3)安装nagios插件
[iyunv@localhost nagios]# tar zxf nagios-plugins-1.4.14.tar.gz
  [iyunv@localhost nagios]# cd nagios-plugins-1.4.14
  [iyunv@localhost nagios-plugins-1.4.14]# ./configure --prefix=/usr/local/nagios/ ; make ; make insta
  
(4)安装Nagios的汉化包
  [iyunv@localhost nagios]# tar jxf nagios-cn-3.2.0.tar.bz2
  [iyunv@localhost nagios]# cd nagios-cn-3.2.0
  [iyunv@localhost nagios-cn-3.2.0]# ./configure ; make all ; make install

  接下来通过浏览器验证nagios是否安装成功
  http:.//nagios的ip/nagios  这里就假如nagios的服务器的ip是:192.168.1.1
  
  
DSC0004.jpg
  
  
用户就是刚才新建的 cainiao  密码是刚才新建此用户的输入的
  
接下来就是难点了
  三:nagios服务器配置过程
  这里先说一下nagios的几个配置文件的作用
  Nagios默认有如下几个配置文件修改:
  # ll /usr/local/nagios/etc/objects
  commands.cfg 设定默认的指令来执行某个监控,也可以自己设定
  contacts.cfg 设定联系人,出问题时的联系人与联系组
  localhost.cfg 设定对本服务器的监控,配置其他服务时可参考此文件
  timeperiods.cfg 设定周一至周五7X24小时不间断,或自定义其他时间段
  hosts.cfg 设定被监控的主机(自己创建)
  services.cfg 设定被监控的服务(自己创建)
  (1) 定义hosts.cfg文件
  [iyunv@localhost ~]# cd /usr/local/nagios/etc/
  [iyunv@localhost etc]# vim hosts.cfg
  define host{
  use     linux-server
  host_name       web
  alias   benet-web
  address 192.168.1.100
  define host{
  use     linux-server
  host_name       mysql
  alias   benet-mysql
  address 192.168.1.200
  }
  define hostgroup{
  hostgroup_name  sa-servers
  alias   sa servers
  members web,mysql
  }
  
( 2 )定义services.cfg
  
################- yuanlin web -##########################
  define service{
  use local-service
  host_name web
  service_description PING
  check_command check_ping!100.0,20%!500.0,60%
  }
  define service{
  use local-service
  host_name web
  service_description SSH
  check_command check_ssh
  }
  define service{
  use local-service
  host_name web
  service_description SSHD
  check_command check_tcp!22
  }
  define service{
  use local-service
  host_name web
  service_description http
  check_command check_http
  }
  
####################-  yuanlin  MYSQL -##########################
  define service{
  use local-service
  host_name mysql
  service_description PING
  check_command check_ping!100.0,20%!500.0,60%
  }
  define service{
  use local-service
  host_name mysql
  service_description SSH
  check_command check_ssh
  }
  define service{
  use local-service
  host_name mysql
  service_description ftp
  check_command check_ftp
  }
  define service{
  use local-service
  host_name mysql
  service_description mysqlport
  check_command check_tcp!3306
  }
  
  (3) 定义cgi.cfg
  
[iyunv@localhost etc]# cd /usr/local/nagios/etc/
  [iyunv@localhost etc]# vim cgi.cfg
  107 default_user_name=benet
  119 authorized_for_system_information=nagiosadmin,cainiao
  131 authorized_for_configuration_information=nagiosadmin,cainiao
  144 authorized_for_system_commands=cainiao
  157 authorized_for_all_services=nagiosadmin,cainiao
  158 authorized_for_all_hosts=nagiosadmin,cainiao
  171 authorized_for_all_service_commands=nagiosadmin,cainiao
  172 authorized_for_all_host_commands=nagiosadmin,cainiao
  
  定义nagios.cfg
  [iyunv@localhost etc]# vim nagios.cfg
  19 log_file=/usr/local/nagios/var/nagios.log
  30 cfg_file=/usr/local/nagios/etc/hosts.cfg
  31 cfg_file=/usr/local/nagios/etc/services.cfg
  32 cfg_file=/usr/local/nagios/etc/commands.cfg
  33 cfg_file=/usr/local/nagios/etc/contacts.cfg
  34 cfg_file=/usr/local/nagios/etc/timeperiods.cfg
  35 cfg_file=/usr/local/nagios/etc/templates.cfg
  [iyunv@localhost etc]# cd objects/
  [iyunv@localhost objects]# cp commands.cfg contacts.cfg timeperiods.cfg templates.cfg localhost.cfg /usr/local/nagios/etc/
  
四:Nagios的运行和维护
  
1) 验证nagios主配置文件的语法正确性
  [iyunv@localhost etc]# /usr/local/nagios/bin/nagios -v /usr/local/nagios/etc/nagios.cfg
  Total Warnings: 0
  Total Errors:   0
  
Things look okay - No serious problems were detected during the pre-flight check
  2) 启动与停止nagios
  [iyunv@localhost etc]# service nagios start
  Starting nagios:This account is currently not available.
  done.
  [iyunv@localhost etc]# service nagios status
  nagios (pid 5106) is running...
  [iyunv@localhost etc]# service nagios stop
  Stopping nagios: .done.
  [iyunv@localhost etc]# service nagios restart
  Running configuration check...done.
  Stopping nagios: No lock file found in /usr/local/nagios//var/nagios.lock
  Starting nagios:This account is currently not available.
  done.
  
一、 Nagios性能分析图表的实现
  1) 安装PNP支持包rrdtool工具
  [iyunv@localhost nagios]# tar zxf rrdtool-1.4.5.tar.gz
  [iyunv@localhost nagios]# cd rrdtool-1.4.5
  [iyunv@localhost rrdtool-1.4.5]# ./configure --prefix=/usr/local/rrdtool ; make ; make install
  2)安装PNP
  [iyunv@localhost rrdtool-1.4.5]# cd /aaa/
  [iyunv@localhost nagios]# tar zxf pnp-0.4.13.tar.gz
  [iyunv@localhost nagios]# cd pnp-0.4.13
  [iyunv@localhost pnp-0.4.13]# ./configure --with-nagios-user=nagios --with-nagios-group=nagios --with-rrdtool=/usr/local/rrdtool/bin/rrdtool --with-perfdata-di
  r=/usr/local/nagios/share/perfdata ; make all ; make install ; make install-config ; make install-init
  3) 配置PNP
  A,创建默认配置文件
  [iyunv@localhost pnp-0.4.13]# cd /usr/local/nagios/etc/pnp/
  [iyunv@localhost pnp]# cp process_perfdata.cfg-sample process_perfdata.cfg
  [iyunv@localhost pnp]# cp npcd.cfg-sample npcd.cfg
  [iyunv@localhost pnp]# cp rra.cfg-sample rra.cfg
  [iyunv@localhost pnp]# chown -R nagios:nagios /usr/local/nagios/etc/pnp/
  B,修改process_perfdata.cfg
  [iyunv@localhost pnp]# vi /usr/local/nagios/etc/pnp/process_perfdata.cfg
  44 LOG_LEVEL = 2
  4) 修改Nagios主配置文件
  A,增加小太阳图标
  [iyunv@localhost pnp]# cd /usr/local/nagios/etc/
  [iyunv@localhost etc]# vi templates.cfg
  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
  }
  B、修改nagios.cfg
  找到如下几行去掉注释
  [iyunv@localhost etc]# vi nagios.cfg
  833 process_performance_data=1
  845 host_perfdata_command=process-host-perfdata
  846 service_perfdata_command=process-service-perfdata
  C、修改commands.cfg
  227 # 'process-host-perfdata' command definition
  228 define command{
  229         command_name    process-host-perfdata
  230         command_line    /usr/local/nagios/libexec/process_perfdata.pl
  231         }
  232
  233
  234 # 'process-service-perfdata' command definition
  235 define command{
  236         command_name    process-service-perfdata
  237         command_line    /usr/local/nagios/libexec/process_perfdata.pl
  238         }
  D、修改hosts.cfg文件和services.cfg文件
  [iyunv@localhost ~]# vi /usr/local/nagios/etc/hosts.cfg
  define host{
  use     linux-server,hosts-pnp
  host_name       web
  alias   benet-web
  address 192.168.1.100
  }
  define host{
  use     linux-server,hosts-pnp
  host_name       mysql
  alias   benet-mysql
  address 192.168.1.200
  }
  define hostgroup{
  hostgroup_name  sa-servers
  alias   sa servers
  members web,mysql
  }
  [iyunv@localhost ~]# vi /usr/local/nagios/etc/services.cfg
  ################- benet web -##########################
  define service{
  use     local-service,services-pnp
  host_name       web
  service_description     PING
  check_command   check_ping!100.0,20%!500.0,60%
  }
  define service{
  use     local-service,services-pnp
  host_name       web
  service_description     SSH
  check_command   check_ssh
  }
  注意项:别心急看小太阳数据,等会儿就有了。
  
DSC0005.jpg
  
点击小太阳,出现对主机或服务的监控页面
  
DSC0006.jpg
  
五,利用插件扩展Nagios的监控功能
  下面我们通过在nagios服务端和客户端安装NRPE插件来搭建一个更加完善的Nagios监控系统。
  
1)配置nagios客户端(即远端被监控主机192.168.1.200)
  A)安装Nagios插件
  登陆192.168.1.200客户端
  [iyunv@localhost ~]# cd /nagios/
  [iyunv@localhost nagios]# useradd -s /sbin/nologin nagios
  [iyunv@localhost nagios]# tar zxf nagios-plugins-1.4.14.tar.gz
  [iyunv@localhost nagios]# cd nagios-plugins-1.4.14
  [iyunv@localhost nagios-plugins-1.4.14]# ./configure ; make ;make install
  [iyunv@localhost nagios-plugins-1.4.14]# chown nagios:nagios /usr/local/nagios/
  [iyunv@localhost nagios-plugins-1.4.14]# chown -R nagios.nagios /usr/local/nagios/libexec/
  B)安装NRPE插件
  [iyunv@localhost nagios]# cd /aaa/
  [iyunv@localhost nagios]# tar zxf nrpe-2.12.tar.gz
  [iyunv@localhost nagios]# cd nrpe-2.12
  [iyunv@localhost nrpe-2.12]# ./configure ; make all ; make install-plugin ; make install-daemon ; make install-daemon-config
  C)修改NRPE配置文件
  [iyunv@localhost nrpe-2.12]# vi /usr/local/nagios/etc/nrpe.cfg
  79 allowed_hosts=127.0.0.1,192.168.1.1(nagios服务器ip)
  192.168.6.199:指定nagios服务器地址
  D)启动NRPE
  [iyunv@localhost nrpe-2.12]# /usr/local/nagios/bin/nrpe -c /usr/local/nagios/etc/nrpe.cfg -d
  [iyunv@localhost nrpe-2.12]# netstat -antl |grep 5666
  tcp        0      0 0.0.0.0:5666                0.0.0.0:*                   LISTEN
  E)测试NRPE
  [iyunv@localhost nrpe-2.12]# /usr/local/nagios/libexec/check_nrpe -H 127.0.0.1
  NRPE v2.12
  如果出现NRPE v2.12表明NRPE已经正常工作
  定义让nagios服务器监控我本机上的内容(当前用户数,CPU,内存,硬盘等信息)
  [iyunv@localhost libexec]# vi /usr/local/nagios/etc/nrpe.cfg
  199 command[check_users_1]=/usr/local/nagios/libexec/check_users -w 5 -c 10
  200 command[check_load_1]=/usr/local/nagios/libexec/check_load -w 15,10,5 -        c 30,25,20
  201 command[check_sda1_1]=/usr/local/nagios/libexec/check_disk -w 20% -c 10        % -p /dev/sda1
  202 command[check_zombie_procs_1]=/usr/local/nagios/libexec/check_procs -w         5 -c 10 -s Z
  203 command[check_total_procs_1]=/usr/local/nagios/libexec/check_procs -w 1        50 -c 200
  重启NRPE
  [iyunv@localhost ~]# ps -elf |grep nrpe
  5 S nagios   22379     1  0  75   0 -  1243 -      17:59 ?        00:00:00 /usr/local/nagios/bin/nrpe -c /usr/local/nagios/etc/nrpe.cfg -d
  0 R root     22586  4247  0  78   0 -  1039 -      18:09 pts/0    00:00:00 grep nrpe
  [iyunv@localhost ~]# kill -9 22379
  [iyunv@localhost ~]# /usr/local/nagios/bin/nrpe -c /usr/local/nagios/etc/nrpe.cfg –d
  被监控端192.168.1.100与192.168.1.200的步骤一样,这里就略过
  2配置nagios服务器端
  登陆nagios服务器端:192.168.1.1
  1)安装NRPE插件
  [iyunv@localhost etc]# cd /aaa/
  [iyunv@localhost aaa]# tar zxf nrpe-2.12.tar.gz
  [iyunv@localhost aaa]# cd nrpe-2.12
  [iyunv@localhost nrpe-2.12]# ./configure ; make ; make install-plugin
  2) 测试插件能否取得与客户端192.168.1.200的联系
  [iyunv@localhost nrpe-2.12]# /usr/local/nagios/libexec/check_nrpe -H 192.168.1.200
  NRPE v2.12
  出现NRPE v2.12就证明成功联系上客户端
  3)定义一个check_nrpe监控命令
  [iyunv@localhost nrpe-2.12]# vi /usr/local/nagios/etc/commands.cfg
  define command{
  command_name check_nrpe
  command_line    $USER1$/check_nrpe -H $HOSTADDRESS$ -c $ARG1$
  }
  5) 添加远程主机监控项
  [iyunv@localhost nrpe-2.12]# vi /usr/local/nagios/etc/services.cfg
  define service{
  use     local-service
  host_name       mysql
  service_description     users
  check_command   check_nrpe!check_users
  }
  define service{
  use     local-service
  host_name       mysql
  service_description     load
  check_command   check_nrpe!check_load
  }
  define service{
  use     local-service
  host_name       mysql
  service_description     disk
  check_command   check_nrpe!check_sda1
  }
  define servicegroup{
  servicegroup_name       servergroup
  alias           server-group
  members         web,PING,web,SSH,web,SSHD,web,http,mysql,users,mysql,load,mysql,disk
  }
  6)测试并重新启动nagios服务
  [iyunv@localhost ~]# service nagios restart
  
  
  
                          139邮件报警和短信报警
  首先要去注册139邮箱,然后收到邮件时手机就会收到邮件
  这个配置特别简单
  确定你的sendmail是否运行  service sendmail status
  如果没有安装就直接 yum -y install sendmail
  修改配置文件:contacts.cfg  只需要修改邮箱即可,如果有多个联系人可以用逗号隔开
DSC0007.jpg
  

  
  本文出自 “linux” 博客,请务必保留此出处http://yangdonglin.blog.iyunv.com/5404572/1064692

运维网声明 1、欢迎大家加入本站运维交流群:群②:261659950 群⑤:202807635 群⑦870801961 群⑧679858003
2、本站所有主题由该帖子作者发表,该帖子作者与运维网享有帖子相关版权
3、所有作品的著作权均归原作者享有,请您和我们一样尊重他人的著作权等合法权益。如果您对作品感到满意,请购买正版
4、禁止制作、复制、发布和传播具有反动、淫秽、色情、暴力、凶杀等内容的信息,一经发现立即删除。若您因此触犯法律,一切后果自负,我们对此不承担任何责任
5、所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其内容的准确性、可靠性、正当性、安全性、合法性等负责,亦不承担任何法律责任
6、所有作品仅供您个人学习、研究或欣赏,不得用于商业或者其他用途,否则,一切后果均由您自己承担,我们对此不承担任何法律责任
7、如涉及侵犯版权等问题,请您及时通知我们,我们将立即采取措施予以解决
8、联系人Email:admin@iyunv.com 网址:www.yunweiku.com

所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其承担任何法律责任,如涉及侵犯版权等问题,请您及时通知我们,我们将立即处理,联系人Email:kefu@iyunv.com,QQ:1061981298 本贴地址:https://www.yunweiku.com/thread-111038-1-1.html 上篇帖子: Nagios+msn+fetion自定义时间发送报警消息 下篇帖子: Zabbix,Nagios,OneAPM Servers 安装部署大比拼
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

扫码加入运维网微信交流群X

扫码加入运维网微信交流群

扫描二维码加入运维网微信交流群,最新一手资源尽在官方微信交流群!快快加入我们吧...

扫描微信二维码查看详情

客服E-mail:kefu@iyunv.com 客服QQ:1061981298


QQ群⑦:运维网交流群⑦ QQ群⑧:运维网交流群⑧ k8s群:运维网kubernetes交流群


提醒:禁止发布任何违反国家法律、法规的言论与图片等内容;本站内容均来自个人观点与网络等信息,非本站认同之观点.


本站大部分资源是网友从网上搜集分享而来,其版权均归原作者及其网站所有,我们尊重他人的合法权益,如有内容侵犯您的合法权益,请及时与我们联系进行核实删除!



合作伙伴: 青云cloud

快速回复 返回顶部 返回列表