Created
May 30, 2020 09:26
-
-
Save maxried/eafc7fdde4dba14d7f968377385fdc89 to your computer and use it in GitHub Desktop.
This file contains 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 | |
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 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment