Linux 监控 IDC 监控
监控的对象:服务器(运维岗位)
监控什么:(资源)
1,硬件的使用率(cpu 内存 存储)
2,监控应用服务的状态(运行 停止)
3,操作系统的运行情况(进程数量 系统的用户数量)
4,网络接口数据流量(进 出)
如何监控?
使用 crond 执行自定义监控脚本
系统提供的对应命令查看
使用软件搭建监控服务做监控
Nagios 监控服务器
监控服务器环境 192.168.4.21
一,部署 LAMP 服务
yum 安装 httpd,mariadb,mariadb-server,php,php-mysql
二,安装 Nagios(源码包)
[root@monitor21 ~]# vim /var/www/html/test.php
$x=mysql_connect("localhost","root","123456");
if($x){echo"ok";}else{echo"err";};
>
#################################################################
2.1 安装准备:编译工具 创建用户和组
2.2 装包:解包 配置 编译 安装 查看安装信息
[root@monitor21 ~]# rpm -q gcc gcc-c++
[root@monitor21 ~]# useradd nagios
[root@monitor21 ~]# groupadd nagcmd
[root@monitor21 ~]# usermod -G nagcmd nagios
--with-nagios-group=nagcmd --with-command-user=nagios \
[root@monitor21 ~]# tar xf nagios-4.2.4.tar.gz
[root@monitor21 ~]# cd nagios-4.2.4
[root@monitor21 nagios-4.2.4]# ./configure --help | more
--with-command-user=
[root@monitor21 nagios-4.2.4]# ./configure --with-nagios-user=nagios \
2.3 安装目录说明
--with - command - group = nagcmd[root@monitor21 nagios - 4.2.4]#make all // 编译
[root@monitor21 nagios - 4.2.4]#make install // 安装程序
[root@monitor21 nagios - 4.2.4]#make install - init // 安装控制脚本
// # vim /etc/rc.d/init.d/nagios 获得此脚本
[root@monitor21 nagios - 4.2.4]#make install - commandmode // 调权限
[root@monitor21 nagios - 4.2.4]#make install - config // 安装配置
[root@monitor21 nagios - 4.2.4]#make install - webconf // 部署网站配置
[root@monitor21 nagios - 4.2.4]#make install - exfoliation // 确定网页风格
[root@monitor21 nagios-4.2.4]# ls /usr/local/nagios/
bin(可执行命令) etc(配置文件) libexec(监控插件) sbin(cgi 文件) share(网页文件) var(日志文件状态信息文件)
nagios(验证配置是否正确) nagiostats(显示监控状态信息)
[root@monitor21 nagios-4.2.4]# cd /usr/local/nagios/
[root@monitor21 nagios]# ls bin
2.4 安装监控插件
2.5 启动 nagios 监控服务
[root@monitor21 ~]# tar -zxf nagios-plugins-2.1.4.tar.gz
[root@monitor21 ~]# cd nagios-plugins-2.1.4/
[root@monitor21 nagios-plugins-2.1.4]# ./configure &&make &&make install
[root@monitor21 nagios-plugins-2.1.4]# ls /usr/local/nagios/libexec/
(1) 设置登录监控页面的用户 (nagiosadmin) 和密码(自定义)
Reloading systemd: [确定]
[root@monitor21 nagios-plugins-2.1.4]# vim /etc/httpd/conf.d/nagios.conf 查看监控页面用户的路径
[root@monitor21 ~]# htpasswd -c /usr/local/nagios/etc/htpasswd.users nagiosadmin
[root@monitor21 ~]# ls /usr/local/nagios/etc/
cgi.cfg htpasswd.users nagios.cfg objects resource.cfg
[root@monitor21 ~]# cat /usr/local/nagios/etc/htpasswd.users
nagiosadmin:$apr1$KGjigEh.$Vtf6Vv.y7LRv4T7t48R7a1
[root@monitor21 ~]# systemctl restart httpd // 重启网站服务
[root@monitor21 ~]# /etc/rc.d/init.d/nagios status // 查看 nagios 状态
No lock file found in /usr/local/nagios/var/nagios.lock
[root@monitor21 ~]# /etc/rc.d/init.d/nagios start // 起 nagios 服务
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 监控插件的使用
[root@monitor21 libexec]# cd /usr/local/nagios/libexec
./ 插件名 -h // 查看插件的帮助信息
[root@monitor21 libexec]# ./check_users -w 1 -c 3 // 设置警告值 1,错误值 3
USERS WARNING - 2 users currently logged in |users=2;1;3;0
[root@monitor21 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 配置文件说明
nagios.cfg(主配置文件)
[root@monitor21 ~]# cd /usr/local/nagios/etc
[root@monitor21 etc# ls
cgi.cfg htpasswd.users nagios.cfg objects resource.cfg
在 / etc/bashrc 下定义别名:alias checknagios='/usr/local/nagios/bin/nagios -v /usr/local/nagios/etc/nagios.cfg'
[root@monitor21 etc]# /usr/local/nagios/bin/nagios -v /usr/local/nagios/etc/nagios.cfg // 验证主配置文件
[root@monitor21 etc]# /usr/local/nagios/bin/nagiostats -c /usr/local/nagios/etc/nagios.cfg // 命令行下输出监控统计信息
resource.cfg 宏定义文件(设置 nagios 服务使用的变量)
commands.cfg 定义监控命令的文件
$USER1$=/usr/local/nagios/libexec // 默认的变量 USER1 = 插件路径
[root@monitor21 etc]# cd objects/
[root@monitor21 objects]# ls
commands.cfg localhost.cfg switch.cfg timeperiods.cfg
contacts.cfg printer.cfg templates.cfg windows.cfg
++++++++++++++++++++++++++++++++++++++
define command{
command_name 命令名
command_line 路径 / 插件名 参数
}
vim commands.cfg
在最后一行自定义一条监控命令localhost.cfg 监控本机配置文件
define command{
command_name check_local_boot
command_line /usr/local/nagios//libexec/check_disk -w 50% -c 25% -p /boot
}
+++++++++++++++++++++++++++++++++++++++++++++++++
监控主机
define host{
use linux-server 模板名
host_name 主机名
address 被监控主机的 ip 地址
}
监控资源
define service{
use local-service 模板名
host_name 主机名
service_description 描述信息
check_command 命令名
contacts.cfg 指定接收监控报警消息邮箱地址
}
+++++++++++++++++++++++++++++++++++++++++++++
[nagios@monitor21 ~]$ mail
34 email nagios@localhost //34 行邮件提醒
vim /etc/hosts // 增加本机主机名在 127.0.0.1,同时测试本机邮件是否能正常使用
[root@monitor21 ~]# mail -s "csadfgas" nagios < /etc/hosts
[root@monitor21 ~]# su - nagios
+++++++++++++++++++++++++++++++++++++++++++
timeperiods.cfg 定义监控时间模版配置文件
templates.cfg 定义监控模版配置文件
4.4 配置远端主机
4.4.1 监控公有数据(服务)
4.4.2 监控私有数据(系统运行情况)
4.5 监控本机 localhost.cfg
添加新的监控项 监控本机的引导分区
修改已有监控项的监控阀值 用户数量
删除已有的监控项 不监控交换分区
4.6 监控报警
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
}
[nagios@monitor21 ~]$ mail
[root@monitor21 ~]# dd if=/dev/zero of=/boot/a.txt bs=1M count=240 使得 / boot 下容量超标,若配置正确,会有报警邮件发出
[root@monitor21 ~]# su - nagios
[nagios@monitor21 ~]$ mail
N 3 nagios@monitor21.loc Mon Jan 8 06 : 08 31 / 897 "PROBLEM Service Al" [root@monitor21~]#rm - rf / boot / a.txt // 删除超标文件,模拟修复成功
N 4 nagios@monitor21.loc Mon Jan 8 06:18 31/881 "RECOVERY Service A"
监控网页也显示正常
+++++++++++++++++++++++++++++++++++++++++++++++
nagios 扩展内容
1 监控时调用自定义监控脚本
2 使用第三方邮件服务器发送报警邮件
3 主机依赖监控
4 服务依赖监控
5 让 nagios 把监控到数据绘制图片
搭建 Nagios 监控服务器
来源: http://www.bubuko.com/infodetail-2454409.html