InfluxDB: Flux - String comparison: Unterschied zwischen den Versionen

Aus Wiki-WebPerfect
Wechseln zu: Navigation, Suche
 
(5 dazwischenliegende Versionen des gleichen Benutzers werden nicht angezeigt)
Zeile 3: Zeile 3:
 
'''Example:''' <br>
 
'''Example:''' <br>
 
*We have to tags (VMName, vNICName)
 
*We have to tags (VMName, vNICName)
*We want to compare theses tags
+
*We want to compare theses tags -> '''strings.compare(v: r.<tag1>, t: r.<tag2>)'''
 
*And show only rows that these tags not matched
 
*And show only rows that these tags not matched
 +
  
 
<pre>
 
<pre>
 
import "strings"
 
import "strings"
  
from(bucket: "telegraf")
+
from(bucket: "<YOUR_BUCKET>")
 
   |> range(start: v.timeRangeStart, stop: v.timeRangeStop)
 
   |> range(start: v.timeRangeStart, stop: v.timeRangeStop)
 
   |> filter(fn: (r) =>  
 
   |> filter(fn: (r) =>  
Zeile 18: Zeile 19:
 
   |> last()
 
   |> last()
 
   |> group() //group to one table because "strings.compare" does not support multiple series
 
   |> group() //group to one table because "strings.compare" does not support multiple series
 +
  |> unique(column: "VMName")
 
   // compare VMName and vNICName
 
   // compare VMName and vNICName
 
   |> map(fn: (r) => ({r with  
 
   |> map(fn: (r) => ({r with  
       comparison: strings.compare(v: r.VMName, t: r.vNICName)
+
       comparison: strings.compare(v: strings.toUpper(v: r.VMName), t: strings.toUpper(v: r.vNICName))
 
   }))
 
   }))
 
   |> map(fn: (r) => ({r with  
 
   |> map(fn: (r) => ({r with  
Zeile 30: Zeile 32:
 
   |> keep(columns: ["_time", "VMName", "vNICName", "IsCloned"])
 
   |> keep(columns: ["_time", "VMName", "vNICName", "IsCloned"])
 
</pre>
 
</pre>
 +
-> ''The second '''map()''' is not needed. I only use the second map to create a new field named "IsCloned".''
  
 
+
''More information's about the compare function: https://docs.influxdata.com/flux/v0.x/stdlib/strings/compare/''
  
  
  
 
[[Kategorie:TIG-Stack]]
 
[[Kategorie:TIG-Stack]]

Aktuelle Version vom 23. November 2021, 09:09 Uhr

In this example I show you how to compare a tag [string] against another tag [string] in Flux.

Example:

  • We have to tags (VMName, vNICName)
  • We want to compare theses tags -> strings.compare(v: r.<tag1>, t: r.<tag2>)
  • And show only rows that these tags not matched


import "strings"

from(bucket: "<YOUR_BUCKET>")
  |> range(start: v.timeRangeStart, stop: v.timeRangeStop)
  |> filter(fn: (r) => 
    r._measurement == "hyperv_vmnet" and
    r._field == "Bytes_Received_persec" and 
    exists r.VMName //Exclude Hyper-V NICs 
  )
  |> last()
  |> group() //group to one table because "strings.compare" does not support multiple series
  |> unique(column: "VMName")
  // compare VMName and vNICName
  |> map(fn: (r) => ({r with 
      comparison: strings.compare(v: strings.toUpper(v: r.VMName), t: strings.toUpper(v: r.vNICName))
  }))
  |> map(fn: (r) => ({r with 
      IsCloned: 
        if r.comparison == 0 then "not cloned"
        else "yes"
  }))
  |> filter(fn: (r) => r.comparison != 0) // filter only cloned VMs
  |> keep(columns: ["_time", "VMName", "vNICName", "IsCloned"])

-> The second map() is not needed. I only use the second map to create a new field named "IsCloned".

More information's about the compare function: https://docs.influxdata.com/flux/v0.x/stdlib/strings/compare/