-
Notifications
You must be signed in to change notification settings - Fork 0
/
nodes.subr
127 lines (100 loc) · 2.76 KB
/
nodes.subr
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
if [ ! "$_CBSD_NODES_SUBR" ]; then
_CBSD_NODES_SUBR=1
###
#update idle in SQLite for $1 nodename
idle_update()
{
cbsdsql nodes "UPDATE nodelist SET idle=datetime('now','localtime') WHERE nodename=\"${1}\""
}
# check age of ssh mux lock. If ctime greater than 1 minutes
# then out "Connected (uptime min)" and return 0
# else - out "Offline" and return 1
# required included inventory for $sqlreplica
# sample:
# if check_locktime /usr/jails/ftmp/shmux_199.48.133.74.lock >/dev/null 2>&1; then
# echo "OK"
# fi
check_locktime()
{
#always return online if no shmux activated
[ "${sqlreplica}" = "0" ] && return 0
local cur_time=$( /bin/date +%s )
local difftime
[ ! -f "${1}" ] && return 1
eval $( /usr/bin/stat -s ${1} )
difftime=$(( ( cur_time - st_mtime ) / 60 ))
if [ $difftime -lt 1 ]; then
return 1
else
echo "$difftime"
return 0
fi
}
# idle must be set (or specified via $1 )
# $ip (of nodes) must be set
conv_idle()
{
local _res
status="Disconnected" # default state
[ -n "${1}" ] && idle="${1}"
[ -z "${idle}" ] && return 1
if [ ${sqlreplica} -eq 1 ]; then
_res=$( check_locktime ${ftmpdir}/shmux_${ip}.lock )
[ $? -eq 0 ] && status="Connected" && idle=${_res} && return 0
fi
idle_time=$( /bin/date -j -f "%Y-%m-%d %H:%M:%S" "${idle}" "+%s" )
_res=$(( ( curtime - idle_time ) / 60 ))
idle="${_res}"
}
# return $node as selected node by id or name from list
# -s "is subject for list"
# -a [0|1] show only active (status=on jail): 0, or all: 1
select_node_by_list()
{
local _res _id i=0
local subject="List of online nodes:"
local active="0" # only online
local sqldelimer=" "
while getopts "a:r:s:e:" opt; do
case "$opt" in
a) active="${OPTARG}" ;;
s) subject="${OPTARG}" ;;
esac
shift $(($OPTIND - 1))
done
eval $( cbsdsql nodes SELECT nodename,ip FROM nodelist |while read nodename ip status; do
mystatus=
if ! check_locktime ${ftmpdir}/shmux_${ip}.lock >/dev/null 2>&1; then
mystatus="1" # Offline
else
mystatus="0" # Online
fi
[ "${mystatus}" != "${active}" ] && continue
echo "local nodename${i}=\"${nodename}\"
echo "local ip${i}=\"${ip}\"
i=$(( i + 1 ))
done )
for i in $( /usr/bin/seq 0 255 ); do
eval _mynode="\$nodename${i}"
[ -z "${_mynode}" ] && break
[ "${_mynode}" = "${nodename}" ] && _mynode="${BOLD}local${NORMAL}"
eval _ip="\$ip${i}"
[ -z "${_ip}" ] && break;
[ $i -eq 0 ] && ${ECHO} "${MAGENTA}${subject}${NORMAL}"
${ECHO} "${BOLD}${i} .. ${GREEN}${_mynode} ${NORMAL}on ${GREEN}${_ip}${NORMAL}"
done
[ $i -eq 0 ] && err 1 "${MAGENTA}No node found${NORMAL}"
echo
printf "${BOLD}Enter id 0-${i} or node name:${NORMAL} "
set -e
read _id
set +e
if ! is_number ${_id}; then
eval node="\$nodename${_id}"
else
eval node="${_id}"
fi
remote=2 # for autoremote
}
###
fi