Use case: logical disk summary with REST API

<< Click to Display Table of Contents >>

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

Use case: logical disk summary with REST API

The mass storage space of devices in your IT landscape is a critical resource, as discussed in the dedicated chapter.  You can rely on the accuracy of reported mass storage usage statistics by Raynet One as a basis for IT strategies. Extract a list of all devices with at least 90 % total mass storage space usage to present it to IT asset management. Select devices with low mass storage space usage as file hosting space for digital content distribution algorithms. Redistribute the software in your company onto devices to achieve even load distribution and reduced failure risk due to being out of disk space.

Requirements

Python 3+

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

API key

frontend URL

pre-collected device inventory

Program content

import requests
 
APIKEY = "PUT API KEY HERE"
FRONTEND = "PUT FRONTEND URL HERE"
 
HEADERS = { "ApiKey": APIKEY }
 
def main():
    str_lds = requests.get(FRONTEND + "/api/v1/LogicalVolumes", headers=HEADERS)
    str_devs = requests.get(FRONTEND + "/api/v1/Devices", headers=HEADERS)
 
    if str_lds.ok == False or str_devs.ok == False:
        print("Failed to request data from the frontend (" + FRONTEND + ")")
        return
        
    LDS = str_lds.json()
    DEVS = str_devs.json()
    
    if LDS == None or DEVS == None:
        print("Invalid data received from the frontend ("+ FRONTEND +")")
        return
        
    devs_storage_info = []
    
    network_total_used_bytes = 0
    network_total_bytes = 0
    
    for D in DEVS:
        total_used_bytes = 0
        total_bytes = 0
        
        for LD in LDS:
            if LD["deviceId"] == D["id"]:
                freeSpace = LD["freeSpace"]
                size = LD["size"]
                
                if freeSpace != None and size != None:
                    total_used_bytes = total_used_bytes + ( size - freeSpace )
                    
                if size != None:
                    total_bytes = total_bytes + size
        
        info = {
            "device_id": D["id"],
            "total_used_bytes": total_used_bytes,
            "total_bytes": total_bytes
        }
        
        if "name" in D:
            info["name"] = D["name"]
        
        devs_storage_info.append(info)
        
        network_total_used_bytes = network_total_used_bytes + total_used_bytes
        network_total_bytes = network_total_bytes + total_bytes
        
    def get_usage_quota_of_device(D):
        if D["total_bytes"] > 0:
            return D["total_used_bytes"] / D["total_bytes"]
        return -1
        
    devs_storage_info.sort(key = get_usage_quota_of_device)
    
    print("* Device disk usage summary (" + str(len(devs_storage_info)) + ")")
    print("=======================")
    
    for DI in devs_storage_info:
        print("ID: " + DI["device_id"])
        if "name" in DI:
            print("Name: " + DI["name"])
        print("Bytes: " + str(DI["total_used_bytes"]) + " / " + str(DI["total_bytes"]))
        USAGE = get_usage_quota_of_device(DI)
        if USAGE >= 0:
            print("Usage: " + str(int(USAGE * 100)) + "%")
        else:
            print("No mass storage usage known")
        print()
        
    if network_total_bytes > 0:
        print( "Total network device space usage: " + str(int( (network_total_used_bytes/network_total_bytes) * 100 ) ) + "%")