SECURITY 04: Nagios监控实战 、 监控概述 、 Nagios监控服务

本文涉及的产品
云数据库 RDS MySQL Serverless,0.5-2RCU 50GB
简介:

抓包: tcpdump 选项 过滤条件
协议分析软件 : wireshark 
搭建CA服务器: CA

配置CA服务的过程:192.168.4.55 ca.tedu.cn tarena
1 部署证书签发环境:/etc/pki/tls/openssl.conf 
工作目录 
证书存放目录
私钥文件名 
根证书文件名
证书编号文件
证书索引文件
证书识别: 国家 省 城市 公司 部门 
匹配策略: 
2 创建私钥文件名: 命令 存储目录 文件名
3 创建根证书文件名:命令 存储目录 文件名
输入:国家 省 城市 公司 部门 邮箱 主机名
4 共享根证书文件 : 使用的服务可以 httpd ftp nfs samba

5 客户端测试并安装根证书文件

++++++++++++++++++++++++++++++++++++++++++++
给网站服务器签发数字证书文件 192.168.4.53 www.tedu.cn

网站服务器的配置:192.168.4.53 
1 创建私钥文件: 命令 存储目录 文件名
2 创建证书请求文件: 命令 存储目录 文件名 
输入:国家 省 城市 公司 部门 邮箱 主机名


3 提交证书请求文件给CA服务器

CA服务器的配置:192.168.4.55
1 审核证书请求文件 签发数字证书文件: 命令 存储目录 文件名
2 下发数字证书文件给网站服务器

3 在网站服务器上配置网站服务在运行时加载私钥文件和数字证书文件,并重启网站服务。

4 在客户端254 验证配置。
+++++++++++++++++++++++++++++++++++++++
DBA 运维工程师 IDC监控

一、监控 : 监视 并 控制

二、监控对象:服务器(网站 和 数据库)

三、监控哪些资源?
网络流量: ethX (进 出 的流量)
硬件资源的使用情况:cpu 内存 存储
服务运行状态: 运行 停止 休眠 僵尸 
操作系统运行情况: 进程数量 用户数量

四、监控的手段?
使用命令查看运行情况: ping sar uptime free df 
systemctl status 服务名 ps 
ps aux | wc -l users
wc -l /etc/passwd

编写监控脚本 做监控 : shell python

搭建监控服务器:对指定的主机做监控
Nagios Cacti Zabbix 
++++++++++++++++++++++++++++++++++++++++
准备新服务器 做监控服务器:配置要求:
#setenforce 0
#systemctl stop firewalld ; systemctl disable firewalld ; 
#配置本地yum源
#ip地址 192.168.4.56

+++++++++++++++++++++++++++++++++++++++++
A、搭建Nagios监控服务器
一、部署Nagios服务运行环境
#yum -y install httpd php
#systemctl start httpd ; systemctl enable httpd

#vim /var/www/html/test.php
<?php
phpinfo();
?>
:wq

在宿主打开浏览器访问 网页文件
http://192.168.4.56/test.php

二、安装Nagios软件(源码包)
2.1 安装准备
]# rpm -q gcc gcc-c++ || yum -y install gcc gcc-c++
[root@mon56 ~]# useradd nagios
[root@mon56 ~]# groupadd nagcmd
[root@mon56 ~]# usermod -G nagcmd nagios
2.2 安装软件包
#tar -zxf nagios-4.2.4.tar.gz
#cd nagios-4.2.4
[root@mon56 nagios-4.2.4]# ./configure \

--with-nagios-user=nagios --with-nagios-group=nagcmd \
--with-command-user=nagios --with-command-group=nagcmd
[root@mon56 nagios-4.2.4]# make all
make install
make install-init
make install-commandmode
make install-config
make install-webconf
make install-exfoliation
#ls /usr/local/nagios
bin etc libexec sbin share var

三、安装监控插件 (软件开发者编写好的脚本)
tar -zxf nagios-plugins-2.1.4.tar.gz
cd nagios-plugins-2.1.4
./configure
make 
make install 
#ls /usr/local/nagios/libexec/check_*

四、 启动监控服务
4.1 启动服务
#/etc/init.d/nagios status | start | stop |restart

#systemctl restart httpd

4.2 设置访问监控页面的用户名和密码
#sed -n '39p;52p' /etc/httpd/conf.d/nagios.conf 
39 Alias /nagios "/usr/local/nagios/share" //定义目录别名
52 AuthUserFile /usr/local/nagios/etc/htpasswd.users //用户认证

[root@mon56 libexec]# htpasswd -c /usr/local/nagios/etc/htpasswd.users nagiosadmin
New password: 
Re-type new password: 
Adding password for user nagiosadmin
[root@mon56 libexec]# 
[root@mon56 libexec]# cat /usr/local/nagios/etc/htpasswd.users
nagiosadmin:$apr1$Qa.IkjEz$PXVWVX9efnwhnCyA4aqeH.
[root@mon56 libexec]#

五、客户端254主机 访问监控服务器查看监控信息
URL: http://192.168.4.56/nagios
nagiosadmin
123456

  • nagios 服务默认监控本机资源,资源如下:
    CPU负载 check_load
    系统登录用户数量 check_users
    网站服务运行状态 check_http
    PING check_ping
    根分区空闲空间 check_disk
    ssh服务 check_ssh
    交换分区使用量 check_swap
    系统总进程数量 check_procs

监控显示状态: ok warning unknown critical pending
正常 警告 监控配置错误 报错 监控中

nagios服务的监控过程?
nagios服务运行时,调用监控插件,并设置监控的阀值,分别是警告值和错误值。nagios服务把插件获取到的数据和设置的阀值比较,然后显示监控状态。

监控到的数据 < warning值 显示 ok 状态
监控到的数据 > warning值 and < critical值 显示warning状态
监控到的数据 > critical值 显示 critical 状态

显示 unknown 状态 表示nagios服务配置文件有问题。
显示 pending 状态 表示正在获取被监控资源的数据

+++++++++++++++++++++++++++++++++++++++
B、配置Nagios监控服务器
1 配置文件说明
#cd /usr/local/ngaios/etc
#ls 
nagios.cfg 主配置文件 //ngios启动时加载的文件
29 cfg_file=/usr/local/nagios/etc/objects/commands.cfg
30 cfg_file=/usr/local/nagios/etc/objects/contacts.cfg
31 cfg_file=/usr/local/nagios/etc/objects/timeperiods.cfg
32 cfg_file=/usr/local/nagios/etc/objects/templates.cfg
33 
34 # Definitions for monitoring the local (Linux) host
35 cfg_file=/usr/local/nagios/etc/objects/localhost.cfg

resource.cfg 宏定义文件 //设置ngios服务使用的变量
$USER1$=/usr/local/nagios/libexec

cgi.cfg //定义CGI文件访问权限

#cd object 
#ls 
templates.cfg //定义监控模板配置文件
timeperiods.cfg //定义监控时间模板配置文件

contacts.cfg //定义接收报警邮件的配置文件
34: email nagios@localhost

commands.cfg //定义监控命令
define command {
command_name check_local_disk //命令名称
command_line $USER1$/check_disk -w $ARG1$ -c $ARG2$ -p $ARG3$ //监控插件
}

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 root_paticion //设置监控资源显示名称
check_command check_local_disk!20%!10%!/ //调用的监控命令
}

如何验证配置是否有错误?
]# /usr/local/nagios/bin/nagios -v /usr/local/nagios/etc/nagios.cfg

]# alias checknagios='/usr/local/nagios/bin/nagios -v /usr/local/nagios/etc/nagios.cfg'

vim ~/.bashrc
alias checknagios='/usr/local/nagios/bin/nagios -v /usr/local/nagios/etc/nagios.cfg'
:wq

2 监控插件的使用
#cd /usr/local/nagios/libexec/
./插件名 --help //查看插件的帮助信息

CPU负载 check_load
系统登录用户数量 check_users
网站服务运行状态 check_http
PING check_ping
根分区空闲空间 check_disk (监控磁盘的空闲空间)
ssh服务 check_ssh
交换分区使用量 check_swap
系统总进程数量 check_procs

]# ./check_users -w 1 -c 2
]# ./check_users -w 10 -c 15

]# ./check_http -I 192.168.4.51
]# ./check_http -I 192.168.4.51 -p 8090

]# ./check_ssh -H 192.168.4.51
]# ./check_ssh -H 192.168.4.51 -p 22122

]# df -h
]# ./check_disk -w 50% -c 25% -p /boot
]# dd if=/dev/zero of=/boot/a.txt bs=1M count=250
]# ./check_disk -w 50% -c 25% -p /boot

]# ./check_disk -w 25% -c 10% -p /

]# ./check_procs -w 20 -c 21 -s R

]# ./check_procs -w 10 -c 11 -s Z (pid 0-65535)

]# ./check_procs -w 20 -c 21 -s S

]# ./check_procs -w 100 -c 101

]# ./check_load -w 0.5,1.0,1.5 -c 1.0,2.0,2.5

]# ./check_ping -H 192.168.4.51 -w 10,50% -c 15,75%

]# ./check_swap -w 50% -c 25%

]# ./check_tcp -H 192.168.4.51 -p 80
]# ./check_tcp -H 192.168.4.51 -p 3306
——————————————————————
监控主机的配置步骤:
1 定义监控命令 commands.cfg
2 创建监控主机的配置文件 vim ser51.cfg
3 在服务主机配置文件nagios.cfg 文件里 加载监控主机的配置文件ser51.cfg
4 检查配置是否有错误 checknagios
5 重启nagios监控服务
6 访问监控页面查看监控信息
7 若监控项有警告状态或严重错误状态的 查看是否收到报警邮件
++++++++++++++++++++++++++++++++
监控本机配置?监控本机时,
监控本机引导分区的使用情况:空闲空间小于50% 警告 空闲空间小于25% 报错,

修改监控本机登录用户数量 警告值修改成1 错误值修改成2 ;

不监控本机的交换分区的使用情况。
+++++++++++++++++++++++++++++++++
3 监控远端主机 192.168.4.51
3.1 监控远端主机的公有资源(各种服务 httpd mysql ssh )

vim commands.cfg
#monitor mysqld
define command {
command_name check_51_mysqld
command_line $USER1$/check_tcp -H 192.168.4.51 -p 3306
}
#monitor httpd
define command {
command_name check_51_httpd
command_line $USER1$/check_http -H 192.168.4.51 -p 8090
}
#monitor sshd
define command {
command_name check_51_sshd
command_line $USER1$/check_ssh -H 192.168.4.51
}
[root@mon56 objects]#

[root@mon56 objects]# cat ser51.cfg 
define host{
use linux-server 
host_name ser51
address 192.168.4.51
}

define service{
use local-service 
host_name ser51
service_description httpd
check_command check_51_httpd
}

define service{
use local-service 
host_name ser51
service_description mysqld
check_command check_51_mysqld
}

define service{
use local-service 
host_name ser51
service_description sshd
check_command check_51_sshd
}
[root@mon56 objects]#

[root@mon56 etc]# grep ser51.cfg nagios.cfg 
cfg_file=/usr/local/nagios/etc/objects/ser51.cfg

#checknagios

#/etc/init.d/nagios restart

++++++++++++++++++++++++++++++++++++++
3.2 监控远端主机的私有资源(系统运行情况)
磁盘使用量 登录用户数量 进程数量

A、配置被监控端 51
1、安装获取私有资源的插件 ,并使用插件
]# tar -zxf nagios-plugins-2.1.4.tar.gz 
]# cd nagios-plugins-2.1.4/
]# rpm -q gcc gcc-c++ || yum -y install gcc gcc-c++
]# ./configure && make && make install
]# ls /usr/local/nagios

/usr/local/nagios/libexec/check_disk -w 20% -c 10% -p /
/usr/local/nagios/libexec/check_disk -w 20% -c 10% -p /boot
/usr/local/nagios/libexec/check_users -w 1 -c 2
/usr/local/nagios/libexec/check_procs -w 10 -c 11 -s R
/usr/local/nagios/libexec/check_procs -w 5 -c 6 -s Z
/usr/local/nagios/libexec/check_procs -w 100 -c 101

2、运行nrpe服务
2.1 装包
#rpm -q openssl openssl-devel
#yum -y install openssl-devel
#useradd nagios
]# tar -zxf nrpe-3.0.1.tar.gz 
]# cd nrpe-3.0.1/
]# ./configure
]# make all
make install-daemon
make install-config
make install-init
make install-plugin

2.2 修改配置文件
]# vim /usr/local/nagios/etc/nrpe.cfg

98 allowed_hosts=127.0.0.1, 192.168.4.56

command[nrpe_51_users]=/usr/local/nagios/libexec/check_users -w 1 -c 2

command[nrpe_51_root]=/usr/local/nagios/libexec/check_disk -w 20% -c 10% -p /

command[nrpe_51_boot]=/usr/local/nagios/libexec/check_disk -w 20% -c 10% -p /boot

command[nrpe_51_prun]=/usr/local/nagios/libexec/check_procs -w 10 -c 11 -s R

command[nrpe_51_pzbi]=/usr/local/nagios/libexec/check_procs -w 5 -c 6 -s Z

command[nrpe_51_psum]=/usr/local/nagios/libexec/check_procs -w 100 -c 101

2.3 启动nrpe服务
]# systemctl enable nrpe ; systemctl start nrpe
]#netstat -utnalp | grep :5666

2.4 测试nrep服务配置
[root@PC51 ~]# /usr/local/nagios/libexec/check_nrpe -H 127.0.0.1 -p 5666 -c nrpe_51_users

USERS OK - 1 users currently logged in |users=1;1;2;0
[root@PC51 ~]#

[root@PC51 ~]# /usr/local/nagios/libexec/check_nrpe -H 127.0.0.1 -p 5666 -c nrpe_51_prun

PROCS OK: 2 processes with STATE = R | procs=2;10;11;0;
[root@PC51 ~]#

B、配置监控服务器:192.168.4.56
1、安装连接nrpe服务的插件check_nrpe 并测试
#yum -y install openssl-devel
]# tar -zxf nrpe-3.0.1.tar.gz 
]# cd nrpe-3.0.1/
]# ./configure
]# make all
]# make install-plugin
]#ls /usr/local/nagios/libexec/check_nrpe

[root@mon56 nrpe-3.0.1]# /usr/local/nagios/libexec/check_nrpe -H 192.168.4.51 -p 5666 -c nrpe_51_users

USERS OK - 0 users currently logged in |users=0;1;2;0
[root@mon56 nrpe-3.0.1]#

2、定义监控远端主机私有资源的监控命令
vim commands.cfg
define command {
command_name check_51_user
command_line $USER1$/check_nrpe -H 192.168.4.51 -p 5666 -c nrpe_51_users
}

3、监控远端主机时,调用定义的监控命令 
vim ser51.cfg
define service{
use local-service
host_name ser51
service_description users
check_command check_51_user
}
:wq
4、配置nagios服务运行时,加载监控远端主机的配置文件
s]# grep ser51.cfg /usr/local/nagios/etc/nagios.cfg 
cfg_file=/usr/local/nagios/etc/objects/ser51.cfg

5、检查配置并重启nagios服务
#checknagios
#systemctl restart nagios

6、登录监控页面查看监控信息

+++++++++++++++++++++++++++++++++
4 配置监控报警(当被监控资源显示的状态是warning 或 critical 时,nagios服务会给指定的邮箱发邮件)
]# grep email /usr/local/nagios/etc/objects/contacts.cfg 
email nagios@localhost

[root@mon56 ~]# hostname
mon56
[root@mon56 ~]#

36 vim /etc/hosts
127.0.0.1 mon56

37 systemctl restart postfix
38 grep nagios /etc/passwd
39 echo 123456 | passwd --stdin nagios
40 mail -s "test" nagios < /etc/hosts
41 su - nagios
42 mail

——————————————————————————————
部署nagios监控服务器的步骤:
1 部署服务运行环境:yum -y install httpd php
2 安装nagios软件:安装准备、 安装软件包、查看安装目录
/usr/local/nagios
3 安装监控插件: 解包 配置 编译 安装 查看是否安装成功
/usr/local/nagios/libexec

4 启动nagios监控服务: 
重启网站务/etc/httpd/conf.d/nagios.conf
设置访问监控页面的管理员nagiosadmin 密码123456 启动nagios监控服务

5 访问管理页面,查看监控信息

  • 默认就监控本机,默认监控的资源如下:

6 配置文件说明:
#cd /usr/local/nagios/etc
#ls
nagios.cfg resource.cfg cgi.cfg

#cd /usr/local/nagios/etc/object
#ls

templates.cfg 监控主机 linux-server 监控资源 local-service

timeperiods.cfg 24x7

contacts.cfg email nagios@localhost

commands.cfg 
define command { 
command_name 命令名
command_line 插件名 选项 参数
}

localhost.cfg 
define host { }
define service { }

检查配置文件是否与语法错误:

++++++++++++++++++++++++++++++
扩展知识:
配置nagios服务监控时,调用自定义监控脚本(shell脚本)http://blog.csdn.net/reblue520/article/details/52045999
配置nagios服务使用短信的方式接收报警消息。
配置nagios主机依赖监控
配置nagios服务依赖监控
配置nagios绘制监控图像



     本文转自sweak_h 51CTO博客,原文链接:http://blog.51cto.com/13478354/2067898,如需转载请自行联系原作者






相关实践学习
基于CentOS快速搭建LAMP环境
本教程介绍如何搭建LAMP环境,其中LAMP分别代表Linux、Apache、MySQL和PHP。
全面了解阿里云能为你做什么
阿里云在全球各地部署高效节能的绿色数据中心,利用清洁计算为万物互联的新世界提供源源不断的能源动力,目前开服的区域包括中国(华北、华东、华南、香港)、新加坡、美国(美东、美西)、欧洲、中东、澳大利亚、日本。目前阿里云的产品涵盖弹性计算、数据库、存储与CDN、分析与搜索、云通信、网络、管理与监控、应用服务、互联网中间件、移动服务、视频服务等。通过本课程,来了解阿里云能够为你的业务带来哪些帮助 &nbsp; &nbsp; 相关的阿里云产品:云服务器ECS 云服务器 ECS(Elastic Compute Service)是一种弹性可伸缩的计算服务,助您降低 IT 成本,提升运维效率,使您更专注于核心业务创新。产品详情: https://www.aliyun.com/product/ecs
相关文章
|
数据采集 运维 监控
配置centreon监控系统| 学习笔记
快速学习配置centreon监控系统。
142 0
配置centreon监控系统| 学习笔记
|
监控 测试技术 PHP
|
监控 Linux Perl
|
Web App开发 监控 关系型数据库
|
Web App开发 监控 PHP