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