Ticket #3: munin-cgi-graph_fastcgi.2.diff

File munin-cgi-graph_fastcgi.2.diff, 11.8 kB (added by do, 4 years ago)

fastscgi patch for trunk

  • munin-cgi-graph.in

    old new  
    1 #!@@PERL@@  -Tw 
     1#!/usr/bin/perl  -Tw 
    22# -*- perl -*- 
    33# 
    44# Copyright (C) 2004 Jimmy Olsen 
     
    1515# 
    1616# You should have received a copy of the GNU General Public License 
    1717# along with this program; if not, write to the Free Software 
    18 # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. 
     18# Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA. 
    1919# 
    2020# $Id$ 
    2121# 
     
    3030use Date::Manip; 
    3131use POSIX qw(strftime); 
    3232use IPC::SysV qw(IPC_CREAT); 
     33use CGI::Fast; 
    3334 
    34 my $GRAPHER = "@@LIBDIR@@/munin-graph"; 
    35 my $conffile = "@@CONFDIR@@/munin.conf"; 
     35my $GRAPHER = "/usr/share/munin/munin-graph"; 
     36my $conffile = "/etc/munin/munin.conf"; 
    3637 
    3738my %TIMES   = ( "day"   => ["--noweek", "--nomonth", "--noyear", "--nosumweek", "--nosumyear"],  
    3839                "week"  => ["--noday", "--nomonth", "--noyear", "--nosumweek", "--nosumyear"],  
     
    4041                "year"  => ["--noday", "--noweek", "--nomonth", "--nosumweek", "--nosumyear"], 
    4142                "week-sum"  => ["--noday", "--nomonth", "--noyear", "--noweek", "--nosumyear"],  
    4243                "year-sum"  => ["--noday", "--noweek", "--nomonth", "--nosumweek", "--noyear"] 
    43             ); 
     44              ); 
    4445 
    4546my %period  = ( "day"   => 300, 
    4647                "week"  => 1800, 
     
    4849                "year"  => 86400, 
    4950                "week-sum" => 1800, 
    5051                "year-sum" => 86400 
    51             ); 
     52              ); 
    5253 
    5354my $log = new IO::Handle; 
    5455my $scale = "day"; 
     
    5657my $serv  = ""; 
    5758my $dom   = ""; 
    5859my $lock  = ""; 
    59 my $IPC_KEY = 89340; 
     60my $IPC_KEY = 9340; 
    6061 
    6162my $config = &munin_readconfig ($conffile); 
    6263 
    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$//; 
     64while (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$//; 
    6871 
    69 &verify_parameters ($dom, $host, $serv, $scale); 
     72       &verify_parameters ($dom, $host, $serv, $scale); 
    7073 
    71 my $filename = get_picture_filename ($config, $dom, $host, $serv, $scale); 
     74       my $filename = get_picture_filename ($config, $dom, $host, $serv, $scale); 
    7275 
    73 my $time = time; 
     76       my $time = time; 
    7477 
    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])); 
    7982 
    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 
    84111        print "Content-Type: image/png\n"; 
    85112        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"; 
    87114        print "\n"; 
    88         exit 0; 
    89     } 
    90 } 
    91115 
    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  
    113116# Try to police the number of concurrent rrdgraph instances.  The 
    114117# third value is the default maximum. 
    115118 
    116119# Fox kindly submitted a patch to convert to SysV IPC semaphores. 
    117120# Lovely! (ticket #499). 
    118121 
    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); 
    120123 
    121 my $opstring;  
     124       my $opstring;  
    122125 
    123126# Get semaphore handle 
    124 my $semid = semget($IPC_KEY, 0, 0 ); 
     127       my $semid = semget($IPC_KEY, 0, 0 ); 
    125128 
    126 if(!$semid) { 
    127     # Or create it if needed 
    128     $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: $!"; 
    130133 
    131     # And initialize to max_cgi_graph_jobs 
    132     $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       
    135138 
    136139# 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); 
    139142 
    140 &graph ($filename); 
     143       &graph ($filename); 
    141144 
    142145# 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
    146149sub graph { 
    147     my $filename = shift; 
     150       my $filename = shift; 
    148151 
    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); 
    152155} 
    153156 
    154157sub 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; 
    160163 
    161     return "$config->{'htmldir'}/$domain/$name-$service-$scale.png"; 
     164       return "$config->{'htmldir'}/$domain/$name-$service-$scale.png"; 
    162165} 
    163166 
    164167sub logger_open { 
    165     my $dirname = shift; 
     168       my $dirname = shift; 
    166169 
    167     if (!$log->opened) 
    168     { 
    169         unless (open ($log, ">>$dirname/munin-cgi-graph.log")) 
     170        if (!$log->opened) 
    170171        { 
    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                } 
    172176        } 
    173     } 
    174177} 
    175178 
    176179sub 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); 
    179182 
    180   if ($log->opened) 
    181  
    182           print $log "$now - $comment\n"; 
    183  
    184   else 
    185  
    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                   else 
    193                  
    194                           print STDERR "Warning: Could not open log file \"$config->{logdir}/munin-cgi-graph.log\" for wr 
    195 iting: $!"; 
    196                           print STDERR "$now - $comment\n"; 
    197                  
    198          
    199           else 
    200          
    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       
    204207} 
    205208 
    206209 
     
    214217        if (!$dom) 
    215218        { 
    216219                print STDERR "Warning: Request for graph without specifying domain. Bailing out.\n"; 
    217                 exit 1
     220                return 0
    218221        } 
    219222        if (!$host) 
    220223        { 
    221224                print STDERR "Warning: Request for graph without specifying host. Bailing out.\n"; 
    222                 exit 1
     225                return 0
    223226        } 
    224227        if (!$serv) 
    225228        { 
    226229                print STDERR "Warning: Request for graph without specifying service. Bailing out.\n"; 
    227                 exit 1
     230                return 0
    228231        } 
    229232 
    230233        if (!$scale) 
    231234        { 
    232235                print STDERR "Warning: Request for graph without specifying scale. Bailing out.\n"; 
    233                 exit 1
     236                return 0
    234237        } 
    235238        else 
    236239        { 
    237240                if (!defined $TIMES{$scale}) 
    238241                { 
    239242                        print STDERR "Warning: Weird scale setting \"$scale\". Bailing out.\n"; 
    240                         exit 1
     243                        return 0
    241244                } 
    242245        } 
     246        return 1; 
    243247} 
    244248 
    245249sub graph_usable { 
    246     my $filename = shift; 
    247     my $time     = shift; 
     250       my $filename = shift; 
     251       my $time     = shift; 
    248252 
    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; 
    252256#print STDERR "Expires in: $expire\n"; 
    253257 
    254         if ($expire >= 0) { 
     258               if ($expire >= 0) { 
    255259#print STDERR "Skipping munin-graph-run for \"$filename\".\n"; 
    256260#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 { 
    259263#print STDERR ("Graph expired for $scale. ($stats[9] , $time, ". ($time%$period{$scale}). ", ". ($time - $time%$period{$scale}). ").\n"); 
    260             return 0; 
     264                        return 0; 
     265                } 
    261266        } 
    262     } 
    263     return 0; 
     267        return 0; 
    264268} 
    265269 
    266270sub 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; 
    270274 
    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; #" 
    273277 
    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"; 
    279283 
    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; 
    289293} 
    290294 
    291295sub modified { 
    292296# Format of since_string If-Modified-Since: Wed, 23 Jun 2004 16:11:06 GMT 
    293297 
    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"); 
    297301 
    298     return 1 if ($ifmodsec < $created); 
    299     return 0; 
     302       return 1 if ($ifmodsec < $created); 
     303       return 0; 
    300304} 
    301305 
    302306# vim: syntax=perl ts=8