Imported Upstream version 2.6.0
[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 # 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 amlibexecdir="@amlibexecdir@"
43 . "${amlibexecdir}/amanda-sh-lib.sh"
44  
45 confdir=@CONFIG_DIR@
46  
47 # add sbin and ucb dirs
48 PATH="$PATH:/usr/sbin:/sbin:/usr/ucb"
49 export PATH
50
51 # we use a different version of the compression variable than amanda itself.
52 COMPRESS=@AMPLOT_COMPRESS@
53
54 # Function to check that awk can do command-line variable
55 # substitution.  If no, then exit; if yes, set $AVARFLAG
56 # to the commandline switch used to introduce a variable. This
57 # check used to be performed at build time in configure; it's
58 # now performed at runtime.
59 test_awk() {
60         local tmpfile result
61         tmpfile=`mktemp /tmp/amplot.XXXXXX`
62         echo 'BEGIN{print i; exit}' > ${tmpfile}
63         result=`$AWK -f ${tmpfile} i=xx | wc -c`
64         if test "$result" -le 1; then
65                 result=`$AWK -f ${tmpfile} -v i=xx | wc -c`
66                 if test "$result" -le 1; then
67                         echo "$AWK does not support command-line variable assignment; amplot cannot run" >&2
68                         rm -fr $tmpfile
69                         exit 1
70                 else
71                         AVARFLAG=-v
72                 fi
73         else
74                 AVARFLAG=''
75         fi
76
77         rm -fr $tmpfile
78 }
79
80 # Function to search for gnuplot and ensure it's working.  This
81 # first tries the location detected/configured when amanda was built,
82 # then tries 'gnuplot', assuming it's in the user's path.  If no
83 # working gnuplot executable is found, it exits with an error.  The
84 # variable $GNUPLOT is set to the resulting executable.
85 find_gnuplot() {
86         if test "x$GNUPLOT" = "x"; then
87                 # look for it in the user's PATH
88                 GNUPLOT=gnuplot
89         fi
90
91         if ${GNUPLOT} --version 2>/dev/null | grep '^gnuplot' >/dev/null; then
92                 : # looks OK
93         else
94                 echo "${GNUPLOT} was not found; amplot cannot run"
95                 exit 1
96         fi
97 }
98
99 # check our environment, using functions from above
100 test_awk
101 find_gnuplot
102
103 if [ $# -eq 0 ] ; then
104         _ 'Usage: %s [-c] [-e] [-g] [-l] [-p] [-t hours] <amdump_files.[gz,z,Z]>\n' $0
105         _ '%s generates plot for screen with fixed dimensions\n' $0
106         _ '     -c      Compress the input amdump files after plotting\n'
107         _ '     -e      Extends x (time) axes if needed\n'
108         _ '     -g      Run gnuplot directly no postscript file generated DEFAULT\n'
109         _ '     -l      Landscape mode suitable for printing\n'
110         _ '     -p      Postscript output (color)\n'
111         _ '     -b      The postscipt will be b/w\n'
112         _ '     -t T    Set the right edge of the plot to be T hours\n'
113         exit 1 
114 fi
115
116 tmp_files="bandw_free disk_alloc dump_idle finished run_queue tape_* title" 
117
118 my_plot=$amlibexecdir/amplot.g
119 paper=0 
120 gnuplot=1
121 cmpres=0
122 para=""
123 maxtime=4
124 bw=0
125
126 # setting up the parameters to pass to [gn]awk 
127 while :; do 
128    case "$1" in
129    -c)  cmpres=1; shift;;
130    -e)  para=$para"$AVARFLAG extend=1 "; shift;;
131    -g)  gnuplot=1; shift;;
132    -l)  paper=1; para=$para"$AVARFLAG paper=1 "; shift;;
133    -p)  gnuplot=0; shift;;
134    -b)  bw=1; shift;;
135    -t)  shift
136         if test "$#" -eq 0; then
137             _ '%s: no argument for -t option\n' $0 1>&2
138             exit 5
139         fi
140         maxtime="$1"; shift;;
141    *) break;;
142    esac
143 done
144 if [ $# -eq 0 ] ; then 
145         _ '%s: no input files\n' $0 1>&2
146         exit 5
147 fi
148 para=$para"$AVARFLAG maxtime=$maxtime"
149
150 if [ $gnuplot  -eq 1 ] ; then
151         my_plot=$my_plot"p"             # use the plot prog that pauses
152         plot=" -geometry 800x700+40+0" 
153         para=$para"$AVARFLAG gnuplot=1 "
154         _ "Displaying graph on the screen, <CR> for next graph"
155
156         if [ "$paper" -eq 1 ] ; then
157                 _ '%s: -l requires -p flag at the same time\n' $0 1>&2
158                 exit 6 
159         fi
160         if [ "$bw" -eq 1 ] ; then
161                 _ '%s: -b requires -p flag at the same time\n' $0 1>&2
162                 exit 6 
163         fi
164 fi
165
166 if [ $bw -eq 1 ]; then
167         para=$para" bw=1"
168 fi
169
170 list="";                # files to compress at the end
171
172 for i in ${1+"$@"}              # for all the input files
173 do
174         f="$i";
175         if [ ! -f "$f" ] ; then 
176                 f=`ls "$i" "$i".*[zZ] 2>/dev/null`
177         fi
178         if [ -f "$f" ] ; then           # found file 
179                 disp=`$AWK -f $amlibexecdir/amcat.awk $AVARFLAG f="$f"`
180                 if [ -z "$disp" ] ; then 
181                         _ 'Do not know how to [gz|z]cat this file\n'
182                 else
183                         /bin/rm -f $tmp_files 
184                         $disp "$f" | $AWK -f $amlibexecdir/amplot.awk $para
185                         $GNUPLOT $plot $my_plot
186                         if [ $disp = "cat" -a  $cmpres -eq 1 ] ; then
187                                 list=$list" "$f
188                         fi
189                 fi
190         else                            # check if file has been compressed
191                 _ 'No such file %s or %s\n' "$i" "$i.*[zZ]"
192         fi
193 done
194
195 /bin/rm -f $tmp_files 
196
197 if [ "$list" != "" ] ; then             # now compress the files we worked on
198 # comment out next line if you do not want compression at the end
199         _ 'Compressing %s\n' "$list"
200         $COMPRESS $list
201 fi
202 exit 0