1 | 1 | simandl | #!/bin/bash |
2 | | | |
3 | | | PATH=/opt/rrdtool/bin:$PATH:/sbin |
4 | | | |
5 | | | stat_dir="/var/statistiky/system" |
6 | | | mkdir -p $stat_dir |
7 | | | |
8 | | | cd $stat_dir |
9 | | | |
10 | | | function create_db() |
11 | | | { |
12 | | | if [ ! -f mpstat.rrd ] ; then |
13 | | | echo "Creating mpstat db" |
14 | | | |
15 | | | # sbirame jednou za minutu |
16 | | | # poslednich 6 hodin chci mit vsechno => 60*6 = 360 PDP |
17 | | | # za posledni tyden chci desetiminutove agregace => 10 dni * 24 hodin * 6 = 1440 rows |
18 | | | # za posledni mesic chci hodinove agregace => 40 dni * 24 hodin = 960 rows |
19 | | | # za posledni rok chci ctyrhodinove agregace => 365 * 6 = 2190 rows |
20 | | | |
21 | | | rrdtool create mpstat.rrd --step 60 --start `date +%s` \ |
22 | | | DS:user:GAUGE:120:0:100 \ |
23 | | | DS:nice:GAUGE:120:0:100 \ |
24 | | | DS:system:GAUGE:120:0:100 \ |
25 | | | DS:iowait:GAUGE:120:0:100 \ |
26 | | | DS:irq:GAUGE:120:0:100 \ |
27 | | | DS:softirq:GAUGE:120:0:100 \ |
28 | | | DS:idle:GAUGE:120:0:100 \ |
29 | | | DS:intrs:GAUGE:120:0:U \ |
30 | | | RRA:AVERAGE:0.7:1:360 \ |
31 | | | RRA:MAX:0.7:10:1440 \ |
32 | | | RRA:MIN:0.7:10:1440 \ |
33 | | | RRA:AVERAGE:0.7:10:1440 \ |
34 | | | RRA:MAX:0.7:60:960 \ |
35 | | | RRA:MIN:0.7:60:960 \ |
36 | | | RRA:AVERAGE:0.7:60:960 \ |
37 | | | RRA:MAX:0.7:240:2190 \ |
38 | | | RRA:MIN:0.7:240:2190 \ |
39 | | | RRA:AVERAGE:0.7:240:2190 |
40 | | | fi |
41 | | | if [ ! -f conntrack.rrd ] ; then |
42 | | | echo "Creating conntrack db" |
43 | | | |
44 | | | |
45 | | | rrdtool create conntrack.rrd --step 60 --start `date +%s` \ |
46 | | | DS:tcp_syn_sent:GAUGE:120:0:65535 \ |
47 | | | DS:tcp_syn_recv:GAUGE:120:0:65535 \ |
48 | | | DS:tcp_established:GAUGE:120:0:65535 \ |
49 | | | DS:tcp_close:GAUGE:120:0:65535 \ |
50 | | | DS:tcp_close_wait:GAUGE:120:0:65535 \ |
51 | | | DS:tcp_fin_wait:GAUGE:120:0:65535 \ |
52 | | | DS:tcp_time_wait:GAUGE:120:0:65535 \ |
53 | | | DS:udp:GAUGE:120:0:65535 \ |
54 | | | DS:ip_conntrack_max:GAUGE:120:0:65535 \ |
55 | | | RRA:AVERAGE:0.7:1:360 \ |
56 | | | RRA:MAX:0.7:10:1440 \ |
57 | | | RRA:MIN:0.7:10:1440 \ |
58 | | | RRA:AVERAGE:0.7:10:1440 \ |
59 | | | RRA:MAX:0.7:60:960 \ |
60 | | | RRA:MIN:0.7:60:960 \ |
61 | | | RRA:AVERAGE:0.7:60:960 \ |
62 | | | RRA:MAX:0.7:240:2190 \ |
63 | | | RRA:MIN:0.7:240:2190 \ |
64 | | | RRA:AVERAGE:0.7:240:2190 |
65 | | | fi |
66 | | | |
67 | | | } |
68 | | | |
69 | | | function wait_to() |
70 | | | # $1 date for waiting to |
71 | | | { |
72 | | | local time_now=`date +%s`; |
73 | | | while [ $time_now -lt $1 ] |
74 | | | do |
75 | | | time_now=`date +%s` |
76 | | | sleep 1; |
77 | | | done |
78 | | | } |
79 | | | |
80 | | | start_time=`date +%s` |
81 | | | # zaokrouhlime interval na deset sekund |
82 | | | interval=$(( $start_time / 10 * 10 + 10 )) |
83 | | | |
84 | | | conntrack_file=/tmp/conntrack_file |
85 | | | |
86 | | | while [ true ] ; do |
87 | | | |
88 | | | create_db |
89 | | | |
90 | | | wait_to $interval |
91 | | | |
92 | | | logfile="$stat_dir/mpstat" |
93 | | | timenow=`date +%s` |
94 | | | cpuline=`mpstat -P 0 1 3 | tail -2 | head -1` |
95 | | | # echo $cpuline |
96 | | | user_time=`echo $cpuline | awk '{print $4;}'` |
97 | | | nice_time=`echo $cpuline | awk '{print $5;}'` |
98 | | | system_time=`echo $cpuline | awk '{print $6;}'` |
99 | | | iowait_time=`echo $cpuline | awk '{print $7;}'` |
100 | | | irq_time=`echo $cpuline | awk '{print $8;}'` |
101 | | | softirq_time=`echo $cpuline | awk '{print $9;}'` |
102 | | | idle_time=`echo $cpuline | awk '{print $10;}'` |
103 | | | intrs=`echo $cpuline | awk '{print $11;}'` |
104 | | | |
105 | | | echo $interval $user_time $nice_time $system_time $iowait_time $irq_time $softirq_time $idle_time $intrs >>/var/log/statistiky/system.log |
106 | | | rrdtool update mpstat.rrd -t user:nice:system:iowait:irq:softirq:idle:intrs $interval:$user_time:$nice_time:$system_time:$iowait_time:$irq_time:$softirq_time:$idle_time:$intrs |
107 | | | |
108 | | | cp /proc/net/ip_conntrack $conntrack_file |
109 | | | tcp_syn_sent=`grep ^tcp.*SYN_SENT.* <$conntrack_file | wc -l` |
110 | | | tcp_syn_recv=`grep ^tcp.*SYN_RECV.* <$conntrack_file | wc -l` |
111 | | | tcp_established=`grep ^tcp.*ESTABLISHED.* <$conntrack_file | wc -l` |
112 | | | tcp_close=`grep ^tcp.*CLOSE.* <$conntrack_file | wc -l` |
113 | | | tcp_close_wait=`grep ^tcp.*CLOSE_WAIT.* <$conntrack_file | wc -l` |
114 | | | tcp_fin_wait=`grep ^tcp.*FIN_WAIT.* <$conntrack_file | wc -l` |
115 | | | tcp_time_wait=`grep ^tcp.*TIME_WAIT.* <$conntrack_file | wc -l` |
116 | | | udp=`grep ^udp.* <$conntrack_file | wc -l` |
117 | | | rm -f $conntrack_file |
118 | | | |
119 | | | ip_conntrack_max=`cat /proc/sys/net/ipv4/netfilter/ip_conntrack_max` |
120 | | | |
121 | | | echo $interval $tcp_syn_sent $tcp_syn_recv $tcp_established $tcp_close $tcp_close_wait $tcp_fin_wait $tcp_time_wait $udp >>/var/log/statistiky/conntrack.log |
122 | | | rrdtool update conntrack.rrd -t tcp_syn_sent:tcp_syn_recv:tcp_established:tcp_close:tcp_close_wait:tcp_fin_wait:tcp_time_wait:udp:ip_conntrack_max \ |
123 | | | $interval:$tcp_syn_sent:$tcp_syn_recv:$tcp_established:$tcp_close:$tcp_close_wait:$tcp_fin_wait:$tcp_time_wait:$udp:$ip_conntrack_max 2>>/var/log/statistiky/conntrack.err |
124 | | | echo "rrdtool update conntrack.rrd -t tcp_syn_sent:tcp_syn_recv:tcp_established:tcp_close:tcp_close_wait:tcp_fin_wait:tcp_time_wait:udp:ip_conntrack_max $interval:$tcp_syn_sent:$tcp_syn_recv:$tcp_established:$tcp_close:$tcp_close_wait:$tcp_fin_wait:$tcp_time_wait:$udp:$ip_conntrack_max" >>/var/log/statistiky/conntrack.history |
125 | | | |
126 | | | interval=$(( $interval + 60 )) |
127 | | | |
128 | | | done |