Python Forum
snmp hex-string to date time
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
snmp hex-string to date time
#1
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
Reply
#2
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.
Reply
#3
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)
Reply
#4
anna Wrote:expect output should be, its manufacture date : - 2018/05/08
How do you know this is the output to expect?
Reply
#5
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
Reply
#6
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)
Reply
#7
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'
Reply
#8
It is because for some reason you passed '\x07\xe2\x05\x08...' to the function conv() instead of '07 E2 05 08...'.
Reply
#9
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)
Reply
#10
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)
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Compare current date on calendar with date format file name Fioravanti 1 123 Mar-26-2024, 08:23 AM
Last Post: Pedroski55
  Date Time Series Help...Please spra8560 2 313 Feb-01-2024, 01:38 PM
Last Post: spra8560
  Python date format changes to date & time 1418 4 518 Jan-20-2024, 04:45 AM
Last Post: 1418
  Downloading time zone aware files, getting wrong files(by date))s tester_V 9 961 Jul-23-2023, 08:32 AM
Last Post: deanhystad
  Formatting a date time string read from a csv file DosAtPython 5 1,161 Jun-19-2023, 02:12 PM
Last Post: DosAtPython
  Wait til a date and time KatManDEW 2 1,390 Mar-11-2022, 08:05 PM
Last Post: KatManDEW
  How to know coming snmp trap version in python pysnmp? ilknurg 0 2,568 Jan-31-2022, 12:16 PM
Last Post: ilknurg
  Receiving snmp traps with more than one Community String ilknurg 0 2,152 Jan-19-2022, 09:02 AM
Last Post: ilknurg
  Date format and past date check function Turtle 5 4,069 Oct-22-2021, 09:45 PM
Last Post: deanhystad
  Print first day of the week as string in date format MyerzzD 2 1,980 Sep-29-2021, 06:43 AM
Last Post: MyerzzD

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020