この付録は、第3章で説明されているサンプル・スクリプトを提供します。このスクリプトは、目的の行をJythonコードにコピーするために使用できます。
例A-1 すべてのターゲットを取得して名前を出力するスクリプト
#emcli_get_targets.py #Import all emcli verbs to current program from emcli import * def print_target_details(target): ''' print the target name and target type given a target tuple. ''' print target['Target Name'] + ' ' + target['Target Type'] #Set the OMS URL to connect to set_client_property('EMCLI_OMS_URL','https://host1.example.com:1234/em') #Accept all the certificates set_client_property('EMCLI_TRUSTALL','true') #Login to the OMS login(username='adminuser') #Invoke get_targets and loop over the targets array targets_array = get_targets().out()['data'] for target in targets_array: #Call print_target_details function to print the target details print_target_details(target)
例A-2 get_targets Verbの関数を組み込んだスクリプト
#emcli_introspect_response.py #Import all emcli verbs to current program from emcli import * #Set the OMS URL to connect to set_client_property('EMCLI_OMS_URL','https://host1.example.com:1234/em') #Accept all the certificates set_client_property('EMCLI_TRUSTALL','true') #Login to the OMS login(username='sysman') res = get_targets() print 'Number of targets:'+str(len(res.out()['data'])) print 'Errors :'+res.error() print 'Exit code :'+str(res.exit_code()) print 'IsJson :'+str(res.isJson())
例A-3 カスタムSQLとlist()関数を組み込んだスクリプト
#emcli_json_processing.py #Import all EM CLI verbs to current program from emcli import * def format(str): ''' Given a string argument returns it back or returns a blank string if it is of None type ''' if str is None: return "" return str def get_targets_with_props(p_prop_name, p_prop_val): ''' Returns targets with given property name and its value. Uses list verb. ''' l_sql = "select target_name, target_type, property_value " \ "from mgmt$target_properties " \ "where property_name = '" + p_prop_name + "' " + " " \ "and property_value like '" + p_prop_val + "'" obj = list(sql=l_sql) return obj #Set the OMS URL to connect to set_client_property('EMCLI_OMS_URL','https://host1.example.com:1234/em') #Accept all the certificates set_client_property('EMCLI_TRUSTALL','true') #Log in to the OMS login(username='sysman') #Find all the targets that have Version property set to release 12 l_targets = get_targets_with_props('Version', '12%') for target in l_targets.out()['data']: tn = target['TARGET_NAME'] tt = target['TARGET_TYPE'] pv = target['PROPERTY_VALUE'] print "Name "+tn + " Type =" + tt + " value=" + pv
例A-4 例外処理を組み込んだスクリプト
#emcli_error_exception_handling.py #import all emcli verbs to current program from emcli import * #import the verbexecutionerror from emcli.exception import VerbExecutionError #Set the OMS URL to connect to set_client_property('EMCLI_OMS_URL','https://host1.example.com:1234/em') #Accept all the certificates set_client_property('EMCLI_TRUSTALL','true') #Login to the OMS login(username='sysman') #Create a group res = create_group(name='Jan_Doe_Group') print res.out() #Try to create the same group again try: #This will trigger an exception as the group exist already create_group(name='Jan_Doe_Group') except VerbExecutionError , e: print e.error() print 'Exit code:'+str(e.exit_code())
例A-5 LifeCyclePropertyChange.py
#Disclaimer #EXCEPT WHERE EXPRESSLY PROVIDED OTHERWISE, THE SITE, AND ALL CONTENT PROVIDED ON #OR THROUGH THE SITE, ARE PROVIDED ON AN "AS IS" AND "AS AVAILABLE" BASIS. ORACLE #EXPRESSLY DISCLAIMS ALL WARRANTIES OF ANY KIND, WHETHER EXPRESS OR IMPLIED, #INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS #FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT WITH RESPECT TO THE SITE AND ALL #CONTENT PROVIDED ON OR THROUGH THE SITE. ORACLE MAKES NO WARRANTY THAT: (A) THE #SITE OR CONTENT WILL MEET YOUR REQUIREMENTS; (B) THE SITE WILL BE AVAILABLE ON AN #UNINTERRUPTED, TIMELY, SECURE,OR ERROR-FREE BASIS; (C) THE RESULTS THAT MAY BE #OBTAINED FROM THE USE OF THE SITE OR ANY CONTENT PROVIDED ON OR THROUGH THE SITE #WILL BE ACCURATE OR RELIABLE; OR (D) THE QUALITY OF ANY CONTENT PURCHASED OR #OBTAINED BY YOU ON OR THROUGH THE SITE WILL MEET YOUR EXPECTATIONS. #ANY CONTENT ACCESSED, DOWNLOADED OR OTHERWISE OBTAINED ON OR THROUGH THE USE OF #THE SITE IS USED AT YOUR OWN DISCRETION AND RISK. ORACLE SHALL HAVE NO #RESPONSIBILITY FOR ANY DAMAGE TO YOUR COMPUTER SYSTEM OR LOSS OF DATA THAT #RESULTS FROM THE DOWNLOAD OR USE OF CONTENT. #ORACLE RESERVES THE RIGHT TO MAKE CHANGES OR UPDATES TO, AND MONITOR THE USE OF, #THE SITE AND CONTENT PROVIDED ON OR THROUGH THE SITE AT ANY TIME WITHOUT NOTICE. from emcli import * search_list = ['PROPERTY_NAME=\'DBVersion\'','TARGET_TYPE= \'oracle_database\'','PROPERTY_VALUE LIKE \'11.2%\''] if len(sys.argv) == 2: print login(username=sys.argv[0]) l_prop_val_to_set = sys.argv[1] l_targets = list(resource="TargetProperties", search=search_list, columns="TARGET_NAME,TARGET_TYPE,PROPERTY_NAME") for target in l_targets.out()['data']: t_pn = 'LifeCycle Status' print "INFO: Setting Property name " + t_pn + " to value " + l_prop_val_to_set print set_target_property_value(property_records=target['TARGET_NAME']+":"+target['TARGET_TYPE']+":"+t_pn+":"+l_prop_val_to_set) else: ] print "\n ERROR: Property value argument is missing" print "\n INFO: Format to run this file is filename.py <username> <Database Target LifeCycle Status Property Value>"
例A-6 dbPasswordChange.py
#Disclaimer #EXCEPT WHERE EXPRESSLY PROVIDED OTHERWISE, THE SITE, AND ALL CONTENT PROVIDED ON #OR THROUGH THE SITE, ARE PROVIDED ON AN "AS IS" AND "AS AVAILABLE" BASIS. ORACLE #EXPRESSLY DISCLAIMS ALL WARRANTIES OF ANY KIND, WHETHER EXPRESS OR IMPLIED, #INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS #FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT WITH RESPECT TO THE SITE AND ALL #CONTENT PROVIDED ON OR THROUGH THE SITE. ORACLE MAKES NO WARRANTY THAT: (A) THE #SITE OR CONTENT WILL MEET YOUR REQUIREMENTS; (B) THE SITE WILL BE AVAILABLE ON AN #UNINTERRUPTED, TIMELY, SECURE,OR ERROR-FREE BASIS; (C) THE RESULTS THAT MAY BE #OBTAINED FROM THE USE OF THE SITE OR ANY CONTENT PROVIDED ON OR THROUGH THE SITE #WILL BE ACCURATE OR RELIABLE; OR (D) THE QUALITY OF ANY CONTENT PURCHASED OR #OBTAINED BY YOU ON OR THROUGH THE SITE WILL MEET YOUR EXPECTATIONS. #ANY CONTENT ACCESSED, DOWNLOADED OR OTHERWISE OBTAINED ON OR THROUGH THE USE OF #THE SITE IS USED AT YOUR OWN DISCRETION AND RISK. ORACLE SHALL HAVE NO #RESPONSIBILITY FOR ANY DAMAGE TO YOUR COMPUTER SYSTEM OR LOSS OF DATA THAT #RESULTS FROM THE DOWNLOAD OR USE OF CONTENT. #ORACLE RESERVES THE RIGHT TO MAKE CHANGES OR UPDATES TO, AND MONITOR THE USE OF, #THE SITE AND CONTENT PROVIDED ON OR THROUGH THE SITE AT ANY TIME WITHOUT NOTICE. from emcli import * from emcli.exception import VerbExecutionError import sys import time def check_job_status(job): count=0 while (count < 10): count = count + 1 obj = emcli.get_jobs(job_id=job) #print obj.out() for entry in obj.out()['data']: l_status = entry['Status ID'] l_exec_id = entry['Execution ID'] #print entry['Status ID'] if (l_status == '5'): print "Job completed successfully" count=100 elif (l_status == '4'): l_resp = get_job_execution_detail(execution=l_exec_id, showOutput=True, xml=True) print "Job failed, error details " print "Output " + str(l_resp.out()) count=100 else: time.sleep(2) def update_db_pwd_for_target(p_target_name, p_target_type, p_old_password, p_new_password): l_target_name = p_target_name l_target_type = p_target_type print "Changing the password for member : name = " + l_target_name + " type = " + l_target_type try : l_resp = update_db_password (target_name=l_target_name, target_type = l_target_type, change_at_target="yes", user_name="dbsnmp", old_password=p_old_password, new_password=p_new_password, retype_new_password=p_new_password) l_job_submitted = l_resp.out()['JobId'] check_job_status(l_job_submitted) except emcli.exception.VerbExecutionError, e: print "ERROR : Change Password failed for name = " + l_target_name + " type = " + l_target_type print "ERROR : " + e.error() def update_db_pwd_for_group(p_group, p_old_password, p_new_password): print "Changing the password for group - " + p_group + " from " + p_old_password + " to " + p_new_password members = get_group_members(name=p_group).out()['data'] for member in members: l_target_name = member['Target Name'] l_target_type = member['Target Type'] update_db_pwd_for_target(l_target_name, l_target_type, p_old_password, p_new_password) #Set the OMS URL to connect to set_client_property('EMCLI_OMS_URL','https://myoms.com/em') #Accept all the certificates set_client_property('EMCLI_TRUSTALL','true') login(username=sys.argv[0]) l_grp_name = 'maurGroup' l_group_members = ['db1:oracle_database','db2:oracle_database','db3:rac_database'] res = create_group(name = l_grp_name, add_targets = l_group_members) print "Listing members for group " + l_grp_name for member in get_group_members(name=l_grp_name).out()['data']: print member y_n_input = raw_input('Now lets change the password for all the members in this group(y/n)') if y_n_input != 'y': exit(0) l_tgt_username = "dbsnmp" l_old_password = "secret1" l_new_password = "secret2" update_db_pwd_for_group(l_grp_name, l_old_password, l_new_password)
例A-7 promote_discovered_dbs.py
#Disclaimer #EXCEPT WHERE EXPRESSLY PROVIDED OTHERWISE, THE SITE, AND ALL CONTENT PROVIDED ON #OR THROUGH THE SITE, ARE PROVIDED ON AN "AS IS" AND "AS AVAILABLE" BASIS. ORACLE #EXPRESSLY DISCLAIMS ALL WARRANTIES OF ANY KIND, WHETHER EXPRESS OR IMPLIED, #INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS #FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT WITH RESPECT TO THE SITE AND ALL #CONTENT PROVIDED ON OR THROUGH THE SITE. ORACLE MAKES NO WARRANTY THAT: (A) THE #SITE OR CONTENT WILL MEET YOUR REQUIREMENTS; (B) THE SITE WILL BE AVAILABLE ON AN #UNINTERRUPTED, TIMELY, SECURE,OR ERROR-FREE BASIS; (C) THE RESULTS THAT MAY BE #OBTAINED FROM THE USE OF THE SITE OR ANY CONTENT PROVIDED ON OR THROUGH THE SITE #WILL BE ACCURATE OR RELIABLE; OR (D) THE QUALITY OF ANY CONTENT PURCHASED OR #OBTAINED BY YOU ON OR THROUGH THE SITE WILL MEET YOUR EXPECTATIONS. #ANY CONTENT ACCESSED, DOWNLOADED OR OTHERWISE OBTAINED ON OR THROUGH THE USE OF #THE SITE IS USED AT YOUR OWN DISCRETION AND RISK. ORACLE SHALL HAVE NO #RESPONSIBILITY FOR ANY DAMAGE TO YOUR COMPUTER SYSTEM OR LOSS OF DATA THAT #RESULTS FROM THE DOWNLOAD OR USE OF CONTENT. #ORACLE RESERVES THE RIGHT TO MAKE CHANGES OR UPDATES TO, AND MONITOR THE USE OF, #THE SITE AND CONTENT PROVIDED ON OR THROUGH THE SITE AT ANY TIME WITHOUT NOTICE. from emcli.exception import VerbExecutionError import sys alltargets=False targetparms=0 uname='' pword='' url='' monitor_pw='' def helpUsage(): print 'Usage: promote_discovered_dbs.py [-help]' print '[-all] Add all discovered Single Instance DBs' print '[-targets <target1:target2:...] Add only targets listed' sys.exit() for i in range(len(sys.argv)): if sys.argv[i] in ("-help"): helpUsage() elif sys.argv[i] in ("-targets"): if i+1 < len(sys.argv): targetparms = sys.argv[i+1] else: print 'Usage: promote_discovered_dbs.py [-help]' print '[-all] Add all discovered Single Instance DBs' print '[-targets <target1:target2:...] Add only targets listed' sys.exit() elif sys.argv[i] in ("-url"): if i+1 < len(sys.argv): url = sys.argv[i+1] elif sys.argv[i] in ("-username"): if i+1 < len(sys.argv): uname = sys.argv[i+1] elif sys.argv[i] in ("-password"): if i+1 < len(sys.argv): pword = sys.argv[i+1] elif sys.argv[i] in ("-monitor_pw"): if i+1 < len(sys.argv): monitor_pw = sys.argv[i+1] elif sys.argv[i] in ("-all"): alltargets = True # Make sure user did not specify target list and all targets. if alltargets<>0 and targetparms <>0: print 'Cannot specify target list and all switch' print 'Usage: promote_discovered_dbs.py -url <EM URL> -username <username> -password <password> -monitor_pw <password>' print '[-all] Add all discovered SI Databses' print '[-targets <target1:target2:...] Add only list targets' print '[-help]' sys.exit() if len(uname)==0 or len(pword)==0 or len(url)==0: print 'Missing required arguments (-url, -username, -password)' print 'Usage: promote_discovered_dbs.py -url <EM URL> -username <username> -password <password> -monitor_pw <password>' print '[-all] Add all discovered SI Databses' print '[-targets <target1:target2:...] Add only list targets' print '[-help]' sys.exit() # Set Connection properties and logon set_client_property('EMCLI_OMS_URL',url) set_client_property('EMCLI_TRUSTALL','true') login(username=uname,password=pword) cred_str = "UserName:dbsnmp;password:" + monitor_pw + ";Role:Normal" if targetparms <> 0: targetparms = targetparms.replace(":",":oracle_database;")+":oracle_database" target_array = get_targets(unmanaged=True,properties=True,targets=targetparms).out()['data'] elif alltargets: target_array = get_targets(targets="oracle_database",unmanaged=True,properties=True ).out()['data'] else: print 'Missing required arguments (-targets or -all)' helpUsage() if len(target_array) > 0: for target in target_array: print 'Adding target ' + target['Target Name'] + '...', for host in str.split(target['Host Info'],";"): if host.split(":")[0] == "host":] print host.split(":")[1] try: res1 = add_target(type='oracle_database',name=target['Target Name'],host=host.split(":")[1], credentials=cred_str,properties=target['Properties']) print 'Succeeded' except VerbExecutionError, e: print 'Failed' print e.error() print 'Exit code:'+str(e.exit_code()) else: print 'INFO: There are no targets to be promoted. Please verify the targets in Enterprise Manager webpages.'