Use case: device log fetch with REST API and File Scan Spec

<< Click to Display Table of Contents >>

Raynet One > 1.1 > User Guide > Technical overview > API 

Use case: device log fetch with REST API and File Scan Spec

Each device running in your IT landscape does have its own set of applications and services. During application execution, log files should be generated about the success or failure of application components. Log files are valuable resources to IT administration as they detail the integrity of your IT landscape. In this use case, you learn how to fetch device log files quickly and efficiently.

 

We use the powerful file scan specifications feature to describe the files we need from the machines.

Requirements

Python 3+

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

API key

frontend URL

file scan specifications below set to the related instruments

the File scan content size limit parameter of each relevant instrument set to 20000000

a folder called output in the same folder as the program

File scan specification

Zero Touch Windows Device Scan

[
    {
        "ContentSizeLimit": 20000000,
        "Drive""C:",
        "Extension""txt",
        "IsContent"true,
        "Path""\\Program Files (x86)\\Raynet\\Runner\\logs\\"
    },
    {
        "ContentSizeLimit": 20000000,
        "Drive""C:",
        "Extension""log",
        "IsContent"true,
        "Path""\\Windows\\Logs\\"
    },
    {
        "ContentSizeLimit": 20000000,
        "Drive""C:",
        "Extension""log",
        "IsContent"true,
        "Path""\\Windows\\Temp\\"
    },
    {
        "ContentSizeLimit": 20000000,
        "Drive""C:",
        "Extension""log",
        "IsContent"true,
        "Path""\\Windows\\"
    }
]

Zero Touch Unix Device Scan

[
    {
        "CaptureFileContentLimit": 20000000,
        "IncludeFilenamePattern""*.txt",
        "IsCaptureFileContent"true,
        "TargetDirectory""/app/logs"
    },
    {
        "CaptureFileContentLimit": 20000000,
        "IncludeFilenamePattern""*.txt",
        "IsCaptureFileContent"true,
        "TargetDirectory""/app/Logs"
    },
    {
        "CaptureFileContentLimit": 20000000,
        "IncludeFilenamePattern""*.log",
        "IsCaptureFileContent"true,
        "TargetDirectory""/var/log"
    },
    {
        "CaptureFileContentLimit": 20000000,
        "IncludeFilenamePattern""*.log.*",
        "IsCaptureFileContent"true,
        "TargetDirectory""/var/log"
    }
]

Program content

import requests
import os
import base64
 
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 escape_path(S):
    return S.replace("/""_").replace("\\""_").replace(" ""-").replace(":""_")
 
def main():
    devs = obtain_paged(FRONTEND + "/api/v1/Devices")
    files = obtain_paged(FRONTEND + "/api/v1/File")
    
    if devs == None or files == None:
        print( "request failure to frontend (" + FRONTEND + ")")
        return
    
    uidx = 1
    
    for F in files:
        if "content" in F:
            dev = None
            
            if "deviceId" in F:
                for D in devs:
                    if D["id"] == F["deviceId"]:
                        if "name" in D:
                            dev = D["name"]
                        else:
                            dev = D["id"]
                        break
            
            path = ""
            
            if dev != None:
                path = path + escape_path(dev) + "="
            
            if "name" in F:
                path = path + escape_path(F["name"])
            
            if len(path) == 0 or os.path.isfile(path):
                path = str(uidx)
                uidx = uidx + 1
            
            print("exporting " + path)
            
            FH = open("output/" + path, "wb")
            if FH:
                C = base64.b64decode(F["content"])
                FH.write(C)
                FH.close()
            else:
                print("failed to open file: " + path)