/usr/lib/cgi-bin/munin-fastcgi-graph is in munin 1.4.6-3ubuntu3.4.
This file is owned by root:root, with mode 0o755.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 | #!/usr/bin/perl -Tw
# -*- perl -*-
#
# Copyright (C) 2004-2009 Jimmy Olsen,
#
# This program is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License
# as published by the Free Software Foundation; version 2 dated June,
# 1991.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
#
# $Id: munin-fastcgi-graph.in 3960 2010-10-24 17:38:36Z feiner.tom $
#
#
use RRDs;
use Munin::Master::Utils;
use Munin::Common::Defaults;
use strict;
use IO::Handle;
use Date::Manip;
use POSIX qw(strftime);
use IPC::SysV qw(IPC_CREAT);
use CGI::Fast;
my $GRAPHER = "$Munin::Common::Defaults::MUNIN_LIBDIR/munin-graph";
my $conffile = "$Munin::Common::Defaults::MUNIN_CONFDIR/munin.conf";
my %TIMES = ( "day" => ["--noweek", "--nomonth", "--noyear", "--nosumweek", "--nosumyear"],
"week" => ["--noday", "--nomonth", "--noyear", "--nosumweek", "--nosumyear"],
"month" => ["--noday", "--noweek", "--noyear", "--nosumweek", "--nosumyear"],
"year" => ["--noday", "--noweek", "--nomonth", "--nosumweek", "--nosumyear"],
"week-sum" => ["--noday", "--nomonth", "--noyear", "--noweek", "--nosumyear"],
"year-sum" => ["--noday", "--noweek", "--nomonth", "--nosumweek", "--noyear"]
);
my %period = ( "day" => 300,
"week" => 1800,
"month" => 7200,
"year" => 86400,
"week-sum" => 1800,
"year-sum" => 86400
);
my $log = new IO::Handle;
my $scale = "day";
my $host = "";
my $serv = "";
my $dom = "";
my $lock = "";
my $IPC_KEY = 9340;
# NOTE: You need to restart apache if the config file changes!
my $config = &munin_readconfig ($conffile);
# Get semaphore handle - Before graphing as recommended by snide.
my $semid = undef;
sem_setup();
# BEGIN FAST-CGI LOOP:
while (new CGI::Fast) {
my $path = $ENV{PATH_INFO} || "";
($dom, $host, $serv, $scale) = $path =~ m#^/(.*)/([^/]+)/(\w+)-(\w+)\.png#; ## avoid bug in vim
if (! &verify_parameters ($dom, $host, $serv, $scale)) {
print "Status: 500\n";
print "Content-Type: text/html\n";
print "\n";
print "Invalid parameters!";
next;
}
my $filename = get_picture_filename ($config, $dom, $host, $serv, $scale);
my $time = time;
# If a "Cache-Control: no-cache" header gets send, we regenerate the image in every case:
my $no_cache = defined($ENV{HTTP_CACHE_CONTROL}) && $ENV{HTTP_CACHE_CONTROL} =~ /no-cache/i;
if ($no_cache or (! &graph_usable ($filename, $time) )) {
next unless draw_graph_or_complain($dom, $host, $serv, $TIMES{$scale});
goto draw;
}
# At this time the file exists. But may be old. Or not.
my @stats = stat ($filename);
my $last_modified = strftime ("%a, %d %b %Y %H:%M:%S GMT", gmtime ($stats[9]));
# "Expires" has to use last modified time as base:
my $expires = strftime ("%a, %d %b %Y %H:%M:%S GMT",
gmtime($stats[9]+($period{$scale}-($stats[9]%$period{$scale}))));
# Check for If-Modified-Since and send 304 if not changed:
if (defined $ENV{HTTP_IF_MODIFIED_SINCE} and
!&modified ($ENV{HTTP_IF_MODIFIED_SINCE}, $stats[9]-1)) {
print "Status: 304\n";
print "Content-Type: image/png\n";
print "Expires: ", $expires, "\n";
print "Last-Modified: $last_modified\n";
print "\n";
next;
}
draw:
@stats = stat ($filename); # restat to be sure
$last_modified = strftime ("%a, %d %b %Y %H:%M:%S GMT", gmtime ($stats[9])) unless defined($last_modified);
# "Expires" has to use last modified time as base:
$expires = strftime ("%a, %d %b %Y %H:%M:%S GMT",
gmtime($stats[9]+($period{$scale}-($stats[9]%$period{$scale})))) unless defined ($expires);
print "Status: 200\n";
print "Content-Type: image/png\n";
print "Content-Length: $stats[7]\n";
print "Expires: ", $expires, "\n";
print "Last-Modified: $last_modified\n";
print "\n";
&graph ($filename);
}
# END FAST-CGI LOOP
sub sem_setup {
$semid = semget($IPC_KEY, 0, 0 );
if(!defined($semid)) {
# Or create it if needed
$semid = semget($IPC_KEY, 1 , oct(666) | IPC_CREAT );
die "Creating semaphore: $!" unless defined($semid);
my $max_cgi_graph_jobs = &munin_get ($config, "max_cgi_graph_jobs" , 6, $dom);
# And initialize to max_cgi_graph_jobs
my $opstring = pack("s!s!s!",0, $max_cgi_graph_jobs,0);
semop($semid,$opstring) || die "$!";
}
}
sub sem_get {
# Call this before doing heavy work.
# Decrement, or lock/hang/yield if already 0
my $opstring = pack("s!s!s!",0, -1, 0);
semop($semid,$opstring);
}
sub sem_release {
# Call this after doing heavy work.
# Increment (and release waiting processes).
my $opstring = pack("s!s!s!",0, 1, 0);
semop($semid,$opstring);
}
sub graph {
# Serve the graph contents. This is not heavy, no semaphore.
my $filename = shift;
open (GRAPH_PNG_FILE, '<', $filename) or die "Warning: Could not open picture file \"$filename\" for reading: $!\n";
print while (<GRAPH_PNG_FILE>);
close (GRAPH_PNG_FILE)
}
sub get_picture_filename {
my $config = shift;
my $domain = shift;
my $name = shift;
my $service = shift;
my $scale = shift;
return "$config->{'htmldir'}/$domain/$name/$service-$scale.png";
}
sub logger_open {
my $dirname = shift;
if (!$log->opened)
{
unless (open ($log, '>>', "$dirname/munin-cgi-graph.log"))
{
print STDERR "Warning: Could not open log file \"$dirname/munin-cgi-graph.log\" for writing: $!";
}
}
}
sub logger {
my ($comment) = @_;
my $now = strftime ("%b %d %H:%M:%S", localtime);
if ($log->opened)
{
print $log "$now - $comment\n";
}
else
{
if (defined $config->{logdir})
{
if (open ($log, '>>', "$config->{logdir}/munin-cgi-graph.log"))
{
print $log "$now - $comment\n";
}
else
{
print STDERR "Warning: Could not open log file \"$config->{logdir}/munin-cgi-graph.log\" for wr
iting: $!";
print STDERR "$now - $comment\n";
}
}
else
{
print STDERR "$now - $comment\n";
}
}
}
sub verify_parameters
{
my $dom = shift;
my $host = shift;
my $serv = shift;
my $scale = shift;
if (!$dom)
{
print STDERR "Warning: Request for graph without specifying domain. Bailing out.\n";
return 0;
}
if (!$host)
{
print STDERR "Warning: Request for graph without specifying host. Bailing out.\n";
return 0;
}
if (!$serv)
{
print STDERR "Warning: Request for graph without specifying service. Bailing out.\n";
return 0;
}
if (!$scale)
{
print STDERR "Warning: Request for graph without specifying scale. Bailing out.\n";
return 0;
}
else
{
if (!defined $TIMES{$scale})
{
print STDERR "Warning: Weird scale setting \"$scale\". Bailing out.\n";
return 0;
}
}
return 1;
}
sub graph_usable {
my $filename = shift;
my $time = shift;
if (-f $filename) {
my @stats = stat (_);
# $stats[9] holds the "last update" time and this needs to be in the last update period:
if ($stats[9] > ($time - $time%$period{$scale})) {
#print STDERR "Skipping munin-graph-run for \"$filename\".\n";
#print STDERR ("Graph unexpired for $scale. ($stats[9] , $time, ". ($time%$period{$scale}). ", ". ($time - $time%$period{$scale}). ").\n");
return 1;
} else {
#print STDERR ("Graph expired for $scale. ($stats[9] , $time, ". ($time%$period{$scale}). ", ". ($time - $time%$period{$scale}). ").\n");
return 0;
}
}
return 0;
}
sub draw_graph {
# Draw a new graph - use semaphore to avoid too many concurrent munin-graph calls.
my $dom = shift;
my $host = shift;
my $serv = shift;
my $scale = shift;
$serv =~ s{[^\w_\/"'\[\]\(\)+=-]}{_}g; $serv =~ /^(.+)$/; $serv = $1; #"
# . needs to be legal in host names
$host =~ s{[^\w_\/"'\[\]\(\)\.+=-]}{_}g; $host =~ /^(.+)$/; $host = $1; #"
my $fqn = "root/$dom/$host/$serv";
my @params = ($GRAPHER);
push @params, @$scale;
push @params, "--skip-locking", "--skip-stats", "--nolazy";
push @params, "--list-images";
push @params, "--host", $host, "--only-fqn", $fqn;
push @params, "--no-fork"; # FastCgi forks for us
push @params, "STDERR>&STDOUT";
my $file = "/dev/null";
my $IN;
sem_get();
# Note: This open is an implicit fork
if (! open ($IN, "-|")) {
%ENV=();
exec @params;
}
$file = join (' ', <$IN>);
$file =~ s/\n$//;
close ($IN);
sem_release();
return $file;
}
sub draw_graph_or_complain {
my $ret = draw_graph(@_);
if (! -f $ret) {
::logger ("Warning: Could not draw graph \"$host-$serv-$scale.png\": $ret");
print "Status: 500\n";
print "Content-Type: image/png\n";
print "\n";
return 0;
} else {
return $ret;
}
}
sub modified {
# See if the file has been modified since "the last time".
# Format of since_string If-Modified-Since: Wed, 23 Jun 2004 16:11:06 GMT
my $since_string = shift;
my $created = shift;
my $ifmodsec = &UnixDate (&ParseDateString ($since_string), "%s");
return 1 if ($ifmodsec < $created);
return 0;
}
# vim: syntax=perl ts=8
|