Imported Upstream version 2.5.2p1
[debian/amanda] / amplot / amplot.sh.in
1 #!@SHELL@
2 # Amanda, The Advanced Maryland Automatic Network Disk Archiver
3 # Copyright (c) 1992-1998 University of Maryland at College Park
4 # All Rights Reserved.
5 #
6 # Permission to use, copy, modify, distribute, and sell this software and its
7 # documentation for any purpose is hereby granted without fee, provided that
8 # the above copyright notice appear in all copies and that both that
9 # copyright notice and this permission notice appear in supporting
10 # documentation, and that the name of U.M. not be used in advertising or
11 # publicity pertaining to distribution of the software without specific,
12 # written prior permission.  U.M. makes no representations about the
13 # suitability of this software for any purpose.  It is provided "as is"
14 # without express or implied warranty.
15 #
16 # U.M. DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING ALL
17 # IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO EVENT SHALL U.M.
18 # BE LIABLE FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
19 # WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION
20 # OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN
21 # CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
22 #
23 # Author: Olafur Gudumundsson, (ogud@tis.com)  Trusted Information Systems
24 # Formerly at:            Systems Design and Analysis Group
25 #                         Computer Science Department
26 #                         University of Maryland at College Park
27 #
28 #       Amplot: a program to generate postscript plots of each nights amanda 
29 #       performance 
30
31 #       Author: Olafur Gudmundsson (ogud@tis.com) 
32 #       Creation Date: April 1992 
33 #       Last modified: April 1995 
34 #       Input: list of amdumps 
35 #       Output: Plot of amdump files as either gnuplots on the screen or
36 #               Postscript files 
37 #
38
39 prefix=@prefix@
40 exec_prefix=@exec_prefix@
41 sbindir=@sbindir@
42 libexecdir=@libexecdir@
43  
44 confdir=@CONFIG_DIR@
45  
46 PATH=$sbindir:$libexecdir:/usr/bin:/bin:/usr/sbin:/sbin:/usr/ucb:$PATH
47 export PATH
48
49 AWK=@AWK@
50 GNUPLOT=@GNUPLOT@
51 COMPRESS=@AMPLOT_COMPRESS@
52
53 #+
54 # Function to:
55 #    Simplify gettext usage by allowing printf format
56 #    strings for translators, use _() identifier for
57 #    xgettext extraction similar to "C" usage and
58 #    collapsing "printf `gettext fmt` ...", which is
59 #    used everywhere, into one function.
60 #-
61 _() {
62         fmt="$1"
63         shift
64         printf "$fmt" $*
65 }
66
67 # Function to check that awk can do command-line variable
68 # substitution.  If no, then exit; if yes, set $AVARFLAG
69 # to the commandline switch used to introduce a variable. This
70 # check used to be performed at build time in configure; it's
71 # now performed at runtime.
72 test_awk() {
73         local tmpfile result
74         tmpfile=`mktemp /tmp/amplot.XXXXXX`
75         echo 'BEGIN{print i; exit}' > ${tmpfile}
76         result=`$AWK -f ${tmpfile} i=xx | wc -c`
77         if test "$result" -le 1; then
78                 result=`$AWK -f ${tmpfile} -v i=xx | wc -c`
79                 if test "$result" -le 1; then
80                         echo "$AWK does not support command-line variable assignment; amplot cannot run" >&2
81                         rm -fr $tmpfile
82                         exit 1
83                 else
84                         AVARFLAG=-v
85                 fi
86         else
87                 AVARFLAG=''
88         fi
89
90         rm -fr $tmpfile
91 }
92
93 # Function to search for gnuplot and ensure it's working.  This
94 # first tries the location detected/configured when amanda was built,
95 # then tries 'gnuplot', assuming it's in the user's path.  If no
96 # working gnuplot executable is found, it exits with an error.  The
97 # variable $GNUPLOT is set to the resulting executable.
98 find_gnuplot() {
99         if test "x$GNUPLOT" = "x"; then
100                 # look for it in the user's PATH
101                 GNUPLOT=gnuplot
102         fi
103
104         if ${GNUPLOT} --version 2>/dev/null | grep '^gnuplot' >/dev/null; then
105                 : # looks OK
106         else
107                 echo "${GNUPLOT} was not found; amplot cannot run"
108                 exit 1
109         fi
110 }
111
112 # check our environment, using functions from above
113 test_awk
114 find_gnuplot
115
116 if [ $# -eq 0 ] ; then
117         _ 'Usage: %s [-c] [-e] [-g] [-l] [-p] [-t hours] <amdump_files.[gz,z,Z]>\n' $0
118         _ '%s generates plot for screen with fixed dimensions\n' $0
119         _ '     -c      Compress the input amdump files after plotting\n'
120         _ '     -e      Extends x (time) axes if needed\n'
121         _ '     -g      Run gnuplot directly no postscript file generated DEFAULT\n'
122         _ '     -l      Landscape mode suitable for printing\n'
123         _ '     -p      Postscript output (color)\n'
124         _ '     -b      The postscipt will be b/w\n'
125         _ '     -t T    Set the right edge of the plot to be T hours\n'
126         exit 1 
127 fi
128
129 tmp_files="bandw_free disk_alloc dump_idle finished run_queue tape_* title" 
130
131 my_plot=$libexecdir/amplot.g
132 paper=0 
133 gnuplot=1
134 cmpres=0
135 para=""
136 maxtime=4
137 bw=0
138
139 # setting up the parameters to pass to [gn]awk 
140 while :; do 
141    case "$1" in
142    -c)  cmpres=1; shift;;
143    -e)  para=$para"$AVARFLAG extend=1 "; shift;;
144    -g)  gnuplot=1; shift;;
145    -l)  paper=1; para=$para"$AVARFLAG paper=1 "; shift;;
146    -p)  gnuplot=0; shift;;
147    -b)  bw=1; shift;;
148    -t)  shift
149         if test "$#" -eq 0; then
150             _ '%s: no argument for -t option\n' $0 1>&2
151             exit 5
152         fi
153         maxtime="$1"; shift;;
154    *) break;;
155    esac
156 done
157 if [ $# -eq 0 ] ; then 
158         _ '%s: no input files\n' $0 1>&2
159         exit 5
160 fi
161 para=$para"$AVARFLAG maxtime=$maxtime"
162
163 if [ $gnuplot  -eq 1 ] ; then
164         my_plot=$my_plot"p"             # use the plot prog that pauses
165         plot=" -geometry 800x700+40+0" 
166         para=$para"$AVARFLAG gnuplot=1 "
167         _ "Displaying graph on the screen, <CR> for next graph"
168
169         if [ "$paper" -eq 1 ] ; then
170                 _ '%s: -l requires -p flag at the same time\n' $0 1>&2
171                 exit 6 
172         fi
173         if [ "$bw" -eq 1 ] ; then
174                 _ '%s: -b requires -p flag at the same time\n' $0 1>&2
175                 exit 6 
176         fi
177 fi
178
179 if [ $bw -eq 1 ]; then
180         para=$para" bw=1"
181 fi
182
183 list="";                # files to compress at the end
184
185 for i in ${1+"$@"}              # for all the input files
186 do
187         f="$i";
188         if [ ! -f "$f" ] ; then 
189                 f=`ls "$i" "$i".*[zZ] 2>/dev/null`
190         fi
191         if [ -f "$f" ] ; then           # found file 
192                 disp=`$AWK -f $libexecdir/amcat.awk $AVARFLAG f="$f"`
193                 if [ -z "$disp" ] ; then 
194                         _ 'Do not know how to [gz|z]cat this file\n'
195                 else
196                         /bin/rm -f $tmp_files 
197                         $disp "$f" | $AWK -f $libexecdir/amplot.awk $para
198                         $GNUPLOT $plot $my_plot
199                         if [ $disp = "cat" -a  $cmpres -eq 1 ] ; then
200                                 list=$list" "$f
201                         fi
202                 fi
203         else                            # check if file has been compressed
204                 _ 'No such file %s or %s\n' "$i" "$i.*[zZ]"
205         fi
206 done
207
208 /bin/rm -f $tmp_files 
209
210 if [ "$list" != "" ] ; then             # now compress the files we worked on
211 # comment out next line if you do not want compression at the end
212         _ 'Compressing %s\n' "$list"
213         $COMPRESS $list
214 fi
215 exit 0