linux Nagios监控

Posted XxSec

tags:

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

监控目标
监控目标主机分为四个部分
硬件资源
操作系统
数据库
应用软件

监控目的:
进行服务器性能调整前,知道调整什么,系统瓶颈在什么地方
被一部分必须同时监控,内容包括吞吐量,反应时间,使用率等

监控命令:
进程监控
ps
top
存储监控
free
swapon -s
df -h
网络监控
ifconfig
netstat
ping
traceroute
性能监控
常用的性能监控命令
uptime cpu负载
mpastat 查看cpu状态
vmstat 查看内存状态
iostat 查看硬盘状态
netstat 查看网络状态
sar 查看以上四类信息

——————————————————————————————————————————————————————————————————

监控服务
Nagios
是一款用来监视系统和网络的开源应用软件,可以在linux或unix操作系统上运行
利用众多的插件实现对本机和远端服务的监控,当监控对象出现异常,nagios就会及时给管理人员告警
可以自定义shell脚本进行监控
监控主机资源和网络服务,通过web页面来监控对象状态

Nagios服务运行时,自动调用监控插件目录下的插件对指定服务器的资源做监控
在调用插件时,管理员可以配置,调用监控插件对资源做监控的值,

值分两种,一种警告值(数字,百分比),一种是错误值(数字,百分百)
如果监控对象的使用率小于警告值就是正常状态,显示ok
如果监控对象的使用率大于警告值且小于错误值,显示warning
监控对象的使用率大于错误值,是错误状态,显示crtual

Cacti
一套基于phpmysql,snmp,rrdtool开发的网络流量监测图形分析工具
cacti通过snmpget获取数据,使用rrdtool绘画图形,最后通过web方式呈现出来
cacti三层架构
数据采集层:通过snmp或自定义脚本进行数据采集
数据存储层:通过cacti模板等数据存放至mysql中
数据展现层:通过web方式呈现出来

Nagios与Cacti区别
Nagios:故障分析比较好,报警机制比较好
Cacti:流量与图形展现比较好

————————————————————————————————————————————————————————————————————

搭建监控服务器
配置思路
1.定义监控命令:
[[email protected] objects]# vim commands.cfg
define command{
command_name (命令名)
command_line (设置使用的插件)
}
2.在监控服务器的配置文件里调用定义的监控命令:
[[email protected] objects]# vim localhost.cfg
define service{
use (监控资源使用的模板)
host_name (监控的主机名)
service_description (监控的描述信息)
check_command (监控命令)
}

3.验证nagios配置格式:
/usr/local/nagios/bin/nagios -v /usr/local/nagios/etc/nagios.cfg
4.重启监控服务:
/etc/init.d/nagios restart
5.访问监控页面,查看监控信息

实例:
在zhuji1服务器上部署nagios监控服务

安装环境:
[[email protected] ~]# groupadd nagcmd(nagios的配置文件里只有这个用户组才有权限)
[[email protected] ~]# useradd nagios(添加用户)
[[email protected] ~]# usermod -G nagcmd nagios(放入组)
[[email protected] ~]# yum -y install gcc gcc-c++(源码包安装,安装编译工具)
[[email protected] ~]# yum -y install httpd(监控通过web页面查看,所以要安装httpd)
[[email protected] ~]# echo hail hydra > /var/www/html/index.html(测试页面)
[[email protected] ~]# yum -y install php(页面是php写的,安装php以解析页面)
[[email protected] ~]# vim /var/www/html/test.php(测试页面)
<?php
echo "hail hydra";
?>
[[email protected] ~]# yum -y install elinks(命令行访问工具)
安装Nagios
[[email protected] ~]# unzip nagios.zip
[[email protected] ~]# cd nagios
[[email protected] nagios]# ls
nagios-3.2.1.tar.gz nrpe-2.12.tar.gz
nagios-plugins-1.4.14.tar.gz ntop-3.3.7.tar.gz
[[email protected] nagios]# tar -xf nagios-3.2.1.tar.gz
[[email protected] nagios-3.2.1]# ./configure --with-nagios-user=nagios --with-nagios-group=nagcmd --with-command-user=nagios --with-command-group=nagcmd(可以使用这个软件的用户,用户组)
type ‘make all‘ to compile the main program and CGIs.(提示执行make all)
[[email protected] nagios-3.2.1]# make all(执行后会提示下一步)
make install(安装)
make install-init(安装初始化启动脚本)
make install-commandmode(修改访问权限)
make install-config(安装配置文件的列子文件)
make install-webconf(安装网页配置文件)
[[email protected] nagios-3.2.1]# make install
[[email protected] nagios-3.2.1]# make install-init
[[email protected] nagios-3.2.1]# make install-commandmode
[[email protected] nagios-3.2.1]# make install-config
[[email protected] nagios-3.2.1]# make install-webconf
[[email protected] ~]# ls /usr/local/nagios/(安装目录)
bin etc libexec sbin share var
安装Nagios监控插件
[[email protected] nagios]# tar -xf nagios-plugins-1.4.14.tar.gz
[[email protected] nagios-plugins-1.4.14]# ./configure
[[email protected] nagios-plugins-1.4.14]# make
[[email protected] nagios-plugins-1.4.14]# make install
[[email protected] ~]# ls /usr/local/nagios/libexec/check_*(插件目录)
启动Nagios监控服务:(默认不用作任何配置,Nagios服务默认监控本机)
[[email protected] ~]# /etc/rc.d/init.d/nagios start
[[email protected] ~]# vim /etc/httpd/conf.d/nagios.conf(打开配置文件)
39 AuthUserFile /usr/local/nagios/etc/htpasswd.users(用户认证的目录)
[[email protected] etc]# vim cgi.cfg(根据这里面的用户信息,创建用户)
authorized_for_system_information=nagiosadmin(看系统认证信息的名)
...... .......... ........... .....
[[email protected] ~]# htpasswd -c /usr/local/nagios/etc/htpasswd.users nagiosadmin(创建用户)
New password: 密码
Re-type new password: 确认密码
访问Nagios监控服务的web页面,查看监控信息
[[email protected] ~]# /etc/init.d/httpd restart
[[email protected] ~]# firefox http://192.168.4.1/nagios
默认监控的资源
Current Load:cpu负载
Current Users:登陆的用户数
HTTP:网站服务
PING:是否在线
Root Partition:系统根分区使用量
SSH:远程服务
Swap Usage:交换分区的使用情况
Total Processes:总进程数

nagios服务配置文件说明
[[email protected] ~]# cd /usr/local/nagios/etc/
[[email protected] etc]# ls
cgi.cfg(定义访问cgi文件的用户名) htpasswd.users nagios.cfg(nagios服务的主配置文件) objects resource.cfg(定义nagios服务使用的变量)
[[email protected] objects]# ls
commands.cfg(定义监控命令) localhost.cfg(监控本机的配置文件) switch.cfg timeperiods.cfg(定义监控时间模板)
contacts.cfg(定义接收报警邮件的邮箱地址) printer.cfg templates.cfg(定义监控模板) windows.cfg

监控插件的使用
[[email protected] ~]# cd /usr/local/nagios/libexec/
监控用户:
[[email protected] libexec]# ./check_users
格式:check_users -w <users> -c <users>
选项:
-w:警告值,登陆用户大于这个值
-c:严重错误,大于这个数错误
[[email protected] libexec]# ./check_users -w 3 -c 5(用户登陆数大于3,小于5警告,)
USERS OK - 2 users currently logged in |users=2;3;5;0
[[email protected] libexec]# ./check_users -w 1 -c 1(用户登陆数大于1,错误)
USERS CRITICAL - 2 users currently logged in |users=2;1;1;0

监控磁盘:
[[email protected] libexec]# ./check_disk
格式:
check_disk -w limit -c limit [-W limit] [-K limit] {-p path | -x device}
[-C] [-E] [-e] [-g group ] [-k] [-l] [-M] [-m] [-R path ] [-r path ]
[-t timeout] [-u unit] [-v] [-X type]
选项:
-w:警告值,登陆用户大于这个值
-c:严重错误,大于这个数错误
-p:指定磁盘
示例:
[[email protected] libexec]# ./check_disk -w 10% -c 5% -p /tmp
DISK OK - free space: / 43784 MB (96% inode=98%);| /=1568MB;43007;45396;0;47786

监控进程:
[[email protected] libexec]# ./check_procs
格式:
check_procs -w <range> -c <range> [-m metric] [-s state] [-p ppid]
[-u user] [-r rss] [-z vsz] [-P %cpu] [-a argument-array]
[-C command] [-t timeout] [-v]
选项:
-w:警告值,登陆用户大于这个值
-c:严重错误,大于这个数错误
--metric:要监控的进程名
示例:
[[email protected] libexec]# ./check_procs -w 10 -c 20 --metric=CPU
CPU WARNING: 1 warn out of 101 processes

监控http:
[[email protected] libexec]# ./check_http
格式:
check_http -H <vhost> | -I <IP-address> [-u <uri>] [-p <port>]
选项:
-H:监控的主机
-I:监控的ip地址
-p:监控的端口
-u:监控的url地址
-w:警告值,登陆用户大于这个值
-c:严重错误,大于这个数错误
实例:
[[email protected] libexec]# ./check_http -H 192.168.4.1 -p 21
拒绝连接(没开21端口)
HTTP CRITICAL - Unable to open TCP socket


配置监控本机
[[email protected] ~]# cd /usr/local/nagios/etc/objects/
localhost.cfg(监控本机的配置文件)
[[email protected] objects]# vim localhost.cfg
define host{
use linux-server(监控模板名)
host_name localhost(定义主机名)
alias localhost(主机的描述信息,别名)
address 127.0.0.1(指定被监控机器的ip地址)
}
define service{
use local-service(监控资源使用的模板)
host_name localhost(监控的主机名)
service_description Swap Usage(监控的描述信息)
check_command check_local_swap!20!10(监控命令)
}

自定义监控资源 commands.cfg
[[email protected] ~]# cd /usr/local/nagios/etc/objects/
[[email protected] objects]# vim commands.cfg
define command{
command_name check_http(命令名)
command_line $USER1$/check_http -I $HOSTADDRESS$ $ARG1$(设置使用的插件)
}


实例:
自定义监控本机资源,监控本机的boot分区,监控本机的ftp服务的状态,不监控交换分区
[[email protected] objects]# vim localhost.cfg
define service{
use local-service
host_name localhost
service_description ftp
check_command check_local_ftp
}

define service{
use local-service
host_name localhost
service_description boot
check_command check_local_boot
}
[[email protected] objects]# vim commands.cfg
define command{
command_name check_local_boot
command_line $USER1$/check_disk -w 60 -c 20 -p /boot
}

define command{
command_name check_local_ftp
command_line $USER1$/check_ftp -H localhost
}

[[email protected] objects]# /usr/local/nagios/bin/nagios -v /usr/local/nagios/etc/nagios.cfg(验证nagios配置格式)
[[email protected] objects]# /etc/init.d/nagios restart(重启监控服务)
[[email protected] objects]# firefox http://192.168.4.1/nagios(访问测试)

配置监控报警
[[email protected] objects]# vim contacts.cfg
email [email protected]
[email protected]
[[email protected] objects]# mail -u nagios(查看邮件)
Heirloom Mail version 12.4 7/29/08. Type ? for help.
"/var/mail/nagios": 1 message 1 new
>N 1 [email protected] Thu Sep 14 15:26 31/869 "** PROBLEM Service Ale"
& 1
Message 1:
From [email protected] Thu Sep 14 15:26:30 2017
Return-Path: <[email protected]>
X-Original-To: [email protected]
Delivered-To: [email protected]
Date: Thu, 14 Sep 2017 15:26:30 +0800
To: [email protected]
Subject: ** PROBLEM Service Alert: localhost/ftp is CRITICAL **
User-Agent: Heirloom mailx 12.4 7/29/08
Content-Type: text/plain; charset=utf-8
From: [email protected]
Status: R

***** Nagios *****

实例:监控远端(192.168.4.2)服务器的ftp服务和http服务
[[email protected] etc]# vim nagios.cfg
cfg_file=/usr/local/nagios/etc/objects/192.168.4.2.cfg(创建新的监控)
[[email protected] etc]# vim /usr/local/nagios/etc/objects/192.168.4.2.cfg(写入配置)
define host {
use linux-server(监控模板)
host_name server2(被监控名主机,随便写)
alias this my server(被监控主机标识)
address 192.168.4.2(被监控主机ip地址)
}

define service {(监控的服务)
use local-service
host_name server2
service_description ftp(监控的服务名)
check_command check_2_ftp(监控命令)
}

define service {(监控的服务)
use local-service
host_name server2
service_description httpd(监控的服务名)
check_command check_2_httpd(监控命令)
}
[[email protected] etc]# vim /usr/local/nagios/etc/objects/commands.cfg
define command{
command_name check_2_ftp
command_line $USER1$/check_ftp -H 192.168.4.2
}

define command{
command_name check_2_httpd
command_line $USER1$/check_httpd -H 192.168.4.2
[[email protected] objects]# /usr/local/nagios/bin/nagios -v /usr/local/nagios/etc/nagios.cfg
[[email protected] objects]# /etc/init.d/nagios restart


实例:配置监控远端服务器上的私有数据(登陆用户数量 引导分区使用量 进程数量)

被监控端配置(zhuji2:192.168.4.2)
安装监控插件
[[email protected] ~]# tar -xf nagios-plugins-1.4.14.tar.gz
[[email protected] nagios-plugins-1.4.14]# ./configure
[[email protected] nagios-plugins-1.4.14]# make
[[email protected] nagios-plugins-1.4.14]# make install(默认就安装在/usr/local/nagios目录下)
[[email protected] ~]# /usr/local/nagios/libexec/check_users -w 3 -c 5(测试运行插件)
USERS OK - 2 users currently logged in |users=2;3;5;0
[[email protected] ~]# /usr/local/nagios/libexec/check_disk -w 20% -c 10% -p /boot
DISK OK - free space: /boot 415 MB (92% inode=99%);| /boot=35MB;380;428;0;476
运行nrpe服务
[[email protected] ~]# tar -xf nrpe-2.12.tar.gz
[[email protected] nrpe-2.12]# useradd nagios
[[email protected] nrpe-2.12]# groupadd nagcmd
[[email protected] nrpe-2.12]# usermod -G nagios nagios
[[email protected] nrpe-2.12]# yum -y install openssl-devel(安装加密传输)
[[email protected] nrpe-2.12]# ./configure
[[email protected] nrpe-2.12]# make
[[email protected] nrpe-2.12]# make install
[[email protected] nrpe-2.12]# make install-plugin(安装插件,这插件专门用来连接的)
[[email protected] nrpe-2.12]# make install-daemon
[[email protected] nrpe-2.12]# make install-daemon-config(此服务的配置文件)
[[email protected] nrpe-2.12]# make install-xinetd
[[email protected] nrpe-2.12]# yum -y install xinetd(这个服务需要xinetd叫醒nrpe)
[[email protected] nrpe-2.12]# vim /etc/xinetd.d/nrpe (nrpe的主配置文件)
only_from = 127.0.0.1 192.168.4.1(监控服务器的ip地址)
[[email protected] nrpe-2.12]# vim /etc/services(添加端口)
nrpe 5666/tcp #nrpe
[[email protected] ~]# /etc/init.d/xinetd restart
修改nrp服务的配置文件nrpe.cfg,定义监控命令
[[email protected] ~]# vim /usr/local/nagios/etc/nrpe.cfg
command[命令名]=使用的插件
command[check_2_users]=/usr/local/nagios/libexec/check_users -w 5 -c 10
command[check_2_load]=/usr/local/nagios/libexec/check_load -w 15,10,5 -c 30, 25,20


监控端配置(zhuji1:192.168.4.1)
定义监控命令
[[email protected] nagios]# tar -xf nrpe-2.12.tar.gz
[[email protected] nagios]# yum -y install openssl-devel
[[email protected] nrpe-2.12]# ./configure
[[email protected] nrpe-2.12]# make all
[[email protected] nrpe-2.12]# make install-plugin(安装插件)
使用插件连接被监控主机的nrpe服务并测试定义的监控命令
[[email protected] nrpe-2.12]# /usr/local/nagios/libexec/check_nrpe -H 192.168.4.2 -c check_2_users(调用zhuji2的监控命令)
USERS OK - 2 users currently logged in |users=2;5;10;0
[[email protected] nrpe-2.12]# /usr/local/nagios/libexec/check_nrpe -H 192.168.4.2 -c check_2_boot
DISK OK - free space: /boot 415 MB (92% inode=99%);| /boot=35MB;380;428;0;476
定义监控命令
[[email protected] ~]# vim /usr/local/nagios/etc/objects/commands.cfg
define command{
command_name check_2_users
command_line $USER1$/check_nrpe -H 192.168.4.2 -c check_2_users
}

define command{
command_name check_2_boot
command_line $USER1$/check_nrpe -H 192.168.4.2 -c check_2_boot
}

define command{
command_name check_2_process
command_line $USER1$/check_nrpe -H 192.168.4.2 -c check_2_procs
}

define command{
command_name check_2_zombieprocess
command_line $USER1$/check_nrpe -H 192.168.4.2 -c check_2_zombie_procs
}
[[email protected] objects]# /usr/local/nagios/bin/nagios -v /usr/local/nagios/etc/nagios.cfg
[[email protected] objects]# /etc/init.d/nagios restart































































































































































































































































































































































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

linux Nagios监控

linux学习笔记-nagios监控的简单配置

Nagios基于NRPE 监控远程Linux主机

Nagios 简介

Linux监控   Nagios

监控三剑客之Nagios监控服务器