Changeset 2734
- Timestamp:
- 10/28/09 01:48:10 (2 years ago)
- Files:
-
- trunk/Makefile (modified) (1 diff)
- trunk/master/_bin/munin-cgi-graph.in (modified) (8 diffs)
- trunk/master/_bin/munin-fastcgi-graph.in (added)
- trunk/master/_bin/munin-graph.in (modified) (4 diffs)
Legend:
- Unmodified
- Added
- Removed
- Modified
- Copied
- Moved
trunk/Makefile
r2725 r2734 101 101 $(INSTALL) -m 0755 build/master/_bin/munin-gather $(LIBDIR)/ 102 102 $(INSTALL) -m 0755 build/master/_bin/munin-cgi-graph $(CGIDIR)/ 103 $(INSTALL) -m 0755 build/master/_bin/munin-fastcgi-graph $(CGIDIR)/ 103 104 104 105 trunk/master/_bin/munin-cgi-graph.in
r2143 r2734 73 73 my $time = time; 74 74 75 if (-f $filename) {76 my @sstats = stat ($filename);77 my $slast_modified = strftime ("%a, %d %b %Y %H:%M:%S %Z", localtime ($sstats[9]));78 79 if (defined $ENV{HTTP_IF_MODIFIED_SINCE} and80 !&modified (gmtime(time+($period{$scale}-($time%$period{$scale}))),81 $sstats[9]-1)) {82 print "Status: 304\n";83 print "Content-Type: image/png\n";84 print "Expires: ", strftime ("%a, %d %b %Y %H:%M:%S GMT", gmtime(time+($period{$scale}-($time%$period{$scale})))), "\n";85 print "Last-Modified: $slast_modified\n";86 print "\n";87 exit 0;88 }89 }90 91 75 # Get semaphore handle - Before graphing as recommended by snide. 92 my $semid = semget($IPC_KEY, 0, 0 ); 93 94 if(!defined($semid)) { 95 # Or create it if needed 96 $semid = semget($IPC_KEY, 1 , oct(666) | IPC_CREAT ); 97 98 die "Creating semaphore: $!" unless defined($semid); 99 100 # And initialize to max_cgi_graph_jobs 101 $opstring = pack("s!s!s!",0, $max_cgi_graph_jobs,0); 102 semop($semid,$opstring) || die "$!"; 103 } 104 105 # Decrement, or lock/hang/yield if already 0 106 $opstring = pack("s!s!s!",0, -1, 0); 107 semop($semid,$opstring); 108 109 if (! &graph_usable ($filename, $time)) 110 { 111 my $ret = (&draw_graph ($host, $serv, $TIMES{$scale}) || "Unknown error"); 112 if (! -f $filename) 113 { 114 ::logger ("Warning: Could not draw graph \"$host-$serv-$scale.png\": $ret"); 115 print "Status: 500\n"; 116 print "Content-Type: image/png\n"; 117 print "\n"; 118 exit 0; 119 } 120 } 121 122 my @stats = stat ($filename); 76 my $semid = undef; 77 78 sem_setup(); 79 80 my $no_cache = defined($ENV{HTTP_CACHE_CONTROL}) && $ENV{HTTP_CACHE_CONTROL} =~ /no-cache/i; 81 82 if ($no_cache or (! &graph_usable($filename,$time) )) { 83 exit 0 unless draw_graph_or_complain($host, $serv, $TIMES{$scale}); 84 goto draw; 85 } 86 87 # At this time the file exists. But may be old. Or not. 88 89 my @stats = stat ($filename); 123 90 my $last_modified = strftime ("%a, %d %b %Y %H:%M:%S %Z", localtime ($stats[9])); 91 # "Expires" has to use last modified time as base: 92 my $expires = strftime ("%a, %d %b %Y %H:%M:%S GMT", 93 gmtime($stats[9]+($period{$scale}-($stats[9]%$period{$scale})))); 94 95 # Check for If-Modified-Since and send 304 if not changed: 96 if (defined $ENV{HTTP_IF_MODIFIED_SINCE} and 97 !&modified ($ENV{HTTP_IF_MODIFIED_SINCE}, $stats[9]-1)) { 98 print "Status: 304\n"; 99 print "Content-Type: image/png\n"; 100 print "Expires: ", $expires, "\n"; 101 print "Last-Modified: $last_modified\n"; 102 print "\n"; 103 exit 0; 104 } 105 106 draw: 107 108 @stats = stat ($filename) unless @stats; 109 110 $last_modified //= strftime ("%a, %d %b %Y %H:%M:%S %Z", localtime ($stats[9])); 111 $expires //= strftime ("%a, %d %b %Y %H:%M:%S GMT", 112 gmtime($stats[9]+($period{$scale}-($stats[9]%$period{$scale})))); 124 113 125 114 print "Content-Type: image/png\n"; … … 128 117 print "\n"; 129 118 130 # Try to police the number of concurrent rrdgraph instances. The131 # third value is the default maximum.132 133 # Fox kindly submitted a patch to convert to SysV IPC semaphores.134 # Lovely! (ticket #499).135 136 my $max_cgi_graph_jobs = &munin_get ($config, "max_cgi_graph_jobs" , 6, $dom);137 138 my $opstring;139 140 # Sending the graph requires I/O but not the amount of CPU that141 # creating the graph requires. So we might consider taking sending142 # the graph out of the semaphore protected area.143 144 119 &graph ($filename); 145 120 146 # Increment (and release waiting processes). 147 $opstring = pack("s!s!s!",0, 1, 0); 148 semop($semid,$opstring); 121 # # # # # # # # # # END OF MAIN 122 123 sub sem_setup { 124 # Try to police the number of concurrent rrdgraph instances. 125 126 # Fox kindly submitted a patch to convert to SysV IPC semaphores. 127 # Lovely! (ticket #499). 128 129 if(!defined($semid)) { 130 semget($IPC_KEY, 0, 0 ); 131 132 # Or create it if needed 133 $semid = semget($IPC_KEY, 1 , oct(666) | IPC_CREAT ); 134 135 die "Creating semaphore: $!" unless defined($semid); 136 137 my $max_cgi_graph_jobs = &munin_get ($config, "max_cgi_graph_jobs" , 6, $dom); 138 139 # And initialize to max_cgi_graph_jobs 140 my $opstring = pack("s!s!s!",0, $max_cgi_graph_jobs,0); 141 semop($semid,$opstring) || die "$!"; 142 } 143 } 144 145 146 sub sem_get { 147 # Call this before doing heavy work. 148 # Decrement, or lock/hang/yield if already 0 149 my $opstring = pack("s!s!s!",0, -1, 0); 150 semop($semid,$opstring); 151 } 152 153 154 sub sem_release { 155 # Call this after doing heavy work. 156 # Increment (and release waiting processes). 157 my $opstring = pack("s!s!s!",0, 1, 0); 158 semop($semid,$opstring); 159 } 160 149 161 150 162 sub graph { 163 # This just serves the file, no file is made. 151 164 my $filename = shift; 152 165 … … 156 169 close ($GRAPH); 157 170 } 171 158 172 159 173 sub get_picture_filename { … … 167 181 } 168 182 183 169 184 sub logger_open { 170 185 my $dirname = shift; … … 178 193 } 179 194 } 195 180 196 181 197 sub logger { … … 248 264 } 249 265 266 250 267 sub graph_usable { 251 my $filename = shift;252 my $time = shift;268 # Check how old the graph is and return 1 if it's new enough and 0 otherwise. 269 my ($filename, $time) = @_; 253 270 254 271 if (-f $filename) { 255 272 my @stats = stat (_); 256 my $expire = ($stats[9] - $time%$period{$scale}+$period{$scale})-$time; 257 #print STDERR "Expires in: $expire\n"; 258 259 if ($expire >= 0) { 260 #print STDERR "Skipping munin-graph-run for \"$filename\".\n"; 273 # $stats[9] holds the "last update" time and this needs to be in the last update period: 274 if ($stats[9] > ($time - $time%$period{$scale})) { 261 275 #print STDERR ("Graph unexpired for $scale. ($stats[9] , $time, ". ($time%$period{$scale}). ", ". ($time - $time%$period{$scale}). ").\n"); 262 276 return 1; … … 269 283 } 270 284 285 271 286 sub draw_graph { 272 287 my $host = shift; … … 284 299 285 300 my $file = "/dev/null"; 286 unless (open (my $IN, "-|")) 287 { 288 %ENV=(); 301 my $IN; 302 sem_get(); 303 304 if (! open ($IN, "-|")) { 305 %ENV=(); 289 306 exec @params; 290 307 } 291 308 $file = join (' ', <$IN>); 309 292 310 close ($IN); 311 sem_release(); 312 293 313 return $file; 294 314 } 295 315 316 317 sub draw_graph_or_complain { 318 my $ret = draw_graph(@_); 319 320 if (! -f $ret) { 321 ::logger ("Warning: Could not draw graph \"$host-$serv-$scale.png\": $ret"); 322 print "Status: 500\n"; 323 print "Content-Type: image/png\n"; 324 print "\n"; 325 return 0; 326 } else { 327 return $ret; 328 } 329 } 330 331 296 332 sub modified { 297 # Format of since_string If-Modified-Since: Wed, 23 Jun 2004 16:11:06 GMT 333 # See if file has been modified since "the last time". 334 335 # Format of since_string If-Modified-Since: Wed, 23 Jun 2004 16:11:06 GMT 298 336 299 337 my $since_string = shift; trunk/master/_bin/munin-graph.in
r2730 r2734 5 5 6 6 Copyright (C) 2002-2009 Jimmy Olsen, Audun Ytterdal, Kjell Magne Ãierud, 7 Nicolai Langfeldt, Linpro AS, Redpill Linpro AS 7 Nicolai Langfeldt, Linpro AS, Redpill Linpro AS and others. 8 8 9 9 This program is free software; you can redistribute it and/or … … 19 19 You should have received a copy of the GNU General Public License 20 20 along with this program; if not, write to the Free Software 21 Foundation, Inc., 5 1 Franklin Street, Fifth Floor, Boston, MA 02110-1301USA.21 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. 22 22 23 23 $Id$ … … 79 79 80 80 # Get options 81 $do_usage=1 unless 81 $do_usage=1 unless 82 82 GetOptions ( "force!" => \$force_graphing, 83 83 "lazy!" => \$force_lazy, … … 903 903 if (my $ERROR = RRDs::error) { 904 904 logger ("Unable to graph ". munin_get_picture_filename ($service, $time) . ": $ERROR"); 905 } elsif ($list_images) { 905 } else { 906 # Set time of png file to the time of the last update of the rrd file. 907 # This makes http's If-Modified-Since more reliable, esp. in combination with 908 # munin-*cgi-graph. 909 910 utime $lastupdate, $lastupdate, munin_get_picture_filename($service, $time); 911 if ($list_images) { 906 912 # Command-line option to list images created 907 913 print munin_get_picture_filename ($service, $time),"\n";
