Use case: fetching RVIA configuration

<< Click to Display Table of Contents >>

Raynet One > 1.1 > User Guide > Technical overview > Custom inventory scripting > Python inventory scripting 

Use case: fetching RVIA configuration

Device inventory scripts can be used to collect specific application inventory. First, the key identification details of application existence on a system need to be clarified. Based on the details, decide on the traits to detect while evaluating against version change and application environment mutation. Then, write the inventory script to capture the details from the inventory device.

 

In this use-case, we capture the RayVentory Inventory Agent (RVIA) configuration from the inventory device. Its presence is good evidence of RVIA deployment.

 

Supported OS types: Windows, Linux

Program content (inventory script)

def main():
    osf = PythonConnector.os_family()
    lines = None
    
    if osf == "Windows":
        lines = PythonConnector.run("type C:\\ProgramData\\Raynet\\RayVentoryInventoryAgent\\rvia.cfg")
    elif osf == "Linux":
        lines = PythonConnector.run("cat /opt/rvia/rvia.cfg")
    else:
        print( "failed to obtain RVIA configuration for unsupported platform" )
        return
        
    if lines:
        content = "\n".join(lines)
        CF = PythonConnector.create_item("RVIAConfig""rvia.cfg")
        PythonConnector.add_property("Content", content, CF)
        PythonConnector.add_item(CF)
        
        print( "Collected RVIA configuration inventory" )
    else:
        print( "failed to read RVIA configuration file" )
    
main()

API fetching program requirements

Python 3+

Requests library (https://docs.python-requests.org/en/latest/)

RNO API key

RNO frontend URL

API fetching program (displayinv.py)

import requests
 
APIKEY = "PUT API KEY HERE"
FRONTEND = "PUT FRONTEND URL HERE"
 
HEADERS = { "ApiKey": APIKEY }
 
def obtain_paged(EP):
    P = {}
    P["count"] = 1000
    
    items = []
    
    while True:
        result = requests.get(EP, params=P, headers=HEADERS)
        
        if result.ok == False:
            break
        
        objs = result.json()
        
        if objs == None:
            return
            
        if len(objs) == 0:
            return
        
        items.extend(objs)
        
        lastobj = objs[len(objs)-1]
        
        P["LastId"] = lastobj["id"]
        
    return items
 
def main():
    script_res = obtain_paged(FRONTEND + "/api/v1/InventoryScriptResults")
    
    if script_res == None:
        print( "failed to get inventory script results from the frontend (" + FRONTEND + ")" )
        return
        
    print( "RVIA device list" )
    print( "======================" )
    
    config_file_count = 0
        
    for SR in script_res:
        if "class" in SR and SR["class"] == "RVIAConfig" and "deviceId" in SR:
            print( "Device-id " + SR["deviceId"] + ": " )
            content = None
            if "properties" in SR:
                for P in SR["properties"]:
                    if "key" in P and P["key"] == "Content" and "value" in P:
                        content = P["value"]
                        break
            if content:
                print( content )
                print( "###" )
            else:
                print( "empty" )
            
            config_file_count = config_file_count + 1
           
    if config_file_count == 0:
        print( "none" )
    else:
        print()
        print( "found config file count: " + str(config_file_count) )
 

Screenshots

clip0133

 

The initial lines of the executed API fetching program. The header is named RVIA device list. Then, each device entry is started with the device id, followed by the configuration file content.

 

clip0132

 

The trailing part of the executed API fetching program. At the end of the listed device entries, the count of displayed entries is written.