文章目录
安装 Nagios
Nagios是一个监视系统运行状态和网络信息的监视系统。Nagios能监视所指定的本地或远程主机以及服务,同时提供异常通知功能等
Nagios可运行在Linux/Unix平台之上,同时提供一个可选的基于浏览器的WEB界面以方便系统管理人员查看网络状态,各种系统问题,以及日志等等。
打开Nagios官方的文档,会发现Nagios基本上没有什么依赖包,只要求系统是Linux或者其他Nagios支持的统。不过如果你没有安装apache(http服务),那么你就没有那么直观的界面来查看监控信息了,所以apache姑且算是一个前提条件。
安装 Apache
sudo yum install httpd
sudo service httpd start
./configure --prefix=/usr/local/nagios --with-nagios-user=nagios --with-nagios-group=nagios --with-perl --with-statefiles-dir=/tmp
安装Nagios
安装环境包
yum install -y \
-- gcc \
-- glibc \
-- glibc-common \
-- gd \
-- gd-devel \
-- xinetd \
-- openssl-devel
创建管理用户
useradd -s /sbin/nologin nagios
创建安装目录,指定属主属组
mkdir /usr/local/nagios
chown -R nagios.nagios /usr/local/nagios
编译安装nagios
wget https://assets.nagios.com/downloads/nagioscore/releases/nagios-4.4.3.tar.gz
tar -zxvf nagios-4.4.3.tar.gz
cd nagios-4.4.3
./configure --prefix=/usr/local/nagios
make all
make install
make install-init
make install-commandmode
make install-config
安装nagios-plugins
wget https://nagios-plugins.org/download/nagios-plugins-2.2.1.tar.gz
tar -zxvf nagios-plugins-2.2.1.tar.gz
cd nagios-plugins-2.2.1
./configure --prefix=/usr/local/nagios
make
make install
配置nagios 监控系统
配置nagios 监控MySQL
使用check_mysql_health
wget https://labs.consol.de/assets/downloads/nagios/check_mysql_health-2.2.2.tar.gz
tar -zxvf check_mysql_health-2.2.2.tar.gz
cd check_mysql_health-2.2.2
./configure --prefix=/usr/local/nagios --with-nagios-user=nagios --with-nagios-group=nagios --with-perl=/usr/bin/perl --with-statefiles-dir=/usr/local/nagios/var/tmp
创建一个用户
grant usage on *.* to 'nagios'@'%' identified by 'nagios';
校验 check_mysql_health 插件
./check_mysql_health --hostname 192.168.192.129 --port 3306 -username nagios --password nagios --mode connection-time --warning 0.001 --critical 0.0011
-- 如果没有 Perl
yum install 'perl(Data::Dumper)'
-- 如果没有 DBI.pm
yum install perl-DBI perl-DBD-MySQL -y
添加配置
服务器监控(自定义) : vim /usr/local/nagios/etc/objects/hosts.cfg
define host{
use linux-server ; 引用主机linux-server的属性信息,linux-server主机在templates.cfg文件中进行了定义。
host_name monitor_mysql_60 ; 主机名,自定义
alias mon_60 ; 主机别名
address 192.168.20.60 ; 被监控的主机地址,这个地址可以是ip,也可以是域名。
}
define hostgroup{ ; 定义一个主机组
hostgroup_name monitor-servers ; 主机组名称,可以随意指定。
alias monitorServers ; 主机组别名
members monitor_mysql_60 ; 主机组成员,其中"monitor_60”就是上面定义的主机。
}
服务器监控(自定义) : vim /usr/local/nagios/etc/objects/services.cfg
define service{
use local-service ; 引用local-service服务的属性值,local-service在templates.cfg文件中进行了定义。
host_name monitor_mysql_60 ; 指定要监控哪个主机上的服务,monitor_mysql_60 在hosts.cfg文件中进行了定义。
service_description check-host-alive ; 对监控服务内容的描述,以供维护人员参考。
check_command check-host-alive ; 指定检查的命令。
}
配置nagios命令行文件 : vim /usr/local/nagios/etc/objects/commands.cfg
# check_mysql_health command definition
define command{
command_name check_mysql_health
command_line $USER1$/check_mysql_health --hostname $ARG1$ --port $ARG2$ --username $ARG3$ --password $ARG4$ --mode $ARG5$ --warning $ARG6$ --critical $ARG7$
}
配置监控服务项(自定义) : vim /usr/local/nagios/etc/objects/mysqlservices.cfg
define service{
use local-service
host_name monitor_mysql_60
service_description check_mysql_connection_time
check_command check_mysql_health!192.168.20.60!3306!nagios!nagios!connection-time!2!10
}
define service{
use local-service
host_name monitor_mysql_60
service_description check_mysql_uptime
check_command check_mysql_health!192.168.20.60!3306!nagios!nagios!uptime!10080!44640
}
define service{
use local-service
host_name monitor_mysql_60
service_description check_mysql_connections
check_command check_mysql_health!192.168.20.60!3306!nagios!nagios!threads-connected!70!150
}
define service{
use local-service
host_name monitor_mysql_60
service_description check_mysql_table_lock
check_command check_mysql_health!192.168.20.60!3306!nagios!nagios!table-lock-contention!1!2
}
define service{
use local-service
host_name monitor_mysql_60
service_description check_mysql_slow_queries
check_command check_mysql_health!192.168.20.60!3306!nagios!nagios!slow-queries!1!2
}
添加自定义的mysqlservices.cfg
到配置文件中 : /usr/local/nagios/etc/nagios.cfg
cfg_file=/usr/local/nagios/etc/objects/hosts.cfg
cfg_file=/usr/local/nagios/etc/objects/services.cfg
cfg_file=/usr/local/nagios/etc/objects/mysqlservices.cfg
校验配置
/usr/local/nagios/bin/nagios -v /usr/local/nagios/etc/nagios.cfg
重启服务
systemctl restart httpd.service
systemctl restart nagios.service
其他命令-具体看官方文档
-- 连接服务器的时间
sudo ./check_mysql_health --hostname 192.168.20.60 --username nagios --password nagios --mode connection-time
sudo ./check_mysql_health --hostname 192.168.20.60 --port 3306 --username nagios --password nagios --mode connection-time --warning 0.001 --critical 0.0011
-- 服务器运行的时间
sudo ./check_mysql_health --hostname 192.168.20.60 --username nagios --password nagios --mode uptime-time
-- 当前打开的连接数
sudo ./check_mysql_health --hostname 192.168.20.60 --port 3306 --username nagios --password nagios --mode threads-connected
-- 线程缓存的命中率
sudo ./check_mysql_health --hostname 192.168.20.60 --port 3306 --username nagios --password nagios --mode threadcache-hitrate
-- 每秒创建的线程数
sudo ./check_mysql_health --hostname 192.168.20.60 --port 3306 --username nagios --password nagios --mode threads-created
-- 当前运行的线程数
sudo ./check_mysql_health --hostname 192.168.20.60 --port 3306 --username nagios --password nagios --mode threads-running
-- 当前缓存的线程数
sudo ./check_mysql_health --hostname 192.168.20.60 --port 3306 --username nagios --password nagios --mode threads-cached
-- 连接终止:每秒中止连接的数量
sudo ./check_mysql_health --hostname 192.168.20.60 --port 3306 --username nagios --password nagios --mode connects-aborted
-- 客户端终止
sudo ./check_mysql_health --hostname 192.168.20.60 --port 3306 --username nagios --password nagios --mode clients-aborted
-- 查询缓存命中率
sudo ./check_mysql_health --hostname 192.168.20.60 --port 3306 --username nagios --password nagios --mode querycache-hitrate
-- 由于内存不足而修剪的查询缓存条目
sudo ./check_mysql_health --hostname 192.168.20.60 --port 3306 --username nagios --password nagios --mode qcache-lowmem-prunes
-- MyISAM key 缓存命中率
sudo ./check_mysql_health --hostname 192.168.20.60 --port 3306 --username nagios --password nagios --mode keycache-hitrate
-- InnoDB 缓冲池命中率
sudo ./check_mysql_health --hostname 192.168.20.60 --port 3306 --username nagios --password nagios --mode bufferpool-hitrate
-- InnoDB缓冲池等待可用的
sudo ./check_mysql_health --hostname 192.168.20.60 --port 3306 --username nagios --password nagios --mode bufferpool-wait-free
-- 由于日志缓冲区太小,InnoDB日志等待
sudo ./check_mysql_health --hostname 192.168.20.60 --port 3306 --username nagios --password nagios --mode log-waits
-- 表缓存命中率
sudo ./check_mysql_health --hostname 192.168.20.60 --port 3306 --username nagios --password nagios --mode tablecache-hitrate
-- 表锁争用
sudo ./check_mysql_health --hostname 192.168.20.60 --port 3306 --username nagios --password nagios --mode table-lock-contention
-- 在磁盘上创建的临时表的百分比
sudo ./check_mysql_health --hostname 192.168.20.60 --port 3306 --username nagios --password nagios --mode tmp-disk-tables
-- 长时间运行的进程
sudo ./check_mysql_health --hostname 192.168.20.60 --port 3306 --username nagios --password nagios --mode long-running-procs
-- 索引
sudo ./check_mysql_health --hostname 192.168.20.60 --port 3306 --username nagios --password nagios --mode index-usage
-- 显示应优化的表
sudo ./check_mysql_health --hostname 192.168.20.60 --port 3306 --username nagios --password nagios --mode table-fragmentation
-- 已打开文件的百分比
sudo ./check_mysql_health --hostname 192.168.20.60 --port 3306 --username nagios --password nagios --mode open-files
-- 慢查询
sudo ./check_mysql_health --hostname 192.168.20.60 --port 3306 --username nagios --password nagios --mode slow-queries
-- 任何返回单个数字的sql命令
sudo ./check_mysql_health --hostname 192.168.20.60 --port 3306 --username nagios --password nagios --mode sql --name 'select 111 from dual'
sudo ./check_mysql_health --hostname 192.168.20.60 --port 3306 --username nagios --password nagios --mode sql --name 'select 111 from dual' --name2 myval --units GB