Opened 7 years ago

Closed 7 years ago

Last modified 2 years ago

#499 closed patch (fixed)

SysV IPC locking in munin-cgi-graph and correct handling of expired graphs

Reported by: Fox Owned by: nobody
Priority: low Milestone:
Component: web-interface Version: 1.3.3
Severity: minor Keywords: munin-cgi-graph cgi lock graph
Cc:

Description

I played a little with munin-cgi-graph. It was too slow too my taste. So i took a hint in munin-cgi-graph and changed the locking scheme to SysV IPC and fixed graph expiration mechanism.

(I'm not a perfect coder and I'm using munin for a few days, so please excuse my code)

Tomas

--- ../build/server/munin-cgi-graph     2007-05-12 14:34:20.571040495 +0200
+++ /home/_www/ssl/cgi-bin/munin-cgi-graph      2007-05-12 14:44:32.672602375 +0200
@@ -25,6 +25,7 @@
 use IO::Handle;
 use Date::Manip;
 use POSIX qw(strftime);
+use IPC::SysV qw(IPC_CREAT);

 my $GRAPHER = "/usr/libexec/munin/munin-graph";
 my $conffile = "/etc/munin/munin.conf";
@@ -49,6 +50,7 @@
 my $serv  = "";
 my $dom   = "";
 my $lock  = "";
+my $IPC_KEY = 89340;

 my $config = &munin_readconfig ($conffile);

@@ -112,19 +114,30 @@

 my $max_cgi_graph_jobs = &munin_get ($config, "max_cgi_graph_jobs" , 6, $dom);

-until ( $lock ) {
-    foreach my $n ( 1 .. $max_cgi_graph_jobs ) {
-       if (&munin_getlock ("$config->{rundir}/munin-cgi-graph-$n.lock")) {
-           $lock = "munin-cgi-graph-$n.lock";
-           last;
-       }
-    }
-    sleep 1;
+#until ( $lock ) {
+#    foreach my $n ( 1 .. $max_cgi_graph_jobs ) {
+#       if (my $x = &munin_getlock ("$config->{rundir}/munin-cgi-graph-$n.lock")) {
+#         $lock = "munin-cgi-graph-$n.lock";
+#           last;
+#       }
+#    }
+#    sleep 1;
+#}
+my $semid = semget($IPC_KEY, 0, 0 );
+if(!$semid)
+{
+       $semid = semget($IPC_KEY, 1 , 0666 | IPC_CREAT ) || die "$!";
+       my $opstring = pack("s!s!s!",0, $max_cgi_graph_jobs,0);
+       semop($semid,$opstring) || die "$!";
 }
+my $opstring = pack("s!s!s!",0, -1, 0);
+semop($semid,$opstring);

 &graph ($filename);
+$opstring = pack("s!s!s!",0, 1, 0);
+semop($semid,$opstring);

-munin_removelock($lock);
+#munin_removelock($lock);

 sub graph
 {
@@ -230,19 +243,22 @@
 {
     my $filename = shift;
     my $time     = shift;
-
+
     if (-f $filename)
     {
        my @stats = stat (_);
-       if (($stats[9]) > ($time - $time%$period{$scale}))
+       my $expire = ($stats[9] - $time%$period{$scale}+$period{$scale})-$time;
+#print STDERR "Expires in: $expire\n";
+
+       if ($expire >= 0)
        {
 #print STDERR "Skipping munin-graph-run for \"$filename\".\n";
-#print STDERR ("Graph unexpired for $scale. ($stats[9] , $time, ", ($time%$period{$scale}), ", ", ($time - $time%$period{$scale}), ").\n");
+#print STDERR ("Graph unexpired for $scale. ($stats[9] , $time, ". ($time%$period{$scale}). ", ". ($time - $time%$period{$scale}). ").\n");
            return 1;
        }
        else
        {
-#print STDERR ("Graph expired for $scale. ($stats[9] , $time, ", ($time%$period{$scale}), ", ", ($time - $time%$period{$scale}), ").\n");
+#print STDERR ("Graph expired for $scale. ($stats[9] , $time, ". ($time%$period{$scale}). ", ". ($time - $time%$period{$scale}). ").\n");
            return 0;
        }
     }
@@ -260,7 +276,7 @@

     my @params = ($GRAPHER);
     push @params, @$scale;
-    push @params, "--skip-locking", "--skip-stats";
+    push @params, "--skip-locking", "--skip-stats", "--nolazy";
     push @params, "--host", $host, "--service", $serv;
     push @params, "STDERR>&STDOUT";

Change History (2)

comment:1 Changed 7 years ago by janl

Folded into r1323

Thanks a lot!

Nicolai

comment:2 Changed 7 years ago by janl

  • Resolution set to fixed
  • Status changed from new to closed
Note: See TracTickets for help on using tickets.