Make use of Python bundled with IPHost to create new monitors and alerts

Make use of Python bundled with IPHost to create new monitors and alerts

Extend bundled Python with new modules to address your monitoring needs

Python on mobile

Scripting languages (VBScript, Python, PowerShell etc) are both flexible and convenient to create small scripts, to handle a simple monitoring task (such as poll a device for data or execute custom alert). Python has an advantage of being general purpose cross-platform scripting language for years, with many well-known scripts either already available on the Net, or quick to compose.

This is why we started to include basic Python environment (at the moment of writing this document, version 3.6.4) along with other IPHost components. Even the built-in modules allow performing wide variety of actions (from establishing network connections to local system resources operations). However, Python is highly extensible; there are many useful modules that can be installed and used out of box. To start using them, you only need to install modules manager, ‘pip’.

Install modules manager

Actually, after IPHost is installed, you are all set to start using the bundled Python and adding modules to it. Run elevated “cmd.exe” (right-click, “Run as Administrator”), then switch to bundled Python directory and install pip by typing the below commands:

cd "C:\Program Files (x86)\IPHost Network Monitor\Python-3.6.4"
..\curl.exe -k >

The first two commands make the bundled Python directory current.

The third command uses bundled cURL application to download official pip installer script.

The fourth command actually installs the pip (which is itself a module).

Now you can run the below command from the same cmd.exe window, to list installed external Python modules:

python -m pip list

which should display the current list of modules and their versions, something like below:

Package       Version
------------- -------
pip           20.2.1
setuptools    49.2.1
wheel         0.34.2

After that, you can explore new possibilities provided by well-known external Python modules. Let’s illustrate that.

Custom SNMP actions

Existing SNMP monitors provide basic functions to poll or set SNMP variables, receive SNMP traps.

In the majority of cases, this is sufficient. However, in case you need more elaborate actions (such as querying and/or complex math over several SNMP variables), you can use Python with pysnmp external module, to handle the task. To install the module, run the below command in bundled Python directory:

python -m pip install pysnmp

Contact us if you would need to create custom Python scripts interacting via SNMP.

Use Selenium for custom operations with Web pages

Similarly, HTTP(S) and Web Transaction Monitors cover most monitoring needs one could encounter. However, if you need something complex, requiring complicated logics, you can install selenium module and do whatever scripting required to process HTTP(S) requests. Similarly, run

python -m pip install selenium

to make that module available.

Note 1: you should do several more actions (such as placing Gecko driver into PATH) before you start using the Selenium.

Note 2: these calls most probably launch controlled Web application, browser; thus you should be careful and do extensive testing before actually putting Selenium-based monitors or actions into use. Failure to quit the controlled browser in time may quickly deplete your system resources.

Using scientific math

You might need, as a response to certain abnormal condition, gather some data and do specific math over it (such as averaging, preparing sources to draw plots and diagrams etc).

In such a case, you could install SciPy module:

python -m pip install scypy

Tips and tricks

If you don’t have other Python distributions installed on your system, consider adding the below two directories to PATH: bundled Python and its Scripts directories, namely

C:\Program Files (x86)\IPHostNetwork Monitor\Python-3.6.4
C:\Program Files (x86)\IPHostNetwork Monitor\Python-3.6.4\Scripts

That would simplify working with Python; you can use “pip” commands directly and do not erly on explicitly setting PATH in scripts in case you need module-specific executables (‘scripts”).

Further reading

We only mentioned three examples. In fact, there are much more to find at Awesome Python, curated collection of Python resources.

Feel free to ask us for help with extending bundled Python, and share with us your experience in using Python scripting for monitoring purposes. Leave your comments below!