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