Created
January 14, 2026 11:16
-
-
Save taiseiue/e75f2e7254872364008249b3d4b96cb7 to your computer and use it in GitHub Desktop.
Nextcloudのジョブの状態をテキストファイルに出力するスクリプト
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #!/usr/bin/env bash | |
| set -euo pipefail | |
| DB_NAME="nextcloud" | |
| TABLE="oc_jobs" | |
| OUT="/var/lib/node_exporter/textfile/nextcloud_jobs.prom" | |
| TMP="$(mktemp)" | |
| trap 'rm -f "$TMP"' EXIT | |
| NOW="$(date +%s)" | |
| HAS_DUR=$( | |
| mysql -N -s -h localhost -u "$DB_USER" -p"$DB_PASS" "$DB_NAME" \ | |
| -e "SELECT COUNT(*) FROM information_schema.COLUMNS | |
| WHERE TABLE_SCHEMA='${DB_NAME}' AND TABLE_NAME='${TABLE}' AND COLUMN_NAME='execution_duration';" | |
| ) | |
| Q="SELECT id, class, COALESCE(last_run,0) AS last_run, COALESCE(execution_duration,0) AS dur FROM ${TABLE};" | |
| # クエリ結果を一旦変数に格納して、空かどうか判定する | |
| RESULT=$(mysql -N -s -h localhost -u "$DB_USER" -p"$DB_PASS" "$DB_NAME" -e "$Q") | |
| if [[ -n "$RESULT" ]]; then | |
| { | |
| echo "# HELP nextcloud_job_last_run_timestamp_seconds Job last_run from oc_jobs" | |
| echo "# TYPE nextcloud_job_last_run_timestamp_seconds gauge" | |
| echo "# HELP nextcloud_job_staleness_seconds Seconds since last_run (now-last_run)" | |
| echo "# TYPE nextcloud_job_staleness_seconds gauge" | |
| if [[ "$HAS_DUR" -eq 1 ]]; then | |
| echo "# HELP nextcloud_job_execution_duration_seconds Last execution duration from oc_jobs" | |
| echo "# TYPE nextcloud_job_execution_duration_seconds gauge" | |
| fi | |
| echo "$RESULT" | while IFS=$'\t' read -r jid klass last_run dur; do | |
| # バックスラッシュをすべてアンダースコアに置換 | |
| label_class="${klass//\\/_}" | |
| label_class="${label_class//\"/\\\"}" | |
| echo "nextcloud_job_last_run_timestamp_seconds{class=\"${label_class}\",job_id=\"${jid}\"} ${last_run}" | |
| echo "nextcloud_job_staleness_seconds{class=\"${label_class}\",job_id=\"${jid}\"} $((NOW - last_run))" | |
| echo "nextcloud_job_execution_duration_seconds{class=\"${label_class}\",job_id=\"${jid}\"} ${dur}" | |
| done | |
| } > "$TMP" | |
| sudo mv "$TMP" "$OUT" | |
| sudo chmod 644 "$OUT" | |
| else | |
| sudo rm -f "$OUT" | |
| fi |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment