Changeset 3389

Show
Ignore:
Timestamp:
02/26/10 21:24:00 (2 years ago)
Author:
steve.schnepp
Message:

- fixed & cleaned the cgi Expires/LastModified
- fixed image size cgi bug
- added a cgitmpdir to make the location of the temporary png configurable

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • people/snide/pre_1.5/common/lib/Munin/Common/Config.pm

    r3139 r3389  
    3838        "onlynullcdef", "group_order", "pipe", "pipe_command", 
    3939        "unknown_limit", "notify_countdown", "dropdownlimit", 
    40         "max_graph_jobs", "munin_cgi_graph_jobs" ); 
     40        "max_graph_jobs", "munin_cgi_graph_jobs", 
     41        "cgitmpdir", 
     42        ); 
    4143 
    4244my %bools = map { $_ => 1} qw(yes no true false on off 1 0); 
  • people/snide/pre_1.5/common/lib/Munin/Common/Defaults.pm

    r2951 r3389  
    2626our $MUNIN_HTMLDIR    = ''; 
    2727our $MUNIN_CGIDIR     = ''; 
     28our $MUNIN_CGITMPDIR     = ''; 
    2829our $MUNIN_DBDIR      = ''; 
    2930our $MUNIN_PLUGSTATE  = '';  
  • people/snide/pre_1.5/master/_bin/munin-fastcgi-graph.in

    r3376 r3389  
    6464my $config = &munin_readconfig ($conffile); 
    6565 
    66 # Get semaphore handle - Before graphing as recommended by snide. 
    67 my $semid = undef; 
    68  
    69 #sem_setup(); 
    70  
    7166# BEGIN FAST-CGI LOOP: 
    7267while (new CGI::Fast) { 
     
    9388    # If a "Cache-Control: no-cache" header gets send, we regenerate the image in every case: 
    9489    my $no_cache = $pinpoint || defined($ENV{HTTP_CACHE_CONTROL}) && $ENV{HTTP_CACHE_CONTROL} =~ /no-cache/i; 
     90    print STDERR "no_cache:$no_cache\n"; 
     91 
     92    # Compute the cache values 
     93    my $graph_ttl = $pinpoint ? 1 : $period{$scale}; 
     94    my $graph_last_expires = $time - $time % $graph_ttl; 
    9595  
    96     if ($no_cache or (! &graph_usable ($filename, $time) )) { 
     96    my $graph_epoch = (! $no_cache) && file_newer_than($filename, $graph_last_expires); 
     97    if ($graph_epoch) { 
     98        # The graph is fresh enough. Sending either IMS if asked, or just skip generation  
     99        # Check for If-Modified-Since and send 304 if not changed: 
     100        if (defined $ENV{HTTP_IF_MODIFIED_SINCE} &&  
     101                ! rfctime_newer_than($ENV{HTTP_IF_MODIFIED_SINCE}, $graph_epoch)) { 
     102                print "Status: 304\n"; 
     103                print "Content-Type: image/png\n"; 
     104                print "\n"; 
     105                 
     106                # We replied, continue with the next request  
     107                next; 
     108        } 
     109    } else { 
     110        # Should generate it 
    97111        my $scale_options; 
    98112        if ($pinpoint) { 
     
    102116        } 
    103117        next unless draw_graph_or_complain($dom, $host, $serv, $scale_options, $filename, "$config->{logdir}/munin-cgi-graph.log"); 
    104         goto draw; 
    105     } 
    106  
    107     # At this time the file exists.  But may be old.  Or not. 
    108  
    109     my @stats         = stat ($filename); 
    110     my $last_modified = strftime ("%a, %d %b %Y %H:%M:%S %Z", localtime ($stats[9])); 
     118    } 
     119 
     120    # At this time the file exists and should be served 
     121    my @stats       = stat ($filename); 
     122    my $mtime_epoch = $stats[9]; 
     123    my $last_modified = get_w3c_date_from_epoch($mtime_epoch); 
     124 
    111125    # "Expires" has to use last modified time as base: 
    112     my $expires       = strftime ("%a, %d %b %Y %H:%M:%S GMT",  
    113                                  gmtime($stats[9]+($period{$scale}-($stats[9]%$period{$scale})))); 
     126    my $graph_next_expires = $mtime_epoch - ($mtime_epoch % $graph_ttl) + $graph_ttl; 
     127    my $expires       = get_w3c_date_from_epoch($graph_next_expires); 
    114128     
    115     # Check for If-Modified-Since and send 304 if not changed: 
    116     if (defined $ENV{HTTP_IF_MODIFIED_SINCE} and 
    117         !&modified ($ENV{HTTP_IF_MODIFIED_SINCE}, $stats[9]-1)) { 
    118         print "Status: 304\n"; 
    119         print "Content-Type: image/png\n"; 
    120         print "Expires: ", $expires, "\n"; 
    121         print "Last-Modified: $last_modified\n"; 
    122         print "\n"; 
    123         next; 
    124     } 
    125  
    126   draw:   
    127     my $period_scale = $pinpoint ? 1 : $period{$scale}; 
    128     @stats           = stat ($filename); # restat to be sure 
    129     $last_modified   = strftime ("%a, %d %b %Y %H:%M:%S %Z", localtime ($stats[9])) unless defined($last_modified); 
    130     # "Expires" has to use last modified time as base: 
    131     #$expires         = strftime ("%a, %d %b %Y %H:%M:%S GMT",  
    132 #                                gmtime($stats[9]+($period{$scale}-($stats[9]%$period_scale)))) unless defined ($expires); 
    133      
     129 
     130    # Sending headers 
    134131    print "Status: 200\n"; 
    135132    print "Content-Type: image/png\n"; 
    136133    print "Content-Length: $stats[7]\n"; 
    137     #print "Expires: ", $expires, "\n"; 
    138     print "Last-Modified: $last_modified\n"; 
     134 
     135    # Conditionaly add timing informations 
     136    print "Expires: $expires\n" if $expires; 
     137    print "Last-Modified: $last_modified\n" if $last_modified; 
    139138    print "\n"; 
    140139 
    141     &graph ($filename); 
    142 
    143  
     140    # Sending graph data 
     141    send_graph_data($filename); 
     142
    144143# END FAST-CGI LOOP 
    145144 
    146 sub sem_setup { 
    147     $semid = semget($IPC_KEY, 0, 0 ); 
    148  
    149     if(!defined($semid)) { 
    150         # Or create it if needed 
    151         $semid = semget($IPC_KEY, 1 , oct(666) | IPC_CREAT ); 
    152  
    153         die "Creating semaphore: $!" unless defined($semid); 
    154  
    155         my $max_cgi_graph_jobs = &munin_get ($config, "max_cgi_graph_jobs" , 6, $dom); 
    156  
    157         # And initialize to max_cgi_graph_jobs 
    158         my $opstring = pack("s!s!s!",0, $max_cgi_graph_jobs,0); 
    159         semop($semid,$opstring) || die "$!"; 
    160     } 
    161 
    162  
    163  
    164 sub sem_get { 
    165     # Call this before doing heavy work. 
    166     # Decrement, or lock/hang/yield if already 0 
    167     my $opstring = pack("s!s!s!",0, -1, 0); 
    168     semop($semid,$opstring); 
    169 
    170  
    171  
    172 sub sem_release { 
    173     # Call this after doing heavy work. 
    174     # Increment (and release waiting processes). 
    175     my $opstring = pack("s!s!s!",0, 1, 0); 
    176     semop($semid,$opstring); 
    177 
    178  
    179  
    180 sub graph { 
    181     # Serve the graph contents.  This is not heavy, no semaphore. 
     145sub get_w3c_date_from_epoch { 
     146        my $epoch = shift; 
     147        print STDERR "get_w3c_date_from_epoch($epoch)\n"; 
     148        return strftime("%a, %d %b %Y %H:%M:%S GMT", gmtime($epoch)); 
     149
     150 
     151sub send_graph_data { 
     152    # Serve the graph contents. 
    182153    my $filename = shift; 
    183154 
     
    195166    my $scale   = shift; 
    196167 
    197     return "/tmp/munin-cgi-png/$domain/$name/$service-$scale.png"; 
     168    my $cgi_tmp_dir = $config->{cgitmpdir} || "/tmp/munin-cgi-tmp"; 
     169 
     170    return "$cgi_tmp_dir/$domain/$name/$service-$scale.png"; 
    198171} 
    199172 
     
    270243 
    271244 
    272 sub graph_usable
     245sub file_newer_than
    273246    my $filename = shift; 
    274247    my $time     = shift; 
     
    276249    if (-f $filename) { 
    277250        my @stats = stat (_); 
    278         # $stats[9] holds the "last update" time and this needs to be in the last update period: 
    279         if ($stats[9] > ($time - $time%$period{$scale})) { 
    280 #print STDERR "Skipping munin-graph-run for \"$filename\".\n"
    281 #print STDERR ("Graph unexpired for $scale. ($stats[9] , $time, ". ($time%$period{$scale}). ", ". ($time - $time%$period{$scale}). ").\n"); 
    282             return 1
     251        # $stats[9] holds the "last update" time and this needs  
     252        # to be in the last update period 
     253       my $last_update = $stats[9]
     254        if ($last_update > $time) { 
     255            return $last_update
    283256        } else { 
    284 #print STDERR ("Graph expired for $scale. ($stats[9] , $time, ". ($time%$period{$scale}). ", ". ($time - $time%$period{$scale}). ").\n"); 
    285257            return 0; 
    286258        } 
    287259    } 
     260 
     261    # No file found 
    288262    return 0; 
    289263} 
     
    353327 
    354328 
    355 sub modified
     329sub rfctime_newer_than
    356330    # See if the file has been modified since "the last time". 
    357  
    358331    # Format of since_string If-Modified-Since: Wed, 23 Jun 2004 16:11:06 GMT 
    359  
    360332    my $since_string = shift; 
    361333    my $created      = shift; 
  • people/snide/pre_1.5/master/lib/Munin/Master/HTMLOld.pm

    r3376 r3389  
    983983 
    984984    for my $scale (@times) { 
     985        # Don't try to find the size if cgi is enabled,  
     986        # otherwise old data might pollute   
     987        next if ($method eq "cgi"); 
    985988        if (my ($w, $h) 
    986989            = get_png_size(munin_get_picture_filename($service, $scale))) { 
     
    994997        $srv{imgyearsum} = "$srv{node}-year-sum.png"; 
    995998        for my $scale (["week", "year"]) { 
     999            next if ($method eq "cgi"); 
    9961000            if (my ($w, $h) 
    9971001                = get_png_size(munin_get_picture_filename($service, $scale, 1)))