File: mini-graph.cgi

package info (click to toggle)
cricket 0.70-2
  • links: PTS
  • area: main
  • in suites: potato
  • size: 868 kB
  • ctags: 229
  • sloc: perl: 4,352; ansic: 246; sh: 107; makefile: 96
file content (146 lines) | stat: -rwxr-xr-x 3,569 bytes parent folder | download
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
#!/usr/bin/perl -w 
# -*- perl -*-

# Cricket: a configuration, polling and data display wrapper for RRD files
#
#    Copyright (C) 1998 Jeff R. Allen and WebTV Networks, Inc.
#
#    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; either version 2 of the License, or
#    (at your option) any later version.
#
#    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., 675 Mass Ave, Cambridge, MA 02139, USA.

BEGIN {
	# This magic attempts to guess the install directory based
	# on how the script was called. If it fails for you, just
	# hardcode it.

#	$gInstallRoot = (($0 =~ m:^(.*/):)[0] || './') . '.';
	$gInstallRoot = '/usr/share/cricket';

	# cached images are stored here... there will be no more than
	# 5 minutes worth of images, so it won't take too much space.
	# If you leave it unset, the default (/tmp or c:\temp) will probably
	# be OK.
	# $gCacheDir = "/path/to/cache";
}

use lib "$gInstallRoot/lib";

use CGI qw(fatalsToBrowser);
use MD5;

use Common::Log;
Common::Log::setLevel('warn');

# cache cleaning params
#
$gPollingInterval = 5 * 60;     # defaults to 5 minutes

$main::gQ = new CGI;

initOS();
doGraph();

# set the cache dir if necessary, and fix up the ConfigRoot if
# we are not on Win32 (where home is undefined)

sub initOS {
	if ($^O eq 'MSWin32') {
		if (! defined($main::gCacheDir)) {
			$main::gCacheDir = 'c:\temp\cricket-cache';
			$main::gCacheDir = "$ENV{'TEMP'}\\cricket-cache"
				if (defined($ENV{'TEMP'}));
		}
	} else {
		if (! defined($main::gCacheDir)) {
			$main::gCacheDir = '/tmp/cricket-cache';
			$main::gCacheDir = "$ENV{'TMPDIR'}/cricket-cache"
				if (defined($ENV{'TMPDIR'}));
		}
	}
}

sub doGraph {
	my($imageName) = generateImageName($main::gQ);

	# check the image's existance (i.e. no error from stat()) and age

	my($mtime);
	if (defined($imageName)) {
		$mtime = (stat($imageName))[9];
	}

	if (!defined($mtime) || ((time() - $mtime) > $main::gPollingInterval)) {
		# this request is actually going to need work... pass it on
		exec("$gInstallRoot/grapher.cgi");
	} else {
		Debug("Cached image exists. Using that.");
		sprayGif($imageName);
	}
}

sub tryGif {
	my($gif) = @_;
	
	# we need to make certain there are no buffering problems here.
	local($|) = 1;
	
	if (! open(GIF, "<$gif")) { 
		return;
	} else { 
		my($stuff, $len); 
		binmode(GIF);
		while ($len = read(GIF, $stuff, 8192)) { 
			print $stuff; 
		} 
		close(GIF); 
	}
	return 1;
}

sub sprayGif {
	my($gif) = @_;

	print $main::gQ->header('image/gif');

	if (! tryGif($gif)) {
		Warn("Could not open $gif: $!");
		if (! tryGif("/var/www/cricket/images/failed.gif")) {
			Warn("Could not send failure gif: $!");
			return;
		}
	}

	return 1;
}

sub generateImageName {
	my($q) = @_;
	my($param, $md5);

	$md5 = new MD5;

	foreach $param ($q->param()) {
		next if ($param eq 'rand');
        if ($param eq 'cache') {
            if (lc($q->param($param)) eq 'no') {
                return;
            }
        }
		$md5->add($param, $q->param($param));
	}
	my($hash) = unpack("H8", $md5->digest());

	return "$main::gCacheDir/cricket-$hash.gif";
}