Verified Environment
Version | |
---|---|
OS | Ubuntu 20.04.5 LTS |
bash | GNU bash, version 5.0.17(1)-release (aarch64-unknown-linux-gnu) |
jq | jq-1.6 |
curl | curl 7.68.0 |
PagerDuty API | V2 2023-02-14 |
What to do
It uses 2 APIs:
- List all of the on-calls
- Filter by escalation policy and
escalation level = 1
- Filter by escalation policy and
- Get a user
Examples
Here is an example of getting the email of the primary on-caller:
#!/bin/bash
if [ "${PAGERDUTY_API_KEY}" = "" ]; then
echo "\$PAGERDUTY_API_KEY is required."
exit 1
fi
get_primary_oncallers() {
local escalation_policy_id="${1}"
curl -s --request GET \
--url "https://api.pagerduty.com/oncalls?escalation_policy_ids%5B%5D=${escalation_policy_id}" \
--header 'Accept: application/vnd.pagerduty+json;version=2' \
--header "Authorization: Token token=${PAGERDUTY_API_KEY}" \
--header 'Content-Type: application/json' | jq -r '.oncalls[] | select(.escalation_level == 1).user.id'
}
get_user_email() {
local user_id="${1}"
curl -s --request GET \
--url "https://api.pagerduty.com/users/${user_id}" \
--header 'Accept: application/vnd.pagerduty+json;version=2' \
--header "Authorization: Token token=${PAGERDUTY_API_KEY}" \
--header 'Content-Type: application/json' | jq -r '.user.email'
}
#!/bin/bash
source "pagerduty.sh"
escalation_policy_id="${1}"
if [ "${escalation_policy_id}" = "" ]; then
echo "\$1 (escalation policy ID) is required."
exit 1
fi
for user_id in $(get_primary_oncallers "${escalation_policy_id}"); do
get_user_email "${user_id}"
done
$ bash main.sh ESCALATION_POLICY_ID