Python Forum

Full Version: snmp hex-string to date time
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Pages: 1 2
Hi All,


My snmpwalk output as below

SNMPv2-SMI::enterprises.6527.3.1.2.2.4.2.1.32.1.35684352 = Hex-STRING: 07 E2 05 08 00 00 00 00 2B 00 00

expect output should be, its manufacture date : - 2018/05/08

tried with below function, its showing incorrect date

import datetime as DT
hexstamps ='07 E2 05 08 00 00 00 00 2B 00 00'
for hexstamp in hexstamps.split():
    print(DT.datetime.utcfromtimestamp(float(int(hexstamp, 16))/16**4))
Output:
1970-01-01 00:00:00.000107 1970-01-01 00:00:00.003448 1970-01-01 00:00:00.000076 1970-01-01 00:00:00.000122 1970-01-01 00:00:00 1970-01-01 00:00:00 1970-01-01 00:00:00 1970-01-01 00:00:00 1970-01-01 00:00:00.000656 1970-01-01 00:00:00 1970-01-01 00:00:00
It could be this but I don't know why. The fact is that it outputs a result
>>> import datetime as DT
>>> x = "07 E2 05 08 00 00 00 00 2B 00 00"
>>> xx = x.replace(' ', '')
>>> DT.datetime.utcfromtimestamp(int(xx, 16)/2**53)
datetime.datetime(2003, 7, 12, 15, 24, 48)
The 53 could be related to the length of the mantissa in the IEEE 754 floating format.
this is what got from Manufacturer



DateAndTime
A date-time specification. field octets contents range ----- ------ -------- ----- 1 1-2 year 0..65536 2 3 month 1..12 3 4 day 1..31 4 5 hour 0..23 5 6 minutes 0..59 6 7 seconds 0..60 (use 60 for leap-second) 7 8 deci-seconds 0..9 8 9 direction from UTC '+' / '-' 9 10 hours from UTC 0..11 10 11 minutes from UTC 0..59 For example, Tuesday May 26, 1992 at 1:30:15 PM EDT would be displayed as: 1992-5-26,13:30:15.0,-4:0
Note that if only local time is known, then timezone information (fields 8-10) is not present.
TEXTUAL-CONVENTION
OCTET STRING Size(8|11)
anna Wrote:expect output should be, its manufacture date : - 2018/05/08
How do you know this is the output to expect?
this is getting through command... example

Quote:===============================================================================
Optical Interface
===============================================================================

Transceiver Data

Transceiver Status : operational
Transceiver Type : SFP
Model Number : none
TX Laser Wavelength: 1310 nm Diag Capable : yes
Connector Code : LC Vendor OUI : 00:90:65
Manufacture date : 2018/11/30 Media : Ethernet
Serial Number : KT18120144
Part Number : KT-S1310-10G-20D
Optical Compliance : 10GBASE-LR
Link Length support: 20km for SMF

===============================================================================
Transceiver Digital Diagnostic Monitoring (DDM), Internally Calibrated
===============================================================================
Value High Alarm High Warn Low Warn Low Alarm
-------------------------------------------------------------------------------
Temperature © +43.0 +90.0 +85.0 +0.0 -5.0
Supply Voltage (V) 3.31 3.80 3.70 2.80 2.70
Tx Bias Current (mA) 32.0 100.0 90.0 0.1 0.0
Tx Output Power (dBm) -1.87 1.00 0.00 -6.00 -7.00
Rx Optical Power (avg dBm) -2.76 1.00 0.00 -15.00 -16.00
===============================================================================

snmp OID are received from vendor Nokia

Quote:1.3.6.1.4.1.6527.3.1.2.2.4.2.1.17 tmnxPortConnectorType LEAF TmnxPortConnectorType
1.3.6.1.4.1.6527.3.1.2.2.4.2.1.25 tmnxPortTransceiverType LEAF INTEGER
1.3.6.1.4.1.6527.3.1.2.2.4.2.1.27 tmnxPortTransceiverLaserWaveLen LEAF Unsigned32
1.3.6.1.4.1.6527.3.1.2.2.4.2.1.28 tmnxPortTransceiverDiagCapable LEAF INTEGER
1.3.6.1.4.1.6527.3.1.2.2.4.2.1.29 tmnxPortTransceiverModelNumber LEAF TNamedItemOrEmpty
1.3.6.1.4.1.6527.3.1.2.2.4.2.1.30 tmnxPortSFPConnectorCode LEAF INTEGER
1.3.6.1.4.1.6527.3.1.2.2.4.2.1.31 tmnxPortSFPVendorOUI LEAF Unsigned32
1.3.6.1.4.1.6527.3.1.2.2.4.2.1.32 tmnxPortSFPVendorManufactureDate LEAF DateAndTime

example-- this is showing connector code... like lc, sc, etc.

Quote:SNMPv2-SMI::enterprises.6527.3.1.2.2.4.2.1.30.1.35684352 = INTEGER: 7
SNMPv2-SMI::enterprises.6527.3.1.2.2.4.2.1.30.1.35717120 = INTEGER: 7
SNMPv2-SMI::enterprises.6527.3.1.2.2.4.2.1.30.1.35749888 = INTEGER: 7

this is showing Hex-string date time

Quote:SNMPv2-SMI::enterprises.6527.3.1.2.2.4.2.1.32.1.35684352 = Hex-STRING: 07 E2 05 08 00 00 00 00 2B 00 00
SNMPv2-SMI::enterprises.6527.3.1.2.2.4.2.1.32.1.35717120 = Hex-STRING: 07 E2 05 08 00 00 00 00 2B 00 00
SNMPv2-SMI::enterprises.6527.3.1.2.2.4.2.1.32.1.35749888 = Hex-STRING: 07 E2 05 08 00 00 00 00 2B 00 00

MIB details

http://www.circitor.fr/Mibs/Html/T/TIMETRA-PORT-MIB.php
This seems to give the correct result
from collections import namedtuple

Record = namedtuple('Record',
    "year month day hour minutes seconds deciseconds "
    "direction hoursfromutc minutesfromutc"
)


def conv(data):
    x = data.replace(' ', '')
    L = [x[0:4]] + [x[i:i+2] for i in range(4, len(x), 2)]
    L = [int(s, 16) for s in L]
    L[7] = chr(L[7])
    return Record(*L)

if __name__ == '__main__':
    DATA = "07 E2 05 08 00 00 00 00 2B 00 00"
    print(conv(DATA))
Output:
Record(year=2018, month=5, day=8, hour=0, minutes=0, seconds=0, deciseconds=0, direction='+', hoursfromutc=0, minutesfromutc=0)
my code is as below, somehow some issue
from easysnmp import Session
import time
import datetime
from math import log10
from collections import namedtuple

def conv(data):
    print(data)
    Record = namedtuple('Record',
    "year month day hour minutes seconds deciseconds "
    "direction hoursfromutc minutesfromutc")
    x = data.replace(' ', '')
    L = [x[0:4]] + [x[i:i+2] for i in range(4, len(x), 2)]
    L = [int(s, 16) for s in L]
    L[7] = chr(L[7])
    manfdate = Record(*L)
    manufactured_date=(str(manfdate[0])+'/'+str(manfdate[1])+'/'+str(manfdate[2]))
    return manufactured_date

portOptical_details_list = []
for host in my_devices:
    try:
        session = Session(hostname=host, community='cacti', version=2)
    except easysnmp.EasySNMPConnectionError as e:
        print('Error:- {}'.format(e))
        sys.exit(3)
    try:
        tmnxPortOperStatus,tmnxPortTransceiverType,tmnxPortTransceiverLaserWaveLen,tmnxPortSFPConnectorCode,\
        tmnxPortSFPVendorManufactureDate,tmnxPortTransceiverDiagCapable,tmnxPortSFPMedia,tmnxPortSFPVendorSerialNum,\
        tmnxPortSFPVendorPartNum  = (list(session.bulkwalk('1.3.6.1.4.1.6527.3.1.2.2.4.2.1.'+c, \
                             non_repeaters=0, max_repetitions=50))for c in ('38','25','27','30','32','28','33','45','46'))
    except easysnmp.EasySNMPNoSuchInstanceError as e:
        print('Error:- {}'.format(e))
    finally:
        portOptical = namedtuple('portOptical','PortOperStatus PortTransceiverType PortTransceiverLaserWaveLen \
                          PortSFPConnectorCode PortSFPVendorManufactureDate PortTransceiverDiagCapable \
                          PortSFPMedia PortSFPVendorSerialNum PortSFPVendorPartNum')

        portOptical_inters = [portOptical(*uple) for uple in zip(*[tmnxPortOperStatus,tmnxPortTransceiverType,\
                      tmnxPortTransceiverLaserWaveLen,tmnxPortSFPConnectorCode,tmnxPortSFPVendorManufactureDate,\
                      tmnxPortTransceiverDiagCapable,tmnxPortSFPMedia,tmnxPortSFPVendorSerialNum,\
                      tmnxPortSFPVendorPartNum])]

        for portOptical in portOptical_inters:
            portOptical_details = {}
            port_oid = portOptical.PortOperStatus.oid
            portOptical_details['host'] = host
            portOptical_details['portID'] = port_oid.split('.')[-1]
            portOptical_details['interface'] = cli_interface(portOptical_details['portID'])
            portOptical_details['PortOperStatus'] = get_PortOperStatus(portOptical.PortOperStatus.value)
            portOptical_details['PortTransceiverType'] = get_PortTransceiverType(portOptical.PortTransceiverType.value)
            portOptical_details['PortTransceiverLaserWaveLen'] = portOptical.PortTransceiverLaserWaveLen.value
            portOptical_details['PortSFPConnectorCode'] = get_tmnxPortSFPConnectorCode(portOptical.PortSFPConnectorCode.value)
            portOptical_details['PortSFPVendorManufactureDate'] = conv(portOptical.PortSFPVendorManufactureDate.value)
            portOptical_details['PortTransceiverDiagCapable'] = get_PortTransceiverDiagCapable\
                                                            (portOptical.PortTransceiverDiagCapable.value)
            portOptical_details['PortSFPMedia'] = get_tmnxPortSFPMedia(portOptical.PortSFPMedia.value)
            portOptical_details['PortSFPVendorSerialNum'] = (portOptical.PortSFPVendorSerialNum.value).strip()
            portOptical_details['PortSFPVendorPartNum'] = (portOptical.PortSFPVendorPartNum.value).strip()
        for items in portOptical_details_list:
            print(items['PortSFPVendorManufactureDate'])
Output:
Traceback (most recent call last): File "NokiaPortOptical.py", line 179, in <module> portOptical_details['PortSFPVendorManufactureDate'] = conv(portOptical.PortSFPVendorManufactureDate.value) File "NokiaPortOptical.py", line 22, in conv L = [int(s, 16) for s in L] File "NokiaPortOptical.py", line 22, in <listcomp> L = [int(s, 16) for s in L] ValueError: invalid literal for int() with base 16: '\x07รข\x05\x08'
It is because for some reason you passed '\x07\xe2\x05\x08...' to the function conv() instead of '07 E2 05 08...'.
I am passing value directly which is getting from snmp, but while snmpwalk from cli its showing like this
Quote:SNMPv2-SMI::enterprises.6527.3.1.2.2.4.2.1.32.1.35684352 = Hex-STRING: 07 E2 05 08 00 00 00 00 2B 00 00

its hex-string 'PortSFPVendorManufactureDate' but why its not passing to function as it is

Quote:portOptical_details['PortSFPVendorManufactureDate'] = conv(portOptical.PortSFPVendorManufactureDate.value)
Try this perhaps. Why don't you adapt the code by yourself?
def conv2(data):
    L = [ord(x) for x in data]
    L[:2] = [256 * L[0] + L[1]]
    L[7] = chr(L[7])
    return Record(*L)
Pages: 1 2