Removed /usr/local from CDPATH
[clearscm.git] / clearadm / plotfs.cgi
1 #!/usr/local/bin/perl
2
3 =pod
4
5 =head1 NAME $RCSfile: plotfs.cgi,v $
6
7 Plot Filesystem 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 plotfs.cgi: system=<system> filesytem=<filesystem> 
34                    [height=<height>] [width=<width>] [color=<color>]
35                    [scaling=<scaling>] [points=<points>] [tiny=<0|1>] 
36
37  Where:
38    <system>:     Name of the system defined in the Clearadm database to
39                  retrieve filesystem snapshots for.
40    <filesystem>: Name of the filesytem 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: lblue)
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 filesystem usage for the system and filesystem passed in.
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 systemdetails.cgi to
55 draw tiny charts in the table. Setting tiny sets a number of the other chart
56 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 Display;
71
72 use CGI qw(:standard :cgi-lib);
73 use GD::Graph::area;
74
75 my %opts = Vars;
76
77 my $VERSION  = '$Revision: 1.13 $';
78   ($VERSION) = ($VERSION =~ /\$Revision: (.*) /);
79
80 $opts{color}  ||= 'lblue';
81 $opts{height} ||= 350;
82 $opts{width}  ||= 800;
83
84 if ($opts{tiny}) {
85   $opts{height}  = 40;
86   $opts{width}   = 150;
87   $opts{points}  = 7;
88   $opts{scaling} = 'Day';
89 } # if
90
91 my $clearadm = Clearadm->new;
92
93 my $graph = GD::Graph::area->new($opts{width}, $opts{height});
94
95 graphError "System is required"
96   unless $opts{system};
97   
98 graphError "Filesystem is required"
99   unless $opts{filesystem};
100
101 graphError "Points not numeric (points: $opts{points})"
102   if $opts{points} and $opts{points} !~ /^\d+$/;
103   
104 my @fs = $clearadm->GetFS(
105   $opts{system},
106   $opts{filesystem},
107   $opts{start},
108   $opts{end},
109   $opts{points},
110   $opts{scaling}
111 );
112
113 graphError "No data found for $opts{system}:$opts{filesystem}"
114   unless @fs;
115
116 my (@x, @y);
117
118 my $i = 0;
119
120 for (@fs) {
121   $i++;
122   my %fs = %{$_};
123   
124   if ($opts{tiny}) {
125     push @x, '';
126   } else {
127     push @x, $fs{timestamp};
128   } # if
129
130   push @y, $opts{meg} ? $fs{used} / (1024 * 1024) :
131                         $fs{used} / (1024 * 1024 * 12024);
132 } # for
133
134 my @data = ([@x], [@y]);
135
136 my $x_label_skip = @x > 1000 ? 200
137                  : @x > 100  ?  20
138                  : @x > 50   ?   2
139                  : @x > 10   ?   1
140                  : 0;
141                  
142 my $x_label = $opts{tiny} ? '' : 'Filesystem Usage';
143 my $y_label = $opts{tiny} ? '' : 
144               $opts{msg}  ? 'Used (Meg)' : 'Used (Gig)';
145 my $title   = $opts{tiny} ? '' : "Filesystem usage for "
146                                . "$opts{system}:$opts{filesystem}";
147 my $labelY  = $opts{tiny} ? '' : '%.2f';
148
149 $graph->set(
150   x_label           =>$x_label,
151   x_labels_vertical => 1,
152   x_label_skip      => $x_label_skip,
153   x_label_position  => .5,
154   y_label           => $y_label,
155   y_number_format   => $labelY,
156   title             => $title,
157   dclrs             => [$opts{color}],
158   bgclr             => 'white',
159   transparent       => 0,
160   long_ticks        => 1,
161   t_margin          => 5,
162   b_margin          => 5,
163   l_margin          => 5,
164   r_margin          => 5,  
165 ) or graphError $graph->error;
166
167 my $image = $graph->plot(\@data)
168   or croak $graph->error;
169
170 unless ($opts{generate}) {
171   print "Content-type: image/png\n\n";
172   print $image->png;
173 } else {
174   print encode_base64 $image->png;
175 } # unless
176
177 =pod
178
179 =head1 CONFIGURATION AND ENVIRONMENT
180
181 DEBUG: If set then $debug is set to this level.
182
183 VERBOSE: If set then $verbose is set to this level.
184
185 TRACE: If set then $trace is set to this level.
186
187 =head1 DEPENDENCIES
188
189 =head2 Perl Modules
190
191 L<CGI>
192
193 L<FindBin>
194
195 L<Getopt::Long|Getopt::Long>
196
197 L<GD::Graph::area|GD::Graph::area>
198
199 =head2 ClearSCM Perl Modules
200
201 =begin man 
202
203  Clearadm
204  ClearadmWeb
205  Display
206
207 =end man
208
209 =begin html
210
211 <blockquote>
212 <a href="http://clearscm.com/php/scm_man.php?file=clearadm/lib/Clearadm.pm">Clearadm</a><br>
213 <a href="http://clearscm.com/php/scm_man.php?file=clearadm/lib/ClearadmWeb.pm">ClearadmWeb</a><br>
214 <a href="http://clearscm.com/php/scm_man.php?file=lib/Display.pm">Display</a><br>
215 </blockquote>
216
217 =end html
218
219 =head1 BUGS AND LIMITATIONS
220
221 There are no known bugs in this script
222
223 Please report problems to Andrew DeFaria <Andrew@ClearSCM.com>.
224
225 =head1 LICENSE AND COPYRIGHT
226
227 Copyright (c) 2010, ClearSCM, Inc. All rights reserved.
228
229 =cut