cancel
Showing results for 
Search instead for 
Did you mean: 

EV in the Snake Pit (Part III)

HanoBotha
Level 2
Employee

python_1.jpg

In Part I and Part II of this series I showed how one would go about reading information from the Enterprise Vault Directory database, using information stored in the registry.

In this section I will bring it all together to create a utility script that will show the free disk space for all EV NTFS partitions.

Step 3. Determine the free space on each disk.

There is not a Python specific library that can be used to determine the available space on a Windows disk. Luckily, the ctypes module is a Python wrapper for a the Windows SDK, and exposes al lot of the functions a Windows programmer will be used to using.

To determine the available disk space, we use the GetDiskFreeSpaceEx function, as exposed through ctypes, as follows.

def GetFreeSpaceInMB(path):

    free_bytes = ctypes.c_ulonglong(0)

    ctypes.windll.kernel32.GetDiskFreeSpaceExW(ctypes.c_wchar_p(path)…)

    return free_bytes.value/1024/1024

The function gets the available space on the disk for the path specified, and converts it to MB (/1024/1024). That is all that is required.

Step 4. Print a pretty report.

The output for the utility can be presented in many form, send to a database or written to a file.

To make the information more usable, I am going to query the partition name from the database and print the information in columns. I change the ‘main’ part of the script to:

if __name__ == "__main__":

    partitions = Partitions()

    print("\tPARTITION\t\t\tFREE SPACE");

    print("\t=========\t\t\t==========");

    for r in partitions:

        print("\t{0}\t{1}".format(r.PartitionName, GetFreeSpaceInMB(r.PartitionRootPath)))

This yields the following output:

        PARTITION                                FREE SPACE

        =========                                ==========

        Express Vault Store Ptn1        31876.859375

        Express Vault Store Ptn2        31876.859375

       

Conclusion

In this series of blogs I have demonstrated how, using Python in an Enterprise Vault environment, it is possible to quickly create and elegantly create useful utilities. This script is less that 40 lines of code, yet it reads the registry, queries the Directory database and file system.

Here is the complete script:

import winreg

import pyodbc

import os

import ctypes


def SqlServer():

   aReg = winreg.ConnectRegistry(None, winreg.HKEY_LOCAL_MACHINE)

   aKey = winreg.OpenKey(aReg, r"SOFTWARE\Wow6432Node\KVS\Enterprise Vault\Directory\DirectoryService")

   val = winreg.QueryValueEx(aKey, "SQLServer Name")

   return val[0]


def Database():

   aReg = winreg.ConnectRegistry(None, winreg.HKEY_LOCAL_MACHINE)

   aKey = winreg.OpenKey(aReg, r"SOFTWARE\Wow6432Node\KVS\Enterprise Vault\Directory\DirectoryService")

   val = winreg.QueryValueEx(aKey, "Database Name")

   return val[0]

def ConnectionString(server, db):

    conStr = "DRIVER={SQL Server};SERVER=" + server + ";DATABASE=" + db

    cnxn = pyodbc.connect(conStr)

    return cnxn.cursor()  


def Partitions():

    cursor = ConnectionString(SqlServer(), Database())

    query = "SELECT [PartitionEntryId], [PartitionRootPath],[PartitionName] FROM [dbo].[PartitionEntry]"

    cursor.execute(query)

    return cursor.fetchall()


def GetFreeSpaceInMB(path):

    free_bytes = ctypes.c_ulonglong(0)

    ctypes.windll.kernel32.GetDiskFreeSpaceExW(ctypes.c_wchar_p(path), None, None, ctypes.pointer(free_bytes))

    return free_bytes.value/1024/1024

   

if __name__ == "__main__":

    partitions = Partitions()

    print("\tPARTITION\t\t\tFREE SPACE");

    print("\t=========\t\t\t==========");

    for r in partitions:

        print("\t{0}\t{1}".format(r.PartitionName, GetFreeSpaceInMB(r.PartitionRootPath)))

As a final though I leave you with the Python philosophy, as can be obtained by typing “import this” at the interactive Python prompt (just type python in the python install directory)

The Zen of Python, by Tim Peters

Beautiful is better than ugly.

Explicit is better than implicit.

Simple is better than complex.

Complex is better than complicated.

Flat is better than nested.

Sparse is better than dense.

Readability counts.

Special cases aren't special enough to break the rules.

Although practicality beats purity.

Errors should never pass silently.

Unless explicitly silenced.

In the face of ambiguity, refuse the temptation to guess.

There should be one-- and preferably only one --obvious way to do it.

Although that way may not be obvious at first unless you're Dutch.

Now is better than never.

Although never is often better than *right* now.

If the implementation is hard to explain, it's a bad idea.

If the implementation is easy to explain, it may be a good idea.

Namespaces are one honking great idea -- let's do more of those!