1
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 1 year has passed since last update.

Ansibleを使ってmackerelのカスタムメトリック設定をする

Last updated at Posted at 2023-08-20

実行環境

  • ansibleコントロールノード
[root@localhost ~]# cat /etc/redhat-release
AlmaLinux release 9.1 (Lime Lynx)

[root@localhost ~]# python3 -V
Python 3.9.14
  • ansibleターゲットノード
[root@vm-001 ~]# cat /etc/redhat-release
AlmaLinux release 8.8 (Sapphire Caracal)

[root@vm-001 ~]# python3 -V
Python 3.6.8

やりたいこと

ターゲットノード(今回は1台)に対して以下を実施。

  1. /opt/scripts配下にmackerelディレクトリを作成
  2. /opt/scripts/mackerelディレクトリにカスタムメトリック用のtest.shを配置
  3. mackerel-agent.confを更新
  4. mackerel-agent.serviceを停止
  5. mackerel-agent.serviceを起動

コントロールノードのansibleディレクトリ構成

[root@localhost ansible]# pwd
/etc/ansible
[root@localhost ansible]# ls -l
合計 8
-rw-r--r--. 1 root root  739  8月 20 18:19 ansible.cfg
-rw-r--r--. 1 root root 1018  8月 20 17:24 hosts
drwxr-xr-x. 2 root root   27  8月 20 23:11 inventory
drwxr-xr-x. 2 root root   25  8月 20 18:20 log
drwxr-xr-x. 2 root root   58  8月 20 23:31 playbook
drwxr-xr-x. 2 root root    6  4月 14 17:29 roles

実行するplaybook

- hosts: vm-001
  tasks:
    - name: Create directory
      ansible.builtin.file:
        path: /opt/scripts/mackerel
        state: directory
        mode: 0755

    - name: Copy test.sh
      ansible.builtin.copy:
        src: /opt/scripts/test.sh
        dest: /opt/scripts/mackerel/test.sh
        mode: 0644

    - name: Update mackerel-agent.conf
      ansible.builtin.copy:
        src: /tmp/mackerel/mackerel-agent.conf
        dest: /etc/mackerel-agent/mackerel-agent.conf

    - name: Stop mackerel-agent.service
      service:
        name: mackerel-agent
        state: stopped

    - name: Start mackerel-agent.service
      service:
        name: mackerel-agent
        state: started

playbook実行

image.png

実行確認

/opt/scripts配下にmackerelディレクトリ作成確認

作成されている。

[root@vm-001 scripts]# ls -ld /opt/scripts/mackerel/
drwxr-xr-x. 2 root root 21 Aug 20 14:53 /opt/scripts/mackerel/

カスタムメトリック用のtest.shの配置確認

配置されている。

[root@vm-001 scripts]# cat /opt/scripts/mackerel/test.sh
#!/bin/bash

unix_time=$(date +%s)
value=$(ps -ef | wc -l)
name="test.process_count"

echo -e "${name}\t${value}\t${unix_time}"

mackerel-agent.conf更新確認

カスタムメトリックの設定を追記したファイルに更新されている。

[root@vm-001 scripts]# tail /etc/mackerel-agent/mackerel-agent.conf
# command = "MUNIN_LIBDIR=/usr/share/munin mackerel-plugin-munin -plugin=/usr/share/munin/plugins/postfix_mailqueue -name=postfix.mailqueue"

# followings are other samples
# [plugin.metrics.vmstat]
# command = "ruby /etc/sensu/plugins/system/vmstat-metrics.rb"
# [plugin.metrics.curl]
# command = "ruby /etc/sensu/plugins/http/metrics-curl.rb"

[plugin.metrics.test]
command = "bash /opt/scripts/mackerel/test.sh"

mackerel-agent.service停止起動確認

起動している。(ターゲットノードの時刻設定はUTCです)

● mackerel-agent.service - mackerel.io agent
   Loaded: loaded (/usr/lib/systemd/system/mackerel-agent.service; enabled; vendor preset: disabled)
   Active: active (running) since Sun 2023-08-20 14:53:54 UTC; 2min 27s ago
     Docs: https://mackerel.io/
  Process: 7595 ExecStopPost=/bin/sh -c [ "$AUTO_RETIREMENT" == "" ] || [ "$AUTO_RETIREMENT" == "0" ] && true || /usr/bin/mackerel-agent retire -force --root $ROOT $OTHER_OPTS (code=exited, status=0/SUCCESS)
  Process: 7737 ExecStartPre=/usr/bin/mkdir -m 777 -p $MACKEREL_PLUGIN_WORKDIR (code=exited, status=0/SUCCESS)
 Main PID: 7739 (mackerel-agent)
    Tasks: 13 (limit: 2083)
   Memory: 10.8M
   CGroup: /system.slice/mackerel-agent.service
           tq7739 /usr/bin/mackerel-agent supervise --root /var/lib/mackerel-agent
           mq7743 /usr/bin/mackerel-agent --root /var/lib/mackerel-agent -child

Aug 20 14:53:54 vm-001 systemd[1]: Starting mackerel.io agent...
Aug 20 14:53:54 vm-001 systemd[1]: Started mackerel.io agent.
Aug 20 14:53:54 vm-001 mackerel-agent[7743]: 2023/08/20 14:53:54 INFO <main> Starting mackerel-agent version:0.77.1, rev:8bb573e, apibase:https://api.mackerelio.com
Aug 20 14:53:55 vm-001 mackerel-agent[7743]: 2023/08/20 14:53:55 INFO <command> Start: apibase = https://api.mackerelio.com, hostName = vm-001, hostID = ********

mackerelカスタムメトリック投稿確認

投稿されている。
image.png

1
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
1
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?