Ticket #3: munin-cgi-graph_fastcgi.2.diff
| File munin-cgi-graph_fastcgi.2.diff, 11.8 kB (added by do, 4 years ago) |
|---|
-
munin-cgi-graph.in
old new 1 #! @@PERL@@-Tw1 #!/usr/bin/perl -Tw 2 2 # -*- perl -*- 3 3 # 4 4 # Copyright (C) 2004 Jimmy Olsen … … 15 15 # 16 16 # You should have received a copy of the GNU General Public License 17 17 # along with this program; if not, write to the Free Software 18 # Foundation, Inc., 5 1 Franklin Street, Fifth Floor, Boston, MA 02110-1301USA.18 # Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. 19 19 # 20 20 # $Id$ 21 21 # … … 30 30 use Date::Manip; 31 31 use POSIX qw(strftime); 32 32 use IPC::SysV qw(IPC_CREAT); 33 use CGI::Fast; 33 34 34 my $GRAPHER = " @@LIBDIR@@/munin-graph";35 my $conffile = " @@CONFDIR@@/munin.conf";35 my $GRAPHER = "/usr/share/munin/munin-graph"; 36 my $conffile = "/etc/munin/munin.conf"; 36 37 37 38 my %TIMES = ( "day" => ["--noweek", "--nomonth", "--noyear", "--nosumweek", "--nosumyear"], 38 39 "week" => ["--noday", "--nomonth", "--noyear", "--nosumweek", "--nosumyear"], … … 40 41 "year" => ["--noday", "--noweek", "--nomonth", "--nosumweek", "--nosumyear"], 41 42 "week-sum" => ["--noday", "--nomonth", "--noyear", "--noweek", "--nosumyear"], 42 43 "year-sum" => ["--noday", "--noweek", "--nomonth", "--nosumweek", "--noyear"] 43 );44 ); 44 45 45 46 my %period = ( "day" => 300, 46 47 "week" => 1800, … … 48 49 "year" => 86400, 49 50 "week-sum" => 1800, 50 51 "year-sum" => 86400 51 );52 ); 52 53 53 54 my $log = new IO::Handle; 54 55 my $scale = "day"; … … 56 57 my $serv = ""; 57 58 my $dom = ""; 58 59 my $lock = ""; 59 my $IPC_KEY = 89340;60 my $IPC_KEY = 9340; 60 61 61 62 my $config = &munin_readconfig ($conffile); 62 63 63 my $path = $ENV{PATH_INFO} || ""; 64 $path =~ s/^\///; 65 ($dom, $host, $serv) = split /\//, $path; 66 ($serv, $scale) = split /-/, $serv, 2; 67 $scale =~ s/\.png$//; 64 while (new CGI::Fast) 65 { 66 my $path = $ENV{PATH_INFO} || ""; 67 $path =~ s/^\///; 68 ($dom, $host, $serv) = split /\//, $path; 69 ($serv, $scale) = split /-/, $serv, 2; 70 $scale =~ s/\.png$//; 68 71 69 &verify_parameters ($dom, $host, $serv, $scale);72 &verify_parameters ($dom, $host, $serv, $scale); 70 73 71 my $filename = get_picture_filename ($config, $dom, $host, $serv, $scale);74 my $filename = get_picture_filename ($config, $dom, $host, $serv, $scale); 72 75 73 my $time = time;76 my $time = time; 74 77 75 if (-f $filename)76 {77 my @sstats = stat ($filename);78 my $slast_modified = strftime ("%a, %d %b %Y %H:%M:%S %Z", localtime ($sstats[9]));78 if (-f $filename) 79 { 80 my @sstats = stat ($filename); 81 my $slast_modified = strftime ("%a, %d %b %Y %H:%M:%S %Z", localtime ($sstats[9])); 79 82 80 if (defined $ENV{HTTP_IF_MODIFIED_SINCE} and 81 !&modified (gmtime(time+($period{$scale}-($time%$period{$scale}))), 82 $sstats[9]-1)) { 83 print "Status: 304\n"; 83 if (defined $ENV{HTTP_IF_MODIFIED_SINCE} and 84 !&modified (gmtime(time+($period{$scale}-($time%$period{$scale}))), 85 $sstats[9]-1)) { 86 print "Status: 304\n"; 87 print "Content-Type: image/png\n"; 88 print "Expires: ", strftime ("%a, %d %b %Y %H:%M:%S GMT", gmtime(time+($period{$scale}-($time%$period{$scale})))), "\n"; 89 print "Last-Modified: $slast_modified\n"; 90 print "\n"; 91 exit 0; 92 } 93 } 94 95 if (! &graph_usable ($filename, $time)) 96 { 97 my $ret = (&draw_graph ($host, $serv, $TIMES{$scale}) || "Unknown error"); 98 if (! -f $filename) 99 { 100 ::logger ("Warning: Could not draw graph \"$host-$serv-$scale.png\": $ret"); 101 print "Status: 500\n"; 102 print "Content-Type: image/png\n"; 103 print "\n"; 104 exit 0; 105 } 106 } 107 108 my @stats = stat ($filename); 109 my $last_modified = strftime ("%a, %d %b %Y %H:%M:%S %Z", localtime ($stats[9])); 110 84 111 print "Content-Type: image/png\n"; 85 112 print "Expires: ", strftime ("%a, %d %b %Y %H:%M:%S GMT", gmtime(time+($period{$scale}-($time%$period{$scale})))), "\n"; 86 print "Last-Modified: $ slast_modified\n";113 print "Last-Modified: $last_modified\n"; 87 114 print "\n"; 88 exit 0;89 }90 }91 115 92 if (! &graph_usable ($filename, $time))93 {94 my $ret = (&draw_graph ($host, $serv, $TIMES{$scale}) || "Unknown error");95 if (! -f $filename)96 {97 ::logger ("Warning: Could not draw graph \"$host-$serv-$scale.png\": $ret");98 print "Status: 500\n";99 print "Content-Type: image/png\n";100 print "\n";101 exit 0;102 }103 }104 105 my @stats = stat ($filename);106 my $last_modified = strftime ("%a, %d %b %Y %H:%M:%S %Z", localtime ($stats[9]));107 108 print "Content-Type: image/png\n";109 print "Expires: ", strftime ("%a, %d %b %Y %H:%M:%S GMT", gmtime(time+($period{$scale}-($time%$period{$scale})))), "\n";110 print "Last-Modified: $last_modified\n";111 print "\n";112 113 116 # Try to police the number of concurrent rrdgraph instances. The 114 117 # third value is the default maximum. 115 118 116 119 # Fox kindly submitted a patch to convert to SysV IPC semaphores. 117 120 # Lovely! (ticket #499). 118 121 119 my $max_cgi_graph_jobs = &munin_get ($config, "max_cgi_graph_jobs" , 6, $dom);122 my $max_cgi_graph_jobs = &munin_get ($config, "max_cgi_graph_jobs" , 6, $dom); 120 123 121 my $opstring;124 my $opstring; 122 125 123 126 # Get semaphore handle 124 my $semid = semget($IPC_KEY, 0, 0 );127 my $semid = semget($IPC_KEY, 0, 0 ); 125 128 126 if(!$semid) {127 # Or create it if needed128 $semid = semget($IPC_KEY, 1 , 0666 |IPC_CREAT ) ||129 die "Creating semaphore: $!";129 if(!$semid) { 130 # Or create it if needed 131 $semid = semget($IPC_KEY, 1 , 0666 | &IPC_CREAT ) || 132 die "Creating semaphore: $!"; 130 133 131 # And initialize to max_cgi_graph_jobs132 $opstring = pack("s!s!s!",0, $max_cgi_graph_jobs,0);133 semop($semid,$opstring) || die "$!";134 }134 # And initialize to max_cgi_graph_jobs 135 $opstring = pack("s!s!s!",0, $max_cgi_graph_jobs,0); 136 semop($semid,$opstring) || die "$!"; 137 } 135 138 136 139 # Decrement, or lock/hang/yield if already 0 137 $opstring = pack("s!s!s!",0, -1, 0);138 semop($semid,$opstring);140 $opstring = pack("s!s!s!",0, -1, 0); 141 semop($semid,$opstring); 139 142 140 &graph ($filename);143 &graph ($filename); 141 144 142 145 # Increment (and release waiting processes) 143 $opstring = pack("s!s!s!",0, 1, 0);144 semop($semid,$opstring);145 146 $opstring = pack("s!s!s!",0, 1, 0); 147 semop($semid,$opstring); 148 } 146 149 sub graph { 147 my $filename = shift;150 my $filename = shift; 148 151 149 open (GRAPH, $filename) or die "Warning: Could not open picture file \"$filename\" for reading: $!\n";150 print while (<GRAPH>);151 close (GRAPH);152 open (GRAPH, $filename) or die "Warning: Could not open picture file \"$filename\" for reading: $!\n"; 153 print while (<GRAPH>); 154 close (GRAPH); 152 155 } 153 156 154 157 sub get_picture_filename { 155 my $config = shift;156 my $domain = shift;157 my $name = shift;158 my $service = shift;159 my $scale = shift;158 my $config = shift; 159 my $domain = shift; 160 my $name = shift; 161 my $service = shift; 162 my $scale = shift; 160 163 161 return "$config->{'htmldir'}/$domain/$name-$service-$scale.png";164 return "$config->{'htmldir'}/$domain/$name-$service-$scale.png"; 162 165 } 163 166 164 167 sub logger_open { 165 my $dirname = shift;168 my $dirname = shift; 166 169 167 if (!$log->opened) 168 { 169 unless (open ($log, ">>$dirname/munin-cgi-graph.log")) 170 if (!$log->opened) 170 171 { 171 print STDERR "Warning: Could not open log file \"$dirname/munin-cgi-graph.log\" for writing: $!"; 172 unless (open ($log, ">>$dirname/munin-cgi-graph.log")) 173 { 174 print STDERR "Warning: Could not open log file \"$dirname/munin-cgi-graph.log\" for writing: $!"; 175 } 172 176 } 173 }174 177 } 175 178 176 179 sub logger { 177 my ($comment) = @_;178 my $now = strftime ("%b %d %H:%M:%S", localtime);180 my ($comment) = @_; 181 my $now = strftime ("%b %d %H:%M:%S", localtime); 179 182 180 if ($log->opened)181 {182 print $log "$now - $comment\n";183 }184 else185 {186 if (defined $config->{logdir})187 {188 if (open ($log, ">>$config->{logdir}/munin-cgi-graph.log"))189 {190 print $log "$now - $comment\n";191 }192 else193 {194 print STDERR "Warning: Could not open log file \"$config->{logdir}/munin-cgi-graph.log\" for wr195 iting: $!";196 print STDERR "$now - $comment\n";197 }198 }199 else200 {201 print STDERR "$now - $comment\n";202 }203 }183 if ($log->opened) 184 { 185 print $log "$now - $comment\n"; 186 } 187 else 188 { 189 if (defined $config->{logdir}) 190 { 191 if (open ($log, ">>$config->{logdir}/munin-cgi-graph.log")) 192 { 193 print $log "$now - $comment\n"; 194 } 195 else 196 { 197 print STDERR "Warning: Could not open log file \"$config->{logdir}/munin-cgi-graph.log\" for wr 198 iting: $!"; 199 print STDERR "$now - $comment\n"; 200 } 201 } 202 else 203 { 204 print STDERR "$now - $comment\n"; 205 } 206 } 204 207 } 205 208 206 209 … … 214 217 if (!$dom) 215 218 { 216 219 print STDERR "Warning: Request for graph without specifying domain. Bailing out.\n"; 217 exit 1;220 return 0; 218 221 } 219 222 if (!$host) 220 223 { 221 224 print STDERR "Warning: Request for graph without specifying host. Bailing out.\n"; 222 exit 1;225 return 0; 223 226 } 224 227 if (!$serv) 225 228 { 226 229 print STDERR "Warning: Request for graph without specifying service. Bailing out.\n"; 227 exit 1;230 return 0; 228 231 } 229 232 230 233 if (!$scale) 231 234 { 232 235 print STDERR "Warning: Request for graph without specifying scale. Bailing out.\n"; 233 exit 1;236 return 0; 234 237 } 235 238 else 236 239 { 237 240 if (!defined $TIMES{$scale}) 238 241 { 239 242 print STDERR "Warning: Weird scale setting \"$scale\". Bailing out.\n"; 240 exit 1;243 return 0; 241 244 } 242 245 } 246 return 1; 243 247 } 244 248 245 249 sub graph_usable { 246 my $filename = shift;247 my $time = shift;250 my $filename = shift; 251 my $time = shift; 248 252 249 if (-f $filename) {250 my @stats = stat (_);251 my $expire = ($stats[9] - $time%$period{$scale}+$period{$scale})-$time;253 if (-f $filename) { 254 my @stats = stat (_); 255 my $expire = ($stats[9] - $time%$period{$scale}+$period{$scale})-$time; 252 256 #print STDERR "Expires in: $expire\n"; 253 257 254 if ($expire >= 0) {258 if ($expire >= 0) { 255 259 #print STDERR "Skipping munin-graph-run for \"$filename\".\n"; 256 260 #print STDERR ("Graph unexpired for $scale. ($stats[9] , $time, ". ($time%$period{$scale}). ", ". ($time - $time%$period{$scale}). ").\n"); 257 return 1;258 } else {261 return 1; 262 } else { 259 263 #print STDERR ("Graph expired for $scale. ($stats[9] , $time, ". ($time%$period{$scale}). ", ". ($time - $time%$period{$scale}). ").\n"); 260 return 0; 264 return 0; 265 } 261 266 } 262 } 263 return 0; 267 return 0; 264 268 } 265 269 266 270 sub draw_graph { 267 my $host = shift;268 my $serv = shift;269 my $scale = shift;271 my $host = shift; 272 my $serv = shift; 273 my $scale = shift; 270 274 271 $serv =~ s/[^\w_\/"'\[\]\(\)+=-]/_/; $serv =~ /^(.+)$/; $serv = $1; #"272 $host =~ s/[^\w_\/"'\[\]\(\)+=-]/_/; $host =~ /^(.+)$/; $host = $1; #"275 $serv =~ s/[^\w_\/"'\[\]\(\)+=-]/_/; $serv =~ /^(.+)$/; $serv = $1; #" 276 $host =~ s/[^\w_\/"'\[\]\(\)+=-]/_/; $host =~ /^(.+)$/; $host = $1; #" 273 277 274 my @params = ($GRAPHER);275 push @params, @$scale;276 push @params, "--skip-locking", "--skip-stats", "--nolazy";277 push @params, "--host", $host, "--service", $serv;278 push @params, "STDERR>&STDOUT";278 my @params = ($GRAPHER); 279 push @params, @$scale; 280 push @params, "--skip-locking", "--skip-stats", "--nolazy"; 281 push @params, "--host", $host, "--service", $serv; 282 push @params, "STDERR>&STDOUT"; 279 283 280 my $file = "/dev/null";281 unless (open (IN, "-|"))282 {283 %ENV=();284 exec @params;285 }286 $file = join (' ', <IN>);287 close (IN);288 return $file;284 my $file = "/dev/null"; 285 unless (open (IN, "-|")) 286 { 287 %ENV=(); 288 exec @params; 289 } 290 $file = join (' ', <IN>); 291 close (IN); 292 return $file; 289 293 } 290 294 291 295 sub modified { 292 296 # Format of since_string If-Modified-Since: Wed, 23 Jun 2004 16:11:06 GMT 293 297 294 my $since_string = shift;295 my $created = shift;296 my $ifmodsec = &UnixDate (&ParseDateString ($since_string), "%s");298 my $since_string = shift; 299 my $created = shift; 300 my $ifmodsec = &UnixDate (&ParseDateString ($since_string), "%s"); 297 301 298 return 1 if ($ifmodsec < $created);299 return 0;302 return 1 if ($ifmodsec < $created); 303 return 0; 300 304 } 301 305 302 306 # vim: syntax=perl ts=8
