You need to enable JavaScript to run this app.
最新活动
大模型
产品
解决方案
定价
生态与合作
支持与服务
开发者
了解我们

为Zabbix触发器添加脚本参数:Rundeck任务状态监控展示需求

Got it, let's walk through how to tweak your Rundeck monitoring script to support Zabbix parameters, run it directly from your Rundeck server, and get those job statuses displaying nicely in your Zabbix dashboard. Here's a step-by-step breakdown that's easy to follow:

1. Refine the Script to Support Zabbix Parameters

Your original script has hardcoded values—let's adjust it to accept dynamic parameters so Zabbix triggers can pass in project names, API tokens, or specific job names. Here's the updated version:

#!/bin/bash

# Usage: ./rundeck_job_status.sh <project_name> <api_token> [job_name]
# Job name is optional; omit it to fetch the latest execution across all jobs
PROJECT_NAME="${1:-demo}"
API_TOKEN="${2:-ZP9znayUp3Ktp26uQjSQGnEfTzDvqPDA}"
JOB_NAME="${3:-}"

# Fetch execution data from Rundeck API
RESPONSE=$(curl -s -H "Accept: application/json" -X GET "http://172.30.61.88:4440/api/20/project/${PROJECT_NAME}/executions?authtoken=${API_TOKEN}")

# Filter and format output (TSV for easy Zabbix parsing)
if [ -n "${JOB_NAME}" ]; then
    echo "${RESPONSE}" | jq -r '.executions[] | select(.job.name == "'"${JOB_NAME}"'") | [.status, .job.name, ."date-ended".date] | @tsv' | sort -k3r | head -n1
else
    echo "${RESPONSE}" | jq -r '.executions[] | select(.job.name != null) | [.status, .job.name, ."date-ended".date] | @tsv' | sort -k3r | head -n1
fi

Key improvements:

  • Replaced hardcoded values with positional parameters
  • Added optional job_name to target specific tasks
  • Uses TSV output (@tsv) so Zabbix can easily split status, job name, and end time
  • Removed the intermediate 1.json file for cleaner, faster execution
2. Deploy the Script on Your Rundeck Server
  • Save the script to a persistent directory, e.g., /usr/local/bin/rundeck_job_status.sh
  • Grant execute permissions:
    chmod +x /usr/local/bin/rundeck_job_status.sh
    
  • Test it to make sure it works:
    • Get latest job status for the demo project:
      ./rundeck_job_status.sh demo ZP9znayUp3Ktp26uQjSQGnEfTzDvqPDA
      
    • Get status for a specific job named "Daily DB Backup":
      ./rundeck_job_status.sh demo ZP9znayUp3Ktp26uQjSQGnEfTzDvqPDA "Daily DB Backup"
      
    You should see output like: SUCCEEDED Daily DB Backup 2024-05-20T15:45:00Z
3. Configure Zabbix to Use the Script

3.1 Set Up a Custom Zabbix Agent Parameter

Edit your Zabbix Agent config file (zabbix_agentd.conf or zabbix_agent2.conf) on the Rundeck server and add this line:

UserParameter=rundeck.job.status[*],/usr/local/bin/rundeck_job_status.sh "$1" "$2" "$3"

Restart the Zabbix Agent to apply changes:

systemctl restart zabbix-agent

3.2 Create a Zabbix Item

  • Go to the Zabbix frontend → Find your Rundeck host → Configuration → Items → Create item
    • Name: Rundeck Job Status - Daily DB Backup (customize to match your job)
    • Key: rundeck.job.status[demo,ZP9znayUp3Ktp26uQjSQGnEfTzDvqPDA,"Daily DB Backup"]
    • Type: Zabbix client (use active/passive based on your Agent setup)
    • Type of information: Text
    • Update interval: Set to your needs (e.g., 5 minutes for frequent jobs)

3.3 Build a Trigger for Failures

  • Go to Configuration → Triggers → Create trigger
    • Name: Rundeck Job "Daily DB Backup" Failed
    • Expression: {Rundeck-Server:rundeck.job.status[demo,ZP9znayUp3Ktp26uQjSQGnEfTzDvqPDA,"Daily DB Backup"].str(0)}=FAILED
    • Severity: Choose based on job importance (e.g., "High" for critical backups)
      This trigger checks the first field of the script's output (the status) and alerts if it's FAILED.
4. Display Status in Zabbix Dashboard
  • Go to Dashboard → Create dashboard
  • Add these widgets to visualize job status:
    • Text box: Use Zabbix macros to show human-readable status:
      Job: {Rundeck-Server:rundeck.job.status[demo,ZP9znayUp3Ktp26uQjSQGnEfTzDvqPDA,"Daily DB Backup"].str(1)}
      Status: *{Rundeck-Server:rundeck.job.status[demo,ZP9znayUp3Ktp26uQjSQGnEfTzDvqPDA,"Daily DB Backup"].str(0)}*
      Last Run: {Rundeck-Server:rundeck.job.status[demo,ZP9znayUp3Ktp26uQjSQGnEfTzDvqPDA,"Daily DB Backup"].str(2)}
      
    • Trigger status: Add your failure trigger to quickly spot issues
    • Latest data: Add the job status item to view historical runs
Quick Notes for Stability
  • Ensure the Zabbix Agent user has permission to execute the script and access the Rundeck API
  • If Rundeck uses HTTPS, update the curl command to https:// and add -k (for self-signed certificates)
  • Store your API token securely (e.g., in an environment variable) instead of hardcoding it for better security

内容的提问来源于stack exchange,提问作者Milister

火山引擎 最新活动