2010-06-25 84 views
0

我需要創建TQL查詢來從UCMDB中查詢數據集。
我有2個問題:需要創建tql查詢

1)我如何才能找到哪些CI之間存在(我沒有管理權限,因此需要做的是在代碼中以某種方式) 我需要它來獲取所需的數據關係。

2)我創建了以下查詢:但我一直將IP屬性值設置爲null。 我檢查了IP有一個名爲ip_address的屬性。
代碼:

import com.hp.ucmdb.api.types.TopologyRelation; 

public class Main { 

    public static void main(String[] args)throws Exception { 
    final String HOST_NAME = "192.168.159.132"; 
    final int PORT = 8080; 

    UcmdbServiceProvider provider = UcmdbServiceFactory.getServiceProvider(HOST_NAME, PORT); 

    final String USERNAME = "username"; 

    final String PASSWORD = "password"; 

    Credentials credentials = provider.createCredentials(USERNAME, PASSWORD); 

    ClientContext clientContext = provider.createClientContext("Test"); 
    UcmdbService ucmdbService = provider.connect(credentials, clientContext); 

    TopologyQueryService queryService = ucmdbService.getTopologyQueryService(); 

    Topology topology = queryService.executeNamedQuery("Host IP"); 

    Collection<TopologyCI> hosts = topology.getAllCIs(); 

    for (TopologyCI host : hosts) { 


     for (TopologyRelation relation : host.getOutgoingRelations()) { 
     System.out.print("Host " + host.getPropertyValue("display_label")); 
     System.out.println (" has IP " + relation.getEnd2CI().getPropertyValue("ip_address")); 

     } 
    } 

} 

在上面查詢輸出:我得到的主機名與IP = null

我對Jython的一個示例查詢對此我無法弄清楚:它只有上面的代碼。

附加給任何人都可以理解它。

import sys 

UCMDB_API="c:/ucmdb/api/ucmdb-api.jar" 

sys.path.append(UCMDB_API) 

from com.hp.ucmdb.api import * 

# 0) Connection settings 
HOST_NAME="192.168.159.132" 
PORT=8080 

USERNAME="username" 
PASSWORD="password" 

# 1) Get a Service Provider from the UcmdbServiceFactory 
provider = UcmdbServiceFactory.getServiceProvider(HOST_NAME, PORT) 

# 2) Setup credentials to log in 
credentials = provider.createCredentials(USERNAME, PASSWORD) 

# 3) Create a client context 
clientContext = provider.createClientContext("TESTING") 

# 4) Connect and retrieve a UcmdbService object 
ucmdbService = provider.connect(credentials, clientContext) 

# 5) Get the TopologyQueryService from the UcmdbService 
queryService = ucmdbService.getTopologyQueryService() 

# ======= Everything After this is specific to the query ======= 

# 6) Execute a Named Query and get the Topology 
topology = queryService.executeNamedQuery('Host IP') 

# 7) Get the hosts 
hosts = topology.getAllCIs() 

# 8) Print the hosts and IPs 
host_ip = {} 

for host in hosts: 
    host_name = host.getPropertyValue("display_label") 
    if host_name in host_ip.keys(): 
     ips = host_ip[host_name] 
    else: 
     ips = {} 
     host_ip[host_name] = ips 
    for relation in host.getOutgoingRelations(): 
     ip_address = relation.getEnd2CI().getPropertyValue("display_label") 
     if ip_address in ips.keys(): 
      pass 
     else: 
      ips[ip_address] = '' 
      print "%s , %s" % (host_name, ip_address) 

請幫忙。

我無法理解如何進一步解決這個問題。

謝謝。

回答

1

最簡單的修復方法是使用IP地址CI中的display_label屬性而不是ip_address屬性。 Jython參考代碼的邏輯使用display_label。

我會有點擔心使用display_label,因爲display_label格式化邏輯可能會更改爲不顯示IP CI的IP地址。直接從ip_address屬性獲取數據是更好的選擇,並且應該在TQL定義爲返回該數據的情況下運行。檢查主機IP TQL,並確保它已配置爲返回IP配置項的ip_address。