得过且过 发表于 2019-1-12 14:39:16

搭建nagios监控步骤

  1、实验环境:
  主机名:            ip            需要的软件环境
  nagios-server   192.168.124.143    Apache、Php、Nagios、nagios-plugins、nrpe
  nagios-linux      192.168.124.203    nagios-plugins、nrpe
  监控机 nagios-server上需要 安装nagios软件,对监控的数据做处理,并且提供 web界面查看和管理。当然也对本机自身的信息进行监控;
  被监控机 nagios-linux需安装NRPE及nagios plugins,根据监控机的请求执行监 控,然后将结果回传给监控机;
  一、Nagios监控机(192.168.124.143)上的环境准备
  1、 安装编译环境
  使用yum安装
  # yum install -y gcc glibc glibc-common gd xinetd openssl-devel mysql-devel
  2、 创建nagios用户和用户组
  #useradd -s /sbin/nologin nagios
  #mkdir /usr/local/nagios
  #chown -R nagios:nagios /usr/local/nagios
  3、 yum安装httpd和php,并配置httpd支持php
  只要能实现浏览器能正常访问 http://本机IP/index.php 既可
  4、 编译安装nagios
  上传nagios源码包至/root目录下
  #tar zxvf nagios-4.0.3.tar.gz
  #cd nagios-4.0.3
  #./configure --prefix=/usr/local/nagios --with-nagiosuser=nagios --with-nagios-group=nagios
  #make all
  #make install
  #make install-init
  #make install-commandmode
  #make install-config
  #make install-webconf
  #chkconfig --add nagios                           #       将nagios加入系统 服务
  #chkconfig nagios on
  5、 验证nagios是否被正确安装
  切换目录到安装路径(这里是/usr/local/nagios),看是否存在etc、bin、 libexec、sbin、share、var这些目录,如果存在则可以表明程序被正确的安装到 系统了。
  Nagios 各个目录用途说明如下:
  目录名             用途
  bin Nagios       可执行程序所在目录
  etc Nagios       配置文件所在目录
  sbin         Nagios CGI文件所在目录,也就是执行外部命令所需文件所在的目录       share          Nagios网页文件所在的目录
  libexec         Nagios 外部插件所在目录
  var Nagios   日志文件、lock 等文件所在的目录
  var/archives    Nagios 日志自动归档目录
  var/rw      用来存放外部命令文件的目录
  6、 安装nagios插件
  上传nagios-plugins-1.5.tar.gz至/root目录下
  #tar zxvf nagios-plugins-1.5.tar.gz
  #cd nagios-plugins-1.5
  #./configure --prefix=/usr/local/nagios --with-mysql --with-nagios-user=nagios --with-nagiosgroup=nagios
  #make && make install
  备注:
  --with-mysql 使nagios插件支持监控mysql(系统要先yum安装好 mysql-devel软件包),在libexec目录下会有 check_mysql和check_mysql_query命令
  7、 安装check_nrpe插件
  上传nrpe-2.12.tar.gz至/root目录下
  #tar zxvf nrpe-2.12.tar.gz
  #cd nrpe-2.12
  #./configure --with-nrpe-user=nagios      --with-nrpe-group=nagios      --with-nagiosuser=nagios      --with-nagios-group=nagios      --enable-command-args      --enable-ssl
  #make all
  #makeinstall-plugin
  监控机上只运行到这一步就行,因为只需要nrpe中的check_nrpe插件
  8、 修改apache的配置文件/etc/httpd/conf/httpd.conf
  #vim /etc/httpd/conf/httpd.conf
  User apache修改为User nagios
  Group apache修改为Group nagios
  DirectoryIndex index.html修改为DirectoryIndex index.php
  保存
  9、 为了安全起见,一般情况下要让nagios 的web 监控页面必须经过授权才能访问            编译安装nagios 4.0.3版本完成后,已自动配置httpd并设定了默认的htpasswd验证文件                  /usr/local/nagios/etc/ htpasswd.users
  #htpasswd -c /usr/local/nagios/etc/htpasswd.users nagiosadmin                     #指定用户 nagiosadmin可以访问
  10、 启动httpd服务
  #service httpd restart
  #service nagios start
  11、 浏览器访问nagios站点
  http://192.168.124.143/nagios
  二、被监控机(192.168.124.203)安装nrpe及nagios-plugins
  1、 安装编译环境
  使用yum安装
  # yum install -y gcc glibc glibc-common gd xinetd openssl-devel mysql-devel
  2、 编译安装nagios插件
  上传nagios-plugins-1.5.tar.gz至/root目录下
  #tar -zxvf nagios-plugins-1.5.tar.gz
  #cd nagios-plugins-1.5
  #./configure--with-mysql --with-nagios-user=nagios --with-nagios-group=nagios
  #make && make install
  这一步完成后会在/usr/local/nagios/下生成两个目录libexec和share,修改目录权限            #useradd -s /sbin/nologin nagios
  #chown-R nagios:nagios /usr/local/nagios
  3、 编译安装nrpe
  上传nrpe-2.12.tar.gz至/root目录下
  #tar -zxvf nrpe-2.12.tar.gz
  #cd nrpe-2.12
  #./configure --with-nrpe-user=nagios      --with-nrpe-group=nagios      --with-nagiosuser=nagios      --with-nagios-group=nagios      --enable-command-args      --enable-ssl
  #make all
  #make install-plugin
  #make install-daemon
  #make install-daemon-config
  #现在再查看/usr/local/nagios 目录就会发现有4个子目录
  #make install-xinetd
  #vim /etc/xinetd.d/nrpe
  修改在末尾项 only_from    = 192.168.124.0/24
  这里设置允许监控机所在的网段访问
  #vim /etc/services
  #编辑/etc/services 文件,末尾增加NRPE服务内容
  在最后添加   nrpe    5666/tcp   #nrpe
  #service xinetd restart
  #netstat -ntulp|grep 5666
  #查看nrpe是否已启动并监听5666端口   

  4、 测试nrpe是否正常工作
  在监控机(192.168.124.143)执行check_nrpe这个插件来测试与被监控机(192.168.124.203)的通讯
  #/usr/local/nagios/libexec/check_nrpe -H 192.168.124.203            如显示   NRPE V2.12   ,则nrpe通讯正常
  三、监控机(192.168.124.143)上的配置
  1、 在commands.cfg文件末尾增加check_nrpe的定义
  #vim /usr/local/nagios/etc/objects/commands.cfg
  #添加如下 代码
  define command{
  command_name    check_nrpe
  command_line    $USER1$/check_nrpe -H $HOSTADDRESS$ -c $ARG1$            }
  2、 定义对Nagios-Linux 主机的监控,创建nagios_linux.cfg文件并加入以下 代码内容
  

  #vim /usr/local/nagios/etc/objects/nagios_linux.cfg
  define host{
  use             linux-server
  host_name         nagios-linux
  alias         nagios-linux
  address          192.168.124.67               #此处设置被监控机的IP地址            }
  define service{          #添加监控CPU负载的服务
  use            local-service
  host_name      nagios-linux
  service_description   CPU Load
  check_command      check_nrpe!check_load
  }
  define service{            #添加监控总进程数的服务
  use             local-service
  host_name         nagios-linux
  service_descriptionTotal Processes
  check_command      check_nrpe!check_total_procs
  }
  上述代码分别定义了CPU Load(cpu负载)以及Total Processes(总进程数)的监 控服务,还可以在此文件中增加其他的监控服务
  3、 在nagios的主配置文件nagios.cfg末尾增加如下代码内容
  #vim /usr/local/nagios/etc/nagios.cfg
  cfg_file=/usr/local/nagios/etc/objects/nagios_linux.cfg
  #在nagios的主配置文件中增 加被监控机的配置文件
  4、 重启各项服务
  #service httpd restart
  #service nagios restart
  5、 验证nagios是否能监控到本机(localhost)以及被监控机nagios-linux
  http://ip/nagios
  6、#service httpd restart
  #service nagios restart
  #重启验证新增的监控信息
  ok!到此结束!希望对你有所帮助
  




页: [1]
查看完整版本: 搭建nagios监控步骤