Check the feasibilty on the ucmdb

Hi,

We would like to understand if the requirement below can be accomplished in UCMDB, either through the REST API or by using built-in conditions.

We have created a query to list all CI results. If the query returns zero results, we want UCMDB to trigger an email notification indicating that the results are zero and that action needs to be taken.

The system should only send an email when the query returns zero results.

Can someone please let us know if this can be achieved through UCMDB or the REST API?

Thanks,

Guru

  • 0  

    Guru;

    This can absolutely be done from the REST API.  If you wrote a query you need to use the /topology endpoint and pass the view name.  An example would be something like this in pseudocode:

    1) Authenticate

    2) Run View

    3) Determine if results are 0

    4) Send email.

    Authorization is covered in our docs:  OpenText Documentation Portal (microfocus.com)

    The call to run a view is pretty simple, for example:

    def runView(token, udserver, view, includeEmptyLayout=False, chunkSize=10000):
        '''
        Retrieves the result of a view defined in UCMDB via a REST API POST 
        call
    
        Parameters
        ----------
        token : dict
            Authentication token created by calling the function 
            'createHeaders' with arguments of ucmdb_user, ucmdb_pass, 
            and ucmdb_server.
        udserver : str
            UCMDB server hostname or IP address.
        view : str
            Name of a view on the UCMDB server.
        includeEmptyLayout : Bool
            Should empty layouts be shown?  Default is False.
        chunkSize : int
            The max number of nodes to return in each chunk.
    
        Returns
        -------
        dict
            Dictionary contains 2 entries, CIs and Relations, each of 
            which is a list of dictionaries. For example:
                {
                    "cis": [],
                    "relations": []
                }
    
        '''
        url = f'/topology?includeEmptyLayoutProperties={includeEmptyLayout}&'\
              f'chunkSize={chunkSize}'
        return requests.post(
            _url(udserver, url),
            headers=token,
            json=view,
            verify=False
        )
    

    -- Hope this helps!

    Keith Paschal

    UCMDB Worldwide Support Lead

  • 0 in reply to   

    Hi Keith, 

    I have written the Python code below. It successfully passes the authentication step. However, when I attempt to retrieve the query by passing the name of the query, it fails and returns the following error. I have tested the same endpoints in Postman, and they work correctly. Below is my code. Please let me know if I need to add any missing parameters.

    Error code

    import requests
    import json

    # Authentication details
    auth_url = ''servername:port
    username = ''
    password = ''

    # Authenticate and get the token
    try:
    auth_response = requests.post(
    auth_url,
    json={"username": username, "password": password},
    headers={"Content-Type": "application/json"}
    )
    auth_response.raise_for_status() # Raise an exception for HTTP errors

    # Extract the token from the authentication response
    auth_data = auth_response.json()
    token = auth_data.get('token')

    if not token:
    raise Exception("Token not found in the authentication response.")
    print("Token received:", token)

    # Define the query endpoint and headers
    query_url = 'server:port/rest-api/topology'
    headers = {
    'Authorization': f'Bearer {token}',
    'Content-Type': 'application/json'
    }
    body = {
    "query": "rest_query" # Correct body format
    }

    # Retrieve the query details
    query_response = requests.post(
    query_url,
    headers=headers,
    json=body
    )
    query_response.raise_for_status() # Raise an exception for HTTP errors

    # Print the query details
    print("Query Details:", query_response.json())
    except requests.exceptions.HTTPError as http_err:
    print(f"HTTP error occurred: {http_err}")
    print("Response content:", http_err.response.content) # Additional info from the response
    except Exception as err:
    print(f"An error occurred: {err}")

  • 0 in reply to 

    above is error if try to retrive the query results through python, The query name it is showing not exist is there but still it failed

  • Suggested Answer

    0   in reply to 

    Guru;

    I think the issue you have is the way you're passing the view to the /topology endpoint.  My code looks like this:

    def runView(token, udserver, view, includeEmptyLayout=False, chunkSize=10000):
        '''
        Retrieves the result of a view defined in UCMDB via a REST API POST 
        call
    
        Parameters
        ----------
        token : dict
            Authentication token created by calling the function 
            'createHeaders' with arguments of ucmdb_user, ucmdb_pass, 
            and ucmdb_server.
        udserver : str
            UCMDB server hostname or IP address.
        view : str
            Name of a view on the UCMDB server.
        includeEmptyLayout : Bool
            Should empty layouts be shown?  Default is False.
        chunkSize : int
            The max number of nodes to return in each chunk.
    
        Returns
        -------
        dict
            Dictionary contains 2 entries, CIs and Relations, each of 
            which is a list of dictionaries. For example:
                {
                    "cis": [],
                    "relations": []
                }
    
        '''
        url = f'/topology?includeEmptyLayoutProperties={includeEmptyLayout}&'\
              f'chunkSize={chunkSize}'
        return requests.post(
            _url(udserver, url),
            headers=token,
            json=view,
            verify=False
        )

    I am using the requests.post method and passing the url:  https://myserver.mycompany.com:8443/rest-api/topology?includeEmptyLayoutProperties=False&chunkSize=10000   with the headers = token (which is the bearer token dictionary) and the json=view  <---- note that 'view' is just the name of the view.  You are passing a dictionary and I'm just passing the view name.  

    -- Hope this helps!

    Keith Paschal

    UCMDB Worldwide Support Lead