Using the InterMapper DataCenter Python interpreter

InterMapper DataCenter ships with an embedded Python interpreter. You may use this interpreter to write command-line probe scripts and command-line notifiers. If you are writing a probe in Python that may be of use to other InterMapper users, please use this Python interpreter for maximum compatibility. In InterMapper DataCenter 4.6, the version of Python we ship is 2.4.4, with optimized system libraries.

An extensive introductory tutorial on Python is available at http://docs.python.org/tut

IMDC's Python interpreter is installed in /usr/local/imdc/core/python/bin/imdc on UNIX platforms and in c:\Program Files\InterMapper\dwf\core\python\imdc.exe on Windows.

As shipped, this Python interpreter requires the use of optimized and stripped mode (-OO), so the interpreter must be invoked as /usr/local/imdc/core/python/bin/imdc -OO script_name on UNIX and c:\Program Files\InterMapper\dwf\core\python\imdc.exe -OO script_name on Windows.

Python scripts that use this interpreter will have the following general form:


#! /usr/local/imdc/core/python/bin/imdc -OO
# Sample script using the InterMapper DataCenter Python interpreter
#
# The first line is a Python comment, and is ignored by Python.
# On a UNIX platform, it identifies the interpreter that should be
# used to run the script.  
#
# On Windows, the initial comment line is ignored, and the command 
# line (see below) must explicitly reference the interpreter, which is:
# c:\Program Files\InterMapper\dwf\core\python\imdc.exe

print "Hello from Python"

A silly sample Python script would look like this. (Save this file as sample.py in the InterMapper Settings/Tools directory.)


#! /usr/local/imdc/core/python/bin/imdc -OO
# Sample probe script using the InterMapper DataCenter Python interpreter
# This script is similar to the sample Perl command-line probe script
# Save this text in a file named sample.py in the Tools directory

import sys

if (len(sys.argv) < 2):
	print "Usage: %s _address_" % sys.argv[0]
	sys.exit(0)
addr = sys.argv[1]

# Code to get status from device at address addr
import random
result = random.randrange(4)

print "Pretending we got result %d from device at address %s" % (result, addr)
sys.exit(result)

A matching probe that uses this script looks like this:


<!--
Comand Line Python Sample (com.dartware.python.sample.txt)
Custom Probe for InterMapper (http://www.intermapper.com)
Please feel free to use it as a base for further development.
Original version 31 Mar 2004 by Christopher L. Sweeney, Dartware, LLC.
Updated 13 Jun 2007 by Stephen P. Ryan, Dartware, LLC, for Python
Updated 28 Dec 2007 to update text descriptions and 
     include display_name header line -reb
-->

<header>
   type="cmd-line"
   package="com.dartware"
   probe_name="python.sample"
   human_name="Python Sample"
   version="1.0"
   address_type="IP"
   display_name = "Miscellaneous/Testing/Python Sample"
</header>

<description>
\GB\Python Sample Command-Line Probe\p\

A sample command line probe which executes a Python script. 

The Python script generates and returns a random number which sets
the device status to one of four values Down/Alarm/Warning/OK.

</description>

<parameters>
</parameters>

<command-line>
   -- Unix/OSX: Empty path forces the InterMapper Settings:Tools directory
   path=""
   cmd="sample.py ${ADDRESS}"
   -- Windows - path must point to Python interpreter; command line program must be in Tools
   -- path="c:\Program Files\InterMapper\dwf\core\python"
   -- cmd="imdc.exe"
   -- arg = "-OO sample.py ${ADDRESS}"
</command-line>

<command-exit>
   down:${EXIT_CODE}=3
   alarm:${EXIT_CODE}=2
   warning:${EXIT_CODE}=1
   okay:${EXIT_CODE}=0
</command-exit>

<command-display>

</command-display>

One change needs to be made for Windows: the command line (cmd=... below) needs to be altered to correctly locate the Python interpreter.


<command-line>
   -- Windows - path must point to Python interpreter; command line program must be in Tools
   path="c:\Program Files\InterMapper\dwf\core\python"
   cmd="imdc.exe"
   arg = "-OO sample.py ${ADDRESS}"
</command-line>

Command-line Notifiers

On Unix, the #! line allows the interpreter to be placed elsewhere, and the operating system will correctly resolve the reference to it. Such a command-line notifier might look like this:


#! /usr/local/imdc/core/python/bin/imdc -OO
# Sample notifier using the InterMapper DataCenter Python interpreter
# For a mythical lava-lamp based notification system.

import sys
import lavalamp_control

state=argv[1]

if state == 'On':
    lavalamp.turn_on
else:
    lavalamp.turn_off

Add this file to the Tools directory, and reference it when creating a notification as "lavalamp.py On|Off".

Command-line notifiers on Windows are unable to use this Python interpreter due to the fact that the executable must reside in ....\InterMapper Settings\Tools, and the Python interpreter isn't in there. While it is possible to work around this limitation, we do not recommend that at this time due to potential security holes that may be introduced as a result (anyone who can set parameters passed to such a script can also run arbitrary Python code, which could be quite dangerous).