You need to enable JavaScript to run this app.
最新活动
大模型
产品
解决方案
定价
生态与合作
支持与服务
开发者
了解我们

如何修改/个性化树莓派SNMP自定义扩展的OID为MIB文件式易识别命名?

How to Customize SNMP OIDs into Human-Readable Names for Your Raspberry Pi Status Check

Hey there! I get it—those auto-generated OIDs from NET-SNMP's extend feature are a total mouthful. Let's walk through how to create a custom MIB file that maps those messy number strings to clean, standard-style names like deviceStatus instead of that unreadable OID.

Step 1: Create a Custom MIB File

First, we'll build a simple MIB file that defines our custom status object with a friendly name and clear description. Create a file named MY-CUSTOM-MIB.txt (you can pick any name, just stick to the .txt extension) with this content:

MY-CUSTOM-MIB DEFINITIONS ::= BEGIN

IMPORTS
    enterprises, Integer32 FROM SNMPv2-SMI
    OBJECT-TYPE FROM SNMPv2-SMI;

-- We'll use a sub-branch under NET-SNMP's enterprise OID (8072) for simplicity
-- For long-term use, you can apply for your own unique enterprise OID from IANA
myCustomMIB OBJECT IDENTIFIER ::= { enterprises 8072 9999 }

deviceStatus OBJECT-TYPE
    SYNTAX      Integer32 (0..1)
    MAX-ACCESS  read-only
    STATUS      current
    DESCRIPTION
        "Device alarm status: 0 = Normal (no alarm), 1 = Alarm triggered"
    ::= { myCustomMIB 1 }

END

This MIB defines a new object deviceStatus under the OID .1.3.6.1.4.1.8072.9999.1, which maps directly to your alarm status value (0 or 1).

Step 2: Install the MIB File

Move your custom MIB file to the standard NET-SNMP MIB directory so SNMP tools can automatically find it:

sudo mv MY-CUSTOM-MIB.txt /usr/share/snmp/mibs/

Now we need to tell snmpd to use our custom OID and connect it to your existing status script. Open the SNMP configuration file:

sudo nano /etc/snmp/snmpd.conf

Add these lines (you can remove the old extend snmp_status line if you don't need it anymore):

-- Map our custom OID to the script that returns the status
pass .1.3.6.1.4.1.8072.9999.1 /home/pi/BDC/snmp_status.py

A quick note: The pass command expects your script to output data in SNMP's required format. If your current script just prints 0 or 1, tweak it with this modified version to handle SNMP requests properly:

#!/usr/bin/env python3
import sys

def get_status():
    # Replace this with your existing logic to read the status file
    with open('/path/to/your/status/file.txt', 'r') as f:
        return f.read().strip()

# Handle SNMP pass request structure
if len(sys.argv) >= 4:
    oid = sys.argv[1]
    request_type = sys.argv[2]
    if request_type == 'get':
        status = get_status()
        print(f"{oid} = INTEGER: {status}")
    elif request_type == 'getnext':
        # We only have one object, so return nothing for getnext requests
        pass
else:
    # Invalid request, return nothing
    pass

Make sure the script is executable:

chmod +x /home/pi/BDC/snmp_status.py

Step 4: Restart SNMP Service and Test

Restart the snmpd service to apply all changes:

sudo systemctl restart snmpd

Now test your new human-readable OID with snmpget:

snmpget -v2c -c public localhost -m +MY-CUSTOM-MIB MY-CUSTOM-MIB::deviceStatus

You should see clean output like this (for normal status):

MY-CUSTOM-MIB::deviceStatus = INTEGER: 0

Or for alarm status:

MY-CUSTOM-MIB::deviceStatus = INTEGER: 1

Quick Additional Tips

  • Enterprise OID: For personal/non-commercial use, using NET-SNMP's branch (enterprises.8072) works perfectly. If you need a permanent, unique OID for a product, apply for one from IANA (it's free for non-commercial use).
  • MIB Validation: Use the smistrip tool to check your MIB for syntax errors: smistrip MY-CUSTOM-MIB.txt—no output means it's valid.
  • Auto-Load MIBs: To avoid adding -m +MY-CUSTOM-MIB every time, add export MIBS=+MY-CUSTOM-MIB to your ~/.bashrc file.

内容的提问来源于stack exchange,提问作者otuga

火山引擎 最新活动