Wednesday, February 6, 2008

Performance Counters - Python

The module win32pdh allows to retrieve performance counters on Windows host
Here is an example published on ASPN.
You will have to use "win32pdhutil.browse" to translate counters names
in your language.

import win32pdh
import win32pdhutil

#Use win32pdh, Kevan Heydon, 2006/06/23


def uptimeServeur(serveur='Python'):
path = win32pdh.MakeCounterPath( ( serveur, 'Système', None, None, 0, "Temps d'activité système") )
query = win32pdh.OpenQuery()
handle = win32pdh.AddCounter( query, path )
win32pdh.CollectQueryData( query )
seconds = win32pdh.GetFormattedCounterValue( handle, win32pdh.PDH_FMT_LONG | win32pdh.PDH_FMT_NOSCALE )[ 1 ]
return "Serveur : %s - Uptime: %d seconds" %(serveur,seconds )

print uptimeServeur()

Sunday, February 3, 2008

HTA - Python - Sorting Table

This example shows a method to sort a html table in Python within a HTA file
The things to note are:
- Inserting dynamically within a url the name of the list to be display.
- How to sort a list by index

To run HTA files, simply copy this peace of code with the extension '.hta'
and of course Python must be installed (ActiveState Python will do the job)

<TITLE>HTML Application Example</TITLE>

<body onLoad="Initialisation()">

<table><tr><td valign="top"><div id="menu">Menu</td><td><div id="content">contenu</div></td></tr></table>

<script language="Python">

from win32com.client import GetObject
import os
import cPickle as pickle
import base64
import operator

sorteddata = [0]*10

donnee = [('Name','Town','Age'),('Albert','Paris',46),

services = {}

ChaineConnexion = r"WinMgMts:\\%s\%s"

<p>Menu Principal</p>
<select name="choices" onChange="SelectOption()">
<option value="service">Service</option>
<option value="process">Process</option>

def getValue(id):
#return document.getElementById(id).value
return document.getElementById(id).getAttribute("value")

def getElt(id):
return document.getElementById(id)

def setValue(divid,data):
elt = document.getElementById(divid)
elt.innerHTML = data

def Pointer(): = "hand"

def Default(): = "default"

def sortTable(idcol,table):
obj = decodeObj(table)
titre = list(obj[0])
data = obj[1:]
index = titre.index(idcol)
sorteddata[index] = 1 - sorteddata[index]
t = formatTable(data)

def decodeObj(s):
objects = base64.decodestring(s)
return pickle.loads(objects)

def encodeObject(obj):
data = base64.encodestring(pickle.dumps(obj))
return data.__repr__()

def formatTable(data):
tablestr = encodeObject(data)
titre = data[0]
numitem = len(titre)
lnktitre = ["""<span onclick="sortTable('%s',%s)" \
onmouseover='Pointer()' \
onmouseout='Default()'>%s</span>"""%(t,tablestr,t) for t in titre]
table = "<table border='1'>"
table += "<tr>" + ("<td>%s</td>"*numitem)%tuple(lnktitre) + "</tr>"
for item in data[1:] :
table += "<tr>" + ("<td>%s</td>"*numitem)%item + "</tr>"
table += "</table>"
return table

def getAllObjects(serveur = ".", espaceDenom = "root\cimv2", classe = "Win32_Service"):
c = GetObject(ChaineConnexion%(serveur,espaceDenom))
objects = c.ExecQuery(r"select * from %s"%classe)
return objects

def getService():
data = [('Caption','State')]
services = getAllObjects()
for svc in services:
setValue('content',formatTable (data))

def getProcess():
data = [('Handle','Name','CommandLine')]
process = getAllObjects(classe="Win32_Process")
for proc in process:
setValue('content',formatTable (data))

def SelectOption():

services['service'] = getService
services['process'] = getProcess

def Initialisation():