Showing posts with label Python. Show all posts
Showing posts with label Python. Show all posts

Tuesday, February 21, 2012

An InterMapper Probe in Python

InterMapper ships with Python -- the recent releases come with Python 2.7, making extending it using Python a natural choice.

So when a colleague diagnosed a problem between Cisco ACE and some application servers it load-balances for, I thought about turning his curl script into a probe. Previously I tried to put curl on the Windows 2008 server where our InterMapper software runs, but I didn't get far with that effort. I turned to Python since then when I need to do anything with InterMapper, like writing notifiers and probes.

My notifier and probe code are available on Google Code. My first probe is one that talks to the Cisco xml_agent process, which seems to be supported in more and more Cisco boxes. The probe itself is in the xmlprobe.xml file -- Simple installation instructions are found in there. It connects to the agent on a Cisco box via HTTPS and runs a command configurable either in a configuration file or via InterMapper's "set probe" dialog.

Lessons learned in writing this simple probe: I really hope InterMapper has more documentation about the details of the probe syntaxes. For example, in the section, the arg value doesn't seem to require quotes around it, which leaves the double-quote to be used for quoting individual command line parameters. Also, Windows handles quoted command line parameters differently than Linux -- That caused my much grief as the command used in this case is "show xlate global %(vip)s | count" and the vertical bar caused error when InterMapper runs the probe although it runs fine manually under Cygwin. After searching around in the InterMapper support forums, I happened upon an earlier Q&A topic and found out how to turn on the detailed logging for troubleshooting a command line probe, which revealed the error that I did not see in my manual command line tests.

This probe still looks messy: The JSON configuration data has to be edited manually. It seems that a web-based front-end tool would be helpful. It seems that InterMapper could extend its HTTP API so that the remote access client could be extended through server side plug-ins.

Thursday, February 9, 2012

Working with Cisco LMS Database in Python

Cisco has published its CiscoWorks LAN Management Solution (LMS) database schema (4.2) since version 3.2. JDBC and ODBC are supported methods for accessing data LMS has collected. Setting up ODBC is a bit involved especially when I don't have the privilege to upgrade OS in a box. I don't particularly like Java either. So I thought about getting access to the data locally and possibly provide that data remotely through a web-based interface using an easier-to-use Python-based framework, maybe something like web2py.

So I installed Jython on the Windows Server 2008 box which runs my LMS 4.1 installation, copied down some sample Java code from Cisco and translated that into Python. After a few tries to get Jython to find the right Java class path, it actually worked.

So here is the code (of course, userid and password have to be filled in) that prints a list of devices in the database:


#!/usr/bin/env jython
##
##      Requires this: export CLASSPATH='E:\CSCOpx\lib\classpath'
##


import time
import java.sql


import com.sybase.jdbc2.jdbc.SybDriver


conn = java.sql.DriverManager.getConnection("jdbc:sybase:Tds:localhost:43441?SERVICENAME=cmfDb", userid, password)
print('[%s] INFO: Connection successful...' % time.strftime("%c"))
stmt = conn.createStatement()


query = "SELECT Device_Id, Device_Display_Name, Management_IPAddress FROM Network_Devices"
rs = stmt.executeQuery(query)


cnt = 0
while rs.next():
        cnt += 1
        print("%d. Device Id: %s" % (cnt, rs.getString("Device_Id")))
        print("  Display name: %s" % rs.getString("Device_Display_Name"))
        print("  Management IP address: %s" % rs.getString("Management_IPAddress"))