TSM maintenance scripts in Python

tsmbuddy

ADSM.ORG Member
Joined
Jul 8, 2010
Messages
32
Reaction score
0
Points
0
Hello All ,

Could some one give some insight on writing a python script for daily maintenance job . A sample python script for TSM activities .
 
You can use a class to create dsmadmc objects. Import this class into your code and connect to your server(s).

Example:
Python:
from tsm_utils.py import Dsmadmc

server1 = Dsmadmc('server1_name', 'username', 'password')

# run a command on the server
cmd_output = server1.run('query process')
# run a select statement on the server
cmd_output = server1.select('select library_name, volume_name, status from libvolumes')

tsm_utils.py:
Python:
from getpass import getpass
from os import popen, path


class Dsmadmc:

    # constructor
    def __init__(self, tsm_server_name = None, tsm_user_name = None, tsm_password = None):

        self.ready = False
        self.tsm_server_name = tsm_server_name
        self.tsm_user_name = tsm_user_name
        self.tsm_password = tsm_password

        self.tsm_admin_name = ""
        self.tsm_email_address = ""
        self.tsm_bac = ""
        self.tsm_opt_file = ""
        self.tsm_options = "-dataonly=yes -tab -noc"

        self.last_process_number = None

        if self.locate_baclient():
            if self.verify_credentials():
                self.ready = True

        if not self.ready:
            print("ERROR: Dmsadmc not ready.")

    def is_ready(self):
        return self.ready
        
    # locate baclient directory
    def locate_baclient(self):
        exe_name, opt_name = "dsmadmc.exe", "dsm.opt"
        default_locations = [
            "c:\\tivoli\\tsm\\baclient\\",
            "c:\\Program Files\\tivoli\\tsm\\baclient\\",
            "c:\\Program Files (x86)\\tivoli\\tsm\\baclient\\"]

        for location in default_locations:
            if path.exists(location):
                if path.isfile(location + exe_name) and path.isfile(location + opt_name):
                    self.tsm_bac = location + exe_name
                    self.tsm_opt_file = location + opt_name
                    return True
        return False

                    
    # obtain tsm credentials
    def obtain_credentials(self, prompt='\nPlease enter your tsm credentials.'):
        while not (self.tsm_user_name and self.tsm_password):
            self.tsm_user_name, self.tsm_password = None, None
            print(prompt)
            self.tsm_user_name = input("Username: ")
            self.tsm_password = getpass("Password: ")
        

    # verify tsm credentials
    def verify_credentials(self):
        for tries in range(3):
            if not (self.tsm_user_name and self.tsm_password):
                self.obtain_credentials()
            output = self.select("SELECT admin_name, contact, email_address FROM admins WHERE admin_name='" + self.tsm_user_name.upper() + "'")
            
            if output:
                if output[0][0] == self.tsm_user_name.upper():
                    if output[0][1] != "":
                        self.tsm_admin_name = output[0][1]
                    else:
                        self.tsm_admin_name = output[0][0]

                    self.tsm_email_address = output[0][2]
                    return True               
                else:
                    print("\nError: invalid username or password.\n--------------------------------")
            self.tsm_user_name, self.tsm_password = None, None
        return False


    # run a command on the tsm server. Do not use double quotes in command.
    def run(self, tsm_cmd):
        if '"' in tsm_cmd: quote = "'"
        else: quote = '"'

        cmd_base = self.tsm_bac + " -optfile=" + self.tsm_opt_file + " -id=" + self.tsm_user_name + " -password=" + self.tsm_password + " " + self.tsm_options + " "
        cmd = cmd_base + quote + tsm_cmd + quote + " 2>&1"
        output = popen(cmd).read()

        # strip trailing newline
        if output[-1] == '\n':
            output = output[:-1]

        # find possible process number
        for line in output.split('\n'):
            if 'ANS8003I Process number' in line:
                self.last_process_number = int(line[24:].split()[0])
                break
            # ANR0609I MOVE MEDIA started as process {process_number}.
            elif 'ANR0609I MOVE MEDIA started as process' in line:
                print('***', line)
                self.last_process_number = int(line.split()[6].replace('.', ''))
                break
        else:
            self.last_process_number = None

        return output


    # run a select statement against the tsm database
    def select(self, tsm_cmd):
        # reject non select commands
        if tsm_cmd[:6].upper() != "SELECT":
            return "Error. Command not a SELECT statement. Use .run() instead."

        if '"' in tsm_cmd: quote = "'"
        else: quote = '"'

        cmd_base = self.tsm_bac + " -optfile=" + self.tsm_opt_file + " -id=" + self.tsm_user_name + " -password=" + self.tsm_password + " " + self.tsm_options + " "
        cmd = cmd_base + quote + tsm_cmd + quote + " 2>&1"
        output = popen(cmd).read()

        # strip trailing newline
        if output[-1] == '\n':
            output = output[:-1]

        # run select and split on newline and tabs
        ret = [line.split("\t") for line in output.split("\n")]

        # catch ANR2034E SELECT: No match found using this criteria.
        if ret[0][0][:8] == "ANR2034E":
            return None
            # return []

        return ret
 
Back
Top