How to retrieve available free memory on a device
From Digi Developer
Contents |
Retrieving the amount of Free memory on a device
This script is designed to retrieve memory on a python enabled Digi product via the custom module digicli.
Requirements
A python enabled Digi product that natively contains the digicli module.
Code
import digicli def get_mem(): (flag, response) = digicli.digicli("display memory") if flag: for line in response: line = line.strip() line = line.lower() if line.startswith("free memory"): meminfo = line.split(":", 1) if len(meminfo) != 2: raise MemoryError("Unable to retrieve memory information from the device") else: try: return int(meminfo[1]) except: return meminfo[1] raise MemoryError("Unable to retrieve memory information from the device")
The digicli module is critical to our script. It allows us to interact with the product similar to how a user could, but in a programmatic way.
In this case, we use the string 'display memory' to give the output of the device's available memory. The return tuple from digicli is a flag to determine the success of the command, and the standard out of the command. If the flag is true, the response is a list of strings, one for each carriage return. If the flag is false, we raise a MemoryError exception, indicating we failed to retrieve the memory information.
We perform the strip() and lower() functions to massage the line into the form we want. We then use the startswith() function to find which line the free memory is on, split the line by ':' and validate it was split correctly. Then return the second element, as that should contain the amount of free memory on the device.
Notes and Source
NOTE: This function is dependent on the format of the memory information. At the time of writing this was performed on a ConnectPort X8 running EOS 7/01/08 daily firmware. The format may change at some time in the future, so retest this code anytime you attempt a new firmware or platform.
if __name__ == '__main__': print get_mem()
For easier testing, the above code can be added so when calling the script directly, it will call the function and display the results, whatever they may be.
