Automation
Automation

Scripting How-To: Update an SNMP MIB with the value of the current mbuf allocation using the check-mbufs script

by Cordelia on ‎08-12-2015 05:20 PM - edited on ‎09-15-2017 04:14 PM by Administrator Administrator (748 Views)

Overview

Update an SNMP MIB with the value of the current mbuf allocation. This applies to SLAX version 1.0 and higher.

 

Description

 

This script runs intermittently via a generated event and fetches the output of "show system buffers". Using regular expressions, it extracts the current mbuf allocation and stuffs it in the utility mib with the key "current-mbufs". An SNMP application can monitor it from there.

 

The script is started using a generated event that makes an event called "check-mbufs-timer" every 600 seconds (10 minutes).

When the event fires off, the script is run, and the MIB value is updated.

 

Source Code

 

GitHub Links

 

The source code below is also available from GitHub at the following locations:

 

 

Example Configuration

 

01	system {
02	    scripts {
03	        op {
04	            file check-mbufs.slax;
05	        }
06	    }
07	}
08	event-options {
09	    generate-event {
10	        check-mbufs-timer time-interval 600;
11	    }
12	    policy check-mbufs {
13	        events check-mbufs-timer;
14	        then {
15	            event-script check-mbufs;
16	        }
17	    }
18	}

 

SLAX Script Contents

 

01	version 1.0;
02	 
03	ns junos = "http://xml.juniper.net/junos/*/junos";
04	ns xnm = "http://xml.juniper.net/xnm/1.1/xnm";
05	ns jcs = "http://xml.juniper.net/junos/commit-scripts/1.0";
06	ns ext = "http://xmlsoft.org/XSLT/namespace";
07	 
08	match / {
09	    <op-script-results> {
10	    var $cmd = <command> "show system buffers";
11	        var $out = jcs:invoke($cmd);
12	     
13	        var $lines = jcs:break_lines($out);
14	        for-each ($lines) {
15	            if (contains(., "current/peak/max")) {
16	                var $pattern = "([0-9]+)/([0-9]+)/([0-9]+) mbufs";
17	                var $split = jcs:regex($pattern, .);
18	                var $result = $split[2];
19	 
20	                var $rpc = <request-snmp-utility-mib-set> {
21	                    <object-type> "integer";
22	                    <instance> "current-mbufs";
23	                    <object-value> $result;
24	                }
25	                var $res = jcs:invoke($rpc);
26	                }
27	        }
28	    }
29	}

XML Script Contents

 

01	<?xml version="1.0"?>
02	<script>
03	  <title>check-mbufs.slax</title>
04	  <author>phil</author>
05	  <synopsis>
06	    Update an SNMP MIB with the value of the current mbuf allocation
07	  </synopsis>
08	  <coe>event</coe>
09	  <type>snmp</type>
10	 
11	  <description>
12	    This script runs intermittently via a generated event and fetches
13	    the output of "show system buffers".  Using regular expressions,
14	    it extracts the current mbuf allocation and stuffs it in the
15	    utility mib with the key "current-mbufs".  An SNMP application
16	    can monitor it from there.
17	  </description>
18	 
19	  <example>
20	    <description>
21	      We fire off the script using a generated event that makes an
22	      event called "check-mbufs-timer" every 600 seconds (10 minutes).
23	      When the event fires off, the script is run, and the MIB value
24	      is updated.
25	    </description>
26	    <config>simple.conf</config>
27	  </example>
28	  <xhtml:script xmlns:xhtml="http://www.w3.org/1999/xhtml"
29	                src="../../../../../web/leaf.js" type="text/javascript"/>
30	</script>