需要创建tql查询

发布于 2024-09-07 19:10:51 字数 3053 浏览 5 评论 0原文

我需要创建 TQL 查询来从 UCMDB 中查询数据集。
我有两个问题:

1)如何找到 CI 之间存在的关系(我没有管理权限,所以需要以某种方式在代码中完成) 我需要这个来获取所需的数据。

2) 我创建了以下查询:但我不断获取 IP 属性值为空。 我检查了 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)

请帮忙。

我无法理解如何进一步解决这个问题。

谢谢。

I need to create TQL queries to query out sets of data from the UCMDB.
I am having 2 problems:

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.

2) I have created the following query: But I keep getting the IP property value as null.
I checked that IP has an attribute called 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

I have a sample query in JYthon which I am unable to figure out: Its for the above code only.

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

夜夜流光相皎洁 2024-09-14 19:10:52

最简单的修复方法是使用 IP 地址 CI 中的 display_label 属性,而不是 ip_address 属性。 Jython 参考代码使用 display_label 作为其逻辑。

我对使用 display_label 有点担心,因为可以将 display_label 格式化逻辑更改为不显示 IP CI 的 IP 地址。直接从 ip_address 属性获取数据是更好的选择,并且如果 TQL 定义为返回该数据,则应该可以工作。检查主机 IP TQL 并确保其配置为返回 IP CI 的 ip_address。

The easiest fix would be use the display_label property from the IP address CI instead of the ip_address property. The Jython reference code uses display_label for its logic.

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. Getting data directly from the ip_address property is a better choice and should work if the TQL is defined to return that data. Check the Host IP TQL and ensure that it's configured to return ip_address for IP CIs.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文