librenms-plugins / Weathermap

MIT License
65 stars 52 forks source link

rrdcached local socket issue #88

Open lwhitworth opened 2 years ago

lwhitworth commented 2 years ago

Weathermap seemed to not be reading RRDs properly when using rrdached on a socket such as unix:/run/rrdcached.sock

Stepping through the cmd as built in map-poller.php got me:

php ./weathermap.php --config /opt/librenms/html/plugins/Weathermap/configs/myconf.conf --base-href /plugins/Weathermap/ --daemon unix:/var/run/rrdcached.sock --chdir '' --debug

This threw:

DEBUG: [wmrrd_read_from_real_rrdtool@WeatherMapDataSource_rrd.php:245] /opt/librenms/html/plugins/Weathermap/configs/myconf.conf: RRD ReadData: Running: /usr/bin/rrdtool fetch myswitch/port-id5044.rrd AVERAGE --start now-800 --end now --daemon unix:/var/run/rrdcached.sock 
ERROR: realpath(myswitch/port-id5044.rrd): No such file or directory

So it's looking for the rrd file on an incomplete path. Running:

 /usr/bin/rrdtool fetch /opt/librenms/rrd/myswitch/port-id5044.rrd AVERAGE --start now-800 --end now --daemon unix:/var/run/rrdcached.sock 

Confirmed it worked, so for my issue to be fixed I had to modify map-poller.php line 71 to read:

if (!empty($config['rrdcached']))
{
    $cmd = $cmd." --daemon ".$config['rrdcached']." --chdir ".$config['rrd_dir'];
}

I then had to comment out two instances of the following in lib/datasources/WeatherMapDataSource_rrd.php:

// rrdcached Support: strip "./" from Data Source
if ($map->daemon)
{
    $rrdfile = trim($rrdfile,"^./"); 
}

Reading through previous pulls / commits I see that plenty of logic has been added recently by @LoveSkylark for their use case which I think uses rrdcached exposed via an ip:port as oppsed to a socket, so I would imagine some extra logic needs to be written around to account for when rrdcached references a socket......unless someone wnats to tell me I'm way off the mark!

wdoekes commented 2 years ago

I ran into the same. Changed these:

diff --git a/lib/datasources/WeatherMapDataSource_rrd.php b/lib/datasources/WeatherMapDataSource_rrd.php
index 40128fc..5da1d0b 100644
--- a/lib/datasources/WeatherMapDataSource_rrd.php
+++ b/lib/datasources/WeatherMapDataSource_rrd.php
@@ -209,7 +209,7 @@ class WeatherMapDataSource_rrd extends WeatherMapDataSource
         // rrdcached Support: strip "./" from Data Source
         if ($map->daemon)
         {
-            $rrdfile = trim($rrdfile,"^./"); 
+            $rrdfile = preg_replace('/^\.\//', '', $rrdfile);
         }

         $args[] = "fetch";
diff --git a/map-poller.php b/map-poller.php
index 1544be6..de733fc 100755
--- a/map-poller.php
+++ b/map-poller.php
@@ -66,7 +66,7 @@ if (is_dir($conf_dir))

                 if (!empty($config['rrdcached']))                                            
                 {                                                                            
-                    $cmd = $cmd." --daemon ".$config['rrdcached']." --chdir ''";         
+                    $cmd = $cmd." --daemon ".$config['rrdcached']." --chdir ".$config['rrd_dir'];
                 }                                                                            
                 else                                                                         
                 {                                                                         
diff --git a/weathermap.php b/weathermap.php
index 489011d..832f0f8 100755
--- a/weathermap.php
+++ b/weathermap.php
@@ -280,6 +280,8 @@ if ($map->ReadConfig($configfile))
     if($rrdbase == '') {
         $rrdbase = isset($chdir) != '' ? $chdir : $rrd_default_path1;
         $map->add_hint('rrd_default_path', $rrdbase);
+        // ^- beware, this does not appear to work sufficiently, we also needed chdir
+        //    "SET rrd_default_path /srv/librenms/rrd"

     }

Note that the original use of trim() is totally wrong.