Ad Widget

Collapse

SNMP : Convert a character string in number

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • caledonien
    Junior Member
    • May 2010
    • 2

    #1

    SNMP : Convert a character string in number

    Hello,

    Do you have a method to convert a character string in number, without using an external script?

    Example:
    Code:
    /usr/bin/snmpget -Ov -v  1 -c public ${HOST} .1.3.6.1.4.1.24681.1.2.1.0
    Gives: STRING: "1.9%"

    To use it in Zabbix, I must go through an external script to not only keep the numbers
    Code:
    echo $(/usr/bin/snmpget -Ov -v  1 -c public ${HOST} .1.3.6.1.4.1.24681.1.2.1.0  | /bin/sed "s/[^0-9.]//g")
    Gives: 1.9

    The use of external scripts a lot of resource, as far as possible I can do without.

    thank you for your help and thank you for having created Zabbix
  • marcel
    Senior Member
    Zabbix Certified Specialist
    • Oct 2010
    • 112

    #2
    Originally posted by caledonien
    Hello,

    Do you have a method to convert a character string in number, without using an external script?

    Example:
    Code:
    /usr/bin/snmpget -Ov -v  1 -c public ${HOST} .1.3.6.1.4.1.24681.1.2.1.0
    Gives: STRING: "1.9%"

    To use it in Zabbix, I must go through an external script to not only keep the numbers
    Code:
    echo $(/usr/bin/snmpget -Ov -v  1 -c public ${HOST} .1.3.6.1.4.1.24681.1.2.1.0  | /bin/sed "s/[^0-9.]//g")
    Gives: 1.9

    The use of external scripts a lot of resource, as far as possible I can do without.

    thank you for your help and thank you for having created Zabbix
    use a native SNMP item in zabbix
    Zabbix Certified Specialist for Large Environments since 12/2010

    Comment

    • caledonien
      Junior Member
      • May 2010
      • 2

      #3
      Zabbix return :

      Code:
      Type of received value [1.9 %] is not suitable for value type [Numeric (float)]

      Comment

      • gdnet
        Junior Member
        • Sep 2011
        • 1

        #4
        Had the same issue with version 1.8.5

        Found out the problem lies within the file "src/libs/zbxcommon/misc.c"
        currently it does not accept any characters in the snmp string. Only digit.
        I had to modify it to stop when it detects an illegal character but still do the conversion.

        See the code below.


        Code:
        int     is_uint64(const char *str, zbx_uint64_t *value)
        {
                register zbx_uint64_t   max_uint64 = ~(zbx_uint64_t)__UINT64_C(0);
                register zbx_uint64_t   value_uint64 = 0, c;
                const char *start = str;
                while ('\0' != *str)
                {
                        if (*str >= '0' && *str <= '9')
                        {
                                c = (zbx_uint64_t)(unsigned char)(*str - '0');
                                if ((max_uint64 - c) / 10 >= value_uint64)
                                        value_uint64 = value_uint64 * 10 + c;
                                else
                                        return FAIL;    /* overflow */
                                str++;
                        }
                        else if (str == start) {
                                return FAIL;    /* not a digit */
                        }
                        else {
                             break;
                        }
                }
        
                if (NULL != value)
                        *value = value_uint64;
        
                return SUCCEED;
        }

        Comment

        • Pada
          Senior Member
          • Apr 2012
          • 236

          #5
          Zabbix 2.1.0 still does not convert String to Int/Float

          I have just upgraded to 2.1.0 and I'm still having the same issue where Zabbix won't convert the string to a decimal/float value, which is very annoying!

          Code:
          [server:temperature] became not supported: Received value [42.5 degrees C] is not suitable for value type [Numeric (float)]
          Will the base Zabbix installation/source ever do this kind of conversion?
          If not, then I'll probably just modify gdnet's code to also do negative and floating point numbers too.

          Currently I'm using marcel's Zabbix agent solution, but that isn't always an option for me, seeing that some of our devices aren't running an OS where I can install/run Zabbix Agent...

          Comment

          • Jass
            Junior Member
            • Mar 2015
            • 1

            #6
            Hello,

            Seems in 2.4 problem still not solved?

            I'm have same problem with Alteon OID: ALTEON-ROOT-MIB::aws-switch.1.2.12.4.0 = STRING: "8 CPS"

            Comment

            Working...