This repository has been archived by the owner on Mar 10, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrelay_poe_control.sh
executable file
·289 lines (271 loc) · 7.8 KB
/
relay_poe_control.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
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
#!/bin/bash
folder=$( cd -- "$( dirname -- "${BASH_SOURCE[0]}" )" &> /dev/null && pwd )
pathStats=$(grep 'pathStats' $folder/config.ini | awk '{ print $3 }')
source $pathStats/config.ini
query(){
if [ -z "$SQL_password" ]
then
mysql -h$DB_IP -P$DB_PORT -u$SQL_user $1 -sN -e "$2;"
else
mysql -h$DB_IP -P$DB_PORT -u$SQL_user -p$SQL_password $1 -sN -e "$2;"
fi
}
stoppoe(){
if [ $useSSH == true ]
then
# echo 'ssh -p $ssh_port $ssh_user@$ssh_ip "snmpset -v $version -c $community $ip:$port 1.3.6.1.2.1.105.1.1.1.3.1.$action i 2"'
ssh -p $ssh_port $ssh_user@$ssh_ip "snmpset -v $version -c $community $ip:$port 1.3.6.1.2.1.105.1.1.1.3.1.$action i 2"
else
# echo "snmpset -v $version -c $community $ip:$port 1.3.6.1.2.1.105.1.1.1.3.1.$action i 2"
snmpset -v $version -c $community $ip:$port 1.3.6.1.2.1.105.1.1.1.3.1.$action i 2
fi
timing=$(date '+%Y%m%d %H:%M:%S')
echo "[$timing] Stop port $action on $device" >> $folder/log.txt
}
startpoe(){
if [ $useSSH == true ]
then
# echo 'ssh -p $ssh_port $ssh_user@$ssh_ip "snmpset -v $version -c $community $ip:$port 1.3.6.1.2.1.105.1.1.1.3.1.$action i 1"'
ssh -p $ssh_port $ssh_user@$ssh_ip "snmpset -v $version -c $community $ip:$port 1.3.6.1.2.1.105.1.1.1.3.1.$action i 1"
else
# echo "snmpset -v $version -c $community $ip:$port 1.3.6.1.2.1.105.1.1.1.3.1.$action i 1"
snmpset -v $version -c $community $ip:$port 1.3.6.1.2.1.105.1.1.1.3.1.$action i 1
fi
timing=$(date '+%Y%m%d %H:%M:%S')
echo "[$timing] Start port $action on $device" >> $folder/log.txt
}
stophilink(){
adjusted_action=$((action-1))
if [ $relaytype == no ]
then
setrelay=off
else
setrelay=on
fi
cd $folder && ./relaytoggle.sh $adjusted_action $setrelay $ip $port
timing=$(date '+%Y%m%d %H:%M:%S')
echo "[$timing] Stop port $action on $device" >> $folder/log.txt
}
starthilink(){
adjusted_action=$((action-1))
if [ $relaytype == no ]
then
setrelay=on
else
setrelay=off
fi
cd $folder && ./relaytoggle.sh $adjusted_action $setrelay $ip $port
timing=$(date '+%Y%m%d %H:%M:%S')
echo "[$timing] Start port $action on $device" >> $folder/log.txt
}
if [[ -z ${1+x} || -z ${2+x} || -z ${3+x} ]]
then
# ask the questions
devices=$(cat $folder/config.ini | grep '\[' | awk '{print $1}' | paste -s -d, - | tr '[' ' ' | tr ']' ' ')
echo "Enter name of switch/relay to be used ($devices):"
read device
echo ""
echo "Action: status, stop, start or (power)cycle"
read activity
echo ""
if [[ $activity != status ]]
then
relays=$(grep -A4 '\[' $folder/config.ini | grep -A4 $device | tail -1 | awk '{ print $3 }')
echo "Enter port number (from 1 to $relays) or all"
read action
echo ""
fi
else
device=$1
activity=$2
action=$3
fi
# get variables
type=$(grep -A1 '\[' $folder/config.ini | grep -A1 $device | tail -1 | awk '{ print $3 }')
ip=$(grep -A2 '\[' $folder/config.ini | grep -A2 $device | tail -1 | awk '{ print $3 }')
port=$(grep -A3 '\[' $folder/config.ini | grep -A3 $device | tail -1 | awk '{ print $3 }')
relays=$(grep -A4 '\[' $folder/config.ini | grep -A4 $device | tail -1 | awk '{ print $3 }')
sleep=$(grep -A5 '\[' $folder/config.ini | grep -A5 $device | tail -1 | awk '{ print $3 }')
if [ $type == hilink ]
then
relaytype=$(grep -A6 '\[' $folder/config.ini | grep -A6 $device | tail -1 | awk '{ print $3 }')
fi
if [ $type == poe ]
then
version=$(grep -A7 '\[' $folder/config.ini | grep -A7 $device | tail -1 | awk '{ print $3 }')
community=$(grep -A8 '\[' $folder/config.ini | grep -A8 $device | tail -1 | awk '{ print $3 }')
fi
useSSH=$(grep 'useSSH' $folder/config.ini | awk '{ print $3 }')
if [ $useSSH == true ]
then
ssh_user=$(grep 'ssh_user' $folder/config.ini | awk '{ print $3 }')
ssh_ip=$(grep 'ssh_ip' $folder/config.ini | awk '{ print $3 }')
ssh_port=$(grep 'ssh_port' $folder/config.ini | awk '{ print $3 }')
fi
# checks
check=$(grep '\[' $folder/config.ini | grep $device | wc -l)
if (( $check == 0 ))
then
echo "$device not found in $folder/config.ini, check settings"
exit 1
fi
if [[ $relays -lt $action ]]
then
echo "$device has $relays ports available, $activity on relay $action is not possible"
exit 1
fi
# stop poe port(s)
if [[ $type == poe && $activity == stop ]]
then
if [ $action != all ]
then
stoppoe
fi
if [ $action == all ]
then
action=1
while (( $action <= $relays )); do
echo ""
echo "Disabling port $action"
stoppoe
sleep 3s
action=$((action+1))
done
fi
fi
# start poe port
if [[ $type == poe && $activity == start ]]
then
if [ $action != all ]
then
startpoe
fi
if [ $action == all ]
then
action=1
while (( $action <= $relays )); do
echo ""
echo "Enabling port $action"
startpoe
sleep $sleep
action=$((action+1))
done
fi
fi
# cycle poe port
if [[ $type == poe && $activity == cycle ]]
then
if [ $action != all ]
then
stoppoe
sleep 5s
startpoe
query "$STATS_DB" "update relay set lastCycle = now(), totCycle = totCycle+1 where name = '$device' and port = '$action'"
fi
if [ $action == all ]
then
action=1
while (( $action <= $relays )); do
echo ""
echo "Power cycling port $action"
stoppoe
sleep 5s
startpoe
query "$STATS_DB" "update relay set lastCycle = now(), totCycle = totCycle+1 where name = '$device' and port = '$action'"
echo "wait till next port $sleep"
sleep $sleep
action=$((action+1))
done
fi
fi
# stop hilink port(s)
if [[ $type == hilink && $activity == stop ]]
then
if [ $action != all ]
then
stophilink
fi
if [ $action == all ]
then
action=1
while (( $action <= $relays )); do
echo ""
echo "Disabling port $action"
stoppoe
sleep 3s
action=$((action+1))
done
fi
fi
# start hilink port
if [[ $type == hilink && $activity == start ]]
then
if [ $action != all ]
then
starthilink
fi
if [ $action == all ]
then
action=1
while (( $action <= $relays )); do
echo ""
echo "Enabling port $action"
startpoe
sleep $sleep
action=$((action+1))
done
fi
fi
# cycle hilink port
if [[ $type == hilink && $activity == cycle ]]
then
if [ $action != all ]
then
stophilink
sleep 5s
starthilink
query "$STATS_DB" "update relay set lastCycle = now(), totCycle = totCycle+1 where name = '$device' and port = '$action'"
fi
if [ $action == all ]
then
action=1
while (( $action <= $relays )); do
echo ""
echo "Power cycling port $action"
stophilink
sleep 5s
starthilink
query "$STATS_DB" "update relay set lastCycle = now(), totCycle = totCycle+1 where name = '$device' and port = '$action'"
echo "wait till next port $sleep"
sleep $sleep
action=$((action+1))
done
fi
fi
# status hilink ports
if [[ $type == hilink && $activity == status ]]
then
cd $folder && ./relaystatus.sh $ip $port
timing=$(date '+%Y%m%d %H:%M:%S')
echo "[$timing] Status request on $device" >> $folder/log.txt
fi
# status poe ports
if [[ $type == poe && $activity == status ]]
then
if [ $useSSH == true ]
then
ssh -p $ssh_port $ssh_user@$ssh_ip "snmpwalk -v $version -c $community $ip:$port 1.3.6.1.2.1.105.1.3.1.1.4 | sed 's/iso.3.6.1.2.1.105.1.3.1.1.4.1 = Gauge32:/PoE power consumption:/g' | sed 's/$/ W/'"
else
snmpwalk -v $version -c $community $ip:$port 1.3.6.1.2.1.105.1.3.1.1.4 | sed "s/iso.3.6.1.2.1.105.1.3.1.1.4.1 = Gauge32:/PoE power consumption:/g" | sed "s/$/ W/"
fi
echo ""
echo "Port status"
if [ $useSSH == true ]
then
ssh -p $ssh_port $ssh_user@$ssh_ip "snmpwalk -v $version -c $community $ip:$port 1.3.6.1.2.1.2.2.1.8 | head -$relays | sed 's/INTEGER: 1/on/g' | sed 's/INTEGER: 2/off/g' | sed 's/iso.3.6.1.2.1.2.2.1.8./#/g'"
else
snmpwalk -v $version -c $community $ip:$port 1.3.6.1.2.1.2.2.1.8 | head -$relays | sed "s/INTEGER: 1/on/g" | sed "s/INTEGER: 2/off/g" | sed "s/iso.3.6.1.2.1.2.2.1.8./#/g"
fi
timing=$(date '+%Y%m%d %H:%M:%S')
echo "[$timing] Status request on $device" >> $folder/log.txt
fi