-
Notifications
You must be signed in to change notification settings - Fork 0
/
newrelic_rest_cli
executable file
·146 lines (136 loc) · 5.61 KB
/
newrelic_rest_cli
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
#!/usr/bin/env bash
set -e -o pipefail -u
[ x"${DEBUG:-}" = "x1" ] && set -x
# Alerts rest api docs: https://docs.newrelic.com/docs/alerts/rest-api-alerts/new-relic-alerts-rest-api/rest-api-calls-new-relic-alerts
# API explorer: https://rpm.newrelic.com/api/explore/
#---------------------------------------#
# Utility functions #
#---------------------------------------#
_api_call () {
# All extra arguments will be passed to curl.
# Note that this is potentially problematic; if an unexpected command-line argument
# goes to curl, curl may think it is a URL.
[ $# -lt 2 ] && _usage "_api_call(TYPE, URL [, ..])"
local TYPE="$1" URL="$2"
shift 2
# The return status is extracted (because curl doesn't return HTTP return status on exit)
# and if it isn't a "2xx" return status, throw an error. The rest of the output is returned on stdout.
if [ x"${DRYRUN:-}" = "x1" ] ; then
echo curl -sL -X "$TYPE" -H "X-Api-Key:$NEWRELIC_API_KEY" -w "\n%{http_code}" "$URL" "$@"
exit 1
fi
local OUTPUT="$(curl -sL -X "$TYPE" -H "X-Api-Key:$NEWRELIC_API_KEY" -w "\n%{http_code}" "$URL" "$@")"
local RETURNCODE="${OUTPUT##*$'\n'}"
printf "${OUTPUT%"$RETURNCODE"}"
if [ ! "${RETURNCODE:0:1}" = "2" ] ; then
echo "$0: Error: HTTP return code was '$RETURNCODE'" 1>&2 ; exit 1
fi
}
_err () { echo "$0: Error: $@" ; exit 1 ; }
_usage () { printf "Usage: $0 $@\n" ; exit 1 ; }
#---------------------------------------#
# Alerts #
#---------------------------------------#
_alerts () {
local cmd="$1" ; shift
[ $# -lt 1 ] && _usage "$cmd alerts COMMAND\nCommands:\n\tlocation_failure_conditions\n\tsynthetic_conditions\n\tconditions\n\tpolicies [..]\n"
local func="$1"; shift
case "$func" in
location_failure_conditions)
_alerts_location_failure_conditions "$cmd" "$@" ;;
synthetic_conditions)
_alerts_synthetic_conditions "$cmd" "$@" ;;
conditions)
_alerts_conditions "$cmd" "$@" ;;
policies)
_alerts_policies "$cmd" "$@" ;;
esac
}
_alerts_location_failure_conditions () {
local cmd="$1"; shift
case "$cmd" in
get)
[ $# -gt 0 ] || _usage "$cmd alerts location_failure_conditions POLICY_ID" ;
_api_call GET "https://api.newrelic.com/v2/alerts_location_failure_conditions/policies/$1.json" ;;
delete)
[ $# -gt 0 ] || _usage "$cmd alerts location_failure_conditions CONDITION_ID" ;
_api_call DELETE "https://api.newrelic.com/v2/alerts_location_failure_conditions/$1.json" ;;
create)
[ $# -gt 1 ] || _usage "$cmd alerts location_failure_conditions POLICY_ID CONDITION_JSON" ;
_api_call POST "https://api.newrelic.com/v2/alerts_location_failure_conditions/policies/$1.json" \
-H 'Content-Type: application/json' -d "$2" ;;
update)
[ $# -gt 1 ] || _usage "$cmd alerts location_failure_conditions CONDITION_ID CONDITION_JSON" ;
_api_call PUT "https://api.newrelic.com/v2/alerts_location_failure_conditions/$1.json" \
-H 'Content-Type: application/json' -d "$2" ;;
esac
}
_alerts_synthetic_conditions () {
local cmd="$1"; shift
case "$cmd" in
get)
[ $# -gt 0 ] || _usage "$cmd alerts synthetic_conditions POLICY_ID" ;
_api_call GET 'https://api.newrelic.com/v2/alerts_synthetics_conditions.json' -d "policy_id=$1" ;;
*)
_err "alerts synthetic_conditions: '$cmd' not implemented" ;;
esac
}
_alerts_conditions () {
local cmd="$1"; shift
case "$cmd" in
get)
[ $# -gt 0 ] || _usage "$cmd alerts conditions POLICY_ID" ;
_api_call GET 'https://api.newrelic.com/v2/alerts_conditions.json' -d "policy_id=$1" ;;
*)
_err "alerts conditions: '$cmd' not implemented" ;;
esac
}
_alerts_policies () {
local cmd="$1"; shift
case "$cmd" in
get)
declare -a args=()
if [ $# -gt 0 ] ; then
if [ "$1" = "--help" ] ; then
_usage "$cmd alerts policies [KEY=VALUE ..]\n\nThe optional KEY=VALUE becomes form data passed to the API as 'filter[KEY]=VALUE'"
fi
for i in "$@" ; do
IFS='=' read -ra kv <<< "$i"
args+=("-d" "filter[${kv[0]}]=${kv[1]}")
done
else
args=("$@")
fi
_api_call GET 'https://api.newrelic.com/v2/alerts_policies.json' "${args[@]}" ;;
delete)
[ $# -lt 1 ] && _usage "$cmd alerts policies POLICY_ID"
_api_call DELETE "https://api.newrelic.com/v2/alerts_policies/$1.json" ;;
*)
_err "alerts policies: '$cmd' not implemented" ;;
esac
}
#---------------------------------------#
# Misc functions
#---------------------------------------#
_basecmd () {
local cmd="$1"; shift
[ $# -lt 1 ] && _usage "$cmd COMMAND\nCommands:\n\talerts\n"
local func="$1"; shift
case "$func" in
alerts)
_alerts $cmd "$@" ;;
esac
}
#---------------------------------------#
# Main program #
#---------------------------------------#
if [ $# -lt 1 ] ; then
_usage "COMMAND\nCommands:\n\tget\n\tdelete\n\tcreate\n\tupdate\n"
fi
if [ -z "${NEWRELIC_API_KEY:-}" ] ; then
echo "$0: Error: please set the NEWRELIC_API_KEY environment variable before continuing" 1>&2 ; exit 1
fi
cmd="$1"; shift
if [ "$cmd" = "get" -o "$cmd" = "delete" -o "$cmd" = "create" -o "$cmd" = "update" ] ; then
_basecmd "$cmd" "$@"
fi