Merge branch 'collectd-4.3' into collectd-4.4
[collectd.git] / contrib / collection3 / bin / graph.cgi
1 #!/usr/bin/perl
2
3 use strict;
4 use warnings;
5 use lib ('../lib');
6
7 use FindBin ('$RealBin');
8 use Carp (qw(confess cluck));
9 use CGI (':cgi');
10 use RRDs ();
11
12 use Collectd::Graph::TypeLoader (qw(tl_read_config tl_load_type));
13
14 use Collectd::Graph::Common (qw(sanitize_type get_selected_files
15       epoch_to_rfc1123));
16 use Collectd::Graph::Type ();
17
18 our $Debug = param ('debug');
19 our $Begin = param ('begin');
20 our $End = param ('end');
21
22 if ($Debug)
23 {
24   print <<HTTP;
25 Content-Type: text/plain
26
27 HTTP
28 }
29
30 tl_read_config ("$RealBin/../etc/collection3.conf");
31
32 { # Sanitize begin and end times
33   $End ||= 0;
34   $Begin ||= 0;
35
36   if ($End =~ m/\D/)
37   {
38     $End = 0;
39   }
40
41   if (!$Begin || !($Begin =~ m/^-?([1-9][0-9]*)$/))
42   {
43     $Begin = -86400;
44   }
45
46   if ($Begin < 0)
47   {
48     if ($End)
49     {
50       $Begin = $End + $Begin;
51     }
52     else
53     {
54       $Begin = time () + $Begin;
55     }
56   }
57
58   if ($Begin < 0)
59   {
60     $Begin = time () - 86400;
61   }
62
63   if (($End > 0) && ($Begin > $End))
64   {
65     my $temp = $End;
66     $End = $Begin;
67     $Begin = $temp;
68   }
69 }
70
71 my $type = param ('type') or die;
72 my $obj;
73
74 $obj = tl_load_type ($type);
75 if (!$obj)
76 {
77   confess ("tl_load_type ($type) failed");
78 }
79
80 $type = ucfirst (lc ($type));
81 $type =~ s/_([A-Za-z])/\U$1\E/g;
82 $type = sanitize_type ($type);
83
84 my $files = get_selected_files ();
85 if ($Debug)
86 {
87   require Data::Dumper;
88   print STDOUT Data::Dumper->Dump ([$files], ['files']);
89 }
90 for (@$files)
91 {
92   $obj->addFiles ($_);
93 }
94
95 my $expires = time ();
96 if (($End == 0) || (($Begin <= $expires) && ($End >= $expires)))
97 {
98   # 400 == width in pixels
99   my $timespan = $expires - $Begin;
100   $expires += int ($timespan / 400);
101 }
102 elsif (($End > 0) && ($End < $expires))
103 {
104   $expires += 366 * 86400;
105 }
106 elsif ($Begin > $expires)
107 {
108   $expires = $Begin;
109 }
110
111 print STDOUT header (-Content_type => 'image/png',
112   -Last_Modified => epoch_to_rfc1123 ($obj->getLastModified ()),
113   -Expires => epoch_to_rfc1123 ($expires));
114
115 my $args = $obj->getRRDArgs (0);
116
117 if ($Debug)
118 {
119   require Data::Dumper;
120   print STDOUT Data::Dumper->Dump ([$obj], ['obj']);
121   print STDOUT join (",\n", @$args) . "\n";
122   print STDOUT "Last-Modified: " . epoch_to_rfc1123 ($obj->getLastModified ()) . "\n";
123 }
124 else
125 {
126   my @timesel = ();
127
128   if ($End) # $Begin is always true
129   {
130     @timesel = ('-s', $Begin, '-e', $End);
131   }
132   else
133   {
134     @timesel = ('-s', $Begin); # End is implicitely `now'.
135   }
136
137   $| = 1;
138   RRDs::graph ('-', '-a', 'PNG', @timesel, @$args);
139   if (my $err = RRDs::error ())
140   {
141     print STDERR "RRDs::graph failed: $err\n";
142     exit (1);
143   }
144 }
145
146 exit (0);
147
148 # vim: set shiftwidth=2 softtabstop=2 tabstop=8 :