From 7d673cbee9041d760b83eadb8002be3bbe4ed32d Mon Sep 17 00:00:00 2001 From: Erich Blume Date: Sat, 17 Jan 2026 21:09:20 -0800 Subject: [PATCH] Add zot_metrics role for Prometheus monitoring Collects zot_up metric via textfile collector every 60 seconds. Additional metrics can be added later if zot exposes them. Co-Authored-By: Claude Opus 4.5 --- ansible/playbooks/indri.yml | 2 + ansible/roles/zot_metrics/defaults/main.yml | 6 +++ ansible/roles/zot_metrics/handlers/main.yml | 6 +++ ansible/roles/zot_metrics/tasks/main.yml | 43 +++++++++++++++++++ .../templates/zot-metrics.plist.j2 | 21 +++++++++ .../zot_metrics/templates/zot-metrics.sh.j2 | 25 +++++++++++ 6 files changed, 103 insertions(+) create mode 100644 ansible/roles/zot_metrics/defaults/main.yml create mode 100644 ansible/roles/zot_metrics/handlers/main.yml create mode 100644 ansible/roles/zot_metrics/tasks/main.yml create mode 100644 ansible/roles/zot_metrics/templates/zot-metrics.plist.j2 create mode 100644 ansible/roles/zot_metrics/templates/zot-metrics.sh.j2 diff --git a/ansible/playbooks/indri.yml b/ansible/playbooks/indri.yml index 5796b52..570b60f 100644 --- a/ansible/playbooks/indri.yml +++ b/ansible/playbooks/indri.yml @@ -101,6 +101,8 @@ tags: devpi_metrics - role: zot tags: zot + - role: zot_metrics + tags: zot_metrics - role: plex_metrics tags: plex_metrics - role: postgresql diff --git a/ansible/roles/zot_metrics/defaults/main.yml b/ansible/roles/zot_metrics/defaults/main.yml new file mode 100644 index 0000000..3280b20 --- /dev/null +++ b/ansible/roles/zot_metrics/defaults/main.yml @@ -0,0 +1,6 @@ +--- +zot_metrics_url: http://localhost:5050/v2/_catalog +zot_metrics_dir: /opt/homebrew/var/node_exporter/textfile +zot_metrics_script: /Users/erichblume/bin/zot-metrics +zot_metrics_interval: 60 # seconds between metric collection +zot_metrics_log_dir: /opt/homebrew/var/log diff --git a/ansible/roles/zot_metrics/handlers/main.yml b/ansible/roles/zot_metrics/handlers/main.yml new file mode 100644 index 0000000..44fe49b --- /dev/null +++ b/ansible/roles/zot_metrics/handlers/main.yml @@ -0,0 +1,6 @@ +--- +- name: Reload zot-metrics + ansible.builtin.shell: | + launchctl unload ~/Library/LaunchAgents/mcquack.eblume.zot-metrics.plist 2>/dev/null || true + launchctl load ~/Library/LaunchAgents/mcquack.eblume.zot-metrics.plist + changed_when: true diff --git a/ansible/roles/zot_metrics/tasks/main.yml b/ansible/roles/zot_metrics/tasks/main.yml new file mode 100644 index 0000000..9b43125 --- /dev/null +++ b/ansible/roles/zot_metrics/tasks/main.yml @@ -0,0 +1,43 @@ +--- +- name: Ensure metrics directory exists + ansible.builtin.file: + path: "{{ zot_metrics_dir }}" + state: directory + mode: '0755' + +- name: Ensure log directory exists + ansible.builtin.file: + path: "{{ zot_metrics_log_dir }}" + state: directory + mode: '0755' + +- name: Ensure bin directory exists + ansible.builtin.file: + path: "{{ zot_metrics_script | dirname }}" + state: directory + mode: '0755' + +- name: Deploy zot-metrics script + ansible.builtin.template: + src: zot-metrics.sh.j2 + dest: "{{ zot_metrics_script }}" + mode: '0755' + +- name: Deploy zot-metrics LaunchAgent plist + ansible.builtin.template: + src: zot-metrics.plist.j2 + dest: ~/Library/LaunchAgents/mcquack.eblume.zot-metrics.plist + mode: '0644' + notify: Reload zot-metrics + +- name: Check if zot-metrics LaunchAgent is loaded + ansible.builtin.command: launchctl list mcquack.eblume.zot-metrics + register: zot_metrics_launchctl_check + changed_when: false + failed_when: false + +- name: Load zot-metrics LaunchAgent if not loaded + ansible.builtin.command: launchctl load ~/Library/LaunchAgents/mcquack.eblume.zot-metrics.plist + when: zot_metrics_launchctl_check.rc != 0 + changed_when: true + failed_when: false diff --git a/ansible/roles/zot_metrics/templates/zot-metrics.plist.j2 b/ansible/roles/zot_metrics/templates/zot-metrics.plist.j2 new file mode 100644 index 0000000..6efae24 --- /dev/null +++ b/ansible/roles/zot_metrics/templates/zot-metrics.plist.j2 @@ -0,0 +1,21 @@ + + + + + + Label + mcquack.eblume.zot-metrics + ProgramArguments + + {{ zot_metrics_script }} + + StartInterval + {{ zot_metrics_interval }} + RunAtLoad + + StandardErrorPath + {{ zot_metrics_log_dir }}/mcquack.zot-metrics.err.log + StandardOutPath + {{ zot_metrics_log_dir }}/mcquack.zot-metrics.out.log + + diff --git a/ansible/roles/zot_metrics/templates/zot-metrics.sh.j2 b/ansible/roles/zot_metrics/templates/zot-metrics.sh.j2 new file mode 100644 index 0000000..24aaffd --- /dev/null +++ b/ansible/roles/zot_metrics/templates/zot-metrics.sh.j2 @@ -0,0 +1,25 @@ +#!/bin/bash +# {{ ansible_managed }} +# Collects Zot registry metrics for node_exporter textfile collector + +set -euo pipefail + +METRICS_URL="{{ zot_metrics_url }}" +OUTPUT_FILE="{{ zot_metrics_dir }}/zot.prom" +TEMP_FILE="${OUTPUT_FILE}.tmp" + +# Start output file with header +cat > "$TEMP_FILE" << 'HEADER' +# HELP zot_up Zot registry is up and responding +# TYPE zot_up gauge +HEADER + +# Check if zot is up +if curl -sf "$METRICS_URL" > /dev/null 2>&1; then + echo "zot_up 1" >> "$TEMP_FILE" +else + echo "zot_up 0" >> "$TEMP_FILE" +fi + +# Atomic move +mv "$TEMP_FILE" "$OUTPUT_FILE"