Video Screencast Help
Enterprise Vault Engineering Blog

EV in the Snake Pit (Part III)

Doing things to EV with Python.
Created: 18 Jun 2014 • Updated: 18 Jun 2014 • 3 comments
HanoBotha's picture
+1 1 Vote
Login to vote

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!

Comments 3 CommentsJump to latest comment

Baris Aydogmusoglu's picture

Thank you for this series.

I have 4 partitions, script result shows 0.0 free space. How can I fix it?

PS. Partitions are located in a folder which is a mount point!

 

Senior System Expert

Microsoft Exchange Server

Symantec Enterprise Vault

http://www.aydogmusoglu.com

http://www.e-vault.info

0
Login to vote
Paul Honey's picture

Hi Baris

Ok, looks like mount point is pertinent to this. The script was developped and tested against partitions in folders on local drives to demonstrate the theory. Leave it with us and we'll see if it can be extended to cater for the mount point scenario as well

Regards

Paul

Paul Honey | Enterprise Vault Engineering | Customer Focus Team (CFT) | Symantec Corporation | www.symantec.com

0
Login to vote
Shinichi Hayashi's picture

Hi Baris

I have updated my post on how to do the same for mount points in PowerShell. 

[PowerShell] Show FreeSpace of each Partition (Pt 4)

Hope this helps.

Regards

Shinichi Hayashi

 

 

0
Login to vote