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