This repository has been archived by the owner on May 21, 2021. It is now read-only.
/
make_ovip_sweeper.sh
187 lines (158 loc) · 4.69 KB
/
make_ovip_sweeper.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
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
#!/bin/sh
# make_ovip_sweeper.sh
# Copyright (c) 2011-2012, Kenichi Kamiya
if ( type 'ovtopodump' >/dev/null ); then
:
elif ( type '/opt/OV/bin/ovtopodump' >/dev/null ); then
alias ovtopodump='/opt/OV/bin/ovtopodump'
else
echo '# !!! Not found ovtopodump command !!!' >&2
exit 1
fi
initialize_environment()
{
set -u
LANG=C; export LANG
}
error()
{
echo "Error: $@"
exit 1
}>&2
filter_ipv4addr()
{
grep -E '^([0-9]{1,3}\.){3}[0-9]{1,3}$'
}
filter_node_id()
{
perl -nle 'if ( $_ =~ /^\s*(\d+)/ ){print $1;}'
}
filter_interface_id()
{
perl -nle 'if ( $_ =~ /^\s*\d+\/(\d+)/ ){print $1;}'
}
filter_main_ipaddr()
{
perl -nle 'if ( $_ =~ /^\s*\d+\s+IP\s+[\w\d]\S+\s+\S+\s+(\S+)/ ){print $1;}'
}
check_ipv4addr_pattern()
{
[ $( echo "$1" | grep -E '^([0-9]{1,3}\.){3}[0-9]{1,3}$' | wc -l ) -eq 1 ]
}
check_ov_oid_pattern()
{
[ $( echo "$1" | grep -E '^[1-9][0-9]*$' | wc -l ) -eq 1 ]
}
get_otd()
{
local -r result=$( ovtopodump -rRISC "$1" 2>/dev/null | tail -n +2 )
local -r error=$( ovtopodump -rRISC "$1" 2>&1 1>/dev/null )
if ( echo "$error" | grep -q -F "ERROR: Could not find object $1" ); then
return 1
fi
if ( echo "$result" | grep -q -E '^ *[0-9]+' ); then
echo "$result"
else
error 'ovtopodump error'
fi
}
get_interface_id()
{
local -r otd="$1"
local -r ipaddr="$2"
local -r result=$( echo "$otd" | awk "\$5==\"$ipaddr\"{print \$0}" | filter_interface_id )
if [ "$result" = '' ]; then
return 1
else
echo "$result"
fi
}
get_node_id()
{
local -r otd="$1"
local -r result=$( echo "$otd" | filter_node_id | uniq )
if check_ov_oid_pattern "$result"; then
echo "$result"
else
error 'Unknown NodeID-pattern.' "\nSource:\n$otd\nResult:\n$result"
fi
}
get_main_ipaddr()
{
local -r otd="$1"
local -r result=$( echo "$otd" | filter_main_ipaddr )
if check_ipv4addr_pattern "$result"; then
echo "$result"
else
error 'Unknown IPAddress-pattern.' "\nSource:\n$otd\nResult:\n$result"
fi
}
make_ovip_sweeper_command() (
readonly ipaddr="$1"
echo '# -----------------------------------------------------------------------------'
echo "# $ipaddr"
echo '# -----------------------------------------------------------------------------'
if ( get_otd "$ipaddr" 1>/dev/null 2>&1 ); then
readonly otd_ip=$( get_otd "$ipaddr" )
else
echo '# Not found in this server'
return 1
fi
if ( get_interface_id "$otd_ip" "$ipaddr" 1>/dev/null 2>&1 ); then
readonly interface_id=$( get_interface_id "$otd_ip" "$ipaddr" )
if check_ov_oid_pattern "$interface_id"; then
:
else
error 'Unknown InterfaceID-pattern.' "\nSource:\n$otd_ip\nResult:\n$interface_id"
fi
else
echo '# Matched under ovtopodump, but without ipaddress-field'
echo '# Not found in this server'
return 1
fi
readonly node_id=$( get_node_id "$otd_ip" )
readonly otd_node=$( get_otd "$node_id" )
readonly relative_interface_ids=$( echo "$otd_node" | filter_interface_id )
readonly main_ipaddr=$( get_main_ipaddr "$otd_node" )
readonly command="/opt/OV/bin/ovtopofix -r $interface_id"
if [ "$interface_id" ]; then
if [ "$interface_id" = "$relative_interface_ids" ] || \
[ "$ipaddr" != "$main_ipaddr" ]; then
echo "$command"
else
echo '# This object has relationships with other interfaces.'
echo "# Check object '$node_id' and comment-in below line."
echo "#$command"
fi
echo "###############################################################################"
echo "# Result of $node_id(NodeID) "
echo "$otd_node" | perl -nle 'print "#$_"'
echo
else
echo '# Not found in this server'
fi
)
main() (
echo '#!/bin/sh'
echo '# -----------------------------------------------------------------------------'
echo '# Setup'
echo '# -----------------------------------------------------------------------------'
echo 'LANG=C; export LANG'
echo
echo '# -----------------------------------------------------------------------------'
echo '# Record the ovtopodump stdout, before running delete commands'
echo '# -----------------------------------------------------------------------------'
echo '/opt/OV/bin/ovtopodump -rRISC > "./$( date +%Y-%m-%d-%H:%M:%S ).$$.ovtopodump.bak.before"'
echo
filter_ipv4addr | while read ipaddr; do
make_ovip_sweeper_command "$ipaddr"
done
echo
echo '# -----------------------------------------------------------------------------'
echo '# Record the ovtopodump stdout, after running delete commands'
echo '# -----------------------------------------------------------------------------'
echo '/opt/OV/bin/ovtopodump -rRISC > "./$( date +%Y-%m-%d-%H:%M:%S ).$$.ovtopodump.bak.after"'
echo
)
initialize_environment
main