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