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