Computer and IT knowledge - things to know
#!/usr/bin/env python3
"""
##############################################################################################
# Mikrotik SNMP Query Script - checkmk localcheck
#
# Description:
# This script queries SNMP OIDs for Tx signal strength, Rx signal strength, and link uptime from a specified SNMP-enabled device.
#
# Tested with device:
# - MikroTik | Cube 60Pro AC
#
# Usage:
# ./snmp_query.py --host <device_ip> [--snmp-v2c <community_string>]
#
# Examples:
# ./snmp_query.py --host 192.168.1.1 --snmp-v2c private
# ./snmp_query.py --host 192.168.1.1
#
# Author:
# mw
#
# Version:
# >>see variable version
#
# Installation
# make sure to have pysnmp installed: pip install pysnmp
#
##############################################################################################
"""
import argparse
from pysnmp.hlapi import *
import sys
version="pn-v2024-08-12"
def snmp_query(oid, target, community='public', port=161):
iterator = getCmd(
SnmpEngine(),
CommunityData(community, mpModel=0),
UdpTransportTarget((target, port)),
ContextData(),
ObjectType(ObjectIdentity(oid))
)
errorIndication, errorStatus, errorIndex, varBinds = next(iterator)
if errorIndication:
print(errorIndication)
return None
elif errorStatus:
print('%s at %s' % (errorStatus.prettyPrint(),
errorIndex and varBinds[int(errorIndex) - 1][0] or '?'))
return None
else:
for varBind in varBinds:
return varBind.prettyPrint().split(' = ')[1]
def is_number(s):
try:
float(s)
return True
except ValueError:
return False
def main():
parser = argparse.ArgumentParser(description="SNMP query script")
parser.add_argument('--host', required=True, help='The IP address of the SNMP device')
parser.add_argument('--snmp-v2c', default='public', help='The SNMP community string')
args = parser.parse_args()
target = args.host
community = args.snmp_v2c
# OIDs
tx_oid = '1.3.6.1.4.1.14988.1.1.1.2.1.13.120.154.24.192.44.11.2'
rx_oid = '1.3.6.1.4.1.14988.1.1.1.2.1.14.120.154.24.192.44.11.2'
uptime_oid = '1.3.6.1.4.1.14988.1.1.1.2.1.11.120.154.24.192.44.11.2'
# Query the OIDs
tx_signal_strength = snmp_query(tx_oid, target, community)
rx_signal_strength = snmp_query(rx_oid, target, community)
link_uptime = snmp_query(uptime_oid, target, community)
if is_number(link_uptime) and int(link_uptime) > 0:
link_uptime = int(link_uptime) / 100
link_uptime = int(link_uptime)
# Print the results
print("<<<check_mk>>>")
print(f"Version: {version}")
print("<<<local>>>")
if tx_signal_strength is None or not is_number(tx_signal_strength) or rx_signal_strength is None or not is_number(rx_signal_strength):
print(f"3 Air-Signal-dBm Tx-dBm=0|Rx-dBm=0 Tx Signal Strength in dBm: no value retrieved, Rx Signal Strength in dBm: no value retrieved")
else:
print(f"0 Air-Signal-dBm Tx-dBm={tx_signal_strength}|Rx-dBm={rx_signal_strength} Tx Signal Strength in dBm: {tx_signal_strength}, Rx Signal Strength in dBm: {rx_signal_strength}")
if link_uptime is None or not is_number(link_uptime):
print(f"3 Air-Link-Uptime uptime=0 Link Uptime in seconds: no value retrieved")
else:
print(f"0 Air-Link-Uptime uptime={link_uptime} Link Uptime in seconds: {link_uptime}")
if __name__ == '__main__':
main()
computer2know :: thank you for your visit :: have a nice day :: © 2024