★はじめに
マカレル公式で紹介してくれるメトリクスの投稿はruby製なのでそのままだとちょっと…と思っていたら
python風にアレンジして見事成功しているサイトがあったのでそれを参考にしたらうまくいったので記念に残す。
★手順
①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形式で表示してることがわかる。
dice.pyできたら
# chmod +x dice.py ←これはしなくてもいい。
★③エージェント修正
# vi /etc/mackerel-agent/mackerel-agent.conf
mackerel-agentのコンフィグチェック
# mackerel-agent configtest
mackerel-agent再起動
# systemctl restart mackerel-agent.service
★④グラフを確認
できてるーーー、しかもやりたかったこと全部できてるーー
~おしまい~