How I Did it: Create a Custom SNMP OID

Shamelessly stealing the title lead in from author and colleague @Greg_Coward , I thought I’d share a solution for a question I answered in the forum around SNMP OIDs (the TL;DR is there)

The request focused on an SNMP OID for the device trust certificate expirations. I walked the mibs and searched through them on the system, but I didn’t find any that covers that scenario. However, I had a thought in my brain’s wayback machine that it was possible to create custom mibs. This article walks through a brief example on how to accomplish exactly that.

Resources

Configure & Test the Expiry Script

The script works as is in the K article, so you can just copy it into a file on your BIG-IP. I used /config/check_status.sh in my test but you probably want something more descriptive of what it’s checking. The steps I used at the bash prompt:

  1. vi /config/check_status.sh
  2. :set paste
  3. i
  4. paste the script from the knowledge article linked above and hit the escape key
  5. :wq!

If you use an inferior (I said what I said… :slight_smile: ) editor, these steps will look a little different. Now that the file is in place, you need to set it to executable (for our test here) and you need to change the SELinux context for later to make that file accessible to snmpd to execute.

  1. chmod +x /config/check_status.sh
  2. chcon -t bin_t /config/check_status.sh

Now run the script from the command line:

[root@bigip02:Active:In Sync] config # ./check_trust.sh 
Certificate is valid until Sep 30 21:46:25 2031 GMT (1895 Days remaining)

Ok, the script is running and reporting accurately against my device trust certificate. Now let’s put it to work.

Configure & Test the Custom OIDs

First, create your custom_mibs.tcl file in the /config/snmp/ directory:

  1. vi /config/snmp/custom_mibs.tcl
  2. :set paste
  3. i
  4. paste the script below these steps and hit the escape key
  5. :wq!
# /config/snmp/custom_mib.tcl
#   .1.3.6.1.4.1.3375.2.100.5.0  ->  trust cert, full status string
#   .1.3.6.1.4.1.3375.2.100.6.0  ->  trust cert, days remaining (int)

register_mib ".5" trust_cert_status string
register_mib ".6" trust_cert_days   int

proc trust_cert_status {} {
    set status [catch {exec /config/check_trust.sh} result]
    if {$status != 0 || $result eq ""} {
        return "ERROR: check_trust.sh failed"
    }
    return [string trim $result]
}

proc trust_cert_days {} {
    set status [catch {exec /config/check_trust.sh} result]
    if {$status != 0} {
        return -1
    }
    if {[regexp {\(([0-9]+) Days remaining\)} $result -> days]} {
        return $days
    }
    return -1
}

A couple notes before we test:

  1. You only need one of these, but I provided both the full string returned from the KA script and the days remaining numeric value as a standalone as well. Your call to keep both or pick one.
  2. If you have other custom mibs already that someone before your time created, you’ll need to make sure you avoid overwriting another value. I picked .5.0 and .6.0 here, but you can set it to whatever you want.
  3. I am using the default SNMP state on my test box, but you hopefully have something more secure configured for running your tests.

Ok, let’s restart snmpd and make sure the custom mibs were picked up:

[root@bigip02:Active:In Sync] snmp # bigstart restart snmpd                                                                    
[root@bigip02:Active:In Sync] snmp # grep -i "custom mib" /var/log/snmpd.log
custom mib initialization completed. total 2 custom mib entry registered

Good to go…now the test with snmpget:

[root@bigip02:Active:In Sync] snmp # snmpget -v2c -c public localhost .1.3.6.1.4.1.3375.2.100.5.0
F5-BIGIP-COMMON-MIB::bigipTrafficMgmt.100.5.0 = STRING: "Certificate is valid until Sep 30 21:46:25 2031 GMT (1895 Days remaining)"
[root@bigip02:Active:In Sync] snmp # snmpget -v2c -c public localhost .1.3.6.1.4.1.3375.2.100.6.0                              
F5-BIGIP-COMMON-MIB::bigipTrafficMgmt.100.6.0 = STRING: "1895"

Huzzah! Now we have functional SNMP OIDs we can target to check status of the device trust certificate expiry.

Thank you to @Blue_whale for asking the question! It was nice to revisit SNMP after so many years away with APIs and all.

3 Likes