Python Monitoring UPS with SNMPWALK
阿新 • • 發佈:2018-11-26
##Background
My co-worker told me he needed to monitor UPS with SNMP module but he only can get hexadecimal digits from UPS module with SNMPWALK commands. Then I had to turn hexadecimal to decimal.
He gave me a passage about IEEE754: http://www.softelectro.ru/ieee754_en.html
C 12 F 15
B 11 E 14
C2 05 FB EE
1100 0010 0000 0101 1111 1011 1110 1110
1 = negative "-"
1
1000 01000
0101 1111 1011 1110 1110
1000 0100 = 132 - 127 = 5 so 2^5 = 32
0000 1011 1111 0111 1101 110 = 392174, so 392174/2^23 = 0.0467507
fomula = - 2^5 x (1+0.0467507) = -33.4
0101 1111 1011 1110 1110 = 392174
C205FBEE
Python Script
#!/usr/bin/env python
#-*- coding:utf-8 -*-
var = input("Please enter dec number:")
b = bin(int(var, 16))
print(b)
S = int(b[2])
#print(S)
e = b[3:11]
#print(e)
E = int(e, 2)
#print(E)
m = b[12:]
#print(m)
M = int(m, 2)
#print(M)
F = ((-1)**S)*(2**(E-127))*(1+M/(2**23))
print(round(F, 10))