Skip to content

Instantly share code, notes, and snippets.

@maxried
Created May 30, 2020 09:26

Revisions

  1. maxried created this gist May 30, 2020.
    29 changes: 29 additions & 0 deletions Zabbix Check Now Script
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,29 @@
    #!/usr/bin/env bash

    ZABBIX_URL="https://zabbix"
    ZABBIX_USER="api-user-that-has-admin-and-rw-access-to-the-items-to-check.-frontend-access-is-not-necessary."
    ZABBIX_PASS="password"

    if [ ! "$*" ]; then
    echo "$0 <item-id> <item-id> ..."
    exit 255
    fi

    IDS=$(printf '%s\n' "$@" | jq -R . | jq -sc .)

    # Login
    TMP_FILE=$(mktemp)
    cat <<< "{\"auth\":null,\"method\":\"user.login\",\"id\":1,\"params\":{\"user\":\"${ZABBIX_USER}\",\"password\":\"${ZABBIX_PASS}\"},\"jsonrpc\":\"2.0\"}" > $TMP_FILE
    API_KEY=$(curl -s -k -d "@$TMP_FILE" -H "Content-Type: application/json-rpc" "${ZABBIX_URL}/api_jsonrpc.php" | sed -e "s/^.*\"result\":\"\([^\"]*\)\".*$/\1/g")
    rm -- "$TMP_FILE"
    if [ $(wc -c <<< "$API_KEY") -ne 33 ]; then
    echo "Didn't receive an API key. Wrong password?" >&2
    exit 1
    fi
    # Check now
    curl -s -k -d "{\"auth\":\"${API_KEY}\",\"method\":\"task.create\",\"id\":1,\"params\":{\"type\":\"6\",\"itemids\":${IDS}},\"jsonrpc\":\"2.0\"}" -H "Content-Type: application/json-rpc" "${ZABBIX_URL}/api_jsonrpc.php" > /dev/null
    # Logout
    curl -s -k -d "{\"auth\":\"${API_KEY}\",\"method\":\"user.logout\",\"id\":1,\"params\":[],\"jsonrpc\":\"2.0\"}" -H "Content-Type: application/json-rpc" "${ZABBIX_URL}/api_jsonrpc.php" > /dev/null