-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathq_datanode.sh
executable file
·73 lines (64 loc) · 1.63 KB
/
q_datanode.sh
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
#!/bin/bash
usage(){
echo "Usage: $0 hostname port"
echo "Note: hostname can not be ip"
exit 1
}
function valid_ip()
{
local ip=$1
local stat=1
if [[ $ip =~ ^[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}$ ]]; then
OIFS=$IFS
IFS='.'
ip=($ip)
IFS=$OIFS
[[ ${ip[0]} -le 255 && ${ip[1]} -le 255 \
&& ${ip[2]} -le 255 && ${ip[3]} -le 255 ]]
stat=$?
fi
return $stat
}
# invoke usage
# call usage() function if filename not supplied
[[ $# -ne 2 ]] && usage
host=$1
port=$2
username="hadoop"
CONN_TIMEOUT=15
localhost=`hostname`
# check hostname
if valid_ip $1; then
usage
fi
# return value : 0 undetermined 1 killed 3 alive
# 1. try to kill the process
ssh -o ConnectTimeout=$CONN_TIMEOUT $username@$host "fuser -v -k -n tcp $port"
ret=$?
echo "$localhost ssh return value $ret"
if [ $ret -eq 0 ]; then
echo "$localhost: Kill the active namenode successfully!"
exit 1
elif [ $ret -eq 2 ]; then
echo "$localhost: Can not ssh when kill!"
exit 0
elif [ $ret -eq 255 ]; then
echo "$localhost: $host ssh service is down!"
exit 0
else
# 2. check the port works
ssh -o ConnectTimeout=$CONN_TIMEOUT $username@$host "nc -z $host $port"
if [ $? -eq 0 ]; then
echo "$localhost: Unable to fence - it is running but we cannot kill it!"
exit 3
elif [ $? -eq 2 ]; then
echo "$localhost: Can not ssh when nc!"
exit 0
elif [ $? -eq 255 ]; then
echo "$localhost: $host ssh service is down!"
exit 0
else
echo "$localhost: Active namenode process have been killed!"
exit 1
fi
fi