LoginSignup
4
5

More than 5 years have passed since last update.

node.jsでお手軽にmuninプラグインを作ってみる

Last updated at Posted at 2016-01-22

はじめに

node.jsとmuninはどちらもお手軽で相性がいいとおもったのでmuninプラグインを作るフレームワークを作ってみました。

インストール

npm i munin-plugin

プログラムの流れ

プラグインつくるうえでやることは二つだけです

  • データの取得
  • データをmuninの形式に変換

メモリ情報を取得するサンプル

/opt/munin-plugin/meminfo.js
#!/usr/bin/node
'use strict'

const fs = require('fs');
const munin = require('munin-plugin');

const getMemInfo = () => {
    return fs.readFileSync('/proc/meminfo', 'utf8').
        split('\n').
        filter(data => data !== '').
        map(data => data.split(':').map(data => data.trim().split(' '))).
        reduce((r,v) => {
            let data = v[1];
            r[v[0]] = { value : data[0], unit : (data.length === 1 ? '' : data[1]) }
            return r
        }, {})
}

const main = () => {
    let info = getMemInfo();
    munin.create([
        () => {
            let g = new munin.Graph('linux memory detail','kbyte','system');
            g.add(new munin.Model.Default('active').setValue(info['Active'].value));
            g.add(new munin.Model.Default('cache').setValue(info['Cached'].value));
            g.add(new munin.Model.Default('buffer').setValue(info['Buffers'].value));
            g.sortLabel();
            return g;
        },
        () => {
            let g = new munin.Graph('linux free memory','kbyte','system');
            g.add(new munin.Model.Default('free').setValue(info['MemFree'].value));
            g.sortLabel();
            return g;
        }
    ].map(f => f()));
}
main();

実行権限をつけます

chmod 755 /opt/munin-plugin/meminfo.js

デバッグする

以下のような形に出力されれば完成

$ ./meminfo.js
multigraph linux_memory_detail
active.value 565380
buffer.value 148280
cache.value 342548
multigraph linux_free_memory
free.value 167124
meminfo.js
$ ./meminfo.js config
multigraph linux_memory_detail
graph_title linux memory detail
graph_vlabel kbyte
graph_category system
graph_scale no
graph_info 
graph_args 
active.label active
active.draw LINE2
active.type GAUGE
buffer.label buffer
buffer.draw LINE2
buffer.type GAUGE
cache.label cache
cache.draw LINE2
cache.type GAUGE
multigraph linux_free_memory
graph_title linux free memory
graph_vlabel kbyte
graph_category system
graph_scale no
graph_info 
graph_args 
free.label free
free.draw LINE2
free.type GAUGE

作ったプラグインをmunin-nodeに認識させる

ln -s /opt/munin-plugin/meminfo.js /etc/munin/plugins/meminfo

グラフに描画

こんな感じに出力されます

linux_memory_detail-day.png

linux_free_memory-day.png

その他

サンプルとしてnetstatの詳細版もあります

npm i -g munin-netstat-detail
4
5
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
4
5