| 1 | 1 | simandl | #!/bin/bash3 |
| 2 | | | # |
| 3 | | | # |
| 4 | | | # 2005-11-27/Libor Predelano cteni seriaku. Ted si ho otevre jako filedescriptor |
| 5 | | | # a cte z nej jako ze souboru. Na Marvinovi se tim (snad) uplne |
| 6 | | | # odstranily chly do nuly, ktere byly nejspis zpusobeny |
| 7 | | | # rychlosti (pomalosti) Marvina. (800MHz VIA C3) |
| 8 | | | # 2005-11-02/Libor konfiguracni skripty na prehazovani primarni FSO <-> backup WiFi |
| 9 | | | # logovani do /var/statistiky/crusader/crusader.log |
| 10 | | | # bash3, nevim proc, ale na Marvinovi mi to s dvojkou nechodilo kvuliva |
| 11 | | | # porovnavani desetinnych cisel pomoci if [ $neco -le $rss ]... |
| 12 | | | # 2005-10-27/Libor pridano prehazovani na WiFi zalohu pri signalu < 300 |
| 13 | | | # |
| 14 | | | # |
| 15 | | | |
| 16 | | | if [ $# -eq 1 ]; then |
| 17 | | | crusader_number=$1 |
| 18 | | | fi |
| 19 | | | if [ $# -gt 1 ]; then |
| 20 | | | echo "ERROR: more than 1 option" >&2 |
| 21 | | | fi |
| 22 | | | if [ $# -lt 1 ]; then |
| 23 | | | crusader_number=0 |
| 24 | | | fi |
| 25 | | | |
| 26 | | | stat_dir="/var/statistiky/crusader" |
| 27 | | | logfile="$stat_dir/crusader.log" |
| 28 | | | mkdir -p $stat_dir |
| 29 | | | |
| 30 | | | config_file=/opt/statistiky/collect_crusader.conf |
| 31 | | | script_set_main=/opt/statistiky/crusader_set_main.sh |
| 32 | | | script_set_backup=/opt/statistiky/crusader_set_backup.sh |
| 33 | | | |
| 34 | | | function Log() |
| 35 | | | # $1 ... message |
| 36 | | | { |
| 37 | | | timenow=`date +"%Y-%m-%d %T"` |
| 38 | | | echo "$timenow $1" |
| 39 | | | echo "$timenow $1" >>$logfile |
| 40 | | | } |
| 41 | | | |
| 42 | | | Log "Crusader start =====" |
| 43 | | | Log "Using config file: $config_file" |
| 44 | | | |
| 45 | | | if [ ! -f $config_file ] |
| 46 | | | then |
| 47 | | | Log "Missing config file $config_file, exiting." |
| 48 | | | exit 1; |
| 49 | | | fi |
| 50 | | | |
| 51 | | | if [ ! -x $script_set_main ]; then |
| 52 | | | Log "Script set up main is not executable: $script_set_main" |
| 53 | | | exit 1; |
| 54 | | | fi |
| 55 | | | if [ ! -x $script_set_backup ]; then |
| 56 | | | Log "Script set up main is not executable: $script_set_backup" |
| 57 | | | exit 1; |
| 58 | | | fi |
| 59 | | | |
| 60 | | | cd $stat_dir |
| 61 | | | |
| 62 | | | function create() |
| 63 | | | # $1 ... netdevice |
| 64 | | | { |
| 65 | | | local file=$1 |
| 66 | | | Log "Creating RRD archive $file" |
| 67 | | | |
| 68 | | | # step = 5s |
| 69 | | | # za posledni 2,5 hodiny chceme vsechny PDP => 2,5 * 3600 / 5 = 1800 hodnot/radku |
| 70 | | | # 20s ~ agregace ze ctyr hodnot (PDP) |
| 71 | | | # za poslednich 10 hodin chceme 20s udaje => 10*3600/20 = 1800 generaci/radku |
| 72 | | | # za poslednich 30 hodin chceme 60s udaje => 30*3600/60 = 1800 generaci/radku |
| 73 | | | # 5 minut = 12 PDP za minutu * 5 minut = 60 PDP |
| 74 | | | # za posledni tyden (9dni) chceme 5minutove udaje => 12*24*9 = 2592 radku (rows) |
| 75 | | | # 20min ~ agregace z 240 PDP |
| 76 | | | # za posledni mesic (38dni) chceme 20minutove useky => 3*24*38 = 2736 radku |
| 77 | | | # dve hodiny ~ 1440 PDP |
| 78 | | | # za posledni rok chceme dvouhodinove useky => 12*400 = 4800 radku |
| 79 | | | |
| 80 | | | rrdtool create ${file}.rrd --step 5 --start `date +%s` \ |
| 81 | | | DS:rss_avg:GAUGE:10:0:999 \ |
| 82 | | | DS:rss_min:GAUGE:10:0:999 \ |
| 83 | | | DS:rss_max:GAUGE:10:0:999 \ |
| 84 | | | RRA:AVERAGE:0.7:1:1800 \ |
| 85 | | | RRA:MAX:0.7:1:1800 \ |
| 86 | | | RRA:MIN:0.7:1:1800 \ |
| 87 | | | RRA:AVERAGE:0.7:4:2880 \ |
| 88 | | | RRA:MAX:0.7:4:2880 \ |
| 89 | | | RRA:MIN:0.7:4:2880 \ |
| 90 | | | RRA:AVERAGE:0.7:12:1800 \ |
| 91 | | | RRA:MAX:0.7:12:1800 \ |
| 92 | | | RRA:MIN:0.7:12:1800 \ |
| 93 | | | RRA:AVERAGE:0.7:60:2592 \ |
| 94 | | | RRA:MAX:0.7:60:2592 \ |
| 95 | | | RRA:MIN:0.7:60:2592 \ |
| 96 | | | RRA:AVERAGE:0.7:240:2736 \ |
| 97 | | | RRA:MAX:0.7:240:2736 \ |
| 98 | | | RRA:MIN:0.7:240:2736 \ |
| 99 | | | RRA:AVERAGE:0.7:1440:4800 \ |
| 100 | | | RRA:MAX:0.7:1440:4800 \ |
| 101 | | | RRA:MIN:0.7:1440:4800 |
| 102 | | | |
| 103 | | | } |
| 104 | | | |
| 105 | | | function wait_to() |
| 106 | | | # $1 date for waiting to |
| 107 | | | { |
| 108 | | | local time_now=`date +%s`; |
| 109 | | | while [ $time_now -lt $1 ] |
| 110 | | | do |
| 111 | | | time_now=`date +%s` |
| 112 | | | sleep 1; |
| 113 | | | done |
| 114 | | | } |
| 115 | | | |
| 116 | | | |
| 117 | | | function activate_wifi() |
| 118 | | | { |
| 119 | | | $script_set_backup |
| 120 | | | # ip route del default |
| 121 | | | # ip route add default via 10.23.13.1 |
| 122 | | | echo "Activated WiFi backup" |
| 123 | | | } |
| 124 | | | function activate_fso() |
| 125 | | | { |
| 126 | | | $script_set_main |
| 127 | | | # ip route del default |
| 128 | | | # ip route add default via 10.23.192.53 |
| 129 | | | echo "Activated FSO primary link" |
| 130 | | | } |
| 131 | | | |
| 132 | | | function rss_check_status() |
| 133 | | | # $1 ... jmeno rusaderu (napr. crusader0) |
| 134 | | | # $2 je sila signalu RSS |
| 135 | | | { |
| 136 | | | local name=$1 |
| 137 | | | local rss=$2 |
| 138 | | | if [ $rss -ge $rss_low_threshold ]; then |
| 139 | | | echo "RSS($rss) >= low_threshold ($rss_low_threshold), erasing rss_low_count" |
| 140 | | | rss_low_count="0" |
| 141 | | | fi |
| 142 | | | if [ $rss -le $rss_good_threshold ]; then |
| 143 | | | echo "RSS($rss) <= good_threshold ($rss_good_threshold), erasing rss_good_count" |
| 144 | | | rss_good_count="0" |
| 145 | | | fi |
| 146 | | | if [ "$rss_status" == "good" ] && [ $rss -lt $rss_low_threshold ]; then |
| 147 | | | let rss_low_count+=1 |
| 148 | | | echo "RSS($rss) < low_threshold ($rss_low_threshold), increasing rss_low_count to $rss_low_count" |
| 149 | | | fi |
| 150 | | | if [ "$rss_status" == "low" ] && [ $rss -gt $rss_good_threshold ]; then |
| 151 | | | let rss_good_count+=1 |
| 152 | | | echo "RSS($rss) > good_threshold ($rss_good_threshold), increasing rss_good_count to $rss_good_count" |
| 153 | | | fi |
| 154 | | | if [ "$rss_status" == "good" ] && [ $rss_low_count -ge $rss_low_counter_go_backup ]; then |
| 155 | | | echo "FSO link goes down" |
| 156 | | | rss_status="low" |
| 157 | | | activate_wifi |
| 158 | | | Log "Crusader $crusader_number: goes down, activating WiFi backup (RSS $rss)" |
| 159 | | | fi |
| 160 | | | if [ "$rss_status" == "low" ] && [ $rss_good_count -ge $rss_good_counter_go_main ]; then |
| 161 | | | echo "FSO link goes up" |
| 162 | | | rss_status="good" |
| 163 | | | activate_fso |
| 164 | | | Log "Crusader $crusader_number: goes up, activating FSO (RSS $rss)" |
| 165 | | | fi |
| 166 | | | } |
| 167 | | | |
| 168 | | | function load_config() |
| 169 | | | { |
| 170 | | | . ${config_file} |
| 171 | | | rss_low_threshold=${RSS_LOW_THRESHOLD[$crusader_number]} |
| 172 | | | rss_good_threshold=${RSS_GOOD_THRESHOLD[$crusader_number]} |
| 173 | | | rss_low_counter_go_backup=${RSS_LOW_COUNTER_TO_GO_BACKUP[$crusader_number]} |
| 174 | | | rss_good_counter_go_main=${RSS_GOOD_COUNTER_TO_GO_MAIN[$crusader_number]} |
| 175 | | | device=${DEVICE[$crusader_number]} |
| 176 | | | name=${NAME[$crusader_number]}${crusader_number} |
| 177 | | | } |
| 178 | | | |
| 179 | | | print_config() |
| 180 | | | { |
| 181 | | | Log "Crusader $crusader_number" |
| 182 | | | Log " RSS low threshold = $rss_low_threshold" |
| 183 | | | Log " RSS good threshold = $rss_good_threshold" |
| 184 | | | Log " RSS low counter go backup = $rss_low_counter_go_backup" |
| 185 | | | Log " RSS good counter go main = $rss_good_counter_go_main" |
| 186 | | | Log " Seriak: $device" |
| 187 | | | } |
| 188 | | | |
| 189 | | | # Inicializace |
| 190 | | | load_config |
| 191 | | | print_config |
| 192 | | | # otevri seriak jako filedescriptor |
| 193 | | | exec 10<${device} |
| 194 | | | read rest <&10 # synchronizacni cteni |
| 195 | | | j=0 |
| 196 | | | rv=2 |
| 197 | | | while [ $rv -eq 2 ] # pokud vyjde 2, pak to neni cislo a test selhal |
| 198 | | | do |
| 199 | | | let j=$j+1 |
| 200 | | | read -t 1 rss rest <&10 |
| 201 | | | test $rss -ge 0 2>/dev/null # slouzi k otestovani, ze rss obsahuje numerickou hodnoru |
| 202 | | | rv=$? |
| 203 | | | done |
| 204 | | | echo "Pocet pokusu: $j" |
| 205 | | | Log " Sila signalu pri startu (RSS): $rss" |
| 206 | | | |
| 207 | | | if [ $rss -gt $rss_good_threshold ]; then |
| 208 | | | Log " Preliminary setting to FSO" |
| 209 | | | activate_fso |
| 210 | | | rss_status="good" |
| 211 | | | rss_status_last="good" |
| 212 | | | else |
| 213 | | | Log " Preliminary set to WiFi backup" |
| 214 | | | activate_wifi |
| 215 | | | rss_status="low" |
| 216 | | | rss_status_last="low" |
| 217 | | | fi |
| 218 | | | |
| 219 | | | # kolikrat byl signal nizsi nez rss_low_threshold opakovane v rade po sobe |
| 220 | | | rss_low_counter=0 |
| 221 | | | # kolikrat byl signal vyssi nez rss_good_threshold opakovane v rade po sobe |
| 222 | | | rss_good_counter=0 |
| 223 | | | |
| 224 | | | start_time=`date +%s` |
| 225 | | | interval=$(( $start_time / 5 * 5 + 5 )) |
| 226 | | | |
| 227 | | | # Hlavni smycka |
| 228 | | | while [ true ] ; do |
| 229 | | | |
| 230 | | | old_device=${device} |
| 231 | | | load_config |
| 232 | | | |
| 233 | | | if [ "$device" != "${old_device}" ]; then |
| 234 | | | Log "Crusader $crusader_number: Zmena seriaku ${old_device} -> ${device}" |
| 235 | | | # zavri seriak |
| 236 | | | exec 10<&- |
| 237 | | | # otevri novy seriak |
| 238 | | | exec 10<${device} |
| 239 | | | fi |
| 240 | | | |
| 241 | | | rrdfile=${name} |
| 242 | | | if [ ! -f $rrdfile.rrd ] ; then |
| 243 | | | create $rrdfile |
| 244 | | | fi |
| 245 | | | |
| 246 | | | soucet_rss="0" |
| 247 | | | rss_min="1000" # ma to jen tri cifry, tj. max 999 => 1000 je vic nez maximum |
| 248 | | | rss_max="0" |
| 249 | | | pocet="0" |
| 250 | | | read rest <&10 # pouze synchronizace, holt o jeden radek prijdeme |
| 251 | | | time_now=`date +%s` |
| 252 | | | while [ $time_now -lt $interval ]; do # cekame na zacatek dalsiho intervalu |
| 253 | | | time_now=`date +%s` |
| 254 | | | |
| 255 | | | read -t 1 rss rest <&10 # tady cteme z file descriptoru (seriak) |
| 256 | | | # echo "rss: '$rss' and the rest of line: '$rest'" |
| 257 | | | test $rss -ge 0 2>/dev/null # slouzi k otestovani, ze rss obsahuje numerickou hodnoru |
| 258 | | | if [ $? -lt 2 ] # pokud vyjde 2, pak to neni cislo a test selhal |
| 259 | | | then |
| 260 | | | soucet_rss=$(( $soucet_rss + $rss )) |
| 261 | | | pocet=$(( $pocet + 1 )) |
| 262 | | | if [ $rss -lt $rss_min ]; then |
| 263 | | | rss_min=$rss |
| 264 | | | fi |
| 265 | | | if [ $rss -gt $rss_max ]; then |
| 266 | | | rss_max=$rss |
| 267 | | | fi |
| 268 | | | fi |
| 269 | | | done |
| 270 | | | |
| 271 | | | if [ $pocet -gt 0 ] |
| 272 | | | then |
| 273 | | | prumer=`echo "${soucet_rss}/${pocet}" | bc -l` |
| 274 | | | echo "prumer: $prumer min: $rss_min max: $rss_max" |
| 275 | | | rrdtool update $rrdfile.rrd -t rss_avg:rss_min:rss_max $interval:$prumer:$rss_min:$rss_max |
| 276 | | | prumer=`echo $prumer | cut -d '.' -f 1` |
| 277 | | | rss_check_status ${name} $prumer |
| 278 | | | else |
| 279 | | | echo "NaN" |
| 280 | | | rrdtool update $rrdfile.rrd -t rss_avg:rss_min:rss_max $interval:U:U:U |
| 281 | | | rss_check_status ${name} 0 |
| 282 | | | fi |
| 283 | | | |
| 284 | | | interval=$(( $interval + 5 )) |
| 285 | | | |
| 286 | | | done |