[CentralOH] Help with learning how to use a class (please don't laugh :) )

Eric Lake ericlake at gmail.com
Wed Jul 9 16:23:19 CEST 2008

I am trying to write a script that I can use to gather information
about the windows servers we have in our network. I started off with
it being just a procedural script and I am now trying to put all of
the functions in a class. This is still very much a work in progress.
What can I do to improve on this / make it work? Any help would be
appreciated. When I run the following from ipython to test it against
my local machine I get this output:

In [64]: server = diskspace.SysInfo('localhost')

In [65]: server.GetLocalDrives()
TypeError                                 Traceback (most recent call last)

C:\Documents and Settings\elake\Desktop\<ipython console> in <module>()

TypeError: GetLocalDrives() takes no arguments (1 given)

import sys
import wmi

class SysInfo(object):
    def __init__(self, hostname):
        self.host = hostname
        server = wmi.WMI(self.host)

    def GetLocalDrives():
        driveList = []
        for disk in self.Win32_LogicalDisk():
            if disk.DriveType == 3:
        return driveList

    def GetTotalDiskSize(drive):
        for disk in server.Win32_LogicalDisk():
            if disk.Name == drive:
                total = long(disk.Size) /1073741824.0
        return total

    def GetFreeDiskSpace(drive):
        for disk in server.Win32_LogicalDisk():
            if disk.Name == drive:
                free = long(disk.FreeSpace) /1073741824.0
        return free

    def GetUsedDiskSpace(drive):
        total = GetTotalDiskSpace(drive)
        free = GetTotalDiskSpace(drive)
        used = (total - free)
        return used

    def GetStorage(self):
        server = wmi.WMI(self.host)
        print "Gather information on %s\n" % self.host
        outfile.write("%s ::\n" % self.host)
        for disk in server.Win32_LogicalDisk():
            if disk.DriveType == 3:
                total = long(disk.Size) /1073741824.0
                free = long(disk.FreeSpace) /1073741824.0
                used = (total - free)
                per_used = (used * 100) / total
                outfile.write("\tDrive %s\n" % disk.Name)
                outfile.write("\t\tTotal Size: %3.2f GB\n" % total)
                outfile.write("\t\tFree Space: %3.2f GB\n" % free)
                outfile.write("\t\tUsed Space: %3.2f GB\n" % used)
                outfile.write('\t\tPercent Used: %.2f \n' % per_used)

    def GetNodeName():
        for os in server.Win32_OperatingSystem():
            activeNode = server.CSName
            return activeNode

if __name__ == '__main__':

    infile = open('san.txt', 'r')
    outfile = open('test4.txt', 'w')

    for host in infile.readlines():
        host = host.strip()
            server = SysInfo(host)
        except KeyboardInterrupt:
            print "The user canceled the job.\n"
            outfile.write("%s does not appear to be available.\n\n" % host)

    print "Done!\n"



Eric Lake

More information about the CentralOH mailing list