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