Docs » Integrations Guide » Use the Smart Agent » Monitors » python-monitor

python-monitor 🔗

Monitor Type: python-monitor (Source)

Accepts Endpoints: Yes

Multiple Instances Allowed: Yes

Overview 🔗

This monitor allows you to generate metrics from a Python script.

Your Python code should be Python 2.7+ AND 3.0+ compatible. The Python runtime bundled with the agent is currently version 2.7 but this could be upgraded at anytime to Python 3. If you want to use Python 3 at this time, you can specify a custom Python binary with the pythonBinary config option.

Module Loading 🔗

The full file path to the Python script that you want to run should be specified in the scriptFilePath config option. This file will be loaded in Python by adding the directory containing the file to the front of the Python path (sys.path) and then dynamically importing the file as a module. For a hypothetical script at /opt/scripts/mymonitor.py, this is roughly equivalent to the following Python code:

import sys
sys.path.insert(0, "/opt/scripts")

import mymodule

There are two ways you can implement a monitor in Python, a simple way or a complex, but more powerful way.

Simple monitor 🔗

The simple way is to write a script that has a run function in it. This function should accept two parameters: config and output. The run function will be called on a regular interval, specified by the common intervalSeconds config option on the monitor config.

Here is an example of a simple monitor.

Complex monitor 🔗

If you need more power and flexibility in defining your monitor, you can use the complex monitor format. With this, you define a class called Monitor in a Python module. Here is a documented example of a complex monitor.

Auto-discovery 🔗

This monitor works with auto-discovery just like other monitors. If you set a discoveryRule on the monitor config, a new instance of the monitor will be created for each matching endpoint and the host and port config fields will be populated with the appropriate values and passed to the Python script in the config dictionary.

Example Config 🔗

This shows loading a Python module from the agent repo using a single custom config value (myconfig):

monitors:
 - type: python-monitor
   # pythonBinary: /usr/bin/python
   scriptFilePath: /usr/src/signalfx-agent/python/sample/monitor_complex.py
   myconfig: [1,2,3]

Configuration 🔗

To activate this monitor in the Smart Agent, add the following to your agent config:

monitors:  # All monitor config goes under this key
 - type: python-monitor
   ...  # Additional config

For a list of monitor options that are common to all monitors, see Common Configuration.

Config option Required Type Description
host no string Host will be filled in by auto-discovery if this monitor has a discovery rule.
port no integer Port will be filled in by auto-discovery if this monitor has a discovery rule. (default: 0)
scriptFilePath no string Path to the Python script that implements the monitoring logic.
pythonBinary no string By default, the agent will use its bundled Python runtime (version 2.7). If you wish to use a Python runtime that already exists on the system, specify the full path to the python binary here, e.g. /usr/bin/python3.
pythonPath no list of strings The PYTHONPATH that will be used when importing the script specified at scriptFilePath. The directory of scriptFilePath will always be included in the path.

The agent does not do any built-in filtering of metrics coming out of this monitor.