用Ansible来设置Mackerel的自定义度量设置
运行环境
- 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
我想要做的事情
对于目标节点(本次为一台),执行以下操作。
-
- 在”/opt/scripts”目录下创建”mackerel”文件夹
-
- 将”test.sh”文件放置在”/opt/scripts/mackerel”文件夹中,用于自定义指标
-
- 更新”mackerel-agent.conf”配置文件
-
- 停止”mackerel-agent.service”服务
- 启动”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
确定执行
确认在/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。
已经启动。(目标节点的时间设置为协调世界时)
● 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 = ********