Changeset 1323

Show
Ignore:
Timestamp:
21/09/07 15:06:12 (4 years ago)
Author:
janl
Message:

- SysV semaphore locking in munin-cgi-graph, thanks to Fox for patch (closes #499)

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • trunk/server/munin-cgi-graph.in

    r1322 r1323  
    1818# Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA. 
    1919# 
    20 # 
    2120# $Id$ 
     21# 
     22# Please see http://munin.projects.linpro.no/wiki/CgiHowto for how to 
     23# use this, and how to convert it to fastcgi which will improve speed. 
     24# 
    2225 
    2326use RRDs; 
     
    2730use Date::Manip; 
    2831use POSIX qw(strftime); 
     32use IPC::SysV qw(IPC_CREAT); 
    2933 
    3034my $GRAPHER = "@@LIBDIR@@/munin-graph"; 
     
    5155my $dom   = ""; 
    5256my $lock  = ""; 
     57my $IPC_KEY = 89340; 
    5358 
    5459my $config = &munin_readconfig ($conffile); 
     
    107112# third value is the default maximum. 
    108113 
    109 # NOTE: The munin_*lock functions are not exactly ideal for race prone 
    110 # locking, they're a bit fuzzy.  A better solution, which would not 
    111 # imply a "sleep 1" would be to use semaphores.  See "perldoc perlipc" 
    112 # and "man semop". 
     114# Fox kindly submitted a patch to convert to SysV IPC semaphores. 
     115# Lovely! (ticket #499). 
    113116 
    114117my $max_cgi_graph_jobs = &munin_get ($config, "max_cgi_graph_jobs" , 6, $dom); 
    115118 
    116 until ( $lock ) { 
    117     foreach my $n ( 1 .. $max_cgi_graph_jobs ) { 
    118        if (&munin_getlock ("$config->{rundir}/munin-cgi-graph-$n.lock")) { 
    119            $lock = "munin-cgi-graph-$n.lock"; 
    120            last; 
    121        } 
    122     } 
    123     sleep 1; 
    124 
     119my $opstring;  
     120 
     121# Get semaphore handle 
     122my $semid = semget($IPC_KEY, 0, 0 ); 
     123 
     124if(!$semid) { 
     125    # Or create it if needed 
     126    $semid = semget($IPC_KEY, 1 , 0666 | IPC_CREAT ) || 
     127        die "Creating semaphore: $!"; 
     128 
     129    # And initialize to max_cgi_graph_jobs 
     130    $opstring = pack("s!s!s!",0, $max_cgi_graph_jobs,0); 
     131    semop($semid,$opstring) || die "$!"; 
     132
     133 
     134# Decrement, or lock/hang/yield if already 0 
     135$opstring = pack("s!s!s!",0, -1, 0); 
     136semop($semid,$opstring); 
    125137 
    126138&graph ($filename); 
    127139 
    128 munin_removelock($config->{rundir}.'/'.$lock); 
     140# Increment (and release waiting processes) 
     141$opstring = pack("s!s!s!",0, 1, 0); 
     142semop($semid,$opstring); 
    129143 
    130144sub graph { 
     
    227241} 
    228242 
    229 sub graph_usable 
    230 
     243sub graph_usable { 
    231244    my $filename = shift; 
    232245    my $time     = shift; 
    233246 
    234     if (-f $filename) 
    235     { 
     247    if (-f $filename) { 
    236248        my @stats = stat (_); 
    237         if (($stats[9]) > ($time - $time%$period{$scale})) 
    238         { 
     249        my $expire = ($stats[9] - $time%$period{$scale}+$period{$scale})-$time; 
     250#print STDERR "Expires in: $expire\n"; 
     251 
     252        if ($expire >= 0) { 
    239253#print STDERR "Skipping munin-graph-run for \"$filename\".\n"; 
    240 #print STDERR ("Graph unexpired for $scale. ($stats[9] , $time, ", ($time%$period{$scale}), ", ", ($time - $time%$period{$scale}), ").\n"); 
     254#print STDERR ("Graph unexpired for $scale. ($stats[9] , $time, ". ($time%$period{$scale}). ", ". ($time - $time%$period{$scale}). ").\n"); 
    241255            return 1; 
    242         } 
    243         else 
    244         { 
    245 #print STDERR ("Graph expired for $scale. ($stats[9] , $time, ", ($time%$period{$scale}), ", ", ($time - $time%$period{$scale}), ").\n"); 
     256        } else { 
     257#print STDERR ("Graph expired for $scale. ($stats[9] , $time, ". ($time%$period{$scale}). ", ". ($time - $time%$period{$scale}). ").\n"); 
    246258            return 0; 
    247259        } 
     
    250262} 
    251263 
    252 sub draw_graph 
    253 
     264sub draw_graph { 
    254265    my $host  = shift; 
    255266    my $serv  = shift; 
    256267    my $scale = shift; 
    257268 
    258     $serv =~ s/[^\w_\/"'\[\]\(\)+=-]/_/; $serv =~ /^(.+)$/; $serv = $1; 
    259     $host =~ s/[^\w_\/"'\[\]\(\)+=-]/_/; $host =~ /^(.+)$/; $host = $1; 
     269    $serv =~ s/[^\w_\/"'\[\]\(\)+=-]/_/; $serv =~ /^(.+)$/; $serv = $1; #" 
     270    $host =~ s/[^\w_\/"'\[\]\(\)+=-]/_/; $host =~ /^(.+)$/; $host = $1; #" 
    260271 
    261272    my @params = ($GRAPHER); 
    262273    push @params, @$scale; 
    263     push @params, "--skip-locking", "--skip-stats"
     274    push @params, "--skip-locking", "--skip-stats", "--nolazy"
    264275    push @params, "--host", $host, "--service", $serv; 
    265276    push @params, "STDERR>&STDOUT"; 
     
    276287} 
    277288 
    278 sub modified 
    279 
     289sub modified { 
    280290# Format of since_string If-Modified-Since: Wed, 23 Jun 2004 16:11:06 GMT 
    281291