Ad Widget

Collapse

Getting numeric data from data with different Units

Collapse
This topic has been answered.
X
X
 
  • Time
  • Show
Clear All
new posts
  • sbocquet
    Junior Member
    • Mar 2020
    • 12

    #1

    Getting numeric data from data with different Units

    Hi,

    I'm trying to pre-process some datas which sometime come with different units.

    For example, I sometime have this string "Usage: 572.20 MB" and sometime this one "Usage: 17.20 GB".

    I use a regex like "(\d+.\d+)" to get the string value and transform it to float, but I cannot always get the right value because of the units.
    If I use a regex like "(\d+.\d+\s\S+)" to get the unit, than I have an error message that I cannot have a string in a float zabbix item.

    What's the best way to process with such datas ?

    Thanks
  • Answer selected by sbocquet at 09-02-2022, 11:48.
    sbocquet
    Junior Member
    • Mar 2020
    • 12

    Hi,

    Thanks for the idea. I personally suck too with JS, that's probably why I haven't tried this solution before.
    Unfortunatly, I get an error while testing (see screen captures).

    Any idea ?

    EDIT : RegEx modified

    Code:
    const re = /\S+\s(\d+.\d+)\s(..)/;
    var array = re.exec(value);
    var unit = array[2];
    switch (unit) {
      case "GB":
        var result = array[1] * 1024;
        break;
      case "MB":
        var result = array[1];
        break;
      case "KB":
        var result = array[1] / 1024;
        break;
    }
    return result;
    Attached Files
    Last edited by sbocquet; 09-02-2022, 11:47.

    Comment

    • tikkami
      Member
      • May 2018
      • 71

      #2
      If you are dealing with Zabbix 5.0 or newer, you may use Javascript preprocessing.

      Comment

      • ISiroshtan
        Senior Member
        • Nov 2019
        • 324

        #3
        I generally trying to avoid JS pre-processing (mostly coz I suck at JS and don't really want to learn it), but it seems it's perfect scenario to use it. Something like below should convert data into MB from GB or KB

        Code:
        const re = /(\d+.\d) (..)/;
        var array = re.exec(value);
        var unit = array[2];
        switch (unit) {
        case "GB":
        var result = array[1] * 1024;
        break;
        case "MB":
        var result = array[1];
        break;
        case "KB":
        var result = array[1] / 1024;
        break;
        }
        return result;

        Comment

        • sbocquet
          Junior Member
          • Mar 2020
          • 12

          #4
          Hi,

          Thanks for the idea. I personally suck too with JS, that's probably why I haven't tried this solution before.
          Unfortunatly, I get an error while testing (see screen captures).

          Any idea ?

          EDIT : RegEx modified

          Code:
          const re = /\S+\s(\d+.\d+)\s(..)/;
          var array = re.exec(value);
          var unit = array[2];
          switch (unit) {
            case "GB":
              var result = array[1] * 1024;
              break;
            case "MB":
              var result = array[1];
              break;
            case "KB":
              var result = array[1] / 1024;
              break;
          }
          return result;
          Attached Files
          Last edited by sbocquet; 09-02-2022, 11:47.

          Comment

          • cyber
            Senior Member
            Zabbix Certified SpecialistZabbix Certified Professional
            • Dec 2006
            • 4807

            #5
            #metoo... for sucking at JS..

            but maybe "const re = /Usage: (\d+\.\d+) (..)/;" works better.

            Comment

            Working...