-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdlux_ops
More file actions
executable file
·278 lines (253 loc) · 8.58 KB
/
dlux_ops
File metadata and controls
executable file
·278 lines (253 loc) · 8.58 KB
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
#!/bin/bash
# @name: dlux_ops
# @revision V.1.0
# @date: 4/12/18
# @author: luzC
# @brief:
# Compilation of tools to verify execution of few tests on windows
# and linux. For help type ./ops.sh --help
# Uncomment line to debug
#set -o xtrace
# ================== Global Operations and Variables =========================
_SCRIPTNAME=$(basename "$0")
_SYSTEMS_FILE=''
_USER=''
_PASSWORD=''
_CMD=''
_OP=''
_BIOSVER=''
OPERATIONS=('bios' 'ping' 'ssh' 'vmm' 'vtx')
function PrintError {
echo "************************" >&2
echo "* $(date +"%F %T.%N") ERROR: $1" >&2
echo "************************" >&2
exit 1
}
function PrintHelp {
echo "Usage:"
echo " ./$_SCRIPTNAME [-f | -l] -o [ ${OPERATIONS[@]} ]"
echo " --help | -h Prints current help text."
echo " --systemsFile | -f File containing one target system name per line."
echo " --user | -u User name for authentication. Default - administrator."
echo " --password | -p Password for authentication. Default - secure123."
echo " --operation | -o The operation to run: [bios ccpy ping ssh vmm vtx]"
echo " bios <version> - Check installed bios version on the systems."
echo " ping - Pings every system."
echo " ssh - Verifies ability to ssh into systems."
echo " vmm - Check that VMs on top of systems are running."
echo " vtx - Check systems have BIOS virtualization option enabled."
echo " "
exit 0
}
# ================== Processes Input Parameters ==============================
while [[ ${1} ]]; do
case "${1}" in
--help|-h)
PrintHelp
;;
--systemsFile|-f)
[[ -z "${2}" || "${2}" == -* ]] && PrintError "Missing file name."
[[ ! -f "${2}" ]] && PrintError "File does not exist."
_SYSTEMS_FILE="${2}"
shift
;;
--user|-u)
[[ -z "${2}" || "${2}" == -* ]] && PrintError "Missing user name."
_USER="${2}"
shift
;;
--password|-p)
[[ -z "${2}" || "${2}" == -* ]] && PrintError "Missing password."
_PASSWORD="${2}"
shift
;;
--operation|-o)
[[ -z "${2}" || "${2}" == -* ]] && PrintError "Missing operation."
for e in ${OPERATIONS[@]}
do
[[ "${2}" == $e ]] && _OP=$e && break;
done
[[ -z "$_OP" ]] && PrintError "Invalid Operation - ${2}"
if [[ "$_OP" == "bios" ]]; then
[[ -z "${3}" ]] && PrintError "Missing BIOS version" || _BIOSVER="${3}"
shift
fi
shift
;;
*)
echo "Invalid Argument."
PrintHelp
shift
esac
shift
done
[[ -z $_SYSTEMS_FILE ]] && PrintError "Missing SUT - file of system names"
[[ -z $_OP ]] && PrintError "Must choose an operation to run."
[[ -z $_USER ]] && _USER='administrator' && echo 'Warning: Using default user "administrator"'
[[ -z $_PASSWORD ]] && _PASSWORD='secure123' && echo 'Warning: Using default password "secure123"'
# ================== Functions for each operation ============================
function check_bios {
: 'Check that system Bios version contains a given version pattern - via OS.
Positional arguments:
1: SystemName or IP to ssh into it.
2: User to authenticate.
3: Password to authenticate.
Note: If user/password fail, will try default ssh-keys.
'
sy="${1}"; user="${2}"; passw="${3}"
check_ssh $sy $user $passw 'False'
[[ $_CMD == 'error' ]] && continue
# Check BIOS
$_CMD -q $user@$sy "uname" &>result
if [[ $( cat result ) == 'Linux' ]]; then
# if Linux
cv=$( $_CMD $user@$sy "dmidecode --type 0|grep Version|sed 's/.*Version:.//g'" )
else
# if Windows
cv=$( $_CMD $user@$sy "powershell; systeminfo | FINDSTR ^BIOS.Version" )
fi
rm -f result
msg=" - $sy BIOS -"
msg1="- Expected Version: $_BIOSVER, Current Version: $cv"
[[ $cv =~ $_BIOSVER ]] && echo "$msg MATCHES" || echo "$msg NOT MATCH $msg1"
}
function check_ping {
: 'Pings a system - Prints only one line to indicate either success or failure.
Positional arguments:
1: SystemName or IP to ping.
Optional arguments:
2: verbose
Defaults to True.
If True - Prints success or failure message.
If False - Prints only failure message (if any), supress success message
If Raw - Prints the ping output as-is
'
sy="${1}"; verbose="${2:-True}"
result=$( ping -c 1 $sy )
[[ $verbose == Raw ]] && echo $result && continue
_reg='Destination Host Unreachable'
msgS=" - $sy - Successful Ping"
msgF=" - $sy - Failed Ping - Host Unreachable \n $result"
[[ $result =~ $_reg ]] && echo -e "$msgF" || [[ $verbose == True ]] && echo $msgS
}
function check_ssh {
: 'Verify system connection via ssh - etiher with user/ password or ssh-keys
Positional arguments:
1: SystemName or IP to ssh into.
2: User to authenticate.
3: Password to authenticate.
Optional arguments:
4: verbose
Defaults to True.
If True - Prints success or failure message.
If False - Prints only failure message (if any), supress success message
Note: If user/password fail, will try default ssh-keys.
'
sy="${1}"; user="${2}"; passw="${3}"; verbose="${4:-True}"
# Check we can ssh - with user-password else try sshkeys
_CMD="sshpass -p $passw ssh"
sshpass -p $passw ssh $user@$sy "exit"
if [[ $? -ne 0 ]]; then
_CMD='ssh'
ssh -o preferredAuthentications=publickey -q $user@$sy "exit"
[[ $? -ne 0 ]] && echo -e "ERROR: UNABLE TO SSH $sy.\n" && _CMD='error'
fi
[[ $_CMD != 'error' && $verbose == 'True' ]] && echo " - $sy - Able to ssh"
}
function check_vmm {
: 'Prints current available VMs details to validate its status.
For linux - Done via virsh
Positional arguments:
1: SystemName or IP to ssh into.
2: User to authenticate.
3: Password to authenticate.
Note: If user/password fail, will try default ssh-keys.
'
sy="${1}"; user="${2}"; passw="${3}"
check_ssh $sy $user $passw 'False'
[[ $_CMD == 'error' ]] && continue
# Check vmm
echo -e "\n\n - $sy - Reviewing VMs on system."
$_CMD -q $user@$sy "uname" &>result
if [[ $( cat result ) == 'Linux' ]]; then
# IF LINUX SYSTEM
# Print current VMs on the host
vmms=$( $_CMD $user@$sy "virsh list --all" )
echo "$vmms"
[[ $vmms =~ "off" ]] && echo " - $sy - Error: something went wrong."
else
# IF WINDOWS SYSTEM
# Print current VMs on the host
$_CMD $user@$sy "powershell; get-vm"
$_CMD $user@$sy 'powershell; if(Select-String -Path c:/vms/vm_status.log -Pattern "heartbeat.failure") { exit 1 }; exit'
# On failure get vm_status.log file
if [[ $? -ne 0 ]]; then
fname="./${sy}.vm_status_$(date '+%Y_%m_%d_%H:%M:%S').log"
echo " - $sy - Error: vm_status.log - HAVE HEARBEAT FAILURES"
echo "Collecting file into $fname"
sshpass -p $passw scp $user@$sy:C:/vms/vm_status.log ${fname}
fi
fi
rm -f result
}
function check_vtx {
: 'Verifies system has Intel Virtual Technology enabled (aka vtx)
TODO: Check also for vtd (direct I/O) via OS
Positional arguments:
1: SystemName or IP to ping.
2: User to authenticate.
3: Password to authenticate.
Note: If user/password fail, will try default ssh-keys.
'
sy="${1}"; user="${2}"; passw="${3}"
check_ssh $sy $user $passw 'False'
[[ $_CMD == 'error' ]] && continue
# Check Virtualization
$_CMD -q $user@$sy "uname" &>result
if [[ $( cat result ) == 'Linux' ]]; then
# if Linux
$_CMD $user@$sy "[[ -c '/dev/kvm' ]] && exit || exit 1"
else
# if Windows - Ensure hyperV reqs are met
$_CMD $user@$sy 'powershell; "if(systeminfo | FINDSTR ^Hyper-V* | FINDSTR .*No) { exit 1 }; exit"'
fi
rm -f result
msg=" - $sy - VTX"
[[ $? -eq 0 ]] && echo "$msg ENABLED" || echo "$msg NOT ENABLED"
}
# ================== Processes selected option ===============================
echo '<--- Getting system list'
if [[ -n $_POOL_NAME ]]; then
python -m cluster.sysman -P ${_POOL_NAME} --status | grep 'machine names:' | sed 's/.*:.//g' | sed "s/[\[,']//g" | sed 's/\]//g' | tr " " "\n" > "${_POOL_NAME}"
# Sort the list
sort <<< "$(cat ${_POOL_NAME})" > ${_POOL_NAME}
_SYSTEMS_FILE="${_POOL_NAME}"
fi
systems=($(cat $_SYSTEMS_FILE | tr "\n" " "))
[[ ${#systems[@]} -eq 0 ]] && PrintError 'No systems available.'
# Run operation on every system of the pool
echo -e "\n<--- Checking $_OP"
for sy in ${systems[@]}
do
case "$_OP" in
bios)
check_bios $sy $_USER $_PASSWORD
;;
ping)
check_ping $sy
;;
ssh)
check_ssh $sy $_USER $_PASSWORD
;;
vmm)
check_vmm $sy $_USER $_PASSWORD
;;
vtx)
check_vtx $sy $_USER $_PASSWORD
;;
*)
PrintError "UnKnown Issue or Option not Implemented."
esac
shift
done
echo -e "\nCompleted Analysis. If errors, it should have been displayed above."