2010-06-25 78 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。