Changeset 2734

Show
Ignore:
Timestamp:
10/28/09 01:48:10 (2 years ago)
Author:
janl
Message:

* munin-graph: set m/a time of png to last modification time of the corresponding rrd file
* Adapt patch in #3 to current munin-cgi-graph for non-fastcgi. Closes #3. Thanks to blueyed
* Adapt patch in #3 to current munin-cgi-graph to obtain munin-fastcgi-graph
* Install munin-fastcgi-graph

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • trunk/Makefile

    r2725 r2734  
    101101        $(INSTALL) -m 0755 build/master/_bin/munin-gather $(LIBDIR)/ 
    102102        $(INSTALL) -m 0755 build/master/_bin/munin-cgi-graph $(CGIDIR)/ 
     103        $(INSTALL) -m 0755 build/master/_bin/munin-fastcgi-graph $(CGIDIR)/ 
    103104 
    104105 
  • trunk/master/_bin/munin-cgi-graph.in

    r2143 r2734  
    7373my $time = time; 
    7474 
    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} and  
    80         !&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  
    9175# 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); 
     76my $semid = undef; 
     77 
     78sem_setup(); 
     79 
     80my $no_cache = defined($ENV{HTTP_CACHE_CONTROL}) && $ENV{HTTP_CACHE_CONTROL} =~ /no-cache/i; 
     81 
     82if ($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 
     89my @stats         = stat ($filename); 
    12390my $last_modified = strftime ("%a, %d %b %Y %H:%M:%S %Z", localtime ($stats[9])); 
     91# "Expires" has to use last modified time as base: 
     92my $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: 
     96if (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 
     106draw: 
     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})))); 
    124113 
    125114print "Content-Type: image/png\n"; 
     
    128117print "\n"; 
    129118 
    130 # Try to police the number of concurrent rrdgraph instances.  The 
    131 # 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 that 
    141 # creating the graph requires.  So we might consider taking sending 
    142 # the graph out of the semaphore protected area. 
    143  
    144119&graph ($filename); 
    145120 
    146 # Increment (and release waiting processes). 
    147 $opstring = pack("s!s!s!",0, 1, 0); 
    148 semop($semid,$opstring); 
     121# # # # # # # # # # END OF MAIN 
     122 
     123sub 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 
     146sub 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 
     154sub 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 
    149161 
    150162sub graph { 
     163    # This just serves the file, no file is made. 
    151164    my $filename = shift; 
    152165 
     
    156169    close ($GRAPH); 
    157170} 
     171 
    158172 
    159173sub get_picture_filename { 
     
    167181} 
    168182 
     183 
    169184sub logger_open { 
    170185    my $dirname = shift; 
     
    178193    } 
    179194} 
     195 
    180196 
    181197sub logger { 
     
    248264} 
    249265 
     266 
    250267sub 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) = @_
    253270 
    254271    if (-f $filename) { 
    255272        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})) { 
    261275#print STDERR ("Graph unexpired for $scale. ($stats[9] , $time, ". ($time%$period{$scale}). ", ". ($time - $time%$period{$scale}). ").\n"); 
    262276            return 1; 
     
    269283} 
    270284 
     285 
    271286sub draw_graph { 
    272287    my $host  = shift; 
     
    284299 
    285300    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=(); 
    289306        exec @params; 
    290307    } 
    291308    $file = join (' ', <$IN>); 
     309 
    292310    close ($IN); 
     311    sem_release(); 
     312 
    293313    return $file; 
    294314} 
    295315 
     316 
     317sub 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 
    296332sub 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 
    298336 
    299337    my $since_string = shift; 
  • trunk/master/_bin/munin-graph.in

    r2730 r2734  
    55 
    66Copyright (C) 2002-2009 Jimmy Olsen, Audun Ytterdal, Kjell Magne Øierud, 
    7 Nicolai Langfeldt, Linpro AS, Redpill Linpro AS 
     7Nicolai Langfeldt, Linpro AS, Redpill Linpro AS and others. 
    88 
    99This program is free software; you can redistribute it and/or 
     
    1919You should have received a copy of the GNU General Public License 
    2020along with this program; if not, write to the Free Software 
    21 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. 
     21Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA. 
    2222 
    2323$Id$ 
     
    7979 
    8080# Get options 
    81 $do_usage=1  unless  
     81$do_usage=1  unless 
    8282GetOptions ( "force!"       => \$force_graphing, 
    8383             "lazy!"        => \$force_lazy, 
     
    903903        if (my $ERROR = RRDs::error) { 
    904904            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) { 
    906912            # Command-line option to list images created 
    907913            print munin_get_picture_filename ($service, $time),"\n";