A Device Monitoring System

My current project is a device monitoring system. The system has following features

  • Reliable device communication through specified interfaces
  • Web interface for system administration and device status monitoring (including remote)
  • SQL Database to archive device status data, plot trending etc.
  • Support SNMP interface to network management systems (NMS).

Notes for system implementation details.

1. For rapid development, Python was chosen.
2. Pyserial is the main package used for serial communication over RS485 serial interface.  To use it,

import serial, time

s = serial.Serial(port=’/dev/ttyUSB0′, timeout=1) # open first usb serial port

s.write(“command to send”)

time.sleep(0.2)

response = s.readline()

Since system may have multiple buses, serial driver will run as separate processes. On linux platform (python2.5 or later), To start a process in python, use

pid = Popen(‘path/prog_name’, arg1,arg2,..).pid

To kill a process, use

import os

os.kill(pid,9)

3. Web Server. To install a simple web server, just use thttpd. Be sure to comment out the chroot in your server configuration file.  Server backend is using python cgi.  To turn on cgi debugging, use,

import cgi, cgitb; cgitb.enable()

Another way is to use “import pdb;pdb.set_trace()”

To enable logging, use

import logging

logging.basicConfig(filename=’my.log’, level=logging.debug)

when you need log something, use

logging.debug(“message or vars to log”)

To get values from forms, use

form=cgi.FieldStorage()

form.getvalue(‘key’) or form.getlist(‘key’)

4. Database backend use Sqlite3 since it is included in python packages.

To create a database, you can use Python DB API 2.0

import sqlite3

c = sqlite3.connect(“test.db”,isolation_level=None) # None means immediate commit to DB for any operation

c.text_factory=str  #  let DB convert unicode to ascii for you

c.execute(“”"create table if not exists bus (bus_id INTEGER PRIMARY KEY, port TEXT, polling INTEGER, timeout INTEGER, state TEXT, pid INTEGER default 0)”"”)   # create a bus table and use bus id as primary key.

For sql query, you can embedded variable in two ways, use ? or use named labels, see following,

bus = c.execute(“select * from bus where bus_id==?”, (bus_id,)).fetchone()

c.execute(“update bus set pid=:pid where bus_id==:busid “,{‘busid’:bus[0],’pid’:pid})

5. Use template and tables to simplify web page

from string import Template

tpl = Template(open(“site.html”).read())

print “Content-type: text/html\r\n\r\n”
print tpl.substitute(dict(current=buses,confirm=confirm))

6. Use PySNMP for SNMP agent  (to be continued..)

Advertisement

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Connecting to %s


Follow

Get every new post delivered to your Inbox.