prollerweg 发表于 2019-1-13 06:43:36

编译安装Nagios监控系统

  监控系统(nagios[报警]+cacti[监控]) | zabbix (zabbix-proxy)
  1> 监控系统资源(私有资源 cpu|内存|磁盘)
  (公共资源 服务 ssh|ftp|httpd)
  (网络资源 交换机|路由器)
  2> 报警(商业短信|邮件|微信API)
  本章目录:
  1、Nagios的概述
  2、Nagios的部署
  3、讲解Nagios配置文件之间的关系(重点)
  4、配置Nagios监控本机的私有服务和公共服务
  5、使用NRPE配置Nagios监控远程主机的私有服务和公共服务
  6、为Nagios增加短信报警功能
  1、Nagios的概述
  Nagios是一款用于系统和网络监控的应用程序。它可以在你设定的条件下对主机和服务进行监控,在状态变差和变好的时候给出告警信息。
  Nagios最初被设计为在Linux系统之上运行,然而它同样可以在类Unix的系统之上运行。
  Nagios更进一步的特征包括:
  监控网络服务(SMTP、POP3、HTTP、NNTP、PING等);   --公共服务
  监控主机资源(处理器负荷、磁盘利用率等);                  --私有服务
  简单地插件设计使得用户可以方便地扩展自己服务的检测方法;
  并行服务检查机制;
  当服务或主机问题产生与解决时将告警发送给联系人(通过EMail、短信、用户定义方式);
  具备定义事件句柄功能,它可以在主机或服务的事件发生时获取更多问题定位;
  可选的WEB界面用于查看当前的网络状态、通知和故障历史、日志文件等;
  nagios的组成:
  1、nagios的核心组件
  2、nagios监控插件
  3、nagios web页面(html/cgi/php)
  4、nrpe远程主机私有服务的监控组件
  ---------------------------------------------------------------------
  nagios部署
  参看官方安装手册
  ftp://192.168.0.254/notes/weekend/project/software/nagios/docs_INSTALL/toc.html
  1) 下载软件
  # lftp 192.168.0.254
  lftp 192.168.0.254:~> cd notes/weekend/project/software/
  lftp 192.168.0.254:/notes/weekend/project/software> mirror nagios/
  lftp 192.168.0.254:/notes/weekend/project/software> exit
  2)软件依赖包
  Apache
  PHP
  GCC compiler
  GD development libraries
  # yum install httpd php
  # yum install gcc glibc glibc-common
  # yum install gd
  --iso 没有gd-devle包
  # yum -y localinstall /root/nagios/gd-devel-2.0.35-10.el6.x86_64.rpm
  3) 新建用户与组
  # /usr/sbin/useradd -m nagios
  # echo 123 | passwd --stdin nagios
  # /usr/sbin/groupadd nagcmd
  # /usr/sbin/usermod -a -G nagcmd nagios
  # /usr/sbin/usermod -a -G nagcmd apache
  4) 解压与编译nagios核心软件
  (1) 解压
  # tar xf /root/nagios/nagios-cn-3.2.0.tar.bz2 -C/usr/local/src/
  (2) 编译
  # cd /usr/local/src/nagios-cn-3.2.0/
  # ./configure --with-command-group=nagcmd
  # make all
  make install      --安装软件
  make install-init   --安装启动脚本
  make install-config --安装配置文件
  make install-commandmode--安装命令模式
  (3)安装与配置
  # make install
  # make install-init
  # make install-config
  # make install-commandmode
  (4) 定义web配置文件
  # make install-webconf
  /usr/bin/install -c -m 644 sample-config/httpd.conf/etc/httpd/conf.d/nagios.conf
  --设置登录密码帐号nagiosadmin密码123
  # htpasswd -c/usr/local/nagios/etc/htpasswd.users nagiosadmin
  New password:
  Re-type new password:
  Adding password for user nagiosadmin
  # service httpd restart
  5) 安装nagios的插件
  (1) 解压
  # tar xf /root/nagios/nagios-plugins-1.4.14.tar.gz -C/usr/local/src/
  (2) 编译
  # cd /usr/local/src/nagios-plugins-1.4.14/
  # ./configure --with-nagios-user=nagios--with-nagios-group=nagios
  # make && make install
  6) 启动nagios
  --定义启动脚本
  # chkconfig --add nagios
  # chkconfig nagios on
  --检测配置文件语法
  # /usr/local/nagios/bin/nagios -v/usr/local/nagios/etc/nagios.cfg|grepTotal
  Total Warnings: 0
  Total Errors:   0
  --启动
  # service nagios start
  --关闭selinux
  # getenforce
  Permissive
  7) 访问nagios
  http://localhost/nagios
  +++++++++++++++++讲解Nagios配置文件之间的关系(重点)+++++++++++++
  监控一台主机包括的内容:主机是否活着/公共服务/私有服务
  配置文件的组成:      --全局变量(变量的调用使用use命令)
  /usr/local/nagios/etc/nagios.cfg   --主配置文件,实现配置文件总调度
  /usr/local/nagios/etc/resource.cfg   --资源
  /usr/local/nagios/etc/objects/commands.cfg --监控使用的命令(通知的命令|监控私有服务的命令|监控公共服务的命令)
  /usr/local/nagios/etc/objects/contacts.cfg--联系人(nagiosadmin-->nagios@localhost)
  /usr/local/nagios/etc/objects/timeperiods.cfg   --监控周期(7x24 5x8(1-5))
  /usr/local/nagios/etc/objects/templates.cfg --模板(通知|genric-host| linux-server| local-server)
  **********监控命令 commands.cfg*************** 24个命令模板
  # 'notify-host-by-email' command definition   ---使用邮件通知主机(私有服务)
  define command{
  command_name    notify-host-by-email
  command_line    /usr/bin/printf "%b" "*****Nagios *****\n\nNotification Type: $NOTIFICATIONTYPE$\nHost: $HOSTNAME$\nState:$HOSTSTATE$\nAddress: $HOSTADDRESS$\nInfo: $HOSTOUTPUT$\n\nDate/Time:$LONGDATETIME$\n" | /bin/mail -s "** $NOTIFICATIONTYPE$ Host Alert:$HOSTNAME$ is $HOSTSTATE$ **" $CONTACTEMAIL$
  }
  邮件内容(通知宏|主机宏|日期/时间宏|...)
  ***** Nagios *****
  Notification Type: $NOTIFICATIONTYPE$ (PROBLEM)
  Host: $HOSTNAME$ (nagios.uplooking.com)
  State: $HOSTSTATE$ (宕机)
  Address: $HOSTADDRESS$ (192.168.0.1)
  Info: $HOSTOUTPUT$ (TTP WARNING: HTTP/1.1 403 Forbidden)
  Date/Time: $LONGDATETIME$(Tue Apr26 15:22:03 CST 2016)
  邮件主题
  /bin/mail -s "** PROBLEM Host Alert: nagios.uplooking.com is 宕机"
  # 'notify-service-by-email' command definition   ---使用邮件通知服务 (公共服务)
  # 'check-host-alive' command definition --检测主机是否存活
  # 'check_local_disk' command definition --磁盘使用率
  # 'check_local_load' command definition--cpu负载
  # 'check_local_users' command definition --用户登录数
  # 'check_local_swap' command definition--swap分区
  ....
  ####################################################################################
  **********联系人 contacts.cfg*************** 1个联系人 1个联系组
  define contact {
  contact_name                   HugoBoss    ---联系人的名称
  use                           generic-contact--使用那个模板
  alias                        康康--别名
  email                        hugoboss@hugo.cc--邮件地址
  }
  define contactgroup{
  contactgroup_name       admins
  alias                   Nagios Administrators
  members               nagiosadmin, HugoBoss--组的成员
  }
  ####################################################################################
  ****************监控周期 timeperiods.cfg*********************************   5个模板
  # grep timeperiod_name/usr/local/nagios/etc/objects/timeperiods.cfg
  timeperiod_name 24x7--全年
  timeperiod_name workhours ---工作日
  timeperiod_name none---全年不通知
  timeperiod_nameus-holidays---美国节假日
  timeperiod_name24x7_sans_holidays --除了美国节假日以外的全年
  ####################################################################################
  ****************监控模板 templates.cfg*********************************8个模板
  # CONTACT TEMPLATES --联系人模板
  define contact{
  name                           generic-contact--模板名称
  service_notification_period    24x7---服务的通知周期
  host_notification_period      24x7 ---主机的通知周期
  service_notification_options   w,u,c,r,f,s --通知服务的类型
  host_notification_options      d,u,r,f,s   --通知主机的类型
  service_notification_commandsnotify-service-by-email---通知服务的邮件方式
  host_notification_commands      notify-host-by-email   ---通知主机的邮件方式
  register                        0   ---只是一个模板 不能当成监控对象使用
  }
  # HOST TEMPLATES--主机模板
  define host{
  name                            generic-host
  define host{
  name                            linux-server   模板名称
  use                           generic-host   调用那个模板
  check_period                  24x7             监控周期
  check_interval                  5                检测时间 5分钟
  retry_interval                  1                失败重试 1分钟
  max_check_attempts            10               最大尝试次数 10次
  check_command                   check-host-alive检测主机命令
  notification_period             workhours         通知周期
  notification_interval         120          通知间隔 120分钟
  notification_options            d,u,r         通知类型 d:宕机 u:主机不能到达 r:恢复
  contact_groups                  admins      通知组
  register                        0
  }
  name windows-server
  name generic-printer
  name generic-switch
  # SERVICE TEMPLATES
  define service{
  name                            generic-service
  define service{
  name                            local-service
  use                           generic-service
  # grep name /usr/local/nagios/etc/objects/templates.cfg
  name                           generic-contact   ; The name of this contact template
  name                            generic-host    ; The name of this host template
  name                linux-server    ;The name of this host template
  name            windows-server;The name of this host template
  name            generic-printer ;The name of this host template
  name            generic-switch;The name of this host template
  name                            generic-service   ; The 'name' of this service template
  name                local-service       ;The name of this service template

页: [1]
查看完整版本: 编译安装Nagios监控系统