Make use of Python bundled with IPNetwork 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 IPNetwork Monitor includes a bundled Python environment along with other components. Even the built-in modules allow a wide variety of actions, from establishing network connections to working with local system resources. However, Python is highly extensible, and many useful third-party modules can be installed and used out of the box. In many cases, pip is already available with the bundled interpreter; if it is not, it can usually be bootstrapped in a few commands.

Install modules manager

Actually, after IPNetwork Monitor 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 try bootstrapping pip with the standard library first:

c:
cd "C:\Program Files (x86)\IPNetwork Monitor\Python-3"
python.exe -m ensurepip --default-pip
python.exe -m pip install --upgrade pip setuptools wheel

If python.exe -m pip --version still does not work, you can fall back to the official bootstrap script:

..\curl.exe -k https://bootstrap.pypa.io/get-pip.py > get-pip.py
python.exe get-pip.py

The first two commands make the bundled Python directory current.

The third command tries to bootstrap pip from the Python standard library, while the fourth upgrades the core packaging tools.

If that still does not make python.exe -m pip available, the fallback commands download and run the official get-pip.py bootstrap script.

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. The exact versions will depend on the Python build bundled with your current IPNetwork Monitor release and on whether you have upgraded the packaging tools afterwards.
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 doing calculations over several SNMP variables), you can use Python with the pysnmp module to handle the task, including custom logic around SNMPv1/v2c/v3 queries. 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 more complex, requiring browser-level interaction and multi-step user flows, you can install the selenium module and automate a real browser session from Python. Similarly, run

python -m pip install selenium

to make that module available.

Note 1: on modern Selenium releases, driver management is often handled automatically by Selenium Manager, so manual steps such as placing GeckoDriver into PATH are no longer always required. You should still verify that the target browser itself is installed and that the environment allows browser automation.

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:

python -m pip install scipy

Before doing so, it is worth verifying compatibility with the bundled Python version, since support for older Python releases changes over time and scientific packages may have stricter requirements than lighter pure-Python modules.

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)\IPNetwork Monitor\Python-3
C:\Program Files (x86)\IPNetwork Monitor\Python-3\Scripts

Also keep in mind that custom scripts and programs may run under the Monitoring Service account by default, so access to network shares, user profiles and browser-related resources can differ from what you see in an interactive command prompt session.
That would simplify working with Python. Even so, when multiple Python installations may exist on the same machine, it is often safer to use commands in the form python -m pip ... rather than relying on a bare pip command.

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.