监控之--Nagios如何监控本地主机及本地服务

Posted

tags:

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

上一节内容介绍了Nagios监控服务在linux环境下的安装过程,本节内容将详细介绍如何使用已经安装的Nagios服务的一些配置文件的使用以及如何监控本地相关服务,如要完成对一台主机的监控Nagios作为主程序是必不可少的,但是仅仅只要nagios也是完成不了的,以下内容简单介绍下nagios的主配置文件和对本地主机的监控

Nagios监控看那个角色配置:

    监控端和被监控端的收据收集(C/S架构),监控端和用户(B/S架构)之间的数据呈现

    Nagios主程序,插件包,nrpe等相关插件

一、配置文件检查

1.1 查看nagios主目录相关功能

[[email protected]]# ll /usr/local/nagios/
bin             #Nagios执行程序所在目录
etc             #nagios配置文件坐在目录,初始安装只有几个*.cfg文件俺
libexec         #监控所用命令,需要安装了nagios-plugins插件了才会有,检测命令,不装是空的
sbin            #Nagios的Cgi文件所在目录,外部命令所需要的文件存放目录
share           #Nagios前端页面
var             #日志文件,pid文件等

1.2 检查配置文件是否存在错误

[[email protected] ~]# /usr/local/nagios/bin/nagios -v /usr/local/nagios/etc/nagios.cfg 
……………………
……………………
Checking obsessive compulsive processor commands...
Checking misc settings...
Total Warnings: 0
Total Errors:   0

配置文件无异常,警告0,错误0

1.3 指定配置文件以守护进程的方式启动

[[email protected] ~]# /usr/local/nagios/bin/nagios -d /usr/local/nagios/etc/nagios.cfg

1.4 nagios.conf主要配置内容

[[email protected] ~]# cat /usr/local/nagios/etc/nagios.cfg  | grep -E -v "^#|^$"
log_file=/usr/local/nagios/var/nagios.log                #日志位置
cfg_file=/usr/local/nagios/etc/objects/commands.cfg      #命令文件位置
cfg_file=/usr/local/nagios/etc/objects/contacts.cfg      #定义联系人
cfg_file=/usr/local/nagios/etc/objects/timeperiods.cfg   #定义时间段
cfg_file=/usr/local/nagios/etc/objects/templates.cfg     #定义模板(联系人主机模板)
cfg_file=/usr/local/nagios/etc/objects/linehost.cfg      #监控远程主机相关配置(监控远程主机添加的配置文件)
cfg_file=/usr/local/nagios/etc/objects/localhost.cfg     #监控本机相关配置
object_cache_file=/usr/local/nagios/var/objects.cache    #
precached_object_file=/usr/local/nagios/var/objects.precache
resource_file=/usr/local/nagios/etc/resource.cfg         #资源变量配置文件,包括$USER1$变量(一个路径)
status_file=/usr/local/nagios/var/status.dat 
status_update_interval=10                                #状态更新时间,单位s
nagios_user=nagios                                       #指定守护进程运行用户
nagios_group=nagios                                      #指定进程运行用户组
check_external_commands=1                                #命令检查
command_check_interval=-1
command_file=/usr/local/nagios/var/rw/nagios.cmd
external_command_buffer_slots=4096
lock_file=/usr/local/nagios/var/nagios.lock
temp_file=/usr/local/nagios/var/nagios.tmp
temp_path=/tmp
event_broker_options=-1
log_rotation_method=d                                    #日志滚动,默认天
log_archive_path=/usr/local/nagios/var/archives          
sleep_time=0.25
service_check_timeout=60                                   #服务器检查超时时间
host_check_timeout=30
event_handler_timeout=30                                   
notification_timeout=30
ocsp_timeout=5
perfdata_timeout=5
[[email protected] ~]# ls /usr/local/nagios/etc/cgi.cfg                  #cgi配置文件,优先级高于nagios.cfg
[[email protected] ~]# cat /usr/local/nagios/etc/cgi.cfg | grep -E -v ‘^#|^$‘
main_config_file=/usr/local/nagios/etc/nagios.cfg               #指定Nagios的配置文件位置 
physical_html_path=/usr/local/nagios/share                      #指定web访问的物理路径
url_html_path=/nagios                                           #在url后加nagios方能访问
show_context_help=0                                             #
use_pending_states=1
use_authentication=1                                            #使用认证功能
use_ssl_authentication=0
 
authorized_for_system_information=nagiosadmin,admin             #认证用户
…………

二、Nagios监控服务的健康状态

2.1 监控本地NFS服务状态

[[email protected] ~]# cd /usr/local/nagios/etc/objects/
[[email protected] objects]# cp localhost.cfg localhost.cfg.bak

在文件末尾添加以下内容
define service{
        use                             local-service         ; Name of service template to use
        host_name                       localhost
        service_description             NFS
        check_command                   check_tcp!2049
        notifications_enabled           0
        }
 配置nagios.cfg主配置文件
 [[email protected] objects]# vim /usr/local/nagios/etc/nagios.cfg 
 cfg_file=/usr/local/nagios/etc/objects/localhost.cfg

 安装和启动NFS服务器

[[email protected] objects]# mkdir /nfs
[[email protected] objects]# vim /etc/exports 
/nfs *(rw)
[[email protected] objects]# /etc/init.d/nfs restart
验证nfs
[[email protected] objects]# showmount -e 192.168.31.101
Export list for 192.168.31.101:
/nfs *

检查localhost是否已经正常启用,检查nagios配置文件,如无错误,则重启nagios服务

[[email protected] objects]# /usr/local/nagios/bin/nagios -v /usr/local/nagios/etc/nagios.cfg 
[[email protected] objects]# service nagios restart

验证

[[email protected] objects]# service nfs stop
[[email protected] objects]# service nfs restart


2.2 Nagios远程监控mysql数据库状态

安装mysql服务器

[[email protected] ~]# yum -y install mysql mysql-server mysql-devel

[[email protected] ~]# service mysqld restart

[[email protected] ~]# chkconfig mysqld on

创建测试数据库

mysql> create database nagiostest;
mysql> grant select on nagiostest.* to [email protected]"localhost";
mysql> flush privileges;

测试

[[email protected] objects]# /usr/local/nagios/libexec/check_mysql -H 127.0.0.1 -u nagiostest -d nagiostest
Uptime: 438  Threads: 2  Questions: 11  Slow queries: 0  Opens: 15  Flush tables: 1  Open tables: 8  Queries per second avg: 0.25|Connections=4c;;; Open_files=16;;; Open_tables=8;;; Qcache_free_memory=0;;; Qcache_hits=0c;;; Qcache_inserts=0c;;; Qcache_lowmem_prunes=0c;;; Qcache_not_cached=0c;;; Qcache_queries_in_cache=0;;; Queries=11c;;; Questions=11c;;; Table_locks_waited=0c;;; Threads_connected=2;;; Threads_running=1;;; Uptime=438c;;;
[[email protected] objects]#

命令添加(添加的command_name必须在/usr/local/nagios/libexec/该文件夹下存在)

[[email protected] objects]# vim commands.cfg     
define command{
        command_name    check_mysql
        command_line    $USER1$/check_mysql -H $HOSTADDRESS$ -u nagiostest -d nagiostest
}
[[email protected] objects]# /usr/local/nagios/bin/nagios -v /usr/local/nagios/etc/nagios.cfg

无报错和警告则重启nagios服务

测试:关闭msyqld服务

[[email protected] libexec]# service mysqld stop

2.3监控远程主机mysql服务及其他服务

[[email protected] objects]# vim ../nagios.cfg 
新增配置监控配置文件及服务
cfg_file=/usr/local/nagios/etc/objects/remodehost.cfg
cfg_file=/usr/local/nagios/etc/objects/services.cfg
define host{
        use             linux-server
        host_name       node2.cn
        alias           MySQL-Serve
        address         192.168.31.102
        icon_image      server.gif
        statusmap_image server.gd2
        2d_coords       500,200
        3d_coords       500,200,100
}

添加监控的服务,可参考localhost.cfg配置文件相关服务配置方式进行配置

[[email protected] objects]# vim services.cfg 
define service{
        use                     local-service
        host_name               node2.cn
        service_groups          MysqlGroup
        service_description     MySqlSev
        check_command           check_mysql
}
define service{
        use                             local-service         ; Name of service template to u
se
        host_name                       node2.cn
        service_description             PING
        check_command                   check_ping!100.0,20%!500.0,60%
        }
define service{
        use                             local-service         ; Name of service template to u
se
        host_name                       node2.cn
        service_description             Root Partition
        check_command                   check_local_disk!20%!10%!/
        }
        …………
define servicegroup{
        servicegroup_name       MysqlGroup
        alias                   MySQLServer
        members                 node2.cn,MySqlSev
}

检查配置文件无错误和警告信息则重启服务

[[email protected] objects]# /usr/local/nagios/bin/nagios -v /usr/local/nagios/etc/nagios.cfg 
[[email protected] objects]# service nagios restart

通过浏览器查看web端监控主机情况






本文出自 “小熊运维” 博客,请务必保留此出处http://maoxiaoxiong.blog.51cto.com/11705634/1980785

以上是关于监控之--Nagios如何监控本地主机及本地服务的主要内容,如果未能解决你的问题,请参考以下文章

运维监控三剑客之Nagios

Nagios监控

监控之--Nagios监控远程windows主机及邮件告警功能的配置

监控利器Nagios之一:监控本地NFS和外部HTTPMySQL服务

监控之--使用NPRE监控Linux主机

监控三剑客之Nagios