1 | 1 | simandl | #!/bin/bash |
2 | | | # |
3 | | | # |
4 | | | # 2008-06-07/Libor Zacinam pokusovat s adaptaci pro Crusader Aggregator 100M |
5 | | | # |
6 | | | # 2006-11-22/Libor Predelano cteni RSS pomoci nove aplikace napsane v C pro snizeni |
7 | | | # zateze CPU, ktera na VIA C3 800MHz ve stavajici podobe dosahovala |
8 | | | # az 100%. |
9 | | | # |
10 | | | # 2005-11-27/Libor Predelano cteni seriaku. Ted si ho otevre jako filedescriptor |
11 | | | # a cte z nej jako ze souboru. Na Marvinovi se tim (snad) uplne |
12 | | | # odstranily chly do nuly, ktere byly nejspis zpusobeny |
13 | | | # rychlosti (pomalosti) Marvina. (800MHz VIA C3) |
14 | | | # 2005-11-02/Libor konfiguracni skripty na prehazovani primarni FSO <-> backup WiFi |
15 | | | # logovani do /var/statistiky/crusader/crusader.log |
16 | | | # bash3, nevim proc, ale na Marvinovi mi to s dvojkou nechodilo kvuliva |
17 | | | # porovnavani desetinnych cisel pomoci if [ $neco -le $rss ]... |
18 | | | # 2005-10-27/Libor pridano prehazovani na WiFi zalohu pri signalu < 300 |
19 | | | # |
20 | | | # |
21 | | | |
22 | | | PATH=$PATH:/opt/statistiky/bin |
23 | | | |
24 | | | if [ $# -eq 1 ]; then |
25 | | | crusader_number=$1 |
26 | | | fi |
27 | | | if [ $# -gt 1 ]; then |
28 | | | echo "ERROR: more than 1 option" >&2 |
29 | | | fi |
30 | | | if [ $# -lt 1 ]; then |
31 | | | crusader_number=0 |
32 | | | fi |
33 | | | |
34 | | | stat_dir="/var/statistiky/crusader" |
35 | | | logfile="$stat_dir/crusader.log" |
36 | | | mkdir -p $stat_dir |
37 | | | |
38 | | | config_file=/opt/statistiky/collect_crusader.conf |
39 | | | script_set_main=/opt/statistiky/crusader_set_main.sh |
40 | | | script_set_backup=/opt/statistiky/crusader_set_backup.sh |
41 | | | |
42 | | | function Log() |
43 | | | # $1 ... message |
44 | | | { |
45 | | | timenow=`date +"%Y-%m-%d %T"` |
46 | | | echo "LOG: $timenow $1" |
47 | | | echo "$timenow $1" >>$logfile |
48 | | | } |
49 | | | |
50 | | | Log "Crusader start =====" |
51 | | | Log "Using config file: $config_file" |
52 | | | |
53 | | | if [ ! -f $config_file ] |
54 | | | then |
55 | | | Log "Missing config file $config_file, exiting." |
56 | | | exit 1; |
57 | | | fi |
58 | | | |
59 | | | if [ ! -x $script_set_main ]; then |
60 | | | Log "Script set up main is not executable: $script_set_main" |
61 | | | exit 1; |
62 | | | fi |
63 | | | if [ ! -x $script_set_backup ]; then |
64 | | | Log "Script set up main is not executable: $script_set_backup" |
65 | | | exit 1; |
66 | | | fi |
67 | | | |
68 | | | cd $stat_dir |
69 | | | |
70 | | | function create() |
71 | | | # $1 ... netdevice |
72 | | | { |
73 | | | local file=$1 |
74 | | | Log "Creating RRD archive $file" |
75 | | | |
76 | | | # step = 5s |
77 | | | # za posledni 2,5 hodiny chceme vsechny PDP => 2,5 * 3600 / 5 = 1800 hodnot/radku |
78 | | | # 20s ~ agregace ze ctyr hodnot (PDP) |
79 | | | # za poslednich 10 hodin chceme 20s udaje => 10*3600/20 = 1800 generaci/radku |
80 | | | # za poslednich 30 hodin chceme 60s udaje => 30*3600/60 = 1800 generaci/radku |
81 | | | # 5 minut = 12 PDP za minutu * 5 minut = 60 PDP |
82 | | | # za posledni tyden (9dni) chceme 5minutove udaje => 12*24*9 = 2592 radku (rows) |
83 | | | # 20min ~ agregace z 240 PDP |
84 | | | # za posledni mesic (38dni) chceme 20minutove useky => 3*24*38 = 2736 radku |
85 | | | # dve hodiny ~ 1440 PDP |
86 | | | # za posledni rok chceme dvouhodinove useky => 12*400 = 4800 radku |
87 | | | |
88 | | | rrdtool create ${file}.rrd --step 5 --start `date +%s` \ |
89 | | | DS:rss_avg:GAUGE:10:0:999 \ |
90 | | | DS:rss_min:GAUGE:10:0:999 \ |
91 | | | DS:rss_max:GAUGE:10:0:999 \ |
92 | | | RRA:AVERAGE:0.7:1:1800 \ |
93 | | | RRA:MAX:0.7:1:1800 \ |
94 | | | RRA:MIN:0.7:1:1800 \ |
95 | | | RRA:AVERAGE:0.7:4:2880 \ |
96 | | | RRA:MAX:0.7:4:2880 \ |
97 | | | RRA:MIN:0.7:4:2880 \ |
98 | | | RRA:AVERAGE:0.7:12:1800 \ |
99 | | | RRA:MAX:0.7:12:1800 \ |
100 | | | RRA:MIN:0.7:12:1800 \ |
101 | | | RRA:AVERAGE:0.7:60:2592 \ |
102 | | | RRA:MAX:0.7:60:2592 \ |
103 | | | RRA:MIN:0.7:60:2592 \ |
104 | | | RRA:AVERAGE:0.7:240:2736 \ |
105 | | | RRA:MAX:0.7:240:2736 \ |
106 | | | RRA:MIN:0.7:240:2736 \ |
107 | | | RRA:AVERAGE:0.7:1440:4800 \ |
108 | | | RRA:MAX:0.7:1440:4800 \ |
109 | | | RRA:MIN:0.7:1440:4800 |
110 | | | |
111 | | | } |
112 | | | |
113 | | | # zastarala: ted cekame pomoci crusader_read_rss |
114 | | | function wait_to() |
115 | | | # $1 date for waiting to |
116 | | | { |
117 | | | local time_now=`date +%s`; |
118 | | | while [ $time_now -lt $1 ] |
119 | | | do |
120 | | | time_now=`date +%s` |
121 | | | sleep 1; |
122 | | | done |
123 | | | } |
124 | | | |
125 | | | |
126 | | | function activate_backup() |
127 | | | { |
128 | | | $script_set_backup |
129 | | | echo "Activated WiFi backup" |
130 | | | } |
131 | | | function activate_fso() |
132 | | | { |
133 | | | $script_set_main |
134 | | | echo "Activated FSO primary link" |
135 | | | } |
136 | | | |
137 | | | function read_remote_status() |
138 | | | { |
139 | | | remote_rss_status=`grep ^RSS: /var/statistiky/crusader/remote_site_status | awk '{ print $2;}'` |
140 | | | echo "Remote RSS status: $remote_rss_status" |
141 | | | } |
142 | | | |
143 | | | function write_status_to_remote() |
144 | | | { |
145 | | | Log " Writing status (RSS: $rss_status) to remote side" |
146 | | | ssh libor@10.23.64.169 "echo \"RSS: $rss_status\" >/var/statistiky/crusader/remote_site_status" |
147 | | | } |
148 | | | |
149 | | | function rss_check_status() |
150 | | | # $1 ... jmeno rusaderu (napr. crusader0) |
151 | | | # $2 je sila signalu RSS |
152 | | | { |
153 | | | local name=$1 |
154 | | | local rss=$2 |
155 | | | if [ $rss -ge $rss_low_threshold ]; then |
156 | | | echo "RSS($rss) >= low_threshold ($rss_low_threshold), erasing rss_low_count" |
157 | | | rss_low_count="0" |
158 | | | fi |
159 | | | if [ $rss -le $rss_good_threshold ]; then |
160 | | | echo "RSS($rss) <= good_threshold ($rss_good_threshold), erasing rss_good_count" |
161 | | | rss_good_count="0" |
162 | | | fi |
163 | | | if [ "$rss_status" == "good" ] && [ $rss -lt $rss_low_threshold ]; then |
164 | | | let rss_low_count+=1 |
165 | | | echo "RSS($rss) < low_threshold ($rss_low_threshold), increasing rss_low_count to $rss_low_count" |
166 | | | fi |
167 | | | if [ "$rss_status" == "low" ] && [ $rss -gt $rss_good_threshold ]; then |
168 | | | let rss_good_count+=1 |
169 | | | echo "RSS($rss) > good_threshold ($rss_good_threshold), increasing rss_good_count to $rss_good_count" |
170 | | | fi |
171 | | | if [ "$rss_status" == "good" ] && [ $rss_low_count -ge $rss_low_counter_go_backup ]; then |
172 | | | echo "RSS goes down" |
173 | | | rss_status="low" |
174 | | | Log "Crusader $crusader_number: goes down, RSS: low ($rss)" |
175 | | | write_status_to_remote |
176 | | | fi |
177 | | | if [ "$rss_status" == "low" ] && [ $rss_good_count -ge $rss_good_counter_go_main ]; then |
178 | | | echo "RSS goes up" |
179 | | | rss_status="good" |
180 | | | Log "Crusader $crusader_number: goes up, RSS: good ($rss)" |
181 | | | write_status_to_remote |
182 | | | fi |
183 | | | |
184 | | | if [ "$link_status" == "FSO" ] && [ "$rss_status" == "low" ] |
185 | | | then |
186 | | | Log "Link to backup" |
187 | | | activate_backup |
188 | | | link_status="backup" |
189 | | | fi |
190 | | | if [ "$link_status" == "backup" ] && [ "$rss_status" == "good" ] && [ "$remote_rss_status" == "good" ] |
191 | | | then |
192 | | | Log "Link to FSO" |
193 | | | activate_fso |
194 | | | link_status="FSO" |
195 | | | fi |
196 | | | |
197 | | | read_remote_status |
198 | | | |
199 | | | if [ "$link_status" == "FSO" ] && [ "$remote_rss_status" == "low" ] |
200 | | | then |
201 | | | Log "Link to backup" |
202 | | | activate_backup |
203 | | | link_status="backup" |
204 | | | write_status_to_remote |
205 | | | fi |
206 | | | } |
207 | | | |
208 | | | # Pocatecni nastaveni linky FSO/backup |
209 | | | function setup_initial_link_status() |
210 | | | { |
211 | | | if [ $rss_avg -gt $rss_good_threshold ]; then |
212 | | | rss_status="good" |
213 | | | rss_status_last="good" |
214 | | | link_status="unknown" |
215 | | | else |
216 | | | rss_status="low" |
217 | | | rss_status_last="low" |
218 | | | link_status="unknown" |
219 | | | fi |
220 | | | write_status_to_remote |
221 | | | read_remote_status |
222 | | | |
223 | | | if [ "$rss_status" == "good" ] && [ "$remote_rss_status" == "good" ] |
224 | | | then |
225 | | | Log "Link to FSO" |
226 | | | activate_fso |
227 | | | link_status="FSO" |
228 | | | else |
229 | | | Log "Link to backup" |
230 | | | activate_backup |
231 | | | link_status="backup" |
232 | | | fi |
233 | | | } |
234 | | | |
235 | | | function load_config() |
236 | | | { |
237 | | | . ${config_file} |
238 | | | rss_low_threshold=${RSS_LOW_THRESHOLD[$crusader_number]} |
239 | | | rss_good_threshold=${RSS_GOOD_THRESHOLD[$crusader_number]} |
240 | | | rss_low_counter_go_backup=${RSS_LOW_COUNTER_TO_GO_BACKUP[$crusader_number]} |
241 | | | rss_good_counter_go_main=${RSS_GOOD_COUNTER_TO_GO_MAIN[$crusader_number]} |
242 | | | device=${DEVICE[$crusader_number]} |
243 | | | name=${NAME[$crusader_number]}${crusader_number} |
244 | | | } |
245 | | | |
246 | | | print_config() |
247 | | | { |
248 | | | Log "Crusader $crusader_number" |
249 | | | Log " RSS low threshold = $rss_low_threshold" |
250 | | | Log " RSS good threshold = $rss_good_threshold" |
251 | | | Log " RSS low counter go backup = $rss_low_counter_go_backup" |
252 | | | Log " RSS good counter go main = $rss_good_counter_go_main" |
253 | | | Log " Seriak: $device" |
254 | | | } |
255 | | | |
256 | | | # Inicializace |
257 | | | load_config |
258 | | | print_config |
259 | | | j=0 |
260 | | | rv=2 |
261 | | | |
262 | | | rss_line=`crusader_read_rss ${device} +2 2>/dev/null` |
263 | | | rss_avg=`echo $rss_line | awk '{ print $2; }'` |
264 | | | if [ -z "$rss_avg" ]; then |
265 | | | rss_avg="0" |
266 | | | fi |
267 | | | Log " Sila signalu pri startu (RSS): $rss_avg" |
268 | | | |
269 | | | # Prvotni nastaveni linky pred hlavni smyckou |
270 | | | #setup_initial_link_status |
271 | | | |
272 | | | # kolikrat byl signal nizsi nez rss_low_threshold opakovane v rade po sobe |
273 | | | rss_low_counter=0 |
274 | | | # kolikrat byl signal vyssi nez rss_good_threshold opakovane v rade po sobe |
275 | | | rss_good_counter=0 |
276 | | | |
277 | | | start_time=`date +%s` |
278 | | | interval=$(( $start_time / 5 * 5 + 5 )) |
279 | | | |
280 | | | # Hlavni smycka |
281 | | | while [ true ] ; do |
282 | | | |
283 | | | load_config |
284 | | | |
285 | | | rrdfile=${name} |
286 | | | if [ ! -f $rrdfile.rrd ] ; then |
287 | | | create $rrdfile |
288 | | | fi |
289 | | | |
290 | | | rss_line=`crusader100_read_rss ${device} ${interval} 2>/dev/null` |
291 | | | echo "RSS LINE: $rss_line" |
292 | | | pocet=`echo $rss_line | awk '{ print $1; }'` |
293 | | | if [ -z "$pocet" ]; then |
294 | | | pocet="0" |
295 | | | fi |
296 | | | rss_avg=`echo $rss_line | awk '{ print $2; }'` |
297 | | | rss_max=`echo $rss_line | awk '{ print $3; }'` |
298 | | | rss_min=`echo $rss_line | awk '{ print $4; }'` |
299 | | | if [ -z "$rss_avg" ]; then |
300 | | | rss_avg="0" |
301 | | | fi |
302 | | | if [ -z "$rss_max" ]; then |
303 | | | rss_max="0" |
304 | | | fi |
305 | | | if [ -z "$rss_min" ]; then |
306 | | | rss_min="0" |
307 | | | fi |
308 | | | |
309 | | | if [ $pocet -gt 0 ] |
310 | | | then |
311 | | | echo "Pocet vzorku: $pocet RSS avg: $rss_avg min: $rss_min max: $rss_max" |
312 | | | rrdtool update $rrdfile.rrd -t rss_avg:rss_min:rss_max $interval:$rss_avg:$rss_min:$rss_max |
313 | | | # rss_check_status ${name} $rss_avg |
314 | | | else |
315 | | | echo "NaN" |
316 | | | rrdtool update $rrdfile.rrd -t rss_avg:rss_min:rss_max $interval:U:U:U |
317 | | | # rss_check_status ${name} 0 |
318 | | | fi |
319 | | | |
320 | | | interval=$(( $interval + 5 )) |
321 | | | |
322 | | | echo "" |
323 | | | done |