0

I wrote a plugin for munin which basically fetches some numbers in a .log file and do an average of these values.

When I run it with munin-cron or directly like this :

# munin-run latency_news 
latency.value 0.1

# ./latency_news
latency.value 0.1

It's working great. But in my html munin page, I just see "Nan" for all values in the graph : enter image description here

(The image is just to show you, not my actual plugin)

Here's the code of the plugin :

#!/usr/bin/env php
<?php

class LatencyLog
{
    private $fname;
    private $fd;
    private $pname;
    private $pos;
    function __construct($page)
    {
        $this->fname = '/var/log/time_'.$page.'.log';
        if (!($this->fd = @fopen($this->fname, 'r')))
        {
            throw new Exception('Unable to read ' . $this->fname);
        }
        $this->pname = $this->fname;
        $this->pos = 0;
        if ($pd = @fopen($this->pname, 'r'))
        {
            $pos = (int) fgets($pd);
            fclose($pd);
            fseek($this->fd, 0, SEEK_END);
            if ($pos <= ftell($this->fd))
            {
                $this->pos = $pos;
            }
        }
        fseek($this->fd, $this->pos);
    }
    function __destruct()
    {
        $pos = ftell($this->fd);
        if ($pos != $this->pos)
        {
            if (!($pd = @fopen($this->pname, 'w')))
            {
                throw new Exception('Unable to write ' . $this->pname);
            }
            fprintf($pd, "%d\n", $pos);
            fclose($pd);
        }
        fclose($this->fd);
    }
    function get_line()
    {
        return fgets($this->fd);
    }
}

class LatencyMonitor
{
    private $query_log;
    private $page;
    function __construct()
    {
        $this->page = explode('_', $_SERVER['SCRIPT_FILENAME']);
        $this->query_log = new LatencyLog($this->page[1]);
    }
    function check($what)
    {
        $queries = 0;
        $results = 0;
        $times = 0;
        while ($line = $this->query_log->get_line())
        {
            $url = explode('=', $line);
            $page = $url[0];
            if ($page == $this->page[1])
            {
                $queries++;
                $times += (float) $url[1];
            }
        }
        if ($queries > 0)
            $times /= $queries;
        echo "$what.value ".str_replace(',', '.', round($times, 2))."\n";
    }
}

$tmp = explode('_', $_SERVER['SCRIPT_FILENAME']);
$name = $tmp[1];
$label = 'average time per query';
$what = 'latency';

if (isset($argv[1]))
{
    if ($argv[1] == 'config')
    {
        $title = $name;
        echo "graph_title $title\n";
        echo "graph_category perfs\n";
        echo "graph_vlabel $label\n";
        echo "$what.label $what\n";
        echo "$what.type GAUGE\n";
        echo "$what.min 0\n";
        exit;
    }
}

$smm = new LatencyMonitor();
$smm->check($what);
?>
  • munin-node executes plugins as user munin (you can change that on a per-plugin basis). Does munin have access to that log? – Dan Feb 16 '15 at 08:50
  • Thanks for your answer ! Munin user has access to the file yes. – Yanis Boucherit Feb 16 '15 at 08:54
  • How come you are graphing 'latency' but the munin graph sais 'locked'? – Dan Feb 16 '15 at 08:58
  • Ok, edited my post, forgot to mention that it was just a screenshot I found on Google to show you. – Yanis Boucherit Feb 16 '15 at 09:03
  • From the munin server, try to `telnet munin.client.hostname 4949` and issue `fetch latency` – Dan Feb 16 '15 at 09:19
  • Ok, so apparently adding "user root" in the plugin conf worked great ! But that's weird... When I used the "munin" user directly from the shell it has access to the file log. But it seems that munin-node doesn't. Well, it's working now. Thanks to you ! – Yanis Boucherit Feb 16 '15 at 09:25
  • As a note, for security reasons you should try to figure out how to not use root. – Dan Feb 16 '15 at 09:41

0 Answers0