Add a script to send queue metrics to CentOS's Zabbix
Fixes: https://pagure.io/fedora-infrastructure/issue/12617 Signed-off-by: Aurélien Bompard <aurelien@bompard.org>
This commit is contained in:
parent
cddfb47925
commit
02abc93d59
5 changed files with 130 additions and 0 deletions
81
roles/rabbitmq_cluster/files/send-rabbitmq-queue.py
Normal file
81
roles/rabbitmq_cluster/files/send-rabbitmq-queue.py
Normal file
|
@ -0,0 +1,81 @@
|
|||
#!/usr/bin/env python3
|
||||
|
||||
from argparse import ArgumentParser
|
||||
from configparser import ConfigParser
|
||||
from socket import getfqdn
|
||||
from subprocess import run
|
||||
from urllib.parse import quote
|
||||
|
||||
import requests
|
||||
|
||||
|
||||
API_URL = "http://localhost:15672/api"
|
||||
CONF_PATH = "/etc/nrpe.d/rabbitmq_args.ini"
|
||||
VHOST = "/pubsub"
|
||||
METRICS = ("messages", "messages_ready", "messages_unacknowledged")
|
||||
|
||||
|
||||
def get_http_client(conf_path):
|
||||
config = ConfigParser()
|
||||
config.read(conf_path)
|
||||
client = requests.Session()
|
||||
client.auth = (config.get("common", "username"), config.get("common", "password"))
|
||||
return client
|
||||
|
||||
|
||||
def parse_args():
|
||||
parser = ArgumentParser()
|
||||
parser.add_argument("queue_name", nargs="+")
|
||||
parser.add_argument(
|
||||
"--conf", default=CONF_PATH, help="path to NRPE's rabbitmq_args.ini file"
|
||||
)
|
||||
parser.add_argument("--vhost", default=VHOST, help="the rabbitmq vhost")
|
||||
parser.add_argument(
|
||||
"-s", "--host", default=getfqdn(), help="the server name in Zabbix"
|
||||
)
|
||||
parser.add_argument("--tls-psk-identity", help="passed down to zabbix_sender")
|
||||
parser.add_argument("--tls-psk-file", help="passed down to zabbix_sender")
|
||||
return parser.parse_args()
|
||||
|
||||
|
||||
def get_queue_values(http, vhost, queue_name):
|
||||
response = http.get(f"{API_URL}/queues/{vhost}/{queue_name}")
|
||||
response.raise_for_status()
|
||||
data = response.json()
|
||||
return {metric: data[metric] for metric in METRICS}
|
||||
|
||||
|
||||
def main():
|
||||
args = parse_args()
|
||||
http = get_http_client(args.conf)
|
||||
vhost = quote(args.vhost, safe="")
|
||||
zabbix_sender_args = []
|
||||
if args.tls_psk_identity:
|
||||
zabbix_sender_args.extend(
|
||||
[
|
||||
"--tls-connect",
|
||||
"psk",
|
||||
"--tls-psk-identity",
|
||||
args.tls_psk_identity,
|
||||
"--tls-psk-file",
|
||||
args.tls_psk_file,
|
||||
]
|
||||
)
|
||||
zabbix_sender_args.extend(["--host", args.host])
|
||||
for queue_name in args.queue_name:
|
||||
data = get_queue_values(http, vhost, queue_name)
|
||||
for metric, value in data.items():
|
||||
command = [
|
||||
"zabbit_sender",
|
||||
*zabbix_sender_args,
|
||||
"--key",
|
||||
f"rabbitmq.queue.{queue_name}.{metric}"
|
||||
"--value",
|
||||
str(value),
|
||||
]
|
||||
print(" ".join(command))
|
||||
# run(command, check=True)
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
main()
|
8
roles/rabbitmq_cluster/files/send-rabbitmq-queue.service
Normal file
8
roles/rabbitmq_cluster/files/send-rabbitmq-queue.service
Normal file
|
@ -0,0 +1,8 @@
|
|||
[Unit]
|
||||
Description=Send queue metrics to Zabbix
|
||||
|
||||
[Service]
|
||||
Type=oneshot
|
||||
ExecStart=/usr/local/bin/send-rabbitmq-queue --tls-psk-identity foo --tls-psk-file bar centos-stream-robosignatory
|
||||
User=nrpe
|
||||
Group=nrpe
|
12
roles/rabbitmq_cluster/files/send-rabbitmq-queue.timer
Normal file
12
roles/rabbitmq_cluster/files/send-rabbitmq-queue.timer
Normal file
|
@ -0,0 +1,12 @@
|
|||
[Unit]
|
||||
Description=Send queue metrics to Zabbix
|
||||
ConditionPathExists=/etc/nrpe.d/rabbitmq_args.ini
|
||||
Wants=network-online.target
|
||||
|
||||
[Timer]
|
||||
OnCalendar=5min
|
||||
Persistent=true
|
||||
|
||||
[Install]
|
||||
WantedBy=timers.target
|
||||
|
22
roles/rabbitmq_cluster/tasks/centos-monitoring.yml
Normal file
22
roles/rabbitmq_cluster/tasks/centos-monitoring.yml
Normal file
|
@ -0,0 +1,22 @@
|
|||
- name: Copy over the check script
|
||||
ansible.builtin.copy:
|
||||
src: send-rabbitmq-queue.py
|
||||
dest: /usr/local/bin/send-rabbitmq-queue
|
||||
mode: 0750
|
||||
owner: root
|
||||
group: nrpe
|
||||
|
||||
- name: Setup systemd unit files
|
||||
ansible.builtin.copy:
|
||||
src: send-rabbitmq-queue.{{item}}
|
||||
dest: /etc/systemd/system/send-rabbitmq-queue.{{item}}
|
||||
with_items:
|
||||
- service
|
||||
- timer
|
||||
notify:
|
||||
- Reload systemd
|
||||
|
||||
- name: Enable systemd timer
|
||||
service:
|
||||
name: send-rabbitmq-queue.timer
|
||||
enabled: yes
|
|
@ -449,6 +449,13 @@
|
|||
ansible.builtin.command: /etc/nagios/selinux-load.sh
|
||||
when: selinux_module is changed
|
||||
|
||||
# Queue monitoring for CentOS Zabbix
|
||||
- import_tasks: centos-monitoring.yml
|
||||
when: inventory_hostname.startswith('rabbitmq03')
|
||||
tags:
|
||||
- rabbitmq_cluster
|
||||
- config
|
||||
|
||||
# Individual applications accounts & queues
|
||||
- import_tasks: apps.yml
|
||||
tags:
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue