Removed /usr/local from CDPATH
[clearscm.git] / clearadm / plotstorage.cgi
1 #!/usr/local/bin/perl
2
3 =pod
4
5 =head1 NAME $RCSfile: plotstorage.cgi,v $
6
7 Plot Clearcse Storage usage
8
9 =head1 VERSION
10
11 =over
12
13 =item Author
14
15 Andrew DeFaria <Andrew@ClearSCM.com>
16
17 =item Revision
18
19 $Revision: 1.13 $
20
21 =item Created:
22
23 Mon Dec 13 09:13:27 EST 2010
24
25 =item Modified:
26
27 $Date: 2011/01/14 16:37:04 $
28
29 =back
30
31 =head1 SYNOPSIS
32
33  Usage plotstorage.cgi: tag=<tag> type=<vob|view> storage=<storage>
34                         [height=<height>] [width=<width>] [color=<color>]
35                         [scaling=<scaling>] [points=<points>] [tiny=<0|1>] 
36
37  Where:
38    <tag>:     Tag of the Clearcase object (vob or view)
39    <type>:    Designates whether <tag> is a vob of a view
40    <storage>: Name of the Clearcase storage pool to plot information for
41    <height>:  Height of chart (Default: 480px - tiny: 40)
42    <width>:   Width of chart (Default: 800px - tiny: 150)
43    <color>:   A GD::Color color value (Default: purple)
44    <scaling>: Currently one of Minute, Hour, Day or Month. Specifies how
45               Clearadm::GetFS will scale the data returned (Default: Minute 
46               - tiny: Day)
47    <points>:  Number of points to plot (Default: all points - tiny: 7)
48    
49 =head1 DESCRIPTION
50
51 Draws a chart of the storage usage for the Clearcase object (vob|view).
52 Parameters such as height, width, color, scaling and points can be set 
53 individually though more often the user will just use the web controls to set 
54 them. Defaults produce a nice chart. Tiny mode is used by
55 <vob|view>details.cgi to draw tiny charts in the table. Setting tiny sets
56 a number of the other chart options to produce a standard, tiny chart.
57
58 =cut
59
60 use strict;
61 use warnings;
62
63 use FindBin;
64 use Convert::Base64;
65
66 use lib "$FindBin::Bin/lib", "$FindBin::Bin/../lib";
67
68 use Clearadm;
69 use ClearadmWeb;
70 use Clearcase;
71 use Display;
72
73 use CGI qw(:standard :cgi-lib);
74 use GD::Graph::area;
75
76 my %opts = Vars;
77
78 my $VERSION  = '$Revision: 1.13 $';
79   ($VERSION) = ($VERSION =~ /\$Revision: (.*) /);
80
81 $opts{color}  ||= $opts{type} eq 'vob' ? 'purple' : 'marine';
82 $opts{height} ||= 350;
83 $opts{width}  ||= 800;
84
85 if ($opts{tiny}) {
86   $opts{height}  = 40;
87   $opts{width}   = 150;
88   $opts{points}  = 7;
89   $opts{scaling} = 'Day';
90 } # if
91
92 my $clearadm = Clearadm->new;
93
94 my $graph = GD::Graph::area->new($opts{width}, $opts{height});
95
96 graphError "Tag is required"     unless $opts{tag};
97 graphError "Type is required"    unless $opts{type};
98 graphError "Storage is required" unless $opts{storage};
99
100 graphError "Points not numeric (points: $opts{points})"
101   if $opts{points} and $opts{points} !~ /^\d+$/;
102   
103 my @storage = $clearadm->GetStoragePool(
104   $opts{type},
105   $opts{tag},
106   $opts{storage},
107   $opts{region},
108   $opts{start},
109   $opts{end},
110   $opts{points},
111   $opts{scaling}
112 );
113
114 graphError "No data found for $opts{type} $opts{tag} for storage pool $opts{storage}"
115   unless @storage;
116
117 my (@x, @y);
118
119 my $i = 0;
120
121 for (@storage) {
122   $i++;
123   my %storage = %{$_};
124   
125   if ($opts{tiny}) {
126     push @x, '';
127   } else {
128     push @x, $storage{timestamp};
129   } # if
130
131   push @y, $opts{meg} ? $storage{size} / (1024 * 1024) :
132                         $storage{size} / (1024 * 1024 * 12024);
133 } # for
134
135 my @data = ([@x], [@y]);
136
137 my $x_label_skip = @x > 1000 ? 200
138                  : @x > 100  ?  20
139                  : @x > 50   ?   2
140                  : @x > 10   ?   1
141                  : 0;
142                  
143 my $storageLabel = ucfirst $opts{storage};
144 my $x_label = $opts{tiny} ? '' : "$storageLabel Storage";
145 my $y_label = $opts{tiny} ? '' : 
146               $opts{msg}  ? 'Used (Meg)' : 'Used (Gig)';
147 my $title   = $opts{tiny} ? '' : "Storage usage for "
148                                . "$opts{type}:$opts{tag} $storageLabel";
149 my $labelY  = $opts{tiny} ? '' : '%.2f';
150
151 $graph->set(
152   x_label           => $x_label,
153   x_labels_vertical => 1,
154   x_label_skip      => $x_label_skip,
155   x_label_position  => .5,
156   y_label           => $y_label,
157   y_number_format   => $labelY,
158   title             => $title,
159   dclrs             => [$opts{color}],
160   bgclr             => 'white',
161   transparent       => 0,
162   long_ticks        => 1,
163   t_margin          => 5,
164   b_margin          => 5,
165   l_margin          => 5,
166   r_margin          => 5,  
167 ) or graphError $graph->error;
168
169 my $image = $graph->plot(\@data)
170   or croak $graph->error;
171
172 unless ($opts{generate}) {
173   print "Content-type: image/png\n\n";
174   print $image->png;
175 } else {
176   print encode_base64 $image->png;
177 } # unless
178
179 =pod
180
181 =head1 CONFIGURATION AND ENVIRONMENT
182
183 DEBUG: If set then $debug is set to this level.
184
185 VERBOSE: If set then $verbose is set to this level.
186
187 TRACE: If set then $trace is set to this level.
188
189 =head1 DEPENDENCIES
190
191 =head2 Perl Modules
192
193 L<CGI>
194
195 L<FindBin>
196
197 L<Getopt::Long|Getopt::Long>
198
199 L<GD::Graph::area|GD::Graph::area>
200
201 =head2 ClearSCM Perl Modules
202
203 =begin man 
204
205  Clearadm
206  ClearadmWeb
207  Display
208
209 =end man
210
211 =begin html
212
213 <blockquote>
214 <a href="http://clearscm.com/php/scm_man.php?file=clearadm/lib/Clearadm.pm">Clearadm</a><br>
215 <a href="http://clearscm.com/php/scm_man.php?file=clearadm/lib/ClearadmWeb.pm">ClearadmWeb</a><br>
216 <a href="http://clearscm.com/php/scm_man.php?file=lib/Display.pm">Display</a><br>
217 </blockquote>
218
219 =end html
220
221 =head1 BUGS AND LIMITATIONS
222
223 There are no known bugs in this script
224
225 Please report problems to Andrew DeFaria <Andrew@ClearSCM.com>.
226
227 =head1 LICENSE AND COPYRIGHT
228
229 Copyright (c) 2010, ClearSCM, Inc. All rights reserved.
230
231 =cut