Code

Initial revision
[rrdtool-all.git] / website / gallery / alex-01.bash
1 #!/bin/bash
3 # Slightly modified version of my script to check the bill
4 # Alex van den Bogaerdt <alex@ergens.op.Het.Net> , oct 11, 2000
5 #
6 # It is hereby in the public domain.  Use at your own risk.
7 #
8 # Prices, bandwidth etcetera are not fetched from a database
9 # in this example.  You need to modify that anyway.
10 #
11 # To be useful, this script needs access to an RRD which covers
12 # at least two months worth of data, in the correct resolution.
13 #
14 # For an RRA which covers 62 days (2 months), 15 minutes per
15 # sample, 512kbps:
16 #
17 # rrdtool create internet.rrd --step 900
18 #       DS:int1in:COUNTER:1000:0:64000 \
19 #       DS:int1out:COUNTER:1000:0:64000 \
20 #       RRA:AVERAGE:0.5:1:6000
22 exec 2>&1
24 FIXEDk=64
25 LIMITk=512
26 FIXED=$((FIXEDk*1000))
27 LIMIT=$((LIMITk*1000))
29 ACCESSLINE=2585.05
30 FIXEDPRICE=530.00
31 PERinb=9.50
32 PERout=0.95
34 RRD=/home/rrdtool/rrddata/internet.rrd
36 /bin/rm -f /home/rrdtool/rrdpng/internet*.png
38 echo 'Content-Type: text/html'
39 echo ''
41 export DATUM=`/bin/date +date/time:\ %Y%m%d\ %H:%M:%S`
42 export FILENAME=`/bin/date +%s`$$
44 #
45 # Calculate 1st of the month
46 #
47 # Tip, use a script here that can parse QUERY_STRING.  This allows you
48 # to setup some links with varying time spans.  You will be interested
49 # in intermediate results; such as "what would the bill be like if the
50 # traffic would be like this the whole month long".
51 #
52 FIRST=`/bin/date +%Y%m01`
53 LAST=`date -d $FIRST +%s`
54 START=end-1m
56 #
57 # And finally the output:
58 #
59 echo '<HTML><HEAD><TITLE>Internet</TITLE></HEAD>'
60 echo '<BODY text="#FFFFFF" bgcolor="#000000" alink="#FFFF00" vlink="#FF00FF" link="#FFFF00">'
61 echo '<TABLE border=0 align=center width=760 cellpadding=0 cellspacing=0>'
62 echo -n '<TR><TD>'
63 echo 'Our internet provider will bill us for the amount of bandwidth used
64         per month.  Part of this bandwidth is prepaid, the rest is paid for
65         on a kilobit-per-second basis.  Bandwidth used outside the red lines
66         is expensive as this needs to be paid for separately in addition to
67         the fee for the connection itself.'
68 echo '</TD></TR>'
70 echo '<TR><TD><HR></TR>'
72 echo -n '<TR><TD>'
74 # Easy workaround for avoiding caches.  Just alter the filename ...
75 /usr/local/bin/rrdtool graph "/home/rrdtool/rrdpng/internet-b-$FILENAME.png"            \
76         --title="Internet Router, Fixed=$FIXED   $DATUM"                \
77         --end "${LAST}" --start "${START}"                              \
78         --vertical-label "bits per second"                              \
79         --imginfo '<IMG src="/rrdpng/%s" WIDTH="%lu" HEIGHT="%lu">'     \
80         --imgformat PNG                                                 \
81         --y-grid 64000:2                                                \
82         --lower-limit -$LIMIT                                           \
83         --upper-limit  $LIMIT                                           \
84         --color CANVAS#000000                                           \
85         --color BACK#101010                                             \
86         --color FONT#C0C0C0                                             \
87         --color MGRID#80C080                                            \
88         --color GRID#808020                                             \
89         --color FRAME#808080                                            \
90         --color ARROW#FFFFFF                                            \
91         --color SHADEA#404040                                           \
92         --color SHADEB#404040                                           \
93         --height 256                                                    \
94         --width 480                                                     \
95         DEF:bytesinb="$RRD":int1in:AVERAGE                              \
96         DEF:bytesout="$RRD":int1out:AVERAGE                             \
97         CDEF:inb=bytesinb,8,*                                           \
98         CDEF:out=bytesout,8,*                                           \
99         CDEF:Iinb=inb,$FIXED,LE,inb,$FIXED,IF                           \
100         CDEF:Iout=out,$FIXED,LE,out,$FIXED,IF                           \
101         CDEF:Oinb=inb,$FIXED,LE,0,inb,$FIXED,-,IF                       \
102         CDEF:Oout=out,$FIXED,LE,0,out,$FIXED,-,IF                       \
103         CDEF:Pinb=Oinb,$PERinb,*,1000,/                                 \
104         CDEF:Pout=Oout,$PERout,*,1000,/                                 \
105         CDEF:Ptotal=Pinb,Pout,$FIXEDPRICE,$ACCESSLINE,+,+,+             \
106         CDEF:NIinb=Iinb,0,EQ,Iinb,Iinb,-1,*,IF                          \
107         CDEF:NOinb=Oinb,0,EQ,Oinb,Oinb,-1,*,IF                          \
108         HRULE:$FIXED#FF0000:"Outside these lines costs extra per kbps"  \
109         COMMENT:"F $PERout for outgoing, F $PERinb for incoming\n"      \
110         COMMENT:"\n"                                                    \
111         COMMENT:"traffic      "                                         \
112         COMMENT:"Max          Min         Average      UBB tarif  (  price   )    Last\n"\
113         AREA:Iout#00C000:"from HC\:"                                    \
114         STACK:Oout#C0FF00                                               \
115         GPRINT:out:MAX:"%6.2lf %sbps"                                   \
116         GPRINT:out:MIN:"%6.2lf %sbps"                                   \
117         GPRINT:out:AVERAGE:"%6.2lf %sbps"                               \
118         GPRINT:Oout:AVERAGE:"%6.2lf %sbps"                              \
119         GPRINT:Pout:AVERAGE:"(Fl%8.2lf)"                                \
120         GPRINT:out:LAST:"%6.2lf %sbps\\n"                               \
121         AREA:NIinb#0000FF:"into HC\:"                                   \
122         STACK:NOinb#C080FF                                              \
123         GPRINT:inb:MAX:"%6.2lf %sbps"                                   \
124         GPRINT:inb:MIN:"%6.2lf %sbps"                                   \
125         GPRINT:inb:AVERAGE:"%6.2lf %sbps"                               \
126         GPRINT:Oinb:AVERAGE:"%6.2lf %sbps"                              \
127         GPRINT:Pinb:AVERAGE:"(Fl%8.2lf)"                                \
128         GPRINT:inb:LAST:"%6.2lf %sbps\\n"                               \
129         HRULE:$LIMIT#000000:"Access Line                              " \
130         COMMENT:"                    Fl $ACCESSLINE   $LIMITk.00 kbps\n"\
131         HRULE:-$FIXED#FF0000:"Fixed tarif                              "\
132         COMMENT:"                    Fl  $FIXEDPRICE    $FIXEDk.00 kbps\n"\
133         COMMENT:"The bill should read, approximately,                 " \
134         GPRINT:Ptotal:AVERAGE:"         Fl %8.2lf\n"            \
135         | sed 's/[0-9]*x[0-9]*//'
136         echo '</TR></TABLE>'
137 echo '</BODY></HTML>'
138 ----cut here----