[CentralOH] parsing Windows command line output

Neil Ludban nludban at osc.edu
Wed Jul 23 17:27:27 CEST 2008


On Wed, 23 Jul 2008 07:52:49 -0400
"Eric Lake" <ericlake at gmail.com> wrote:
> I have been looking for a way to monitor a Windows cluster that we
> have at work. I was really hoping for a good wmi class that I could
> use from my XP machine to do it but I have not been lucky finding one.
> I did however come across the 'cluster' command. I am thinking that I
> could just call that from within my python code and parse the output
> to get what I need. I want to get all of the data from the output and
> then put it in a database. I can not see how to split the lines up
> though. It would be easy if it was comma delimited but it seems to use
> a varying number of spaces. My only thought is that I could do a split
> on anything that is more than 2 spaces.

#!/usr/local/bin/python

import re

fin = open('cluster.txt', 'r')

fin.readline()	# Listing status for all available resources:
fin.readline()	#
fin.readline()	# Resource Group Node Status
nom_len = [ len(x)	# - - - -
            for x in re.findall('-+', fin.readline().strip()) ]
#print nom_len	# [20, 20, 15, 6]

def combinations(items, n):
    if len(items) < n:
        return
    if n == 1:
        for i in items:
            yield [ i ]
        return
    for i in range(len(items)):
        for rest in combinations(items[i+1:], n-1):
            yield [ items[i] ] + rest
    return

#for c in combinations([ 'a', 'b', 'c', 'd' ], 2):
#    print c

def score(cols):
    s = 100.0
    # small penalty if too long
    s -= 0.20 * sum([ max(len(c) - n, 0.0)
                      for c, n in zip(cols, nom_len) ])
    # large penalty if too short
    s -= 0.80 * sum([ max(n - len(c), 0.0)
                      for c, n in zip(cols, nom_len) ])
    # bonus for trailing whitespace?
    return s

for line in fin.readlines():
    line = line.strip()
    cols = re.findall('\s+|[^\s]+', line)
    #print cols
    seps = range(1, len(cols), 2)
    #print [ cols[i] for i in seps ]
    best_cols = None
    best_score = None
    for seps in combinations(seps, 3):
        tmp = [ ]
        a = 0
        for b in seps:
            tmp.append(''.join(cols[a:b+1]))
            a = b + 1
        tmp.append(''.join(cols[a:]))
        #print tmp
        s = score(tmp)
        if ((best_cols is None) or (s > best_score)):
            best_cols = tmp
            best_score = s
    print [ c.strip() for c in best_cols ]

#--#


More information about the CentralOH mailing list