简体   繁体   English

需要创建tql查询

[英]Need to create tql queries

I need to create TQL queries to query out sets of data from the UCMDB. 我需要创建TQL查询以从UCMDB中查询出数据集。
I am having 2 problems: 我有2个问题:

1) How can I find relationships which exists between CIs ( i do not have administrative privileges so need to do it in code somehow) I need this to get required data. 1)我如何找到配置项之间存在的关系(我没有管理特权,因此需要以某种方式在代码中进行处理)我需要这样做才能获取所需的数据。

2) I have created the following query: But I keep getting the IP property value as null. 2)我创建了以下查询:但是我一直将IP属性值获取为null。 I checked that IP has an attribute called ip_address . 我检查了IP是否具有一个名为ip_address的属性。
Code: 码:

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")); 

      } 
     }

}

In the above query output: I get the host names with IP = null 在上面的查询输出中:我获得IP = null的主机名

I have a sample query in JYthon which I am unable to figure out: Its for the above code only. 我在JYthon中有一个示例查询,但我无法弄清楚:它仅适用于以上代码。

Attaching it for anyone who can understand it. 附加给任何可以理解它的人。

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)

Please help. 请帮忙。

I am unable to understand how to go about this further. 我无法理解该如何做。

Thank you. 谢谢。

The easiest fix would be use the display_label property from the IP address CI instead of the ip_address property. 最简单的解决方法是使用IP地址CI中的display_label属性而不是ip_address属性。 The Jython reference code uses display_label for its logic. Jython参考代码使用display_label作为其逻辑。

I'd be a little concerned about using display_label since the display_label formatting logic could be changed to no display the IP address for IP CIs. 我会有点担心使用display_label,因为display_label格式设置逻辑可以更改为不显示IP CI的IP地址。 Getting data directly from the ip_address property is a better choice and should work if the TQL is defined to return that data. 直接从ip_address属性获取数据是更好的选择,并且如果定义了TQL以返回该数据,则应该可以使用。 Check the Host IP TQL and ensure that it's configured to return ip_address for IP CIs. 检查主机IP TQL,并确保已将其配置为返回IP CI的ip_address。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM