Nagios

Linux监控:学习使用3款主流的软件搭建监控服务器

1 什么是监控? 监视并控制
2 监控目的? 问题没有出现或出现后第一时间处理
3 监控谁? 监控服务器(网站 数据库 邮件 文件)
4 都监控什么?(资源 /监控项目)
网络流量 (进出数据流的百分比)
网络接口的状态 (up down) ping
硬件使用率 (用了多少 还剩多少)
df -h free -m top uptime
服务的运行状态(运行 停止)
netstat -utnalp | grep httpd :80
ps aux | grep httpd
systemctl status httpd
nmap做扫描
操作系统运行情况(进程数 登录的用户数 系统总用户数)
ps aux | wc -l
users
wc -l /etc/passwd


5 如何实现?
使用系统自带的命令 或安装第3方软件提供的命令
计划任务 + 自定义监控脚本
搭建监控服务器做监控(Nagios Cacti Zabbix)

部署监控服务器的配置步骤:
1部署服务运行环境
2安装包
3修改配置文件
4启动服务
5监控配置
5.1 监控本机
5.2 监控远端主机
5.3 监控报警
6 访问监控服务器查看监控信息(登录监控页面查看监控信息)

+++++++++++++++++++++++++++++++
要求:在主机x.x.x.13部署Nagios监控服务
1部署服务运行环境
#yum -y install httpd php
# systemctl start httpd ;systemctl enable httpd
#echo "hello world" > /var/www/html/index.html
#yum -y install elinks
#elinks --dump http://localhost
hello world
[root@localhost ~]# cat /var/www/html/test.php
<?php
phpinfo();
?>
[root@localhost ~]#

#firefox http://192.168.4.13/test.php

2安装包
2.1 安装提供服务的包
#rpm -q gcc gcc-c++
#useradd nagios
#groupadd nagcmd
#usermod -G nagcmd nagios

# ./configure --with-nagios-user=nagios --with-

nagios-group=nagcmd --with-command-user=nagios

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

安装目录说明
bin 可执行命令
etc 配置文件
libexec 监控插件
sbin cgi文件
share 网页文件
var 日志文件状态信息文件

2.1 安装监控插件
#tar -zxvf nagios-plugins-2.1.4.tar.gz
#cd nagios-plugins-2.1.4/
#./configure
#make
#make install
#ls /usr/local/nagios/libexec/check_*

4 设置登录监控页面的用户和密码
# htpasswd -c /usr/local/nagios/etc/htpasswd.users

nagiosadmin
#cat /usr/local/nagios/etc/htpasswd.users
# grep "nagiosadmin" /usr/local/nagios/etc/cgi.cfg |
5启动nagios监控服务
*默认监控本机
# /etc/init.d/nagios start|stop
#systemctl restart httpd
6 在客户端 访问监控页面查看监控信息
ping 192.168.4.13
firefox http://192.168.4.13/nagios

7 nagios服务监控过程?
服务运行时,自动调用监控插件,根据插件的阀值返回监控状态

信息,调用插件时,可以定义警告值 和错误值

监控到的数据比警告值小 OK
监控到的数据大于警告值且小于错误值 WARNING
找不到调用的监控插件时 UNKOWN
监控到的数据大于错误值 CRITICAL
正在获取数据 PENDING
监控插件的使用 cd /usr/local/nagios/libexec
查看插件的帮助信息
cd /usr/local/nagios/libexec
./插件名 -h
/usr/local/nagios/libexec/插件名 -h

65 ./check_users -w 1 -c 3
66 ./check_users -w 5 -c 10
70 ./check_procs -w 100 -c 101
71 ./check_procs -w 10 -c 15 -s Z
72 ./check_procs -w 10 -c 15 -s R
78 df -h
76 ./check_disk -w 50% -c 25% -p /dev/vda1
77 dd if=/dev/zero of=/boot/test.txt bs=1M count=230
79 ./check_disk -w 50% -c 25% -p /dev/vda1
80 ./check_disk -w 50% -c 25% -p /
96 ./check_http -H 192.168.4.12 -p 8080
97 ./check_http -H 192.168.4.12
91 ./check_ssh -H 192.168.4.12
108 ./check_tcp -H 127.0.0.1 -p 21
109 ./check_tcp -H 192.168.4.12 -p 21
110 ./check_tcp -H 192.168.4.12 -p 22


8配置文件
nagios.cfg 主配置文件
resource.cfg 宏定义文件(设置nagios服务使用的变量)
commands.cfg 定义监控命令的文件
contacts.cfg 指定接收监控报警消息邮箱地址
timeperiods.cfg 定义监控时间模版配置文件
templates.cfg 定义监控模版配置文件
localhost.cfg 监控本机配置文件

define host { # 定义监控主机
use linux-server 模版
host_name localhost 主机名
address 127.0.0.1 ip地址
}

define service{ #定义监控资源
use local-service 模版
host_name localhost 主机名
service_description Total Processes 描述信息
check_command check_local_procs!250!

400!RSZDT 使用的监控命令

}

vim commands.cfg
90 define command{
91 command_name check_local_procs
92 command_line $USER1$/check_procs -w $ARG1$ -c

$ARG2$ -s $ARG3$
93 }
/usr/local/nagios/libexec/check_procs -w 100 -c 110 -s Z
check_local_procs
$USER1$/check_procs -w $ARG1$ -c $ARG2$ -s ARG3$
check_local_procs!90!100!R
check_local_procs!10!11!Z
check_local_disk!20%!15%!/boot
check_local_disk!25%!10%!/
5监控配置
5.1 监控本机
配置要求:
1 监控本机ftp服务的运行状态
2监控本机引导分区的使用情况,当空闲空间小于20%时是报警状态 当空闲

空间小于10%时是报错状态
3不监控本机交换分区
4监控登录本机系统用户数量时,登录用户数大于1时是报警状态 大于3时是报

错状态
vim localhost.cfg
89 define service{
90 use local-service
91 host_name localhost
92 service_description Current Users
93 check_command check_local_users!1!3
94 }
124 #define service{
125 # use local-service
126 # host_name localhost
127 # service_description Swap Usage
128 # check_command check_local_swap!20!10
129 # }
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
}
:wq
vim commands.cfg
define command {
command_name check_local_boot
command_line $USER1$/check_disk -w 20% -c 10% -p /boot
}
define command {
command_name check_local_ftp
command_line $USER1$/check_tcp -H localhost -p 21
}
:wq
# /usr/local/nagios/bin/nagios -v /usr/local/nagios/etc/nagios.cfg
#alias plj='/usr/local/nagios/bin/nagios -v

/usr/local/nagios/etc/nagios.cfg'
#plj
#/etc/init.d/nagios stop
# /etc/init.d/nagios start


5.2 监控远端主机
5.2.1 监控公有数据 (监控时通过连接端口就可以获取到信息)
要求:监控远端主机192.168.4.12 httpd服务和FTP服务的运行状态

vim nagios.cfg
cfg_file=/usr/local/nagios/etc/objects/host12.cfg
:wq

# grep -v "#" localhost.cfg > host12.cfg
#vim /usr/local/nagios/etc/objects/host12.cfg
define host{
use linux-server
host_name web-server
address 192.168.4.12
}

define service{
use local-service
host_name web-server
service_description httpd
check_command check_12_httpd
}

define service{
use local-service
host_name web-server
service_description ftp
check_command check_12_ftp
}
:wq

vim commands.cfg
define command {
command_name check_12_ftp
command_line $USER1$/check_tcp -H 192.168.4.12 -p 21
}

define command {
command_name check_12_httpd
command_line $USER1$/check_tcp -H 192.168.4.12 -p 80
}
:wq

#/usr/local/nagios/bin/nagios -v /usr/local/nagios/etc/nagios.cfg

#/etc/init.d/nagios stop
#/etc/init.d/nagios start


5.2.2 监控私有数据(进程数量 用户数 磁盘 cpu 内存)
要求:监控远端主机192.168.4.12 引导分区的使用情况:空闲空间小于30%

报警 空闲空间小于15% 报错 。

5.2.2.1 被监控的配置
a 安装监控插件
#rpm -q gcc gcc_c++
#tar -zxvf nagios-plugins-2.1.4.tar.gz
#cd nagios-plugins-2.1.4/
#./configure
#make
#make install
#ls /usr/local/nagios/libexec/check_*

#/usr/local/nagios/libexec/check_disk -w 30% -c 15% -p /boot
#/usr/local/nagios/libexec/check_procs -w 100 -c 110 -s R
#/usr/local/nagios/libexec/check_users -w 1 -c 2

b 运行nrpe服务
1 装包
#useradd nagios
#rpm -q openssl openssl-devel
#yum -y install openssl-devel
#tar -zxvf nrpe-3.0.1.tar.gz
#cd nrpe-3.0.1/
#./configure
#make all
#make install
#make install-config
#make install-init
#ls /usr/local/nagios/bin/nrpe
#ls /usr/local/nagios/etc/nrpe.cfg
#ls /usr/local/nagios/libexec/check_nrpe
#ls /usr/lib/systemd/system/nrpe.service

b 修改配置 nrpe.cfg
vim /usr/local/nagios/etc/nrpe.cfg
98 allowed_hosts=127.0.0.1, 192.168.4.13

command[nrpe_check_boot]=/usr/local/nagios/libexec/check_disk

-w 30% -c 15% -p /boot

command[nrpe_check_rprocs]

=/usr/local/nagios/libexec/check_procs -w 100 -c 110 -s R

command[nrpe_check_users]

=/usr/local/nagios/libexec/check_users -w 1 -c 2
:wq

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

d 连接本机的nrpe服务测试配置
#/usr/local/nagios/libexec/check_nrpe -h

#/usr/local/nagios/libexec/check_nrpe -H 127.0.0.1 -p 5666 -c

nrpe_check_boot

#/usr/local/nagios/libexec/check_nrpe -H 127.0.0.1 -p 5666 -c

nrpe_check_rprocs

#/usr/local/nagios/libexec/check_nrpe -H 127.0.0.1 -p 5666 -c

nrpe_check_users

# /usr/local/nagios/libexec/check_nrpe -H 127.0.0.1 -p 5666 -c

check_zombie_procs

5.2.2.2 监控服务器的配置
a 安装提供check_nrpe插件的软件包
#rpm -q openssl openssl-devel
#yum -y install openssl-devel
#tar -zxvf nrpe-3.0.1.tar.gz
#cd nrpe-3.0.1/
#./configure
#make all
#make install
# ls /usr/local/nagios/libexec/check_nrpe

b 使用插件check_nrpe连接被监控主机
#/usr/local/nagios/libexec/check_nrpe -H 192.168.4.12 -p 5666 -c

check_zombie_procs

# /usr/local/nagios/libexec/check_nrpe -H 192.168.4.12 -p 5666 -c

nrpe_check_users

c 定义监控命令
vim commands.cfg
define command {
command_name check_12_users
command_line $USER1$/check_nrpe -H 192.168.4.12 -p 5666

-c nrpe_check_users
}

define command {
command_name check_12_procs
command_line $USER1$/check_nrpe -H 192.168.4.12 -p 5666

-c nrpe_check_rprocs
}

define command {
command_name check_12_boot
command_line $USER1$/check_nrpe -H 192.168.4.12 -p 5666

-c nrpe_check_boot
}
:wq
d 在监控远端主机12配置文件里调用定义的监控命令
vim host12.cfg
define service{
use local-service
host_name web-server
service_description boot
check_command check_12_boot
}

define service{
use local-service
host_name web-server
service_description running-procs
check_command check_12_procs
}

define service{
use local-service
host_name web-server
service_description users
check_command check_12_users
}

e 启动nagios服务
#/usr/local/nagios/bin/nagios -v /usr/local/nagios/etc/nagios.cfg
#/etc/init.d/nagios stop
#/etc/init.d/nagios start


192.168.4.12
# dd if=/dev/zero of=/boot/a.txt bs=1M count=240

5.3 监控报警
#grep nagios /etc/passwd
#rpm -q postfix
#systemctl status postfix
# mail -s "xxx" nagios < /etc/hosts
# su - nagios
#mail

6 访问监控服务器查看监控信息(登录监控页面查看监控信息)
http://192.168.4.13/nagios
++++++++++++++++++++++++++++++++++++++++
nagios扩展内容
1 监控时调用自定义监控脚本
2 使用第三方邮件服务器发送报警邮件
3 主机依赖监控
4 服务依赖监控
5 让nagios 把监控到数据绘制图片

猜你喜欢

转载自www.cnblogs.com/fuzhongfaya/p/8952986.html