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

nagios监控搭建及配置

[复制链接]

尚未签到

发表于 2019-1-15 08:09:54 | 显示全部楼层 |阅读模式
  系统:centos 6.3 最小化安装的操作系统
  nagios-3.2.3.tar.gz      nagios 的服务安装包
  nagios-plugins-1.4.14.tar.gz   命令插件
  nagios-cn-3.2.3.tar.bz2  中文补丁
  httpd-2.2.4.tar.gz        apache安装文件
  php-5.2.6.tar.gz          php的安装文件
  安装前准备工作
  useradd  nagios
  mkdir /usr/local/nagios    创建nagios 的安装目录
  chown -R nagios.nagios /usr/local/nagios  将nagios 目录下所有的内容加入nagios用户与nagios组
  yum install gcc gcc-c++ libtool libxml2-devel zlib-devel gd-devel gd-devel perl-devel ncurses-devel  glib2-devel make pango-devel cairo-devel perl-Time-HiRes    安装这些文件 后面再继续安装有关nagios的服务 就不会再出现报错。如果在报错 就可能是版本不同或者系统不同导致的
  安装nagios
  将所有准备好的安装包 上传至 /tmp里面
  tar -xvf nagios-3.2.3.tar.gz
  cd nagios.3.2.3
  ./configure --prefix=/usr/local/nagios             指定了nagios的安装目录
  make  all  && make install            编译 并安装 nagios主程序
  make install-init                在/etc/rc.d/init.d目录下创建nagios的启动脚本
  make install-commandmode           通过此命令来配置目录权限
  mak install-config             用来安装nagios 配置文件,这里安装路径是/usr/local/nagios/etc
  设置开机自动启动
  chkconfig --add nagios    将nagios 启动项加入到系统服务
  chkconfig --list nagios     查看nagios 是否成功加入
  nagios 各个目录的名称及其用途说明:
  bin              nagios的命令所在的目录
  etc              nagios的配置文件都在此目录
  sbin            nagiosCGI文件的目录,执行外部命令所需要文件的目录
  share           nagios的网页文件所在目录
  libexec         nagios-plugins 安装在此目录,此目录默认为空,安装了插件会生成可执行文件
  var               存放日志文件的目录
  安装nagios 插件
  cd /tmp
  tar -xvf nagios-plugins-1.4.14
  cd nagios-plugins-1.4.14
  ./configure --prefix=/usr/local/nagios
  make && make install
  安装汉化插件
  cd /tmp
  tar -xvf nagios-cn-3.2.3.tar.bz2
  cd nagios-cn.3.2.3
  ./configure --prefix=/usr/local/nagios
  make all && make install
  安装 apache
  tar -xvf httpd-2.2.4.tar.gz
  cd httpd-2.2.4
  ./configure --prefix=/usr/local/apache2 \
  --enable-so \
  --enable-mods-shared=most \
  --enable-proxy \
  --enable-proxy-connect \
  还需要什么模块可以根据自己的需要对apache 进行安装 做为监控其实也无所谓了
  安装php
  tar -xvf php-5.2.6.tar.gz
  cd php.5.2.6
  ./configure --prefix=/usr/local/php \
  --with-apxs2=/usr/local/apache2/bin/apxs
  make && make install
  配置apache
  apache  的基本修改 就不多讲了,这里只记录关于nagios的配置
  vi /usr/local/apache2/conf/http.conf
  修改    User        nagios
  Group     nagios
  DirectoryIndex     index.php   index.html
  添加以下内容
  AddType application/x-httpd-php  .php
  为了安全,我们访问nagios的页面 需要设置账号密码的验证,获得许可后才能浏览。
  在最后 添加以下内容
  ScriptAlias /nagios/cgi-bin "/usr/local/nagios/sbin"
  
  AuthType Basic
  Options ExecCGi
  AllowOverride None
  Order allow,deny
  Allow from all
  AuthName "Nagios Access"
  AuthUserFile /usr/local/nagios/etc/htpasswd
  Require valid-user
  
  Alias /nagios "/usr/local/nagios/share"
  
  AuthType Basic
  Options None
  AllowOverride None
  Order allow,deny
  Allow from all
  AuthName "Nagios Access"
  AuthUserFile /usr/local/nagios/etc/htpasswd
  Require valid-user
  
  创建浏览验证文件
  在apache的bin目录下的 htpasswd
  /usr/local/apache2/bin/htpasswd -c /usr/local/nagios/etc/htpasswd admin
  New password:123456"这里根据自己需要自行修改"
  Re-type new password:请再输入一次
  Adding password for user admin     这里就建好了一个账号为admin密码为123456的验证信息
  测试一下服务是否可以正常运行
  关闭防火墙 service iptables stop 或者允许80端口不然 无法浏览页面
  /usr/local/apache2/bin/apachectl start  启动apache
  service nagios start   启动nagios
  然后打开 浏览器  输入 http://ip/nagios
  弹出 登录验证框  输入账号密码 就可以正常访问

  ***********************至此nagios的运行环境搭建好了***********************
  现在讲下nagios配置文件
  配置文件 放在nagios/etc下面
  cgi.cfg                   控制CGI访问的配置文件
  nagios.cfg              nagios主配置文件
  resource.cfg           变量定义文件,这个一般不做修改。
  /etc/objects/            这是存放配置文件模板的目录,定义 要监控的对象和相关信息的配置文件
  objects/commands.cfg      定义监控某个功能所使用的命令的配置文件。
  objects/contacts.cfg          定义常用联系人和联系组的配置文件
  objects/localhost.cfg          定义本地主机的配置文件
  objects/templates.cfg          定义主机和服务的一个模板
  objects/timeperiods.cfg       定义监控时间段的配置文件
  还有printer.cfg    switch.cfg    windows.cfg   这几个默认不启动的配置文件,不做介绍了。
  等了解了naigos  的配置文件之间的关系,自己就可以随意添加项要监控的内容了。
  配置文件之间的关系
  nagios 这些配置文件 之间存在很多相互引用,因为相互引用这样,使得nagios 的管理更加方便,但是 对于 刚刚接触nagios的朋友来说,反而显得 关系很混乱,但是只要把关系弄清楚,就算是熟练掌握了nagios了
  配置nagios
  (1)templates.cfg文件
  define contact{
  name generic-contact             定义联系人名称
  service_notification_period 24
  #定义出现服务异常时,发送通知的时间段,24在timeperiods.cfg定义的,这24参数也算引用
  host_notification_period 24
  #定义出现主机异常时,发送通知的时间段,24在timeperiods.cfg定义的
  service_notification_options w,u,c,r
  #定义通知可以被发错的情况,w表示警告warn,u表示不明unknown
  #c表示紧急criticle,r表示恢复recover
  host_notification_options d,u,r
  #d:宕机   u:不可到达状态   r:恢复状态
  service_notification_commands notify-service-by-email
  #服务故障时,通过email方式进行通知,notify-service-by-email 在commands.cfg中定义的
  host_notification_commands notify-host-by-email
  #主机故障时,通过email方式进行通知,notify-host-by-email在commands.cfg中定义的
  register 0
  }
  define host{
  name generic-host   定义主机模板的名称,不是电脑的主机名,只针对这个define host模板
  notifications_enabled 1
  event_handler_enabled 1
  flap_detection_enabled 1
  failure_prediction_enabled 1
  process_perf_data 1   开启数据传输功能,后面将用到
  retain_status_information 1
  retain_nonstatus_information 1
  notification_period 24
  register 0
  }
  define host{
  name linux-server   定义这个主机模板的名称,每个模板 里面的属性不一样,不要闹混了。
  use generic-host     引用generic-host这个主机的属性,到当前主机当中,use表示引用
  check_period 24     表示检查主机的时间段
  check_interval 5       表示检查主机的时间间隔 这里是5分钟
  retry_interval 1          第一次检查失败时,重试检查间隔 这里是1分钟
  max_check_attempts 10  主机出现异常时,会尝试10次检查,再最后判断是否真正的出现异常
  check_command check-host-alive   指定检查主机状态的命令,在commands.cfg中定义的
  notification_period 24    主机故障时,发送通知的时间范围,在timeperiods.cfg中定义的
  notification_interval 120  表示异常后,间隔120分钟进行第二次通知,若为0 表示只进行1次通知
  notification_options d,u,r 定义 主机在什么状态下会发送通知d:宕机 u:不可到达 r:恢复状态
  contact_groups admins  指定联系人组 这个admin 在contacts.cfg文件中定义的
  register 0
  }
  define service{
  name generic-service  定义一个服务的名称
  active_checks_enabled  1
  passive_checks_enabled  1
  parallelize_check  1
  obsess_over_service  1
  check_freshness   0
  notifications_enabled  1
  event_handler_enabled  1
  flap_detection_enabled  1
  failure_prediction_enabled 1
  process_perf_data  1
  retain_status_information 1
  retain_nonstatus_information 1
  is_volatile   0
  check_period   24
  max_check_attempts  3   指定了nagios对服务的最大检查次数
  normal_check_interval  10  设置服务检查时间间隔
  retry_check_interval  2     重试检查时间间隔,这里是2分钟
  contact_groups   admins   指定联系人组
  notification_options  w,u,c,r
  notification_interval  60
  notification_period  24
  register   0
  }
  define service{
  name   local-service
  use   generic-service
  max_check_attempts 2
  normal_check_interval 5
  retry_check_interval 1
  register  0
  }
  (2)resource.cfg  定义nagios的变量文件  文件内容 只用到了一行
  $USER1$=/usr/local/nagios/libexec
  不用解释相信大家也明白这句意思了。
  (3)commands.cfg
  以chkec_ping 为例:
  define command{
  command_name    check_ping
  command_line        $USER1$/check_ping -H $HOSTADDRESS$ -W $ARG1$ -C $ARG2$ -P 5
  }
  nagios服务运行后 就会检查ping 如果能ping通 代表机器运行正常,仅此而已
  里面有很多命令 ,把自己不需要的注释掉,自定义项添加的 可以根据里面的格式自己编辑
  (4)host.cfg文件,次文件默认不存在,需要自己创建。里面是被监控主机的属性。
  define host{
  use linux-server    引用linux-server的属性到当前主机里
  host_name web    定义被监控的主机名
  alias  yanzhe-web  主机别名 nagios浏览器显示的就是主机别名
  address  192.168.1.101   被监控的主机IP地址,也可以是域名
  }
  define host{
  use linux-server
  host_name mysql
  alias  yanzhe-mysql
  address  192.168.1.102
  }
  define hostgroup{                      定义一个主机组
  hostgroup_name sa-servers         主机组名称
  alias  sa servers                            主机组的别名
  members  web,mysql                    组包括的成员
  }
  (5)services.cfg文件 进行定义了都对主机进行 那些监控服务,用什么命令。
  define service{
  use local-service           引用local-service服务的属性。
  host_name web             监控web主机上的服务  web在hosts.cfg中定义的。
  service_description PING     监控服务的内容 。
  check_command check_ping!100.0,20%!500.0,60%  指定检查的命令 check_ping在commands.cfg中进行的定义,后跟两个参数 命令与参数之间用!分割开。
  }
  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
  }
  #******************************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
  }
  (6)contacts.cfg 文件 定义联系人和联系组的,出现故障后,通过email或者短信发送给谁。
  define contact{
  contact_name sasystem   定义联系人的名称
  use  generic-contact         引用generic-contact的属性
  alias  sa-system                联系人别名
  email  13406351516@139.com    用139邮箱 手机也可以收到短信,但比直接用短信通知慢一些
  }
  define contactgroup{
  contactgroup_name admins             定义联系人组名称
  alias   system administrator group    联系人组的描述
  members   sasystem                               就是上面定义的联系人。
  }
  (7)timeperiods.cfg文件   定义监控时间段。
  define timeperiod{
  timeperiod_name 24
  alias           24 Hours A Day, 7 Days A Week
  sunday          00:00-24:00
  monday          00:00-24:00
  tuesday         00:00-24:00
  wednesday       00:00-24:00
  thursday        00:00-24:00
  friday          00:00-24:00
  saturday        00:00-24:00
  }
  这里来设置时间段的控制 其他的设置不用修改
  (8)cgi.cfg文件 添加 admin的权限
  default_user_name=admin
  authorized_for_system_information=nagiosadmin,admin
  authorized_for_configuration_information=nagiosadmin,admin
  authorized_for_system_commands=admin
  authorized_fro_all_services=nagiosadmin,admin
  authorized_fro_all_hosts=nagiosadmin,admin
  authorized_fro_all_service_commands=nagiosadmin,admin
  authorized_fro_all_host_commands=nagiosadmin,admin
  (9)nagios.cfg文件
  添加hosts.cfg 和 services.cfg 的 指引路径
  cfg_file=/usr/local/nagios/etc/objects/hosts.cfg
  cfg_file=/usr/local/nagios/etc/objects/services.cfg
  注释localhost.cfg的指引路径 #cfg_file=/usr/local/nagios/etc/objects/localhost.cfg
  修改:
  command_check_interval=2
  *******************nagios的配置已经完成了*******************
  出处:http://yanzhe.blog.运维网.com/5279577/1129608


运维网声明 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-663377-1-1.html 上篇帖子: nagios监控之(监控配置) 下篇帖子: Installing Nagios 4.0.2 On CentOS 6.5 (Including NRPE installation)
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

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

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

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

扫描微信二维码查看详情

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


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


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


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



合作伙伴: 青云cloud

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