Skip to content

Commit b0df122

Browse files
committed
Added optional arguments for apiCall and added event list
1 parent 1b5e629 commit b0df122

File tree

7 files changed

+119
-119
lines changed

7 files changed

+119
-119
lines changed

adc.sh

Lines changed: 51 additions & 58 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
#!/bin/bash
22
ADC_VERSION="v0.2.0"
3-
ADC_LAST_COMMIT="423890fe40e000d1f75fa6fc96065877ecdb9e2c"
3+
ADC_LAST_COMMIT="1b5e629b1d5f9ec47d11e15e71e62df6a301988c"
44
USER_CONFIG="$HOME/.appdynamics/adc/config.sh"
55
GLOBAL_CONFIG="/etc/appdynamics/adc/config.sh"
66
CONFIG_CONTROLLER_COOKIE_LOCATION="/tmp/appdynamics-controller-cookie.txt"
@@ -13,6 +13,12 @@ CONFIG_PORTAL_COOKIE_LOCATION="/tmp/appdynamics-portal-cookie.txt"
1313
# - output (msg to stdout)
1414
# An empty string silents all output
1515
CONFIG_OUTPUT_VERBOSITY="error,output"
16+
# Default Colors
17+
COLOR_WARNING="\033[0;33m"
18+
COLOR_INFO="\033[0;32m"
19+
COLOR_ERROR="\033[0;31m"
20+
COLOR_DEBUG="\033[0;35m"
21+
COLOR_RESET="\033[0m"
1622
GLOBAL_COMMANDS=""
1723
GLOBAL_HELP=""
1824
GLOBAL_LONG_HELP_COUNTER=0
@@ -30,11 +36,6 @@ function describe {
3036
GLOBAL_LONG_HELP_COMMANDS[${#GLOBAL_LONG_HELP_COMMANDS[@]}]="$1"
3137
GLOBAL_LONG_HELP_STRINGS[${#GLOBAL_LONG_HELP_STRINGS[@]}]=$(cat)
3238
}
33-
COLOR_WARNING="\033[0;33m"
34-
COLOR_INFO="\033[0;32m"
35-
COLOR_ERROR="\033[0;31m"
36-
COLOR_DEBUG="\033[0;35m"
37-
COLOR_RESET="\033[0m"
3839
function debug {
3940
if [ "${CONFIG_OUTPUT_VERBOSITY/debug}" != "$CONFIG_OUTPUT_VERBOSITY" ]; then
4041
echo -e "${COLOR_DEBUG}DEBUG: $*${COLOR_RESET}"
@@ -106,6 +107,7 @@ function recursiveSource {
106107
}
107108
function apiCall {
108109
local OPTS
110+
local OPTIONAL_OPTIONS=""
109111
local METHOD="GET"
110112
while getopts "X:d:" opt "$@";
111113
do
@@ -127,39 +129,53 @@ function apiCall {
127129
OLDIFS=$IFS
128130
IFS="\$"
129131
for MATCH in $PAYLOAD ; do
130-
if [ "${MATCH::1}" = "{" ] && [ "${MATCH:2:1}" = "}" ] ; then
131-
MATCH=${MATCH:1}
132-
OPT=${MATCH%%\}*}:
132+
if [[ $MATCH =~ \{([a-zA-Z])(\??)\} ]]; then
133+
OPT=${BASH_REMATCH[1]}:
134+
if [ "${BASH_REMATCH[2]}" = "?" ] ; then
135+
OPTIONAL_OPTIONS=${OPTIONAL_OPTIONS}${OPT}
136+
fi
133137
OPTS="${OPTS}${OPT}"
134138
fi
135139
done;
136140
for MATCH in $ENDPOINT ; do
137-
if [ "${MATCH::1}" = "{" ] && [ "${MATCH:2:1}" = "}" ] ; then
138-
MATCH=${MATCH:1}
139-
OPT=${MATCH%%\}*}:
141+
if [[ $MATCH =~ \{([a-zA-Z])(\??)\} ]]; then
142+
OPT=${BASH_REMATCH[1]}:
143+
if [ "${BASH_REMATCH[2]}" = "?" ] ; then
144+
OPTIONAL_OPTIONS=${OPTIONAL_OPTIONS}${OPT}
145+
fi
140146
OPTS="${OPTS}${OPT}"
141147
fi
142148
done;
143149
IFS=$OLDIFS
150+
debug "Identified Options: ${OPTS}"
151+
debug "Optional Options: $OPTIONAL_OPTIONS"
144152
if [ -n "$OPTS" ] ; then
145153
while getopts ${OPTS} opt;
146154
do
147-
PAYLOAD=${PAYLOAD//\$\{$opt\}/$OPTARG}
148-
ENDPOINT=${ENDPOINT//\$\{$opt\}/$OPTARG}
155+
local ARG=`urlencode "$OPTARG"`
156+
debug "Applying $opt with $ARG"
157+
# PAYLOAD=${PAYLOAD//\$\{${opt}\}/$OPTARG}
158+
# ENDPOINT=${ENDPOINT//\$\{${opt}\}/$OPTARG}
159+
while [[ $PAYLOAD =~ \${$opt\??} ]] ; do
160+
PAYLOAD=${PAYLOAD//${BASH_REMATCH[0]}/$ARG}
161+
done;
162+
while [[ $ENDPOINT =~ \${$opt\??} ]] ; do
163+
ENDPOINT=${ENDPOINT//${BASH_REMATCH[0]}/$ARG}
164+
done;
149165
done
150166
shiftOptInd
151167
shift $SHIFTS
152168
fi
153-
while [[ $PAYLOAD =~ \${[^}]*} ]] ; do
154-
if [ -z "$1" ] ; then
169+
while [[ $PAYLOAD =~ \${([a-zA-Z])(\??)} ]] ; do
170+
if [ -z "$1" ] && [[ "${OPTIONAL_OPTIONS}" != *"${BASH_REMATCH[1]}"* ]] ; then
155171
error "Please provide an argument for paramater -${BASH_REMATCH:2:1}"
156172
return;
157173
fi
158174
PAYLOAD=${PAYLOAD//${BASH_REMATCH[0]}/$1}
159175
shift
160176
done
161-
while [[ $ENDPOINT =~ \${[^}]*} ]] ; do
162-
if [ -z "$1" ] ; then
177+
while [[ $ENDPOINT =~ \${([a-zA-Z])(\??)} ]] ; do
178+
if [ -z "$1" ] && [[ "${OPTIONAL_OPTIONS}" != *"${BASH_REMATCH[1]}"* ]] ; then
163179
error "Please provide an argument for paramater -${BASH_REMATCH:2:1}"
164180
return;
165181
fi
@@ -742,48 +758,18 @@ describe dbmon_delete << EOF
742758
Delete a database collector. Provide the collector id as parameter.
743759
EOF
744760
function event_create {
745-
local APPLICATION=${CONFIG_CONTROLLER_DEFAULT_APPLICATION}
746-
local NODE
747-
local TIER
748-
local SEVERITY
749-
local EVENTTYPE
750-
local BT
751-
local COMMENT
752-
while getopts "n:e:s:t:c:a:" opt "$@";
753-
do
754-
case "${opt}" in
755-
n)
756-
NODE=${OPTARG}
757-
;;
758-
t)
759-
TIER=${OPTARG}
760-
;;
761-
s)
762-
SEVERITY=${OPTARG}
763-
;;
764-
e)
765-
EVENTTYPE=${OPTARG}
766-
;;
767-
b)
768-
BT=${OPTARG}
769-
;;
770-
c)
771-
COMMENT=`urlencode "$OPTARG"`
772-
;;
773-
a)
774-
APPLICATION=${OPTARG}
775-
;;
776-
esac
777-
done;
778-
shiftOptInd
779-
shift $SHIFTS
780-
SUMMARY=`urlencode "$*"`
781-
debug -X POST "/controller/rest/applications/${APPLICATION}/events?summary=${SUMMARY}&comment=${COMMENT}&eventtype=${EVENTTYPE}&severity=${SEVERITY}&bt=${BT}&node=${NODE}&tier=${TIER}"
782-
controller_call -X POST "/controller/rest/applications/${APPLICATION}/events?summary=${SUMMARY}&comment=${COMMENT}&eventtype=${EVENTTYPE}&severity=${SEVERITY}&bt=${BT}&node=${NODE}&tier=${TIER}"
761+
apiCall -X POST "/controller/rest/applications/\${a}/events?summary=\${s}&comment=\${c?}&eventtype=\${e}&severity=\${l}&bt=&\${b?}node=\${n?}&tier=\${t?}" "$@"
783762
}
784763
register event_create Create a custom event for a given application
785764
describe event_create << EOF
786-
Create a custom event for a given application
765+
Create a custom event for a given application. Application, summary, event type and severity are required parameters.
766+
EOF
767+
function event_list {
768+
apiCall '/controller/rest/applications/${a}/events?time-range-type=${t}&duration-in-mins=${d?}&start-time=${b?}&end-time=${f?}&event-types=${e}&severities=${s}' "$@"
769+
}
770+
register event_list List all events for a given time range.
771+
describe event_list << EOF
772+
List all events for a given time range.
787773
EOF
788774
function timerange_create {
789775
local START_TIME=-1
@@ -893,7 +879,7 @@ else
893879
warning "File ${USER_CONFIG} not found!"
894880
fi
895881
# Parse global options
896-
while getopts "H:C:J:D:P:S:F:v" opt;
882+
while getopts "H:C:J:D:P:S:F:Nv" opt;
897883
do
898884
case "${opt}" in
899885
H)
@@ -924,6 +910,13 @@ do
924910
CONFIG_PORTAL_CREDENTIALS=${OPTARG}
925911
debug "Set CONFIG_PORTAL_CREDENTIALS=${CONFIG_PORTAL_CREDENTIALS}"
926912
;;
913+
N)
914+
COLOR_WARNING=""
915+
COLOR_INFO=""
916+
COLOR_ERROR=""
917+
COLOR_DEBUG=""
918+
COLOR_RESET=""
919+
;;
927920
F)
928921
CONTROLLER_INFO_XML=${OPTARG}
929922
debug "Reading CONFIG_CONTROLLER_HOST from $CONTROLLER_INFO_XML"

commands/event/create.sh

Lines changed: 2 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -1,47 +1,10 @@
11
#!/bin/bash
22

33
function event_create {
4-
local APPLICATION=${CONFIG_CONTROLLER_DEFAULT_APPLICATION}
5-
local NODE
6-
local TIER
7-
local SEVERITY
8-
local EVENTTYPE
9-
local BT
10-
local COMMENT
11-
while getopts "n:e:s:t:c:a:" opt "$@";
12-
do
13-
case "${opt}" in
14-
n)
15-
NODE=${OPTARG}
16-
;;
17-
t)
18-
TIER=${OPTARG}
19-
;;
20-
s)
21-
SEVERITY=${OPTARG}
22-
;;
23-
e)
24-
EVENTTYPE=${OPTARG}
25-
;;
26-
b)
27-
BT=${OPTARG}
28-
;;
29-
c)
30-
COMMENT=`urlencode "$OPTARG"`
31-
;;
32-
a)
33-
APPLICATION=${OPTARG}
34-
;;
35-
esac
36-
done;
37-
shiftOptInd
38-
shift $SHIFTS
39-
SUMMARY=`urlencode "$*"`
40-
debug -X POST "/controller/rest/applications/${APPLICATION}/events?summary=${SUMMARY}&comment=${COMMENT}&eventtype=${EVENTTYPE}&severity=${SEVERITY}&bt=${BT}&node=${NODE}&tier=${TIER}"
41-
controller_call -X POST "/controller/rest/applications/${APPLICATION}/events?summary=${SUMMARY}&comment=${COMMENT}&eventtype=${EVENTTYPE}&severity=${SEVERITY}&bt=${BT}&node=${NODE}&tier=${TIER}"
4+
apiCall -X POST "/controller/rest/applications/\${a}/events?summary=\${s}&comment=\${c?}&eventtype=\${e}&severity=\${l}&bt=&\${b?}node=\${n?}&tier=\${t?}" "$@"
425
}
436

447
register event_create Create a custom event for a given application
458
describe event_create << EOF
46-
Create a custom event for a given application
9+
Create a custom event for a given application. Application, summary, event type and severity are required parameters.
4710
EOF

commands/event/list.sh

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
#!/bin/bash
2+
3+
function event_list {
4+
apiCall '/controller/rest/applications/${a}/events?time-range-type=${t}&duration-in-mins=${d?}&start-time=${b?}&end-time=${f?}&event-types=${e}&severities=${s}' "$@"
5+
}
6+
7+
register event_list List all events for a given time range.
8+
describe event_list << EOF
9+
List all events for a given time range.
10+
EOF

helpers/apiCall.sh

Lines changed: 28 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
function apiCall {
44
local OPTS
5+
local OPTIONAL_OPTIONS=""
56
local METHOD="GET"
67

78
while getopts "X:d:" opt "$@";
@@ -26,43 +27,58 @@ function apiCall {
2627
OLDIFS=$IFS
2728
IFS="\$"
2829
for MATCH in $PAYLOAD ; do
29-
if [ "${MATCH::1}" = "{" ] && [ "${MATCH:2:1}" = "}" ] ; then
30-
MATCH=${MATCH:1}
31-
OPT=${MATCH%%\}*}:
30+
if [[ $MATCH =~ \{([a-zA-Z])(\??)\} ]]; then
31+
OPT=${BASH_REMATCH[1]}:
32+
if [ "${BASH_REMATCH[2]}" = "?" ] ; then
33+
OPTIONAL_OPTIONS=${OPTIONAL_OPTIONS}${OPT}
34+
fi
3235
OPTS="${OPTS}${OPT}"
3336
fi
3437
done;
3538

3639
for MATCH in $ENDPOINT ; do
37-
if [ "${MATCH::1}" = "{" ] && [ "${MATCH:2:1}" = "}" ] ; then
38-
MATCH=${MATCH:1}
39-
OPT=${MATCH%%\}*}:
40+
if [[ $MATCH =~ \{([a-zA-Z])(\??)\} ]]; then
41+
OPT=${BASH_REMATCH[1]}:
42+
if [ "${BASH_REMATCH[2]}" = "?" ] ; then
43+
OPTIONAL_OPTIONS=${OPTIONAL_OPTIONS}${OPT}
44+
fi
4045
OPTS="${OPTS}${OPT}"
4146
fi
4247
done;
4348
IFS=$OLDIFS
4449

50+
debug "Identified Options: ${OPTS}"
51+
debug "Optional Options: $OPTIONAL_OPTIONS"
52+
4553
if [ -n "$OPTS" ] ; then
4654
while getopts ${OPTS} opt;
4755
do
48-
PAYLOAD=${PAYLOAD//\$\{$opt\}/$OPTARG}
49-
ENDPOINT=${ENDPOINT//\$\{$opt\}/$OPTARG}
56+
local ARG=`urlencode "$OPTARG"`
57+
debug "Applying $opt with $ARG"
58+
# PAYLOAD=${PAYLOAD//\$\{${opt}\}/$OPTARG}
59+
# ENDPOINT=${ENDPOINT//\$\{${opt}\}/$OPTARG}
60+
while [[ $PAYLOAD =~ \${$opt\??} ]] ; do
61+
PAYLOAD=${PAYLOAD//${BASH_REMATCH[0]}/$ARG}
62+
done;
63+
while [[ $ENDPOINT =~ \${$opt\??} ]] ; do
64+
ENDPOINT=${ENDPOINT//${BASH_REMATCH[0]}/$ARG}
65+
done;
5066
done
5167
shiftOptInd
5268
shift $SHIFTS
5369
fi
5470

55-
while [[ $PAYLOAD =~ \${[^}]*} ]] ; do
56-
if [ -z "$1" ] ; then
71+
while [[ $PAYLOAD =~ \${([a-zA-Z])(\??)} ]] ; do
72+
if [ -z "$1" ] && [[ "${OPTIONAL_OPTIONS}" != *"${BASH_REMATCH[1]}"* ]] ; then
5773
error "Please provide an argument for paramater -${BASH_REMATCH:2:1}"
5874
return;
5975
fi
6076
PAYLOAD=${PAYLOAD//${BASH_REMATCH[0]}/$1}
6177
shift
6278
done
6379

64-
while [[ $ENDPOINT =~ \${[^}]*} ]] ; do
65-
if [ -z "$1" ] ; then
80+
while [[ $ENDPOINT =~ \${([a-zA-Z])(\??)} ]] ; do
81+
if [ -z "$1" ] && [[ "${OPTIONAL_OPTIONS}" != *"${BASH_REMATCH[1]}"* ]] ; then
6682
error "Please provide an argument for paramater -${BASH_REMATCH:2:1}"
6783
return;
6884
fi

helpers/output.sh

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,5 @@
11
#!/bin/bash
22

3-
COLOR_WARNING="\033[0;33m"
4-
COLOR_INFO="\033[0;32m"
5-
COLOR_ERROR="\033[0;31m"
6-
COLOR_DEBUG="\033[0;35m"
7-
COLOR_RESET="\033[0m"
8-
93
function debug {
104
if [ "${CONFIG_OUTPUT_VERBOSITY/debug}" != "$CONFIG_OUTPUT_VERBOSITY" ]; then
115
echo -e "${COLOR_DEBUG}DEBUG: $*${COLOR_RESET}"

main.sh

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,13 @@ CONFIG_PORTAL_COOKIE_LOCATION="/tmp/appdynamics-portal-cookie.txt"
1616
# An empty string silents all output
1717
CONFIG_OUTPUT_VERBOSITY="error,output"
1818

19+
# Default Colors
20+
COLOR_WARNING="\033[0;33m"
21+
COLOR_INFO="\033[0;32m"
22+
COLOR_ERROR="\033[0;31m"
23+
COLOR_DEBUG="\033[0;35m"
24+
COLOR_RESET="\033[0m"
25+
1926
GLOBAL_COMMANDS=""
2027
GLOBAL_HELP=""
2128
GLOBAL_LONG_HELP_COUNTER=0
@@ -78,6 +85,7 @@ source ./commands/dbmon/list.sh
7885
source ./commands/dbmon/delete.sh
7986

8087
source ./commands/event/create.sh
88+
source ./commands/event/list.sh
8189

8290
source ./commands/timerange/create.sh
8391
source ./commands/timerange/list.sh
@@ -104,7 +112,7 @@ else
104112
fi
105113

106114
# Parse global options
107-
while getopts "H:C:J:D:P:S:F:v" opt;
115+
while getopts "H:C:J:D:P:S:F:Nv" opt;
108116
do
109117
case "${opt}" in
110118
H)
@@ -135,6 +143,13 @@ do
135143
CONFIG_PORTAL_CREDENTIALS=${OPTARG}
136144
debug "Set CONFIG_PORTAL_CREDENTIALS=${CONFIG_PORTAL_CREDENTIALS}"
137145
;;
146+
N)
147+
COLOR_WARNING=""
148+
COLOR_INFO=""
149+
COLOR_ERROR=""
150+
COLOR_DEBUG=""
151+
COLOR_RESET=""
152+
;;
138153
F)
139154
CONTROLLER_INFO_XML=${OPTARG}
140155
debug "Reading CONFIG_CONTROLLER_HOST from $CONTROLLER_INFO_XML"

0 commit comments

Comments
 (0)