0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

Mackerel公式のDice.rbをDice.pyで書き起こしてグラフ化する

Posted at

★はじめに
マカレル公式で紹介してくれるメトリクスの投稿はruby製なのでそのままだとちょっと…と思っていたら
python風にアレンジして見事成功しているサイトがあったのでそれを参考にしたらうまくいったので記念に残す。

★お手本
https://sky-joker.tech/2017/04/23/mackerel%E3%81%A7%E3%82%AB%E3%82%B9%E3%82%BF%E3%83%A0%E3%83%A1%E3%83%88%E3%83%AA%E3%83%83%E3%82%AF%E3%82%92%E6%8A%95%E7%A8%BF%E3%81%97%E3%81%A6%E3%81%BF%E3%81%9F/

★手順
①python3のインストール ←なくても作れる(CentOS7はpython2がデフォルトで入っているから)
②dice.pyの作成
③エージェント修正
④グラフを確認

★①python3のインストール
[python使ってMackerelのメトリクスを投稿する(いろいろがんばるも失敗…)]の末尾に書いたのでそれを見て-

★②dice.pyの作成
↓↓のコードはpython2でもうごく。

・/opt/mackerel/dice.py

# !/usr/bin/python
import json
import os
import time
import sys
from random import randint
from collections import defaultdict

def multi_dimension_dict(dimension, callable_obj=int):
    nodes = defaultdict(callable_obj)
    for i in range(dimension-1):
        p = nodes.copy()
        nodes = defaultdict(lambda : defaultdict(p.default_factory))
    return nodes

if(os.environ.get('MACKEREL_AGENT_PLUGIN_META') == '1'):
    meta = multi_dimension_dict(2)
    metrics = [{'name':'d6','label':'Die(d6)'},{'name':'d20','label':'Die(20)'}]
    meta['graphs']['super.pdice'] = {'label': 'My pDice', 'unit': 'integer', 'metrics': metrics}
    print('# mackerel-agent-plugin')
    print(json.dumps(meta))
    sys.exit(0)

print('\t'.join(['super.pdice.d6', str(randint(1,6)), str(time.time())]))
print('\t'.join(['super.pdice.d20', str(randint(1,20)), str(time.time())]))

動作確認。

# python /opt/mackerel/dice.py

mackerel-agentではTSV形式で表示してることがわかる。

Image.png

dice.pyできたら

# chmod +x dice.py ←これはしなくてもいい。

Image.png

★③エージェント修正

# vi /etc/mackerel-agent/mackerel-agent.conf

Image.png

mackerel-agentのコンフィグチェック

# mackerel-agent configtest

Image.png

mackerel-agent再起動

# systemctl restart mackerel-agent.service

★④グラフを確認
できてるーーー、しかもやりたかったこと全部できてるーー

Image.png

~おしまい~

0
0
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
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?