|
| 1 | +#!/usr/bin/env sh |
| 2 | + |
| 3 | +#Support OpsGenie API integration |
| 4 | + |
| 5 | +#OPSGENIE_API_KEY="" Required, opsgenie api key |
| 6 | +#OPSGENIE_REGION="" Optional, opsgenie region, can be EU or US (default: US) |
| 7 | +#OPSGENIE_PRIORITY_SUCCESS="" Optional, opsgenie priority for success (default: P5) |
| 8 | +#OPSGENIE_PRIORITY_ERROR="" Optional, opsgenie priority for error (default: P2) |
| 9 | +#OPSGENIE_PRIORITY_SKIP="" Optional, opsgenie priority for renew skipped (default: P5) |
| 10 | + |
| 11 | +_OPSGENIE_AVAIL_REGION="US,EU" |
| 12 | +_OPSGENIE_AVAIL_PRIORITIES="P1,P2,P3,P4,P5" |
| 13 | + |
| 14 | +opsgenie_send() { |
| 15 | + _subject="$1" |
| 16 | + _content="$2" |
| 17 | + _status_code="$3" #0: success, 1: error, 2($RENEW_SKIP): skipped |
| 18 | + |
| 19 | + OPSGENIE_API_KEY="${OPSGENIE_API_KEY:-$(_readaccountconf_mutable OPSGENIE_API_KEY)}" |
| 20 | + if [ -z "$OPSGENIE_API_KEY" ]; then |
| 21 | + OPSGENIE_API_KEY="" |
| 22 | + _err "You didn't specify an OpsGenie API key OPSGENIE_API_KEY yet." |
| 23 | + return 1 |
| 24 | + fi |
| 25 | + _saveaccountconf_mutable OPSGENIE_API_KEY "$OPSGENIE_API_KEY" |
| 26 | + export _H1="Authorization: GenieKey $OPSGENIE_API_KEY" |
| 27 | + |
| 28 | + OPSGENIE_REGION="${OPSGENIE_REGION:-$(_readaccountconf_mutable OPSGENIE_REGION)}" |
| 29 | + if [ -z "$OPSGENIE_REGION" ]; then |
| 30 | + OPSGENIE_REGION="US" |
| 31 | + _info "The OPSGENIE_REGION is not set, so use the default US as regeion." |
| 32 | + elif ! _hasfield "$_OPSGENIE_AVAIL_REGION" "$OPSGENIE_REGION"; then |
| 33 | + _err "The OPSGENIE_REGION \"$OPSGENIE_REGION\" is not available, should be one of $_OPSGENIE_AVAIL_REGION" |
| 34 | + OPSGENIE_REGION="" |
| 35 | + return 1 |
| 36 | + else |
| 37 | + _saveaccountconf_mutable OPSGENIE_REGION "$OPSGENIE_REGION" |
| 38 | + fi |
| 39 | + |
| 40 | + OPSGENIE_PRIORITY_SUCCESS="${OPSGENIE_PRIORITY_SUCCESS:-$(_readaccountconf_mutable OPSGENIE_PRIORITY_SUCCESS)}" |
| 41 | + if [ -z "$OPSGENIE_PRIORITY_SUCCESS" ]; then |
| 42 | + OPSGENIE_PRIORITY_SUCCESS="P5" |
| 43 | + _info "The OPSGENIE_PRIORITY_SUCCESS is not set, so use the default P5 as priority." |
| 44 | + elif ! _hasfield "$_OPSGENIE_AVAIL_PRIORITIES" "$OPSGENIE_PRIORITY_SUCCESS"; then |
| 45 | + _err "The OPSGENIE_PRIORITY_SUCCESS \"$OPSGENIE_PRIORITY_SUCCESS\" is not available, should be one of $_OPSGENIE_AVAIL_PRIORITIES" |
| 46 | + OPSGENIE_PRIORITY_SUCCESS="" |
| 47 | + return 1 |
| 48 | + else |
| 49 | + _saveaccountconf_mutable OPSGENIE_PRIORITY_SUCCESS "$OPSGENIE_PRIORITY_SUCCESS" |
| 50 | + fi |
| 51 | + |
| 52 | + OPSGENIE_PRIORITY_ERROR="${OPSGENIE_PRIORITY_ERROR:-$(_readaccountconf_mutable OPSGENIE_PRIORITY_ERROR)}" |
| 53 | + if [ -z "$OPSGENIE_PRIORITY_ERROR" ]; then |
| 54 | + OPSGENIE_PRIORITY_ERROR="P2" |
| 55 | + _info "The OPSGENIE_PRIORITY_ERROR is not set, so use the default P2 as priority." |
| 56 | + elif ! _hasfield "$_OPSGENIE_AVAIL_PRIORITIES" "$OPSGENIE_PRIORITY_ERROR"; then |
| 57 | + _err "The OPSGENIE_PRIORITY_ERROR \"$OPSGENIE_PRIORITY_ERROR\" is not available, should be one of $_OPSGENIE_AVAIL_PRIORITIES" |
| 58 | + OPSGENIE_PRIORITY_ERROR="" |
| 59 | + return 1 |
| 60 | + else |
| 61 | + _saveaccountconf_mutable OPSGENIE_PRIORITY_ERROR "$OPSGENIE_PRIORITY_ERROR" |
| 62 | + fi |
| 63 | + |
| 64 | + OPSGENIE_PRIORITY_SKIP="${OPSGENIE_PRIORITY_SKIP:-$(_readaccountconf_mutable OPSGENIE_PRIORITY_SKIP)}" |
| 65 | + if [ -z "$OPSGENIE_PRIORITY_SKIP" ]; then |
| 66 | + OPSGENIE_PRIORITY_SKIP="P5" |
| 67 | + _info "The OPSGENIE_PRIORITY_SKIP is not set, so use the default P5 as priority." |
| 68 | + elif ! _hasfield "$_OPSGENIE_AVAIL_PRIORITIES" "$OPSGENIE_PRIORITY_SKIP"; then |
| 69 | + _err "The OPSGENIE_PRIORITY_SKIP \"$OPSGENIE_PRIORITY_SKIP\" is not available, should be one of $_OPSGENIE_AVAIL_PRIORITIES" |
| 70 | + OPSGENIE_PRIORITY_SKIP="" |
| 71 | + return 1 |
| 72 | + else |
| 73 | + _saveaccountconf_mutable OPSGENIE_PRIORITY_SKIP "$OPSGENIE_PRIORITY_SKIP" |
| 74 | + fi |
| 75 | + |
| 76 | + case "$OPSGENIE_REGION" in |
| 77 | + "US") |
| 78 | + _opsgenie_url="https://api.opsgenie.com/v2/alerts" |
| 79 | + ;; |
| 80 | + "EU") |
| 81 | + _opsgenie_url="https://api.eu.opsgenie.com/v2/alerts" |
| 82 | + ;; |
| 83 | + *) |
| 84 | + _err "opsgenie region error." |
| 85 | + return 1 |
| 86 | + ;; |
| 87 | + esac |
| 88 | + |
| 89 | + case $_status_code in |
| 90 | + 0) |
| 91 | + _priority=$OPSGENIE_PRIORITY_SUCCESS |
| 92 | + ;; |
| 93 | + 1) |
| 94 | + _priority=$OPSGENIE_PRIORITY_ERROR |
| 95 | + ;; |
| 96 | + 2) |
| 97 | + _priority=$OPSGENIE_PRIORITY_SKIP |
| 98 | + ;; |
| 99 | + *) |
| 100 | + _priority=$OPSGENIE_PRIORITY_ERROR |
| 101 | + ;; |
| 102 | + esac |
| 103 | + |
| 104 | + _subject_json=$(echo "$_subject" | _json_encode) |
| 105 | + _content_json=$(echo "$_content" | _json_encode) |
| 106 | + _subject_underscore=$(echo "$_subject" | sed 's/ /_/g') |
| 107 | + _alias_json=$(echo "acme.sh-$(hostname)-$_subject_underscore-$(date +%Y%m%d)" | base64 --wrap=0 | _json_encode) |
| 108 | + |
| 109 | + _data="{ |
| 110 | + \"message\": \"$_subject_json\", |
| 111 | + \"alias\": \"$_alias_json\", |
| 112 | + \"description\": \"$_content_json\", |
| 113 | + \"tags\": [ |
| 114 | + \"acme.sh\", |
| 115 | + \"host:$(hostname)\" |
| 116 | + ], |
| 117 | + \"entity\": \"$(hostname -f)\", |
| 118 | + \"priority\": \"$_priority\" |
| 119 | +}" |
| 120 | + |
| 121 | + if response=$(_post "$_data" "$_opsgenie_url" "" "" "application/json"); then |
| 122 | + if ! _contains "$response" error; then |
| 123 | + _info "opsgenie send success." |
| 124 | + return 0 |
| 125 | + fi |
| 126 | + fi |
| 127 | + _err "opsgenie send error." |
| 128 | + _err "$response" |
| 129 | + return 1 |
| 130 | +} |
0 commit comments