Examples

Hacking PyMeter

Below are examples of working with PyMeter's internals. 'pymeterlib' is a Python package that contains all of the PyMeter modules and classes. It contains some useful API's for creating external monitoring scripts or tools.


Collecting CPU Utilization stat from a Solaris host (local)

using a single plugin:

#!/usr/bin/env python

from pymeterlib import *
from pymeterlib.plugins import *

#create an instance of the CPUUtilization plugin
plugin = solaris.CPUUtilization.CPUUtilization()

#create a Host instance with your plugin
my_host = Host.Host(plugin)

#call the check_stat method to execute a check
my_host.check_stats()

#print some results
print my_host.stat_labels
print my_host.last_stats

Output:

['CPU Utilization']
['10']

Collecting CPU Utilization stat from a remote Solaris host over a TELNET connection

using a single plugin:

#!/usr/bin/env python

from pymeterlib import *
from pymeterlib.plugins import *

#create an instance of the CPUUtilization plugin
plugin = solaris.CPUUtilization.CPUUtilization()

#create a RemoteHost instance with your connection parameters, connection type, and plugin
my_host = RemoteHost.RemoteHost("foo.goldb.org", "foo", "welcome", "-bash-2.05b$", "telnet", plugin)

#call the check_stat method to execute a check
my_host.check_stats()

#print some results
print my_host.stat_labels
print my_host.last_stats

Output:

['CPU Utilization']
['15']

Collecting multiple stats from a remote Solaris host over a TELNET connection

Using a list of plugins:

#!/usr/bin/env python

from pymeterlib import *
from pymeterlib.plugins import *

#create a list of plugin instances
plugins = [
	solaris.CPUUtilization.CPUUtilization(), 
	solaris.CPUUser.CPUUser(), 
	solaris.Uptime.Uptime()
]

#create a RemoteHost instance with your connection parameters, connection type, and plugins
my_host = RemoteHost.RemoteHost("foo.goldb.org", "foo", "welcome", "-bash-2.05b$", "telnet", *plugins)

#call the check_stat method to execute a check
my_host.check_stats()

#print some results
print my_host.stat_labels
print my_host.last_stats

Output:

['CPU Utilization', 'CPU User Time', 'System Uptime']
['15', '14', '16 day(s)']

Using TelnetController to run commands on a remote host

#!/usr/bin/env python

from pymeterlib import TelnetController

host_name = "foo.goldb.org"
user_name = "foo"
password = "welcome"
prompt = "-bash-2.05b$"

#create a TelnetController instance with your connection parameters
tc = TelnetController.TelnetController(host_name, user_name, password, prompt)

#run a single system command on the remote unix host
print tc.run_atomic_command("python -V")

#run multiple commands on the remote unix host using a single TELNET session
tc.login()
print tc.run_command("pwd")
print tc.run_command("uptime")
tc.logout()

Output:

Python 2.3.4
/home/cmg
 11:58:13 up 12 days, 23:25,  3 users,  load average: 0.04, 0.11, 0.13
Validate: XHTML | CSS