d33afd9337ced3a3a3c41c1aec856dee77d74acb
[clearscm.git] / clearadm / plotloadavg.cgi
1 #!/usr/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
63 use lib "$FindBin::Bin/lib", "$FindBin::Bin/../lib";
64
65 use Clearadm;
66 use ClearadmWeb;
67 use Display;
68
69 use CGI qw (:standard :cgi-lib);
70 use GD::Graph::area;
71
72 my %opts = Vars;
73
74 my $VERSION  = '$Revision: 1.15 $';
75   ($VERSION) = ($VERSION =~ /\$Revision: (.*) /);
76   
77 $opts{color}  ||= 'lyellow';
78 $opts{height} ||= 400;
79 $opts{width}  ||= 800;
80
81 if ($opts{tiny}) {
82   $opts{height}  = 40;
83   $opts{width}   = 150;
84   $opts{points}  = 24;
85   $opts{scaling} = 'Hour';
86 } # if
87
88 sub labelY ($) {
89   my ($value) = @_;
90
91   return $opts{tiny} ? '' : $value;  
92 } # labelY
93
94 my $clearadm = Clearadm->new;
95
96 my $graph = GD::Graph::area->new ($opts{width}, $opts{height});
97
98 graphError "System is required"
99   unless $opts{system};
100   
101 graphError "Points not numeric (points: $opts{points})"
102   if $opts{points} and $opts{points} !~ /^\d+$/;
103
104 my @loads = $clearadm->GetLoadavg (
105   $opts{system},
106   $opts{start},
107   $opts{end},
108   $opts{points},
109   $opts{scaling}
110 );
111
112 graphError "No loadavg data found for system $opts{system}"
113   unless @loads;
114
115 my (@x, @y);
116
117 foreach (@loads) {
118   my %load = %{$_};
119   
120   if ($opts{tiny}) {
121         push @x, '';
122   } else {
123     push @x, $load{timestamp};
124   } # if
125   
126   push @y, $load{loadavg};
127 } # foreach
128
129 my @data = ([@x], [@y]);
130
131 my $x_label_skip = @x > 1000 ? 200
132                  : @x > 100  ?  20
133                  : @x > 50   ?   2
134                  : @x > 10   ?   1
135                  : 0;
136
137 my $x_label = $opts{tiny} ? '' : 'Time';
138 my $y_label = $opts{tiny} ? '' : 'Load';
139 my $title   = $opts{tiny} ? '' : "Load Average for $opts{system}";
140
141 $graph->set (
142   x_label           => $x_label,
143   x_labels_vertical => 1,
144   x_label_skip      => $x_label_skip,
145   x_label_position  => .5,
146   y_label           => $y_label,
147   y_number_format   => &labelY,
148   title             => $title,
149   dclrs             => [$opts{color}],
150   bgclr             => 'white',
151   transparent       => 0,
152   long_ticks        => 1,
153   t_margin          => 5,
154   b_margin          => 5,
155   l_margin          => 5,
156   r_margin          => 5,
157 ) or graphError $graph->error;
158
159 my $image = $graph->plot(\@data)
160   or croak $graph->error;
161
162 print "Content-type: image/png\n\n";
163 print $image->png;
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/cvs_man.php?file=clearadm/lib/Clearadm.pm">Clearadm</a><br>
199 <a href="http://clearscm.com/php/cvs_man.php?file=clearadm/lib/ClearadmWeb.pm">ClearadmWeb</a><br>
200 <a href="http://clearscm.com/php/cvs_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