nagios监控mysql(check_mysql)及内存使用率(check_mem)

本文涉及的产品
RDS MySQL Serverless 基础系列,0.5-2RCU 50GB
云数据库 RDS MySQL,集群版 2核4GB 100GB
推荐场景:
搭建个人博客
云数据库 RDS MySQL,高可用版 2核4GB 50GB
简介:

一、监控本地(localhost)内存

1、上传监控脚本 check_mem 到/usr/local/nagios/libexec

1
2
# chown -R nagios.nagios check_mem
# chmod +x check_mem

2、修改commands配置

1
2
3
4
5
# vim /usr/local/nagios/etc/objects/commands.cfg 
define  command {
         command_name        check_mem
         command_line        $USER1$ /check_mem  -w $ARG1$ -c $ARG2$
         }

3、修改localhost.cfg

1
2
3
4
5
6
7
# vim /usr/local/nagios/etc/objects/localhost.cfg 
define service{
         use                              local -service
         host_name                       localhost
         service_description             check_mem
         check_command                   check_mem!20!10
         }

4、重启nagios服务

1
# service nagios restart

5、check_mem 脚本

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
#!/bin/bash  
USAGE= "`basename $0` [-w|--warning]<percent free> [-c|--critical]<percent free>"
THRESHOLD_USAGE= "WARNING threshold must be greater than CRITICAL: `basename $0` $*"
calc= /tmp/memcalc
percent_free= /tmp/mempercent
critical= ""
warning= ""
STATE_OK=0
STATE_WARNING=1
STATE_CRITICAL=2
STATE_UNKNOWN=3
# print usage  
if  [[ $ # -lt 4 ]]
then
         echo  ""  
         echo  "Wrong Syntax: `basename $0` $*"  
         echo  ""  
         echo  "Usage: $USAGE"  
         echo  ""  
         exit  0
fi
# read input  
while  [[ $ # -gt 0 ]]  
   do
         case  "$1"  in
                -w|--warning)
                shift
                warning=$1
         ;;
                -c|--critical)
                shift
                critical=$1
         ;;
         esac
         shift
   done
 
# verify input  
if  [[ $warning - eq  $critical || $warning -lt $critical ]]
then
         echo  ""  
         echo  "$THRESHOLD_USAGE"  
         echo  ""  
         echo  "Usage: $USAGE"  
         echo  ""  
         exit  0
fi
# Total memory available  
total=` free  -m |  head  -2 | tail  -1 | gawk  '{print $2}' `
# Total memory used  
used=` free  -m |  head  -2 | tail  -1 | gawk  '{print $3}' `
# Calc total minus used  
free =` free  -m |  head  -2 | tail  -1 | gawk  '{print $2-$3}' `
# normal values  
#echo "$total"MB total  
#echo "$used"MB used  
#echo "$free"MB free  
# make it into % percent free = ((free mem / total mem) * 100)  
echo  "5"  > $calc  # decimal accuracy  
echo  "k"  >> $calc  # commit  
echo  "100"  >> $calc  # multiply  
echo  "$free"  >> $calc  # division integer  
echo  "$total"  >> $calc  # division integer  
echo  "/"  >> $calc  # division sign  
echo  "*"  >> $calc  # multiplication sign  
echo  "p"  >> $calc  # print  
percent=` /usr/bin/dc  $calc| /bin/sed  's/^\./0./' | /usr/bin/tr  "."  " " | /usr/bin/gawk  { 'print $1' }`
#percent1=`/usr/bin/dc $calc`  
#echo "$percent1"  
if  [[  "$percent"  - le   $critical ]]
         then
                 echo  "CRITICAL - $free MB ($percent%) Free Memory"  
                 exit  2
fi
if  [[  "$percent"  - le   $warning ]]
         then
                 echo  "WARNING - $free MB ($percent%) Free Memory"  
                 exit  1
fi
if  [[  "$percent"  -gt  $warning ]]
         then
                 echo  "OK - $free MB ($percent%) Free Memory"  
                 exit  0
fi

二、监控客户端内存使用情况

1、上传监控脚本 check_mem 到/usr/local/nagios/libexec

1
2
# chown -R nagios.nagios check_mem
# chmod +x check_mem

2、修改nrpe.cfg

1
2
3
4
5
6
7
8
9
# vim /usr/local/nagios/etc/nrpe.cfg 
command [check_users]= /usr/local/nagios/libexec/check_users  -w 3 -c 5
command [check_load]= /usr/local/nagios/libexec/check_load  -w 15,10,5 -c 30,25,20
command [check_xvda]= /usr/local/nagios/libexec/check_disk  -w 10% -c 5% -p  /dev/xvda
command [check_zombie_procs]= /usr/local/nagios/libexec/check_procs  -w 5 -c 10 -s Z
command [check_total_procs]= /usr/local/nagios/libexec/check_procs  -w 150 -c 200 
command [check_xvdb2]= /usr/local/nagios/libexec/check_disk  -w 10% -c 5% -p  /dev/xvdb2
command [check_swap]= /usr/local/nagios/libexec/check_swap  -w 20% -c 10%
command [check_mem]= /usr/bin/sudo  /usr/local/nagios/libexec/check_mem  -w 20 -c 10    #增加此行

3、在nagios服务器端增加监控服务

1
2
3
4
5
6
7
# vim /usr/local/nagios/etc/servers/192.168.200.111.cfg
define service{
         use                             generic-service
         host_name                       192.168.200.111
         service_description             check_mem
         check_command                   check_nrpe!check_mem
         }

三、排错

1、NRPE: Unable to read output

(1)为nagios用户增加sudo权限

1
2
# visudo
nagios  ALL=(ALL) NOPASSWD: /usr/local/nagios/libexec/check_mem

(2)注释掉一下行,表示不需要控制终端

1
2
# visudo
#Defaults    requiretty



四、nagios监控mysql

1、check_mysql

    nagios监控mysql使用的是 check_mysql 这个插件,需要在nagios服务器上先安装mysql-devel,然后再重新安装nagios-plugin,这样才会出现check_mysql。否则即使copy了一份,也照样用不了。

2、编译或重新编译 nagios-plugin

1
2
3
4
#yum -y install mysql-devl
#cd nagios-plugins-2.0.3
#./configure --with-nagios-user=nagios --with-nagios-group=nagios
#make && make install

3、查看 check_mysql

1
# ls /usr/local/nagios/libexec/check_mysql

4、建立专用数据库

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
# mysql -uroot -p
mysql> create database nagios_monitor;
 
mysql> grant  select  on nagios_monitor.* to nagios@ '%'  identified by  '123qaz!@#' ;
Query OK, 0 rows affected (0.00 sec)
 
mysql> flush privileges;
Query OK, 0 rows affected (0.00 sec)
 
mysql>  select  User,Password,Host from mysql.user;
+--------+-------------------------------------------+--------------+
| User   | Password                                  | Host         |
+--------+-------------------------------------------+--------------+
| root   | *B9627CB37815863D1E98D0C41E0233A772355E2B | localhost    |
| root   | *B9627CB37815863D1E98D0C41E0233A772355E2B | 127.0.0.1    |
| root   | *B9627CB37815863D1E98D0C41E0233A772355E2B | ::1          |
| cacti  | *BC3E1F14C7940F9C8BCDB05A38385754BB55CD64 | localhost    |
| nagios | *BC3E1F14C7940F9C8BCDB05A38385754BB55CD64 | %            |
+--------+-------------------------------------------+--------------+
8 rows  in  set  (0.00 sec)

5、check 一下

1
2
3
4
5
6
7
8
# /usr/local/nagios/libexec/check_mysql -H 192.168.200.105 -unagios -dnagios_monitor -p 123qaz!@#
# 报了一个错
/usr/local/nagios/libexec/check_mysql : error  while  loading shared libraries: libmysqlclient.so.18: cannot  open  shared object  file : No such  file  or directory
# 解决:
ln  -sv  /usr/local/mysql/lib/libmysqlclient .so.18  /usr/lib64/libmysqlclient .so.18
# 再重新测试
# /usr/local/nagios/libexec/check_mysql -H 192.168.200.111 -unagios -dnagios_monitor -p 123qaz!@#
Uptime: 13991  Threads: 5  Questions: 1242101  Slow queries: 0  Opens: 159  Flush tables: 1  Open tables: 60  Queries per second avg: 88.778|Connections=315c;;; Open_files=85;;; Open_tables=60;;; Qcache_free_memory=16285768;;; Qcache_hits=1210926c;;; Qcache_inserts=16654c;;; Qcache_lowmem_prunes=0c;;; Qcache_not_cached=2c;;; Qcache_queries_in_cache=283;;; Queries=1242101c;;; Questions=1242101c;;; Table_locks_waited=2c;;; Threads_connected=5;;; Threads_running=1;;; Uptime=13991c;;;

6、监控localhost

(1)修改 commands.cfg

1
2
3
4
5
# vim /usr/local/nagios/etc/objects/commands.cfg
define  command {
         command_name check_mysql
         command_line $USER1$ /check_mysql  -H $HOSTADDRESS$ -unagios -dnagios_monitor -p123qaz!@ #
}

(2)修改 localhost.cfg

1
2
3
4
5
6
7
# vim /usr/local/nagios/etc/objects/localhost.cfg
define service{
         use                              local -service
         host_name                       localhost
         service_description             check_mysql
         check_command                   check_mysql
         }

(3)重启 nagios

1
# service nagios restart

7、监控客户端

(1)check 一下

1
# /usr/local/nagios/libexec/check_mysql -H 192.168.200.111 -unagios -dnagios_monitor -p123qaz!@#

(2)客户端修改 nrpe.cfg

1
2
# vim /usr/local/nagios/etc/nrpe.cfg 
command [check_mysql]= /usr/local/nagios/libexec/check_mysql  -H 192.168.200.111 -unagios -dnagios_monitor -p123qaz!@ #

(3)重启 nrpe

1
2
# killall nrpe
# /usr/local/nagios/bin/nrpe -c /usr/local/nagios/etc/nrpe.cfg -d

(4)服务端定义服务

1
2
3
4
5
6
7
# vim /usr/local/nagios/etc/servers/192.168.200.111.cfg 
define service{
         use                             generic-service
         host_name                       192.168.200.111
         service_description             check_mysql
         check_command                   check_nrpe!check_mysql
         }

(5)重启 nagios 服务

1
# service nagios restart









本文转自 nmshuishui 51CTO博客,原文链接:http://blog.51cto.com/nmshuishui/1553445,如需转载请自行联系原作者
相关实践学习
基于CentOS快速搭建LAMP环境
本教程介绍如何搭建LAMP环境,其中LAMP分别代表Linux、Apache、MySQL和PHP。
全面了解阿里云能为你做什么
阿里云在全球各地部署高效节能的绿色数据中心,利用清洁计算为万物互联的新世界提供源源不断的能源动力,目前开服的区域包括中国(华北、华东、华南、香港)、新加坡、美国(美东、美西)、欧洲、中东、澳大利亚、日本。目前阿里云的产品涵盖弹性计算、数据库、存储与CDN、分析与搜索、云通信、网络、管理与监控、应用服务、互联网中间件、移动服务、视频服务等。通过本课程,来了解阿里云能够为你的业务带来哪些帮助 &nbsp; &nbsp; 相关的阿里云产品:云服务器ECS 云服务器 ECS(Elastic Compute Service)是一种弹性可伸缩的计算服务,助您降低 IT 成本,提升运维效率,使您更专注于核心业务创新。产品详情: https://www.aliyun.com/product/ecs
目录
相关文章
|
1天前
|
Java Linux
在业务有损的情况下,遇到JAVA内存使用率高的问题,应该如何快速止损
在业务有损的情况下,遇到JAVA内存使用率高的问题,应该如何快速止损
|
16天前
|
缓存 Prometheus 监控
Java面试题:如何监控和优化JVM的内存使用?详细讲解内存调优的几种方法
Java面试题:如何监控和优化JVM的内存使用?详细讲解内存调优的几种方法
34 3
|
23天前
|
监控 Python
paramiko 模块 ---Python脚本监控当前系统的CPU、内存、根目录、IP地址等信息
paramiko 模块 ---Python脚本监控当前系统的CPU、内存、根目录、IP地址等信息
|
2月前
|
监控 Java 网络性能优化
容器内存可观测性新视角:WorkingSet 与 PageCache 监控
本文介绍了 Kubernetes 中的容器工作内存(WorkingSet)概念,它用于表示容器内存的实时使用量,尤其是活跃内存。
56159 14
容器内存可观测性新视角:WorkingSet 与 PageCache 监控
|
21天前
|
监控
主机状态监控,通过top命令查看CPU、内存使用情况,ctrl + c退出,输入top整个页面就变成一个任务管理器的形式了,Ctrl + C直接退出,Q也可以退掉了
主机状态监控,通过top命令查看CPU、内存使用情况,ctrl + c退出,输入top整个页面就变成一个任务管理器的形式了,Ctrl + C直接退出,Q也可以退掉了
|
1月前
|
缓存 关系型数据库 MySQL
MySQL数据库——InnoDB引擎-架构-内存结构(Buffer Pool、Change Buffer、Adaptive Hash Index、Log Buffer)
MySQL数据库——InnoDB引擎-架构-内存结构(Buffer Pool、Change Buffer、Adaptive Hash Index、Log Buffer)
76 3
|
1月前
|
监控 Rust 安全
Rust代码在公司电脑监控软件中的内存安全监控
使用 Rust 语言开发的内存安全监控软件在企业中日益重要,尤其对于高安全稳定性的系统。文中展示了如何用 Rust 监控内存使用:通过获取向量长度和内存大小来防止泄漏和溢出。此外,代码示例还演示了利用 reqwest 库自动将监控数据提交至公司网站进行实时分析,以保证系统的稳定和安全。
135 2
|
2月前
|
监控 Linux API
LabVIEW监控实时嵌入式目标上的CPU和内存使用情况
LabVIEW监控实时嵌入式目标上的CPU和内存使用情况
215 4
|
2月前
|
监控
LabVIEW监控VI中的执行时间和内存使用情况
LabVIEW监控VI中的执行时间和内存使用情况
227 1
|
2月前
|
监控 关系型数据库 MySQL
实时计算 Flink版产品使用合集之监控 MySQL 数据写入到 StarRocks 中,在初始化成功后,但无法监控到插入的数据是什么导致的
实时计算Flink版作为一种强大的流处理和批处理统一的计算框架,广泛应用于各种需要实时数据处理和分析的场景。实时计算Flink版通常结合SQL接口、DataStream API、以及与上下游数据源和存储系统的丰富连接器,提供了一套全面的解决方案,以应对各种实时计算需求。其低延迟、高吞吐、容错性强的特点,使其成为众多企业和组织实时数据处理首选的技术平台。以下是实时计算Flink版的一些典型使用合集。