Ticket #3: munin-cgi-graph_fastcgi.diff

File munin-cgi-graph_fastcgi.diff, 4.6 kB (added by blueyed, 4 years ago)

Patch for munin-cgi-graph (Fixed patch, just called "rediff" on the previous one)

  • /usr/lib/cgi-bin/munin-cgi-graph

    old new  
    4040use IO::Handle; 
    4141use Date::Manip; 
    4242use POSIX qw(strftime); 
     43use CGI::Fast; 
    4344 
    4445my $GRAPHER = "/usr/share/munin/munin-graph"; 
    4546my $conffile = "/etc/munin/munin.conf"; 
     
    6667 
    6768my $config = &munin_readconfig ($conffile); 
    6869 
    69 my $path = $ENV{PATH_INFO} || ""; 
    70 $path =~ s/^\///; 
    71 ($dom, $host, $serv) = split /\//, $path;   
    72 ($serv, $scale) = split /-/, $serv, 2; 
    73 $scale =~ s/\.png$//; 
    7470 
    75 &verify_parameters ($dom, $host, $serv, $scale); 
     71# BEGIN FAST-CGI LOOP: 
     72while (new CGI::Fast) 
     73
     74    my $path = $ENV{PATH_INFO} || ""; 
     75    $path =~ s/^\///; 
     76    ($dom, $host, $serv) = split /\//, $path; 
     77    ($serv, $scale) = split /-/, $serv, 2; 
     78    $scale =~ s/\.png$// if defined $scale; 
    7679 
    77 my $filename = get_picture_filename ($config, $dom, $host, $serv, $scale); 
     80    if (! &verify_parameters ($dom, $host, $serv, $scale)) 
     81    { 
     82        print "Status: 500\n"; 
     83        print "Content-Type: text/html\n"; 
     84        print "\n"; 
     85        print "Invalid parameters!"; 
     86        next; 
     87    } 
    7888 
    79 my $time = time
     89    my $filename = get_picture_filename ($config, $dom, $host, $serv, $scale)
    8090 
    81 if (-f $filename) 
    82 
    83     my @sstats = stat ($filename); 
    84     my $slast_modified = strftime ("%a, %d %b %Y %H:%M:%S %Z", localtime ($sstats[9])); 
     91    my $time = time; 
    8592 
    86     if (defined $ENV{HTTP_IF_MODIFIED_SINCE} and  
    87             !&modified ($ENV{HTTP_IF_MODIFIED_SINCE}, $sstats[9]-1)) 
     93    if (-f $filename) 
    8894    { 
    89         print "Status: 304\n"; 
    90         print "Content-Type: image/png\n"; 
    91         print "Expires: ", strftime ("%a, %d %b %Y %H:%M:%S GMT", gmtime(time+($period{$scale}-($time%$period{$scale})))), "\n"; 
    92         print "Last-Modified: $slast_modified\n"; 
    93         print "\n"; 
    94         exit 0; 
     95        my @sstats = stat ($filename); 
     96        my $slast_modified = strftime ("%a, %d %b %Y %H:%M:%S %Z", localtime ($sstats[9])); 
     97 
     98        if (defined $ENV{HTTP_IF_MODIFIED_SINCE} and 
     99            !&modified ($ENV{HTTP_IF_MODIFIED_SINCE}, $sstats[9]-1)) 
     100        { 
     101            print "Status: 304\n"; 
     102            print "Content-Type: image/png\n"; 
     103            print "Expires: ", strftime ("%a, %d %b %Y %H:%M:%S GMT", gmtime(time+($period{$scale}-($time%$period{$scale})))), "\n"; 
     104            print "Last-Modified: $slast_modified\n"; 
     105            print "\n"; 
     106            next; 
     107        } 
    95108    } 
    96 } 
    97109 
    98 if (! &graph_usable ($filename, $time)) 
    99 
    100     my $ret = (&draw_graph ($host, $serv, $TIMES{$scale}) || "Unknown error"); 
    101     if (! -f $filename) 
     110    if (! &graph_usable ($filename, $time)) 
    102111    { 
    103         ::logger ("Warning: Could not draw graph \"$host-$serv-$scale.png\": $ret"); 
    104         print "Status: 500\n"; 
    105         print "Content-Type: image/png\n"; 
    106         print "\n"; 
    107         exit 0; 
     112        my $ret = (&draw_graph ($host, $serv, $TIMES{$scale}) || "Unknown error"); 
     113        if (! -f $filename) 
     114        { 
     115        ::logger ("Warning: Could not draw graph \"$host-$serv-$scale.png\": $ret"); 
     116        print "Status: 500\n"; 
     117        print "Content-Type: image/png\n"; 
     118        print "\n"; 
     119        next; 
     120        } 
    108121    } 
    109 } 
    110122 
    111 my @stats = stat ($filename); 
    112 my $last_modified = strftime ("%a, %d %b %Y %H:%M:%S %Z", localtime ($stats[9])); 
     123    my @stats = stat ($filename); 
     124    my $last_modified = strftime ("%a, %d %b %Y %H:%M:%S %Z", localtime ($stats[9])); 
    113125 
    114 print "Content-Type: image/png\n"; 
    115 print "Expires: ", strftime ("%a, %d %b %Y %H:%M:%S GMT", gmtime(time+($period{$scale}-($time%$period{$scale})))), "\n"; 
    116 print "Last-Modified: $last_modified\n"; 
    117 print "\n"; 
     126    print "Content-Type: image/png\n"; 
     127    print "Expires: ", strftime ("%a, %d %b %Y %H:%M:%S GMT", gmtime(time+($period{$scale}-($time%$period{$scale})))), "\n"; 
     128    print "Last-Modified: $last_modified\n"; 
     129    print "\n"; 
    118130 
    119131 
    120 &graph ($filename); 
     132    &graph ($filename); 
     133    } 
     134# END FAST-CGI LOOP 
     135 
    121136 
    122137sub graph 
    123138{ 
     
    191206        if (!$dom) 
    192207        { 
    193208                print STDERR "Warning: Request for graph without specifying domain. Bailing out.\n"; 
    194                 exit 1
     209                return 0
    195210        } 
    196211        if (!$host) 
    197212        { 
    198213                print STDERR "Warning: Request for graph without specifying host. Bailing out.\n"; 
    199                 exit 1
     214                return 0
    200215        } 
    201216        if (!$serv) 
    202217        { 
    203218                print STDERR "Warning: Request for graph without specifying service. Bailing out.\n"; 
    204                 exit 1
     219                return 0
    205220        } 
    206221 
    207222        if (!$scale) 
    208223        { 
    209224                print STDERR "Warning: Request for graph without specifying scale. Bailing out.\n"; 
    210                 exit 1
     225                return 0
    211226        } 
    212227        else 
    213228        { 
    214229                if (!defined $TIMES{$scale}) 
    215230                { 
    216231                        print STDERR "Warning: Weird scale setting \"$scale\". Bailing out.\n"; 
    217                         exit 1
     232                        return 0
    218233                } 
    219234        } 
     235        return 1; 
    220236} 
    221237 
    222238sub graph_usable