cancel
Showing results for 
Search instead for 
Did you mean: 

EV in the Snake Pit (Part II)

HanoBotha
Level 2
Employee

python_0.jpg

In the previous blog I showed how we could get the Directory database details from the Enterprise Vault server registry.

In this blog I will show how you can use this information to get information about the EV partitions.

Step 2. Query the Directory database for the partitions

Connecting to the Directory database with Python is as simple creating an ODBC connection string and connecting to the database using the pyodbc module.

import pyodbc


def ConnectionString(server, db):

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

    cnxn = pyodbc.connect(conStr)

    return cnxn.cursor()  

The function  takes a server and database parameter. This is formatted into a connection string. The connection string is used to create an ODBC connection to the database. This can then be used to execute queries against the database.

The query we would like to execute against the Directory database is:

SELECT [PartitionEntryId],[PartitionRootPath] FROM [dbo].[PartitionEntry]

Ignoring the status and device type for now, this query will give us the Partition Identity and the path. To execute this query with Python, we define the following function:

def Partitions():

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

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

    cursor.execute(query)

    return cursor.fetchall()

The Partitions function calls the ConnectionString function with the output of the SqlServer and Database functions defined in the first blog.

The returned connection object is used to execute the query. The whole result set is returned from the function.

 

To use the output of the  function, I add the following code to the end of the script:

if __name__ == "__main__":

    partitions = Partitions()

    for r in partitions:

        print("Partition:{0}".format(r.PartitionEntryId))

        print("Path:\t{0}".format(r.PartitionRootPath))

This piece of code is in the global scope of the script. Everything in a scripts global scope is always executed. A python script can be executed on its own, or used as a module to be included with other scripts. There are some hidden on ‘magic’ variables that are available at runtime for the executing script. __name__ is one of these. This variable is set to “__main__” if the script is executed from the command line.

Executing the script from the command line, calls the Partitions function.

The rest of the code iterates over the dataset returned from Partitions function and prints the Entry Id and Path for each partition.

 

Hint: Literal strings are objects in Python, allowing you to call the string class’s ‘format’ function on a literal string. The {0} is index place holders in the string that will be replaced with the parameters passed to the format function. So if you specify {0}, {1}, {2}, format needs to have 3 parameter.

 

The output is something like this:

Partition:1049B05B33887924BB5212950FC8F10C61q10000HBAIO2.local

Path:   F:\EVStorage\Express Vault Store\Express Vault Store Ptn1

Partition:1969F9F02A980164CAFFA4BEC3E2E17BC1q10000HBAIO2.local

Path:   F:\EVStorage\Express Vault Store\VP1

 

In the next blog I will use the information read from the database to find the remaining space on each partition.

The full scrip is added below. To execute it, open a command window and browse to your Python 3.3 install directory. Execute the script by typing issuing the command c>:Python.exe “your scripts full path”.

import winreg

import pyodbc


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] FROM [dbo].[PartitionEntry]"

    cursor.execute(query)

    return cursor.fetchall()


if __name__ == "__main__":

    partitions = Partitions()

    for r in partitions:

        print("Partition:{0}".format(r.PartitionEntryId))

        print("Path:\t{0}".format(r.PartitionRootPath))