[Linux实用工具]munin-node插件配置和插件编写

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

前面介绍了2篇munin使用的相关文章:

 
这次介绍一下munin-node的插件的安装配置和插件的编写。
 

插件配置

munin-node本身就集成了很多的插件,只需要直接建个软链就可以了。像Nginx、Apach、mysql都是有现成的插件可以使用的。
munin的插件默认保存在/etc/munin/plugins里面。进去查看会发现很多软链,软链到/usr/share/munin/plugins 这个目录下的文件。没错,munin的插件都是放置在/usr/share/munin/plugins 下的。使用相当简单,只需要参考原有的例子,直接建软链过去就可以了。
 
下面以mysql的插件为例:
ln -s /usr/share/munin/plugins/mysql_threads /etc/munin/plugins/mysql_threads
ln -s /usr/share/munin/plugins/mysql_queries /etc/munin/plugins/mysql_queries
ln -s /usr/share/munin/plugins/mysql_bytes /etc/munin/plugins/mysql_bytes
ln -s /usr/share/munin/plugins/mysql_innodb /etc/munin/plugins/mysql_innodb

 
然后重启一下munin-node 就可以生效了。
service munin-node restart

 
 

插件编写

如果需要监控的应用没有相关的插件支持怎办?也很简单。随意打开一个现有的插件,会发现具体的代码内容,观察发现编写一个插件也不是很难。而且munin-node的插件支持shell、python、perl。也可以根据自己熟悉的语言来编写插件。
 

munin-node 提供的插件实例

以threads (/usr/share/munin/plugins/threads)插件为例,我们可以查看内部代码:
#!/bin/sh
# -*- sh -*-
# vim: ft=sh
 
if [ "$1" = "autoconf" ]; then
    grep -q '^Threads' /proc/$$/status && echo "yes" || echo "no"
    exit 0
fi
 
if [ "$1" = "config" ]; then
    echo 'graph_title Number of threads'
    #echo 'graph_args --base 1000 -l 0 '
    echo 'graph_vlabel number of threads'
    echo 'graph_category processes'
    echo 'graph_info This graph shows the number of threads.'
    echo 'threads.label threads'
    echo 'threads.info The current number of threads.'
    exit 0
fi
 
grep -s '^Threads' /proc/[0-9]*/status | awk '{ sum += $2; } END { print "threads.value", sum; }'

 
可以直接执行插件的文件查看输出数据的结构。可以发现所有的输出的格式都是一样的,而且都有1个参数。
分别使用参数autoconf、config和不使用参数,查看输出内容:
sh threads autoconf
yes
 
sh threads config
graph_title Number of threads
graph_vlabel number of threads
graph_category processes
graph_info This graph shows the number of threads.
threads.label threads
threads.info The current number of threads.
 
sh threads
threads.value 174
 

参数说明

autoconf
用于检测是否有进程的存在。非必要。
 
config
使用config参数的时候,输出的内容是绘图的配置信息,查看字段意思也可以知道字段的意思。
 
不使用参数
不使用参数的时候,输出的是具体的进程数量。这个跟具体的命令输出有关系。
grep -s '^Threads' /proc/[0-9]*/status | awk '{ sum += $2; } END { print "threads.value", sum; }'
 

自定义插件

既然知道每个参数需要输出的具体内容,那么我们就可以依葫芦画瓢,我们就可以写出我们自己的插件。下面给出一个具体的例子,该例子用于监控一个游戏服务器的内存和CPU。
#!/bin/sh
 
keyword=munin-node
 
case $1 in
   (config)
      cat <<'EOM'
graph_title MY_TITLE
graph_vlabel (%)
graph_args --base 1000 -l 0
graph_scale no
graph_category MY_CATEGORY
EOM
ps aux | grep $keyword | grep -v grep | awk '{print $3,$4}' | while read cpu mem
do
   echo "CPU.label %CPU"
   echo "CPU.warning 200"
   echo "CPU.critical 400"
   echo "CPU.value $cpu"
 
   echo "MEM.label %MEM"
   echo "MEM.warning 20"
   echo "MEM.critical 50"
   echo "MEM.value $mem"
 
done
 exit 0;;
esac
ps aux | grep $keyword | grep -v grep | awk '{print $3,$4}' | while read cpu mem
do
   echo "CPU.value $cpu"
   echo "MEM.value $mem"
done

这里没有使用autoconf参数,其实应该使用的,万一该进程不存在怎么办,是吧。其实没有统计到数值的时候,图表是空的,所有才说autoconf参数不是必须的。
这个例子与threads不一样的是,这里收集到的是多条数据。
插件写好了之后,需要在/etc/munin/plugin-conf 配置一下。加入上面例子以munin-node_cpu 文件名保存。
在/etc/munin/plugin-conf 下新增一个文件,名字可以任意,如munin_info:
# cat munin_info
[munin-node_cpu]
user root

 
然后重启munin-node就可以了。
 
是吧,插件编写其实很简单吧。只要输出的格式符合要求,随便你使用perl、python还是shell。具体的监控完全可以自定义。譬如我假设有访问游戏服在线人数的接口,我也可以拿出来监控,当在线人数超过多少的时候给予相关提示。

 

开始使用独立博客了,原文地址在这里:

相关实践学习
基于CentOS快速搭建LAMP环境
本教程介绍如何搭建LAMP环境,其中LAMP分别代表Linux、Apache、MySQL和PHP。
全面了解阿里云能为你做什么
阿里云在全球各地部署高效节能的绿色数据中心,利用清洁计算为万物互联的新世界提供源源不断的能源动力,目前开服的区域包括中国(华北、华东、华南、香港)、新加坡、美国(美东、美西)、欧洲、中东、澳大利亚、日本。目前阿里云的产品涵盖弹性计算、数据库、存储与CDN、分析与搜索、云通信、网络、管理与监控、应用服务、互联网中间件、移动服务、视频服务等。通过本课程,来了解阿里云能够为你的业务带来哪些帮助 &nbsp; &nbsp; 相关的阿里云产品:云服务器ECS 云服务器 ECS(Elastic Compute Service)是一种弹性可伸缩的计算服务,助您降低 IT 成本,提升运维效率,使您更专注于核心业务创新。产品详情: https://www.aliyun.com/product/ecs
相关文章
|
JavaScript 数据库 Windows
nodejs 将node命令启动为windows服务运行(node-windows使用)
nodejs 将node命令启动为windows服务运行(node-windows使用)
nodejs 将node命令启动为windows服务运行(node-windows使用)
|
15天前
|
JavaScript 前端开发 Linux
vue3在Linux下无法正常启动:esbuild-linux-64、cantnot start service :host version “0.13.15“,esbuild EACCESS
vue3在Linux下无法正常启动:esbuild-linux-64、cantnot start service :host version “0.13.15“,esbuild EACCESS
|
17天前
|
Linux Windows
教你在Linux上安装Node并用Electron打包deb和rpm包
教你在Linux上安装Node并用Electron打包deb和rpm包
31 9
|
1月前
|
算法 Unix Linux
【Linux 库管理工具】深入解析pkg-config与CMake的集成与应用
【Linux 库管理工具】深入解析pkg-config与CMake的集成与应用
68 0
|
安全 Linux
linux 安装 node 及环境配置
linux 安装 node 及环境配置
267 0
|
10月前
|
Linux Shell 程序员
【Linux】Linux环境基础工具的基本使用及配置(yum、vim)(下)
【Linux】Linux环境基础工具的基本使用及配置(yum、vim)(下)
|
存储 缓存 JavaScript
Windows环境安装配置NVM和Node.js
本文介绍如何在Windows环境下安装配置NVM和Node.js。
1332 0
Windows环境安装配置NVM和Node.js
|
负载均衡 监控 JavaScript
Linux 系统安装 Node.js
Linux 系统安装 Node.js
149 0
Linux 系统安装 Node.js
|
JavaScript Docker Windows
Windows下构建Node.js的Docker Nano Server基础镜像
本文讲的是Windows下构建Node.js的Docker Nano Server基础镜像【编者的话】本文介绍了如何在Windows下制作Nano Server的Docker镜像,并用镜像来部署Node.js应用。
2527 0
|
Linux 开发工具 git
puppet连载七:linux基础组件安装模块
linux基础组件安装模块linuxbaseinstall 更换源,安装gcc gcc-c++ glibc-devel make ncurses-devel openssl-devel autoconf git mkdir -p /etc/puppet...
1046 0