1 | 1 | simandl | #!/bin/bash |
2 | | | |
3 | | | PATH=/opt/rrdtool/bin:$PATH:/sbin |
4 | | | |
5 | | | stat_dir="/var/statistiky/rates" |
6 | | | mkdir -p $stat_dir |
7 | | | |
8 | | | config_file=/opt/statistiky/collect_rates.conf |
9 | | | |
10 | | | if [ ! -f $cwd/$config_file ] |
11 | | | then |
12 | | | echo "Missing config file $config_file" |
13 | | | exit 1; |
14 | | | fi |
15 | | | |
16 | | | cd $stat_dir |
17 | | | |
18 | | | function create() |
19 | | | # $1 ... netdevice |
20 | | | { |
21 | | | local device=$1 |
22 | | | echo "Creating device $device" |
23 | | | |
24 | | | # 20s ~ agregace ze ctyr hodnot (PDP) |
25 | | | # za poslednich 16 hodin chceme 20s udaje => 16*3600/20 = 2880 generaci/radku |
26 | | | # 5 minut = 12 PDP za minutu * 5 minut = 60 PDP |
27 | | | # za posledni tyden chceme 5minutove udaje => 12*24*9 = 2592 radku (rows) |
28 | | | # 20min ~ agregace z 240 PDP |
29 | | | # za posledni mesic chceme 20minutove useky => 3*24*38 = 2736 radku |
30 | | | # dve hodiny ~ 1440 PDP |
31 | | | # za posledni rok chceme dvouhodinove useky => 12*400 = 4800 radku |
32 | | | |
33 | | | rrdtool create ${device}.rrd --step 5 --start `date +%s` \ |
34 | | | DS:input:COUNTER:10:0:10000000 \ |
35 | | | DS:output:COUNTER:10:0:10000000 \ |
36 | | | RRA:AVERAGE:0.7:1:1440 \ |
37 | | | RRA:MAX:0.7:4:2880 \ |
38 | | | RRA:MIN:0.7:4:2880 \ |
39 | | | RRA:AVERAGE:0.7:4:2880 \ |
40 | | | RRA:MAX:0.7:60:2592 \ |
41 | | | RRA:MIN:0.7:60:2592 \ |
42 | | | RRA:AVERAGE:0.7:60:2592 \ |
43 | | | RRA:MAX:0.7:240:2736 \ |
44 | | | RRA:MIN:0.7:240:2736 \ |
45 | | | RRA:AVERAGE:0.7:240:2736 \ |
46 | | | RRA:MAX:0.7:1440:4800 \ |
47 | | | RRA:MIN:0.7:1440:4800 \ |
48 | | | RRA:AVERAGE:0.7:1440:4800 |
49 | | | |
50 | | | } |
51 | | | |
52 | | | function wait_to() |
53 | | | # $1 date for waiting to |
54 | | | { |
55 | | | local time_now=`date +%s`; |
56 | | | while [ $time_now -lt $1 ] |
57 | | | do |
58 | | | time_now=`date +%s` |
59 | | | sleep 1; |
60 | | | done |
61 | | | } |
62 | | | |
63 | | | # first time rrd files initialization |
64 | | | . ${config_file} |
65 | | | do_sleep="no" |
66 | | | for device in $RATES_DEVICES; do |
67 | | | |
68 | | | if [ ! -f $device.rrd ] ; then |
69 | | | create $device |
70 | | | do_sleep="yes" |
71 | | | fi |
72 | | | done |
73 | | | if [ "$do_sleep" == "yes" ]; then |
74 | | | sleep 5 |
75 | | | fi |
76 | | | unset do_sleep |
77 | | | |
78 | | | start_time=`date +%s` |
79 | | | interval=$(( $start_time / 5 * 5 + 5 )) |
80 | | | |
81 | | | |
82 | | | |
83 | | | while [ true ] ; do |
84 | | | |
85 | | | . ${config_file} |
86 | | | |
87 | | | wait_to $interval |
88 | | | |
89 | | | for device in $RATES_DEVICES; do |
90 | | | |
91 | | | if [ ! -f $device.rrd ] ; then |
92 | | | create $device |
93 | | | # first measurement will be in the next round |
94 | | | continue; |
95 | | | fi |
96 | | | |
97 | | | timenow=`date +%s` |
98 | | | dev=`cat /proc/net/dev | grep $device` |
99 | | | values=`echo $dev | cut -d ':' -f 2` |
100 | | | rxbytes=`echo $values | awk '{print $1;}'` |
101 | | | txbytes=`echo $values | awk '{print $9;}'` |
102 | | | |
103 | | | if [ ! -z "${rxbytes}" ] && [ ! -z "${txbytes}" ]; then |
104 | | | rrdtool update $device.rrd $interval:$rxbytes:$txbytes |
105 | | | fi |
106 | | | done |
107 | | | |
108 | | | interval=$(( $interval + 5 )) |
109 | | | |
110 | | | done |