c4se記:さっちゃんですよ☆

.。oO(さっちゃんですよヾ(〃l _ l)ノ゙☆)

.。oO(此のblogは、主に音樂考察Programming に分類されますよ。ヾ(〃l _ l)ノ゙♬♪♡)

音樂は SoundCloud に公開中です。

考察は現在は主に Scrapbox で公表中です。

Programming は GitHub で開發中です。

mackerel-agent の plugin は Bash script で簡單に作れます

Calendar for Mackerel | Advent Calendar 2022 - Qiitaの 12/11 (日) 分です。昨日は id:Windymelt さんのPage Speed InsightsのスコアをMackerelに投稿するツールを作った(バイナリあります) - Lambdaカクテルでした。

クリスマスを樂しみにする advent calendar ですので、n 番煎じの手頃な記事でいきませう。

皆さん Mackerel にメトリックを送ってゐますか? Mackerel にメトリックを送るには、mackerel-agent を install したり (エージェントをインストールする - Mackerel ヘルプ)、mackerel-container-agent を install したり (コンテナを監視する - Mackerel ヘルプ)API からメトリックを投稿したり (ホストメトリック - Mackerel API ドキュメント (v0)サービスメトリック - Mackerel API ドキュメント (v0)) できますし、plugin を利用する (mkr plugin installでプラグインをインストールする - Mackerel ヘルプ) 事もできます。よい plugin が見當たらなければ作れもします。plugin を作るのに Mackerel 公式からは go-mackerel-plugin といふ Go の library が提供されてゐます (go-mackerel-pluginを利用してカスタムメトリックプラグインを作成する - Mackerel ヘルプ) が、一般に公開するのでもなければこの library を使はなくても簡單に plugin を作れます。

mackerel-agent や mackerel-container-agent の plugin とは實行 file であり、以下ができなければなりません。

  • メトリックを取得する。何をだう取得するかは、何を觀測したいかによります。plugin を作る時に難しいのはここです
  • メトリックを Graphite 形式の文字列で標準出力に出力する

以上を、實行したら 60 秒以內 (できれば 30 秒以內) に行なひませう。

加へて以下ができると尚よいのです。

  • 環境變數 MACKEREL_AGENT_PLUGIN_META=1 が設定され實行された時に、グラフ定義を JSON 文字列で標準出力に出力し、終了する

まづ必須の事、メトリックを取得し、出力するものだけを作りませう。多くの Linux に入ってゐる Bash の script で書いてみませう。

#!/bin/bash

set -eu

# get_metrics の中身は何とかして實裝しませう
function get_metrics() { 
  metric1=42
}

get_metrics

now="$(date +"%s")"
echo -e "example.example1\t$metric1\t$now"

これを mackerel-plugin-example といふ file 名で保存し、實行權限を付與しませう (chmod +x mackerel-plugin-example)

實行します。

$ ./mackerel-plugin-example
example.example1    42  1670684400

これを mackerel-agent.conf に設定すれば、custom.example.example1 といふメトリックが該當するホストに投稿されます。

複數のメトリックを取得してみませう。

#!/bin/bash

set -eu

# get_metrics の中身は何とかして實裝しませう
function get_metrics() {
  metric1=42
  metric2=42
}

get_metrics

now="$(date +"%s")"
echo -e "example.example1\t$metric1\t$now"
echo -e "example.example2\t$metric2\t$now"
$ ./mackerel-plugin-example
example.example1    42  1670684400
example.example2    42  1670684400

mackerel-agent の plugin とはこれだけのものです。Go 等で實裝すると廣く公開し易く使ふのに便利ですが、樂に作りたければ使ひ慣れた programming 言語や library で作るとよいのではないでせうか。私は Babashka (Clojure) で實裝する事が多くあります。

ついでにグラフ定義も出力する樣にしてみませう。グラフ定義はホストのカスタムメトリックを投稿する - Mackerel ヘルプの樣な形で記載します。

#!/bin/bash

set -eu

# get_metrics の中身は何とかして實裝しませう
function get_metrics() {
  metric1=42
  metric2=42
}

function print_graph_def() {
  echo "# mackerel-agent-plugin"
  echo "$(cat <<JSON
{
  "graphs": {
    "example": {
      "label": "Example",
      "unit": "integer",
      "metrics": [
        {
          "name": "example1",
          "label": "1"
        },
        {
          "name": "example2",
          "label": "2"
        }
      ]
    }
  }
}
JSON
)"
}

function print_metrics() {
  get_metrics

  now="$(date +"%s")"
  echo -e "example.example1\t$metric1\t$now"
  echo -e "example.example2\t$metric2\t$now"
}

if [[ ${MACKEREL_AGENT_PLUGIN_META:-"0"} = "1" ]]; then
  print_graph_def
else
  print_metrics
fi
$ MACKEREL_AGENT_PLUGIN_META=1 ./mackerel-plugin-example
# mackerel-agent-plugin
{
  "graphs": {
    "example": {
      "label": "Example",
      "unit": "integer",
      "metrics": [
        {
          "name": "example1",
          "label": "1"
        },
        {
          "name": "example2",
          "label": "2"
        }
      ]
    }
  }
}

$ ./mackerel-plugin-example
example.example1    42  1670684400
example.example2    42  1670684400

これで Example といふグラフに 1 と 2 といふメトリックが投稿されるやうになります。

mackerel-container-agent でも同じですよ。では plugin を作りませうね。

明日は POcean10 さんです。