sni / lmd

Livestatus Multitool Daemon - Create livestatus federation from multiple sources
https://labs.consol.de/omd/packages/lmd/
GNU General Public License v3.0
42 stars 31 forks source link

LMD panics with type assertion and index out of bound errors #142

Closed hweidner closed 6 months ago

hweidner commented 6 months ago

Setup

Checkmk Enterprise 2.1 Central Site -> LMD -> 8 Checkmk Enterprise 2.1 Remote Sites

Problem

When displaying the "Alert statistics" view in the CMK Central Site, the LMD panics and terminates with "interface conversion: interface {} is string, not float64".

Analysis

The erroneous code is the type assertion result[0][i].(float64) in lmd/peer.go:2210. When safeguarding this line by using a conditional type assertion, an index out of bound error appears in the following line.

With the following patch, the CMK view still does not display sensible contents, but the LMD crash can be avoided:

diff --git a/lmd/peer.go b/lmd/peer.go
index 7d4a291..41665ed 100644
--- a/lmd/peer.go
+++ b/lmd/peer.go
@@ -2207,8 +2207,11 @@ func (p *Peer) PassThroughQuery(res *Response, passthroughRequest *Request, virt
                // apply stats queries
                if len(result) > 0 {
                        for i := range result[0] {
-                               val := result[0][i].(float64)
-                               res.Request.StatsResult.Stats[""][i].ApplyValue(val, int(val))
+                               if val, ok := result[0][i].(float64); ok {
+                                       if len(res.Request.StatsResult.Stats[""]) > i {
+                                               res.Request.StatsResult.Stats[""][i].ApplyValue(val, int(val))
+                                       }
+                               }
                        }
                }
        }
sni commented 6 months ago

thanks, i reworked the type assertions completely in d852c14f8b28b74b5b11e1908e5534fa3eca4bb3. At least it should not crash with the next update.

hweidner commented 5 months ago

LMD v2.2.0 still crashes in peer.go:2295 with an index out of bounds error when displaying the Checkmk "Alert statistics" view.

The following patch fixes this:

diff --git a/pkg/lmd/peer.go b/pkg/lmd/peer.go
index b3668fc..882b261 100644
--- a/pkg/lmd/peer.go
+++ b/pkg/lmd/peer.go
@@ -2292,7 +2292,9 @@ func (p *Peer) PassThroughQuery(ctx context.Context, res *Response, passthroughR
                if len(result) > 0 {
                        for i := range result[0] {
                                val := interface2float64(result[0][i])
-                               res.Request.StatsResult.Stats[""][i].ApplyValue(val, int(val))
+                               if len(res.Request.StatsResult.Stats[""]) > i {
+                                       res.Request.StatsResult.Stats[""][i].ApplyValue(val, int(val))
+                               }
                        }
                }
        }
sni commented 5 months ago

i see, i guess that's because the number of actual columns is different than the number of requested columns with stats queries.

sni commented 5 months ago

should be fine now.