1 | 1 | simandl | #!/bin/bash3 |
2 | | | # |
3 | | | # |
4 | | | # 2005-11-02/Libor konfiguracni skripty na prehazovani primarni FSO <-> backup WiFi |
5 | | | # logovani do /var/statistiky/crusader/crusader.log |
6 | | | # bash3, nevim proc, ale na Marvinovi mi to s dvojkou nechodilo kvuliva |
7 | | | # porovnavani desetinnych cisel pomoci if [ $neco -le $rss ]... |
8 | | | # 2005-10-27/Libor pridano prehazovani na WiFi zalohu pri signalu < 300 |
9 | | | # |
10 | | | # |
11 | | | |
12 | | | stat_dir="/var/statistiky/crusader" |
13 | | | logfile="$stat_dir/crusader.log" |
14 | | | mkdir -p $stat_dir |
15 | | | |
16 | | | config_file=/opt/statistiky/collect_crusader.conf |
17 | | | script_set_main=/opt/statistiky/crusader_set_main.sh |
18 | | | script_set_backup=/opt/statistiky/crusader_set_backup.sh |
19 | | | |
20 | | | function Log() |
21 | | | # $1 ... message |
22 | | | { |
23 | | | timenow=`date +"%Y-%m-%d %T"` |
24 | | | echo "$timenow $1" |
25 | | | echo "$timenow $1" >>$logfile |
26 | | | } |
27 | | | |
28 | | | Log "Crusader start =====" |
29 | | | Log "Using config file: $config_file" |
30 | | | |
31 | | | if [ ! -f $config_file ] |
32 | | | then |
33 | | | Log "Missing config file $config_file, exiting." |
34 | | | exit 1; |
35 | | | fi |
36 | | | |
37 | | | if [ ! -x $script_set_main ]; then |
38 | | | Log "Script set up main is not executable: $script_set_main" |
39 | | | exit 1; |
40 | | | fi |
41 | | | if [ ! -x $script_set_backup ]; then |
42 | | | Log "Script set up main is not executable: $script_set_backup" |
43 | | | exit 1; |
44 | | | fi |
45 | | | |
46 | | | cd $stat_dir |
47 | | | |
48 | | | function create() |
49 | | | # $1 ... netdevice |
50 | | | { |
51 | | | local file=$1 |
52 | | | Log "Creating RRD archive $file" |
53 | | | |
54 | | | # step = 5s |
55 | | | # za posledni 2,5 hodiny chceme vsechny PDP => 2,5 * 3600 / 5 = 1800 hodnot/radku |
56 | | | # 20s ~ agregace ze ctyr hodnot (PDP) |
57 | | | # za poslednich 10 hodin chceme 20s udaje => 10*3600/20 = 1800 generaci/radku |
58 | | | # za poslednich 30 hodin chceme 60s udaje => 30*3600/60 = 1800 generaci/radku |
59 | | | # 5 minut = 12 PDP za minutu * 5 minut = 60 PDP |
60 | | | # za posledni tyden (9dni) chceme 5minutove udaje => 12*24*9 = 2592 radku (rows) |
61 | | | # 20min ~ agregace z 240 PDP |
62 | | | # za posledni mesic (38dni) chceme 20minutove useky => 3*24*38 = 2736 radku |
63 | | | # dve hodiny ~ 1440 PDP |
64 | | | # za posledni rok chceme dvouhodinove useky => 12*400 = 4800 radku |
65 | | | |
66 | | | rrdtool create ${file}.rrd --step 5 --start `date +%s` \ |
67 | | | DS:rss_avg:GAUGE:10:0:999 \ |
68 | | | DS:rss_min:GAUGE:10:0:999 \ |
69 | | | DS:rss_max:GAUGE:10:0:999 \ |
70 | | | RRA:AVERAGE:0.7:1:1800 \ |
71 | | | RRA:MAX:0.7:1:1800 \ |
72 | | | RRA:MIN:0.7:1:1800 \ |
73 | | | RRA:AVERAGE:0.7:4:2880 \ |
74 | | | RRA:MAX:0.7:4:2880 \ |
75 | | | RRA:MIN:0.7:4:2880 \ |
76 | | | RRA:AVERAGE:0.7:12:1800 \ |
77 | | | RRA:MAX:0.7:12:1800 \ |
78 | | | RRA:MIN:0.7:12:1800 \ |
79 | | | RRA:AVERAGE:0.7:60:2592 \ |
80 | | | RRA:MAX:0.7:60:2592 \ |
81 | | | RRA:MIN:0.7:60:2592 \ |
82 | | | RRA:AVERAGE:0.7:240:2736 \ |
83 | | | RRA:MAX:0.7:240:2736 \ |
84 | | | RRA:MIN:0.7:240:2736 \ |
85 | | | RRA:AVERAGE:0.7:1440:4800 \ |
86 | | | RRA:MAX:0.7:1440:4800 \ |
87 | | | RRA:MIN:0.7:1440:4800 |
88 | | | |
89 | | | } |
90 | | | |
91 | | | function wait_to() |
92 | | | # $1 date for waiting to |
93 | | | { |
94 | | | local time_now=`date +%s`; |
95 | | | while [ $time_now -lt $1 ] |
96 | | | do |
97 | | | time_now=`date +%s` |
98 | | | sleep 1; |
99 | | | done |
100 | | | } |
101 | | | |
102 | | | |
103 | | | function activate_wifi() |
104 | | | { |
105 | | | $script_set_backup |
106 | | | # ip route del default |
107 | | | # ip route add default via 10.23.13.1 |
108 | | | echo "Activated WiFi backup" |
109 | | | } |
110 | | | function activate_fso() |
111 | | | { |
112 | | | $script_set_main |
113 | | | # ip route del default |
114 | | | # ip route add default via 10.23.192.53 |
115 | | | echo "Activated FSO primary link" |
116 | | | } |
117 | | | |
118 | | | rss_offset=100 |
119 | | | # kolikrat byl signal nizsi nez rss_low_threshold opakovane v rade po sobe |
120 | | | rss_low_count=0 |
121 | | | # pocet detekovanych slabych signalu v rade, kdy se prepoji inet na zalohu |
122 | | | rss_low_count_down=3 |
123 | | | # pokud sila signalu klesne pod threshold je indikovana jako slaby signal |
124 | | | let rss_low_threshold=300+$rss_offset |
125 | | | # kolikrat byl signal vyssi nez rss_good_threshold opakovane v rade po sobe |
126 | | | rss_good_count=0 |
127 | | | # pocet detekovanych dobrych signalu v rade, kdy se prepoji inet zpet na Crusader |
128 | | | rss_good_count_up=12 |
129 | | | # pokud sila signalu stoupne nad threshold je indikovana jako dobry signal |
130 | | | let rss_good_threshold=350+$rss_offset |
131 | | | # stav sily signalu, muze byt 'low' nebo 'good' |
132 | | | # zaciname s 'good' protoze prepnuti good->low je rychlejsi a taky startup skripty |
133 | | | # pocitaji s optickou linkou, WiFi je zaloha |
134 | | | rss_status="good" |
135 | | | rss_status_last="good" |
136 | | | |
137 | | | Log "RSS offset = $rss_offset" |
138 | | | Log "RSS low threshold = $rss_low_threshold" |
139 | | | Log "RSS good threshold = $rss_good_threshold" |
140 | | | |
141 | | | function rss_check_status() |
142 | | | # $1 ... jmeno rusaderu (napr. crusader0) |
143 | | | # $2 je sila signalu RSS |
144 | | | { |
145 | | | local name=$1 |
146 | | | local rss=$2 |
147 | | | if [ $rss -ge $rss_low_threshold ]; then |
148 | | | echo "RSS($rss) >= low_threshold ($rss_low_threshold), erasing rss_low_count" |
149 | | | rss_low_count="0" |
150 | | | fi |
151 | | | if [ $rss -le $rss_good_threshold ]; then |
152 | | | echo "RSS($rss) <= good_threshold ($rss_good_threshold), erasing rss_good_count" |
153 | | | rss_good_count="0" |
154 | | | fi |
155 | | | if [ "$rss_status" == "good" ] && [ $rss -lt $rss_low_threshold ]; then |
156 | | | let rss_low_count+=1 |
157 | | | echo "RSS($rss) < low_threshold ($rss_low_threshold), increasing rss_low_count to $rss_low_count" |
158 | | | fi |
159 | | | if [ "$rss_status" == "low" ] && [ $rss -gt $rss_good_threshold ]; then |
160 | | | let rss_good_count+=1 |
161 | | | echo "RSS($rss) > good_threshold ($rss_good_threshold), increasing rss_good_count to $rss_good_count" |
162 | | | fi |
163 | | | if [ "$rss_status" == "good" ] && [ $rss_low_count -ge $rss_low_count_down ]; then |
164 | | | echo "FSO link goes down" |
165 | | | rss_status="low" |
166 | | | activate_wifi |
167 | | | Log "$name goes down, activating WiFi backup (RSS $rss)" |
168 | | | fi |
169 | | | if [ "$rss_status" == "low" ] && [ $rss_good_count -ge $rss_good_count_up ]; then |
170 | | | echo "FSO link goes up" |
171 | | | rss_status="good" |
172 | | | activate_fso |
173 | | | Log "$name goes up (RSS $rss)" |
174 | | | fi |
175 | | | } |
176 | | | |
177 | | | start_time=`date +%s` |
178 | | | interval=$(( $start_time / 5 * 5 + 5 )) |
179 | | | |
180 | | | while [ true ] ; do |
181 | | | |
182 | | | . ${config_file} |
183 | | | |
184 | | | device=${DEVICE[0]} |
185 | | | name=${NAME[0]}0 |
186 | | | rrdfile=$name |
187 | | | |
188 | | | if [ ! -f $rrdfile.rrd ] ; then |
189 | | | create $rrdfile |
190 | | | fi |
191 | | | |
192 | | | soucet_rss="0" |
193 | | | rss_min="1000" # ma to jen tri cifry, tj. max 999 => 1000 je vic nez maximum |
194 | | | rss_max="0" |
195 | | | pocet="0" |
196 | | | read rss <${device} # pouze synchronizace, holt o jeden radek prijdeme |
197 | | | time_now=`date +%s` |
198 | | | while [ $time_now -lt $interval ]; do # cekame na zacatek dalsiho intervalu |
199 | | | time_now=`date +%s` |
200 | | | |
201 | | | read -t 1 rss rest <${device} # tady cteme |
202 | | | # echo "rss: '$rss' and the rest of line: '$rest'" |
203 | | | test $rss -ge 0 2>/dev/null # slouzi k otestovani, ze rss obsahuje numerickou hodnoru |
204 | | | if [ $? -lt 2 ] # pokud vyjde 2, pak to neni cislo a test selhal |
205 | | | then |
206 | | | soucet_rss=$(( $soucet_rss + $rss )) |
207 | | | pocet=$(( $pocet + 1 )) |
208 | | | if [ $rss -lt $rss_min ]; then |
209 | | | rss_min=$rss |
210 | | | fi |
211 | | | if [ $rss -gt $rss_max ]; then |
212 | | | rss_max=$rss |
213 | | | fi |
214 | | | fi |
215 | | | done |
216 | | | |
217 | | | if [ $pocet -gt 0 ] |
218 | | | then |
219 | | | prumer=`echo "${soucet_rss}/${pocet}" | bc -l` |
220 | | | echo "prumer: $prumer min: $rss_min max: $rss_max" |
221 | | | rrdtool update $rrdfile.rrd -t rss_avg:rss_min:rss_max $interval:$prumer:$rss_min:$rss_max |
222 | | | prumer=`echo $prumer | cut -d '.' -f 1` |
223 | | | rss_check_status $name $prumer |
224 | | | else |
225 | | | echo "NaN" |
226 | | | rrdtool update $rrdfile.rrd -t rss_avg:rss_min:rss_max $interval:U:U:U |
227 | | | rss_check_status $name 0 |
228 | | | fi |
229 | | | |
230 | | | interval=$(( $interval + 5 )) |
231 | | | |
232 | | | done |