Difference between revisions of "Pager Duty API Bash"

From UVOO Tech Wiki
Jump to navigation Jump to search
(Created page with "https://developer.pagerduty.com/docs/ZG9jOjExMDI5NTc3-events-api-v1 https://gist.github.com/alperkokmen/11057941 ``` #!/bin/bash CONTENT_TYPE="application/json" DESCRIPTION...")
 
Line 7: Line 7:
  
 
CONTENT_TYPE="application/json"
 
CONTENT_TYPE="application/json"
DESCRIPTION="bad things™ are happening"
 
 
URL="https://events.pagerduty.com/generic/2010-04-15/create_event.json"
 
URL="https://events.pagerduty.com/generic/2010-04-15/create_event.json"
  
if [ $# -ne 3 ]; then
+
# if [ $# -ne 3 ]; then
 +
if [ $# -lt 3 ]; then
 
   echo "Usage: pd-event.sh [TYPE] [SERVICE KEY] [INCIDENT KEY]"
 
   echo "Usage: pd-event.sh [TYPE] [SERVICE KEY] [INCIDENT KEY]"
 
   echo "  - TYPE: [t]rigger | [a]cknowledge | [r]esolve"
 
   echo "  - TYPE: [t]rigger | [a]cknowledge | [r]esolve"
 
   echo "  - SERVICE KEY: unique identifier for service"
 
   echo "  - SERVICE KEY: unique identifier for service"
 
   echo "  - INCIDENT KEY: unique identifier for incident"
 
   echo "  - INCIDENT KEY: unique identifier for incident"
 +
  echo "  - Description: description text for incdient (optional as only used for trigger)"
 
   exit 1
 
   exit 1
 
fi
 
fi
Line 22: Line 23:
 
   t)  curl -H "Content-type: ${CONTENT_TYPE}" \
 
   t)  curl -H "Content-type: ${CONTENT_TYPE}" \
 
           -X POST \
 
           -X POST \
           -d "{ \"service_key\": \"$2\", \"event_type\": \"trigger\", \"incident_key\": \"$3\", \"description\": \"${DESCRIPTION}\" }" \
+
           -d "{ \"service_key\": \"$2\", \"event_type\": \"trigger\", \"incident_key\": \"$3\", \"description\": \"$4\" }" \
 
           "${URL}"
 
           "${URL}"
 
       ;;
 
       ;;
Line 37: Line 38:
  
 
esac
 
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
 
```
 
```

Revision as of 17:18, 5 October 2022

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 -H "Content-type: ${CONTENT_TYPE}" \
           -X POST \
           -d "{ \"service_key\": \"$2\", \"event_type\": \"trigger\", \"incident_key\": \"$3\", \"description\": \"$4\" }" \
           "${URL}"
      ;;
  a)  curl -H "Content-type: ${CONTENT_TYPE}" \
           -X POST \
           -d "{ \"service_key\": \"$2\", \"event_type\": \"acknowledge\", \"incident_key\": \"$3\" }" \
           "${URL}"
      ;;
  r)  curl -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