Python script as an input source

There are two documents that we should read before we use our Python script as an input source:

One might wonder what are the use cases for using Python script as an extension for data inputs? One of the use cases would be to provide monitoring to resources that do not have a corresponding OID. Say, we would like to know how many times the access list permit_snmp has allowed the host 172.16.1.173 for conducting an SNMP query. We know we can see the number of matches via the CLI:

iosv-1#sh ip access-lists permit_snmp | i 172.16.1.173
10 permit 172.16.1.173 log (6362 matches)

However, chances are that there are no OIDs associated with this value (or we can pretend that there is none). This is where we can use an external script to produce an output that can be consumed by the Cacti host.

We can reuse the Pexpect script we discussed in Chapter 2, Low-Level Network Device Interactions, chapter1_1.py. We will rename it to cacti_1.py. Everything should be familiar to the original script, except that we will execute the CLI command and save the output:

for device in devices.keys():
...
child.sendline('sh ip access-lists permit_snmp | i 172.16.1.173')
child.expect(device_prompt)
output = child.before
...
The output in its raw form will appear as below: 
b'sh ip access-lists permit_snmp | i 172.16.1.173 10 permit 172.16.1.173 log (6428 matches) '

We will use the split() function for the string to only leave the number of matches and print them out on standard output in the script:

print(str(output).split('(')[1].split()[0])

To test, we can see the number of increments by executing the script a number of times:

$ ./cacti_1.py
6428
$ ./cacti_1.py
6560
$ ./cacti_1.py
6758

We can make the script executable and put it into the default Cacti script location:

$ chmod a+x cacti_1.py
$ sudo cp cacti_1.py /usr/share/cacti/site/scripts/

Cacti documentation, available at http://www.cacti.net/downloads/docs/html/how_to.html, provides detailed steps on how to add the script result to the output graph. The steps include adding the script as a data input method, adding the input method to a data source, then creating a graph to be viewed:

SNMP is a common way to provide network monitoring services to the devices. RRDtool with Cacti as the frontend provides a good platform to be used for all the network devices via SNMP.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.147.71.94