搭建Nagios监控服务器

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了搭建Nagios监控服务器相关的知识,希望对你有一定的参考价值。

Linux监控 IDC监控

监控的对象:服务器(运维岗位)
监控什么:(资源)
1、硬件的使用率(cpu 内存 存储)
2、监控应用服务的状态(运行 停止)
3、操作系统的运行情况(进程数量 系统的用户数量)
4、网络接口数据流量(进 出)

如何监控?
使用crond执行自定义监控脚本
系统提供的对应命令查看
使用软件搭建监控服务做监控

Nagios监控服务器
监控服务器环境192.168.4.21
一、部署LAMP服务
yum安装httpd、mariadb、mariadb-server、php、php-mysql
[[email protected] ~]# vim /var/www/html/test.php
<?php
$x=mysql_connect("localhost","root","123456");
if($x){ echo "ok"; }else{ echo "err"; };
?>

#################################################################
二、安装Nagios(源码包)
2.1 安装准备:编译工具 创建用户和组
[[email protected] ~]# rpm -q gcc gcc-c++
[[email protected] ~]# useradd nagios
[[email protected] ~]# groupadd nagcmd
[[email protected] ~]# usermod -G nagcmd nagios
2.2 装包:解包 配置 编译 安装 查看安装信息
[[email protected] ~]# tar xf nagios-4.2.4.tar.gz
[[email protected] ~]# cd nagios-4.2.4
[[email protected] nagios-4.2.4]# ./configure --help | more
--with-command-user=

[[email protected] nagios-4.2.4]# ./configure --with-nagios-user=nagios \

--with-nagios-group=nagcmd --with-command-user=nagios \
--with-command-group=nagcmd
[[email protected] nagios-4.2.4]# make all //编译
[[email protected] nagios-4.2.4]# make install //安装程序
[[email protected] nagios-4.2.4]# make install-init //安装控制脚本
// # vim /etc/rc.d/init.d/nagios 获得此脚本
[[email protected] nagios-4.2.4]# make install-commandmode //调权限
[[email protected] nagios-4.2.4]# make install-config //安装配置
[[email protected] nagios-4.2.4]# make install-webconf //部署网站配置
[[email protected] nagios-4.2.4]# make install-exfoliation //确定网页风格

2.3 安装目录说明
[[email protected] nagios-4.2.4]# ls /usr/local/nagios/
bin(可执行命令) etc(配置文件) libexec(监控插件) sbin(cgi文件) share(网页文件) var(日志文件状态信息文件)
[[email protected] nagios-4.2.4]# cd /usr/local/nagios/
[[email protected] nagios]# ls bin
nagios(验证配置是否正确) nagiostats(显示监控状态信息)

2.4 安装监控插件
[[email protected] ~]# tar -zxf nagios-plugins-2.1.4.tar.gz
[[email protected] ~]# cd nagios-plugins-2.1.4/
[[email protected] nagios-plugins-2.1.4]# ./configure &&make &&make install
[[email protected] nagios-plugins-2.1.4]# ls /usr/local/nagios/libexec/

2.5 启动nagios监控服务
(1) 设置登录监控页面的用户(nagiosadmin)和密码(自定义)
[[email protected] nagios-plugins-2.1.4]# vim /etc/httpd/conf.d/nagios.conf 查看监控页面用户的路径
[[email protected] ~]# htpasswd -c /usr/local/nagios/etc/htpasswd.users nagiosadmin
[[email protected] ~]# ls /usr/local/nagios/etc/
cgi.cfg htpasswd.users nagios.cfg objects resource.cfg
[[email protected] ~]# cat /usr/local/nagios/etc/htpasswd.users
nagiosadmin:$apr1$KGjigEh.$Vtf6Vv.y7LRv4T7t48R7a1
[[email protected] ~]# systemctl restart httpd //重启网站服务
[[email protected] ~]# /etc/rc.d/init.d/nagios status //查看nagios状态
No lock file found in /usr/local/nagios/var/nagios.lock
[[email protected] ~]# /etc/rc.d/init.d/nagios start //起nagios服务
Reloading systemd: [ 确定 ]
Starting nagios (via systemctl): [ 确定 ]

默认不用任何配置,就监控本机:
cpu负载 check_load
登录系统用户数量 check_users -w 1 -c 3
网站服务运行状态 check_http -H ip地址 [ -p 端口 ]
PING check_ping
根分区 check_disk
ssh服务 check_ssh
交换分区 check_swap
进程数量 check_procs
连接目标主机的tcp协议端口 check_tcp -H ip地址 -p 端口
###################################################################

三、在客户端 访问监控页面查看监控信息
ping -c 2 192.168.4.21
firefox http://192.168.4.21/nagios

###################################################################

四、配置nagios服务
4.1 监控过程
监控状态:Ok(正常) Warning(警告) Unknown(不知道) Critical(严重错误) Pending(监控中)
nagios服务运行是加载主配置文件ngaios.cfg,在配置文件中调用监控插件,运维人员是可以设置插件的监控阀值(警告值 和 错误值);nagios服务的插件把监控到的数据和 监控阀值 比较,根据比较结果来显示监控状态:
监控到的数据 < 警告值 显示OK
监控到的数据 > 警告值 且<错误值 显示Warning
监控到的数据 > 错误值 显示Critical
找不到调用的监控插件时 Unknown
正在获取数据 Pending
#################################################################
4.2 监控插件的使用
[[email protected] libexec]# cd /usr/local/nagios/libexec
./插件名 -h //查看插件的帮助信息
[[email protected] libexec]# ./check_users -w 1 -c 3 //设置警告值1,错误值3
USERS WARNING - 2 users currently logged in |users=2;1;3;0
[[email protected] libexec]# ./check_ping -H 192.168.4.17 -w 10,50% -c 20,75%
PING OK - Packet loss = 0%, RTA = 0.48 ms|rta=0.476000ms;10.000000;20.000000;0.000000 pl=0%;50;75;0

###########################################################
4.3 配置文件说明
[[email protected] ~]# cd /usr/local/nagios/etc
[[email protected] etc# ls
cgi.cfg htpasswd.users nagios.cfg objects resource.cfg

nagios.cfg(主配置文件)
[[email protected] etc]# /usr/local/nagios/bin/nagios -v /usr/local/nagios/etc/nagios.cfg //验证主配置文件
[[email protected] etc]# /usr/local/nagios/bin/nagiostats -c /usr/local/nagios/etc/nagios.cfg //命令行下输出监控统计信息
在/etc/bashrc下定义别名:alias checknagios=‘/usr/local/nagios/bin/nagios -v /usr/local/nagios/etc/nagios.cfg‘

resource.cfg 宏定义文件(设置nagios服务使用的变量)
$USER1$=/usr/local/nagios/libexec //默认的变量USER1=插件路径

[[email protected] etc]# cd objects/
[[email protected] objects]# ls
commands.cfg localhost.cfg switch.cfg timeperiods.cfg
contacts.cfg printer.cfg templates.cfg windows.cfg
++++++++++++++++++++++++++++++++++++++
commands.cfg 定义监控命令的文件

define command{
command_name 命令名
command_line 路径/插件名 参数
}
vim commands.cfg
在最后一行自定义一条监控命令
define command{
command_name check_local_boot
command_line /usr/local/nagios//libexec/check_disk -w 50% -c 25% -p /boot
}
+++++++++++++++++++++++++++++++++++++++++++++++++
localhost.cfg 监控本机配置文件
监控主机
define host{
use linux-server模板名
host_name 主机名
address 被监控主机的ip地址
}

监控资源
define service{
use local-service模板名
host_name 主机名
service_description 描述信息
check_command 命令名
}
+++++++++++++++++++++++++++++++++++++++++++++
contacts.cfg 指定接收监控报警消息邮箱地址
34 email [email protected] //34行邮件提醒
vim /etc/hosts //增加本机主机名在127.0.0.1,同时测试本机邮件是否能正常使用
[[email protected] ~]# mail -s "csadfgas" nagios < /etc/hosts
[[email protected] ~]# su - nagios
[[email protected] ~]$ mail
+++++++++++++++++++++++++++++++++++++++++++
timeperiods.cfg 定义监控时间模版配置文件
templates.cfg 定义监控模版配置文件

4.4 配置远端主机
4.4.1 监控公有数据(服务)
4.4.2 监控私有数据(系统运行情况)
4.5 监控本机 localhost.cfg
添加新的监控项 监控本机的引导分区
修改已有监控项的监控阀值 用户数量
删除已有的监控项 不监控交换分区
vim localhost.cfg
###############my monitor############################
define service{
use local-service ; Name of service template to use
host_name localhost
service_description boot
check_command check_local_boot
}





4.6 监控报警
[[email protected] ~]# dd if=/dev/zero of=/boot/a.txt bs=1M count=240 使得/boot下容量超标,若配置正确,会有报警邮件发出
[[email protected] ~]# su - nagios
[[email protected] ~]$ mail
N 3 [email protected] Mon Jan 8 06:08 31/897 " PROBLEM Service Al"
[[email protected] ~]# rm -rf /boot/a.txt //删除超标文件,模拟修复成功
[[email protected] ~]$ mail
N 4 [email protected] Mon Jan 8 06:18 31/881 "
RECOVERY Service A"
监控网页也显示正常
+++++++++++++++++++++++++++++++++++++++++++++++

nagios扩展内容
1 监控时调用自定义监控脚本
2 使用第三方邮件服务器发送报警邮件
3 主机依赖监控
4 服务依赖监控
5 让nagios 把监控到数据绘制图片

以上是关于搭建Nagios监控服务器的主要内容,如果未能解决你的问题,请参考以下文章

Nagios监控服务搭建

Nagios监控服务的搭建

Nagios NSCA 被动监控服务器搭建

搭建基于Nagios的监控系统——之监控远程Windows服务器

搭建基于Nagios的监控系统——之监控远程Linux服务器

Nagios+NSClient+nrpe环境搭建