• Please help support our sponsors by considering their products and services.
    Our sponsors enable us to serve you with this high-speed Internet connection and fast webservers you are currently using at ADSM.ORG.
    They support this free flow of information and knowledge exchange service at no cost to you.

    Please welcome our latest sponsor Tectrade . We can show our appreciation by learning more about Tectrade Solutions
  • Community Tip: Please Give Thanks to Those Sharing Their Knowledge.

    If you receive helpful answer on this forum, please show thanks to the poster by clicking "LIKE" link for the answer that you found helpful.

  • Community Tip: Forum Rules (PLEASE CLICK HERE TO READ BEFORE POSTING)

    Click the link above to access ADSM.ORG Acceptable Use Policy and forum rules which should be observed when using this website. Violators may be banned from this website. This notice will disappear after you have made at least 3 posts.

TSM maintenance scripts in Python

tsmbuddy

ADSM.ORG Member
#1
Hello All ,

Could some one give some insight on writing a python script for daily maintenance job . A sample python script for TSM activities .
 

CoMaboy

ADSM.ORG Senior Member
#2
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
 

Advertise at ADSM.ORG

If you are reading this, so are your potential customer. Advertise at ADSM.ORG right now.

UpCloud high performance VPS at $5/month

Get started with $25 in credits on Cloud Servers. You must use link below to receive the credit. Use the promo to get upto 5 month of FREE Linux VPS.

The Spectrum Protect TLA (Three-Letter Acronym): ISP or something else?

  • Every product needs a TLA, Let's call it ISP (IBM Spectrum Protect).

    Votes: 18 18.6%
  • Keep using TSM for Spectrum Protect.

    Votes: 59 60.8%
  • Let's be formal and just say Spectrum Protect

    Votes: 12 12.4%
  • Other (please comement)

    Votes: 8 8.2%

Forum statistics

Threads
31,664
Messages
134,990
Members
21,691
Latest member
luciano91vs
Top