Pager Duty API Bash

From UVOO Tech Wiki
Revision as of 17:27, 5 October 2022 by Busk (talk | contribs)
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to navigation Jump to search

https://developer.pagerduty.com/docs/ZG9jOjExMDI5NTc3-events-api-v1

https://gist.github.com/alperkokmen/11057941

#!/bin/bash

CONTENT_TYPE="application/json"
URL="https://events.pagerduty.com/generic/2010-04-15/create_event.json"

# if [ $# -ne 3 ]; then
if [ $# -lt 3 ]; then
  echo "Usage: pd-event.sh [TYPE] [SERVICE KEY] [INCIDENT KEY]"
  echo "  - TYPE: [t]rigger | [a]cknowledge | [r]esolve"
  echo "  - SERVICE KEY: unique identifier for service"
  echo "  - INCIDENT KEY: unique identifier for incident"
  echo "  - Description: description text for incdient (optional as only used for trigger)"
  exit 1
fi

case $1 in

  t)  curl -s -H "Content-type: ${CONTENT_TYPE}" \
           -X POST \
           -d "{ \"service_key\": \"$2\", \"event_type\": \"trigger\", \"incident_key\": \"$3\", \"description\": \"$4\" }" \
           "${URL}"
      ;;
  a)  curl -s -H "Content-type: ${CONTENT_TYPE}" \
           -X POST \
           -d "{ \"service_key\": \"$2\", \"event_type\": \"acknowledge\", \"incident_key\": \"$3\" }" \
           "${URL}"
      ;;
  r)  curl -s -H "Content-type: ${CONTENT_TYPE}" \
           -X POST \
           -d "{ \"service_key\": \"$2\", \"event_type\": \"resolve\", \"incident_key\": \"$3\" }" \
           "${URL}"
      ;;

esac

And a simple script to check url

#!/bin/sh

interval_seconds=60
service_integration_id=<your id>

ctrl_c () {
  echo "** Trapped CTRL-C"
  echo counter is $counter
  exit 1
}


main () {
  trap ctrl_c INT
  counter=0
  status=0
  # url="https://mail.extendhealth.com"
  # string='Outlook'
  url="https://www.uvoo.io"
  string='Uvoo'
  ts=$(date)
  echo Starting URL monitor on $ts.
  while true; do
    r=$(curl -sLv ${url} 2>&1 | grep ${string})
    if [ -z "$r" ]; then
      ts=$(date)
      msg="E: $ts $url string match failed!"
      echo "$msg"
      status=1
      ./pagerduty t $service_integration_id busktest "$msg"
    elif [ ! -z "$r" ] && [ $status -eq 1 ]; then
      echo $url resolved
      ./pagerduty r $service_integration_id busktest
      status=0
    else
      true
    fi
    counter=$((counter ++ 1))
    sleep $interval_seconds
  done
}


main