-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmanage-monitoring
360 lines (313 loc) · 8.98 KB
/
manage-monitoring
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
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
#!/bin/bash
# _________________________________________________
# ////////// AIXtreme Research Limited \\\\\\\\\\\\
# \_________________________________________________/
#
############################################################
# Project: Redhat-upgrade-project
# Customer:
# Created: 10/2018
# Requires: curl / jq
# Tested: cygwin on win10 & linux.
#
# Authors: Steve Maher
#
# Ref: https://github.com/solarwinds/orionsdk-python
# Ref: https://thwack.solarwinds.com/thread/115778
# Ref: https://gist.github.com/cdown/1163649/8a35c36fdd24b373788a7057ed483a5bcd8cd43e
# Ref: https://jqplay.org/
# Ref: http://solarwinds.github.io/OrionSDK/schema/
# Ref:
#
############################################################
# .netrc !? - maybe not....
############################################################
function _create-netrc
{
cat <<-EOF >${NETRC}
machine ${SWHOST}
login ${USERNAME}
password ${1}
EOF
}
# url encode
############################################################
function _encode
{
typeset _input=$*
typeset _length="${#_input}"
typeset -i _offset
for (( _offset = 0 ; _offset < _length ; _offset++ ))
do
_print_offset="${_input:_offset:1}"
case "${_print_offset}" in
[a-zA-Z0-9.~_-])
printf "${_print_offset}"
;;
' ') printf "+"
;;
*) printf '%%%X' "'${_print_offset}"
;;
esac
done
}
#
############################################################
function _query
{
eval ${CURL_GET} ${CURL_AUTH} "${JSON_NODE}/Query?query=$( _encode $* )"
}
# Create a jq CSV, depends on well-formed jq input !
############################################################
function _jq-csv
{
jq -r "$1 | @csv"
}
# Status mapping
############################################################
function _swStatusText
{
# @@SGM we should really populate this from the table - it could change...
############################################################
swStatus=( 'Unknown' 'Up' 'Down' 'Warning' 'Shutdown' 'Testing' 'Dormant' 'Not Present' 'Lower Layer Down' 'Unmanaged' 'Unplugged' 'External' 'Unreachable' 'Undefined' 'Critical' 'Partly Available' 'Misconfigured' 'Could Not Poll' 'Undefined' 'Unconfirmed' 'Undefined' 'Undefined' 'Active' 'Undefined' 'Inactive' 'Expired' 'Monitoring Disabled' 'Disabled' 'Not Licensed' 'Other' 'Not Running' )
if [[ $1 == "-" ]]
then
for i in ${!swStatus[*]}
do
if [[ ${swStatus[${i}]^^} == ${2^^} ]]
then
printf "%d" "${i}"
break
fi
done
return
else
[[ $1 =~ ^[0-9]+ ]] && printf "%s" "${swStatus[$1]}"
fi
}
# Get the NodeID from a hostname (SQL wildcarded - will return multiple IDs)
############################################################
function _NodeID
{
_query "Select Uri From Orion.Nodes Where NodeName Like '$1'" \
| jq -r '.results[].Uri' \
| awk -F"=" '{ printf "%s "$2 }'
}
# List all the "nodes" in solarwinds and their status
# or - select a given status..
############################################################
function _listAll
{
if [[ -n $1 ]]
then
if [[ $1 =~ ^[0-9] ]]
then
whereClause=" Where NodeID='$1'"
else
whereClause=" Where Status='$( _swStatusText - $1 )'"
fi
fi
if [[ -n ${customProperties} ]]
then
_query "SELECT NodeID,Caption,MachineType,LastBoot,Status FROM Orion.Nodes${whereClause}" \
| _jq-csv ".results[] | [ .NodeID, .Caption, .MachineType, .LastBoot, .Status ]" \
| while IFS=, read -r NodeID Caption MachineType LastBoot Status
do
Status=$( _swStatusText ${Status} )
printf '%-30s %30s %30s %10s\n' "${Caption}" "${MachineType}" "${LastBoot}" "${Status}"
_query "SELECT * FROM Orion.Nodes LEFT JOIN Orion.NodesCustomProperties ON Orion.NodesCustomProperties.NodeID=Orion.Nodes.NodeID WHERE Orion.Nodes.NodeID='${NodeID}' " \
#| _jq-csv ".results[] | [ .DisplayName, .Description ]"
# @@SGM - need to do a join of the custom fields
exit
done
else
_query "SELECT Caption,MachineType,LastBoot,Status FROM Orion.Nodes${whereClause}" \
| _jq-csv ".results[] | [ .Caption, .MachineType, .LastBoot, .Status ]" \
| while IFS=, read -r Caption MachineType LastBoot Status
do
Status=$( _swStatusText ${Status} )
printf '%-30s %30s %30s %10s\n' "${Caption}" "${MachineType}" "${LastBoot}" "${Status}"
done
fi
}
#
############################################################
function f_usage
{
echo "usage: ${ProgName} [ [-S [state] | -l | -L | [-t hours] [-s|-m|-u]] [node] [node] ...."
echo " -S known state, ie Up, Down, Disabled, Unmanaged]"
echo " -l List all known nodes and states"
echo " -L List all Unmanaged nodes"
echo ""
echo " -s [nodes..] Status of nodes"
echo " -r [nodes..] Remanage nodes"
echo " -u [nodes..] Unmanage nodes"
echo " -t [hours] number of hours before monitoring is remanaged"
echo " -T [%m-%d-%Y %r] exact time when monitoring will remanage"
echo ""
echo " Note: nodenames are SQL like statements so % for wildcards"
echo ""
echo " examples:"
echo " ${ProgName} -S Down"
echo " ${ProgName} -u uhmt% -t 12"
echo " ${ProgName} -r uhmt%"
echo " ${ProgName} -L"
echo ""
}
set -o noglob
typeset ProgName=$( basename $0 )
typeset DirName=$( dirname $0 ); [[ $DirName == "." ]] && DirName=$PWD
typeset TTY=$( tty -s && tty )
typeset -i ERC
typeset -i TERC
typeset IS_CYGWIN
typeset NETRC=~/.${ProgName}-netrc
typeset CURL="curl -s -k"
typeset CURL_CONTENT="-H 'Content-Type: application/json'"
typeset CURL_GET="${CURL} ${CURL_CONTENT} -X GET"
typeset CURL_POST="${CURL} ${CURL_CONTENT} -X POST"
typeset SWHOST=${SWHOST:-"orion"}
typeset JSON_NODE="https://${SWHOST}:17778/SolarWinds/InformationService/v3/Json"
#typeset JSON_NODE="http://${SWHOST}/SolarWinds/InformationService/v3/Json"
typeset TMPDIR=${TMPDIR:-"/tmp"}
typeset JSON_POST="${TMPDIR}/${ProgName}.${USER}.dat"
typeset PASSWORD="${SWIN_PWD}"
#if ! eval date >/dev/null 2>&1
#then
# alias date='/bin/busybox.exe date'
#fi
# Is this a cygwin server
############################################################
if uname -s | grep -q "CYGWIN"
then
IS_CYGWIN=true
JSON_POST="${TMPDIR}/solar-winds-node-data.dat"
fi
if [[ -s ${NETRC} ]]
then
CURL_AUTH="-n --netrc-file ${NETRC}"
elif [[ -z ${PASSWORD} ]]
then
if [[ -z "${TTY}${PASSSWORD}" ]]
then
echo "[E] Password is missing and no TTY for input."
exit 1
fi
echo "[I] Password required for ${USERNAME} @ ${JSON_NODE}"
echo -e "[P] Enter Password :\c"
stty -echo
read PASSWORD
stty echo
echo ""
#_create-netrc ${PASSWORD}
CURL_AUTH="-u ${USERNAME}:${PASSWORD}"
else
CURL_AUTH="-u ${USERNAME}:${PASSWORD}"
fi
while getopts :ht:T:sCulLrS: OPT
do
case $OPT in
s) MONITOR_MODE="status"
;;
u) MONITOR_MODE="Unmanage"
dateStart=$( TZ=+1 date +%m"-"%d"-"%Y" "%r )
dateEnd=$( TZ=-4 date +%m"-"%d"-"%Y" "%r )
;;
r) MONITOR_MODE="Remanage"
dateStart=$( TZ=+1 date +%m"-"%d"-"%Y" "%r )
dateEnd=$( TZ=-4 date +%m"-"%d"-21"%g" "%r )
;;
T) dateEnd=$( date -d "${OPTARG}" +%m"-"%d"-"%Y" "%r )
if [[ -z $dateEnd ]]
then
echo "[E] Invalid input date."
exit 1
else
echo "[I] End Date ${dateEnd}"
fi
;;
t) if [[ ${OPTARG} =~ !/[0-9]*/ ]]
then
echo "usage error: -t flag requires a number of whole hours"
exit 1
fi
dateEnd=$( TZ=-${OPTARG} date +%m"-"%d"-"%Y" "%r )
;;
l) _listAll
exit $?
;;
L) _listAll unmanaged
exit $?
;;
S) _listAll ${OPTARG}
exit $?
;;
C) customProperties=true
;;
[Hh]*) f_usage
exit
;;
*) echo "usage error: unknown flag" >&2
exit 1
;;
esac
done
if [[ -z ${MONITOR_MODE} ]]
then
echo "usage error: you must specify -l (list) -u (unmanage) or -r (remanage)" >&2
f_usage
exit 1
fi
shift $(( $OPTIND-1 ))
if [[ -z $1 ]]
then
echo "usage error: you must pass node names."
exit 1
fi
for nodeName in $*
do
echo "[I] Searching for NodeID for ${nodeName}"
NodeIDs=$( _NodeID ${nodeName} )
if [[ -z "${NodeIDs}" ]]
then
echo "[E] Could not find NodeID for ${nodeName}."
let TERC+=1
continue
fi
for NodeID in ${NodeIDs}
do
if [[ ${MONITOR_MODE} == "status" ]]
then
_listAll ${NodeID}
else
echo "[I] $1 NodeID=${NodeID} ${MONITOR_MODE}"
# example output:
############################################################
# ["N:${NODEID}","10-23-2018 04:10:00 PM","10-23-2019 06:00:00 PM","false"]
############################################################
cat <<-EOF >${JSON_POST}
["N:${NodeID}","${dateStart}","${dateEnd}","false"]
EOF
eval ${CURL_POST} ${CURL_AUTH} -d@"${JSON_POST}" "${JSON_NODE}/Invoke/Orion.Nodes/${MONITOR_MODE}" \
| grep -qw null
ERC=$?
if (( ERC ))
then
echo "[E] Error in ${MONITOR_MODE} node."
let TERC+=ERC
else
if [[ $MONITOR_MODE == "Unmanage" ]]
then
echo "[I] Monitoring will Remanage at ${dateEnd}."
else
echo "[I] Done."
fi
fi
fi
done
done
[[ -e ${JSON_POST} ]] && \
rm -f ${JSON_POST}
# The end..
############################################################
exit ${TERC}