Here, we will be creating a Napalm script to find the current usage of the memory on the device. Start by importing Napalm along with the JSON library. We will be using JSON to format the data.
Below is the compete code to display memory usage from the device
from napalm import get_network_driver
import json
driver = get_network_driver('ios')
with driver('131.226.217.143','developer','C1sco12345') as device:
output = json.dumps(device.get_environment(), indent=2)
mem_usage = json.loads(output)
print(mem_usage['memory'])
Output
{'used_ram': 255665200, 'available_ram': 735893060}
Process finished with exit code 0
After connecting to a device, the command is sent to get the environmental details of the device. The output variable holds data in string format. This is a change to the dictionary by using the json.loads() function. Our program is to display the current usage of memory, and for that, we used the parameter “memory” along with the variable mem_usage.
We can modify the parameter for the variable mem_usage to display only “available memory”.
from napalm import get_network_driver
import json
driver = get_network_driver('ios')
with driver('131.226.217.143','developer','C1sco12345') as device:
output = json.dumps(device.get_environment(), indent=2)
mem_usage = json.loads(output)
print('Available Memory',mem_usage['memory']['available_ram'])
Output
Available Memory 735893060
Process finished with exit code 0
In a similar fashion, we can modify the parameter to display the used memory.
from napalm import get_network_driver
import json
driver = get_network_driver('ios')
with driver('131.226.217.143','developer','C1sco12345') as device:
output = json.dumps(device.get_environment(), indent=2)
mem_usage = json.loads(output)
print('Used Memory',mem_usage['memory']['used_ram'])
Output
Used Memory 255655372
Process finished with exit code 0