自定義服務python腳本依賴的模塊是resource_management
,該模塊的位置在/usr/lib/ambari-agent/lib/resource_management
,ambari的自定義服務程序環境就是依賴的這個目錄。
如何獲取集羣內的一些參數值
獲取ambari-server所在的主機,即主節點
from resource_management.libraries.script.script import Script
config = Script.get_config()
ambari_server_hostname = config['clusterHostInfo']['ambari_server_host'][0]
獲取集羣名稱
cluster_name = str(config['clusterName'])
獲取當前主機名稱
hostname = config['agentLevelParams']['hostname']
獲取已安裝服務組件所在主機
clusterHostInfo = config['clusterHostInfo']
## 返回的clusterHostInfo是一個數組,我們用”,”將其分割爲字符串,來看一下里面是什麼
clusterHostInfo = ",".join(params.clusterHostInfo)
## clusterHostInfo 的值爲:
snamenode_host,nm_hosts,metrics_collector_hosts,hive_metastore_host,ranger_tagsync_hosts,elasticsearch_service_hosts,ranger_usersync_hosts,slave_hosts,spark_jobhistoryserver_hosts,mymaster_hosts,infra_solr_hosts,hive_server_host,hue_server_hosts,hbase_rs_hosts,webhcat_server_host,ranger_admin_hosts,ambari_server_host,zookeeper_hosts,mysalve_hosts,spark_thriftserver_hosts,app_timeline_server_hosts,livy_server_hosts,all_ping_ports,rm_host,all_hosts,ambari_server_use_ssl,metrics_monitor_hosts,oozie_server,all_racks,all_ipv4_ips,hs_host,metrics_grafana_hosts,phoenix_query_server_hosts,ambari_server_port,namenode_host,hbase_master_hosts
## 解析:上述格式爲:component_name_hosts
## 假如我們需要查看namenode所在的主機,需要怎麼做呢?
namenode = config['clusterHostInfo']['nm_hosts']
## 或者
namenode = default("/clusterHostInfo/nm_hosts", [“localhost”])
## 上面這行代碼的意思是,如果nm_hosts不存在,則以localhost代替。可見,default這種方法比config的那種方法要更全面。
## 注意:以上namenode是一個數組,所以我們需要再做進一步處理,這裏就不再進行demo演示了
獲取ambari系統內其它已安裝服務的xml屬性值
configurations = config['configurations']
configurations = ",".join(configurations)
## configurations 的值爲:
spark-defaults,livy-log4j-properties,ranger-hdfs-audit,webhcat-log4j,ranger-yarn-plugin-properties,ranger-hdfs-policymgr-ssl,pig-env,hue-hive-site,hdfs-logsearch-conf,slider-env,ranger-hive-policymgr-ssl,hivemetastore-site,llap-cli-log4j2,spark-hive-site-override,ranger-hive-security,spark-log4j-properties,ams-logsearch-conf,ams-hbase-security-site,oozie-env,mapred-site,hue-mysql-site,spark-env,hdfs-site,hue-hadoop-site,ams-env,ams-site,ams-hbase-policy,zookeeper-log4j,hadoop-metrics2.properties,hue-env,hdfs-log4j,hbase-site,ranger-hbase-plugin-properties,ams-log4j,ranger-yarn-audit,hive-interactive-env,ranger-hdfs-plugin-properties,hue-pig-site,pig-properties,oozie-log4j,hawq-limits-env,oozie-logsearch-conf,ams-hbase-site,hive-env,ams-hbase-log4j,hadoop-env,hue-solr-site,hive-logsearch-conf,tez-interactive-site,yarn-site,parquet-logging,hive-exec-log4j,webhcat-site,sqoop-site,hawq-sysctl-env,hive-log4j,ranger-hdfs-security,hiveserver2-site,sqoop-atlas-application.properties,mapred-env,ranger-hive-audit,ranger-hbase-security,slider-client,ssl-client,sqoop-env,livy-conf,ams-grafana-env,ranger-yarn-policymgr-ssl,ranger-hbase-audit,livy-env,hive-log4j2,hive-site,spark-logsearch-conf,spark-javaopts-properties,ams-ssl-client,yarn-client,hbase-policy,webhcat-env,hive-atlas-application.properties,hue-ugsync-site,hcat-env,tez-site,slider-log4j,spark-thrift-sparkconf,spark-thrift-fairscheduler,hue-hbase-site,mapred-logsearch-conf,yarn-log4j,hue-oozie-site,ams-grafana-ini,livy-spark-blacklist,hadoop-policy,ranger-hive-plugin-properties,ams-ssl-server,tez-env,hive-interactive-site,hawq-env,ams-hbase-env,core-site,yarn-env,hawq-site,spark-metrics-properties,hbase-logsearch-conf,hue-desktop-site,hdfs-client,yarn-logsearch-conf,zookeeper-logsearch-conf,beeline-log4j2,hiveserver2-interactive-site,ranger-yarn-security,capacity-scheduler,hbase-log4j,oozie-site,ssl-server,llap-daemon-log4j,hbase-env,hawq-check-env,zoo.cfg,ranger-hbase-policymgr-ssl,hue-spark-site,hive-exec-log4j2,zookeeper-env,pig-log4j,cluster-env
## 例如,我要獲取oozie-site.xml內oozie.base.url的值
oozie_url = config['configurations']['oozie-site']['oozie.base.url']
獲取當前安裝hdp的版本
hdp_version = default("/commandParams/version", None)
# 說明:返回結果爲2.6.4.0-91,如果沒有/commandParams/version的話,結果返回None
一些特殊約定
tmp_dir = Script.get_tmp_dir()
# 結果:/var/lib/ambari-agent/tmp
config補充
config = Script.get_config()
## 打印config,內容如下:
agentConfigParams,credentialStoreEnabled,taskId,configurations,clusterName,localComponents,commandType,configuration_attributes,repositoryFile,roleParams,public_hostname,configurationTags,commandId,roleCommand,configuration_credentials,commandParams,componentVersionMap,hostname,hostLevelParams,kerberosCommandParams,serviceName,role,forceRefreshConfigTagsBeforeExecution,stageId,clusterHostInfo,requestId
# 如果是數組的話,就以“,”分隔
Logger.info(",".join(config['configurations']))
# 如果是字符串就直接打印出來
Logger.info(config['configurations'])
打印日誌
from resource_management.core.logger import Logger
Logger.info("Starting sample Service")