使用SNMPSHARPNET为SNMPV2发送DateTime数据的正确方法是什么?

时间:2015-03-16 15:30:57

标签: c# snmp

使用SNMPSHARPNET以UTC方式将DateTime发送到SNMPV2事件的正确方法是什么?

http://www.snmpsharpnet.com/

我们一直在使用TimeTicks AsnType,但在29天半的时间里遇到了数据问题。

这是供参考的代码:

    AsnType newMIBValue = null;
if (!string.IsNullOrEmpty(MIBValueString))
{
    switch (DataType)
    {
        case MIBDataType.DateAndTime:
            //newMIBValue = new TimeTicks(MIBValueString);
            newMIBValue = ConvertDateTimeToOctetString(MIBValueString);
            break;
        case MIBDataType.SnmpAdminString:
            newMIBValue = new OctetString(MIBValueString);
            break;
        case MIBDataType.TimeTicks:
            newMIBValue = new TimeTicks(MIBValueString);
            break;
        case MIBDataType.IPAddress:
            newMIBValue = new IpAddress(MIBValueString);
            break;
        case MIBDataType.Integer:
            newMIBValue = new Integer32(MIBValueString);
            break;
        default:
            break;
    }
}

2 个答案:

答案 0 :(得分:1)

将OCTET STRING用于日期时间。 TimeTicks是一个非负整数,它指定两个事件之间经过的时间,以百分之一秒为单位。

答案 1 :(得分:0)

我的一位同事帮我提了这段代码。显然,我们已经为每个SNMP规范发送了一个十六进制值的字节数组。

private OctetString ConvertStringToOctetString(DateTime dateTimeValueInUTCDateAndTime)
    {
        var yearInString = dateTimeValueInUTCDateAndTime.Year.ToString("X");            
        byte[] bytesArray = ConvertToByteArray(string.Format("{0}{1}{2}{3}{4}{5}{6}", 
            yearInString,
            dateTimeValueInUTCDateAndTime.Month.ToString("X").PadLeft(2, '0'),
            dateTimeValueInUTCDateAndTime.Day.ToString("X").PadLeft(2, '0'),
            dateTimeValueInUTCDateAndTime.Hour.ToString("X").PadLeft(2, '0'),
            dateTimeValueInUTCDateAndTime.Minute.ToString("X").PadLeft(2, '0'),
            dateTimeValueInUTCDateAndTime.Second.ToString("X").PadLeft(2, '0'),
            "00"
            ));         
        return new OctetString(bytesArray);
    }
    public byte[] ConvertToByteArray(string value)
    {
        byte[] bytes = null;
        if (String.IsNullOrEmpty(value))
            bytes = null;
        else
        {
            int string_length = value.Length;
            int character_index = (value.StartsWith("0x", StringComparison.Ordinal)) ? 2 : 0; // Does the string define leading HEX indicator '0x'. Adjust starting index accordingly.               
            int number_of_characters = string_length - character_index;

            bool add_leading_zero = false;
            if (0 != (number_of_characters % 2))
            {
                add_leading_zero = true;

                number_of_characters += 1;  // Leading '0' has been striped from the string presentation.
            }

            bytes = new byte[number_of_characters / 2]; // Initialize our byte array to hold the converted string.

            int write_index = 0;
            if (add_leading_zero)
            {
                bytes[write_index++] = FromCharacterToByte(value[character_index], character_index);
                character_index += 1;
            }

            for (int read_index = character_index; read_index < value.Length; read_index += 2)
            {
                byte upper = FromCharacterToByte(value[read_index], read_index, 4);
                byte lower = FromCharacterToByte(value[read_index + 1], read_index + 1);

                bytes[write_index++] = (byte)(upper | lower);
            }
        }

        return bytes;
    }
    private byte FromCharacterToByte(char character, int index, int shift = 0)
    {
        byte value = (byte)character;
        if (((0x40 < value) && (0x47 > value)) || ((0x60 < value) && (0x67 > value)))
        {
            if (0x40 == (0x40 & value))
            {
                if (0x20 == (0x20 & value))
                    value = (byte)(((value + 0xA) - 0x61) << shift);
                else
                    value = (byte)(((value + 0xA) - 0x41) << shift);
            }
        }
        else if ((0x29 < value) && (0x40 > value))
            value = (byte)((value - 0x30) << shift);
        else
            throw new InvalidOperationException(String.Format("Character '{0}' at index '{1}' is not valid alphanumeric character.", character, index));

        return value;
    }