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