• CloudVision Automated snapshot using Cloudvision API

 
 
Print Friendly, PDF & Email

Purpose

The purpose of this document is to build an automated task to create container based snapshots using the CloudVision API along with a scheduled cron job from any reachable Unix/Linux/Mac server.

This script will come in handy to compare network status/configuration of your entire network by taking snapshot on a predefined schedule and can be modified if an administrator’s requirements change.

After we create a snapshot template in CloudVision (by default, CloudVision uses the show_inventory template, but can be customized with the desired set of commands),   we will write a simple Python script that leverages the snapshot template to perform a snapshot using CVP Restful API. Finally, we will create a cron job task to perform the operation at scheduled intervals.

In this example, we will have the script perform snapshot every Friday and take a snapshot of Tenant (root) container.

Prerequisites

  • In order to take advantage of the snapshot feature, please be sure your instance of CloudVision portal is of version 2017.1.0 or later. The CloudVision Portal upgrade procedure can be found here:
    https://www.arista.com/en/support/software-download (CloudVision_ConfigGuide-2017.1.0.pdf)
  • The server (CloudVision Portal server/Unix /Linux /Mac ) where script is running requires the installation of  CloudVsion Portal restful API client (Cvprac) which can be installed from here: link

Steps

Step 1 – Create a snapshot template

Snapshot template is the way to define the set of commands which are required to be a part of snapshot. This is highly customizable and also enables command validation. By default CloudVision Portal has a template named Show_inventory which captures the show inventory information.

To create a snapshot template perform the following steps:

Once we perform the above 3 steps a snapshot template is ready. Now we are ready to use the snapshot in our script.

Step 2 – Write a python script to perform Snapshot operation

The program leverages the CloudVision Portal API to

  1. GET
    template-ID for snapshot template: AutoSnapshot
    container-ID for container: Tenant (Root container)
  2. POST
    request for CVP to capture snapshot configs for every switch in the Tenant container.

Note: the container, snapshot template, username, password, ip address can be modified

#!/usr/bin/env python
from cvprac.cvp_client import CvpClient # Import CvpClient class module
login = "admin"
password = "admin"
clnt = CvpClient()
ip_addr = ["192.168.10.1"] # create clnt object from CvpClient class
clnt.connect(ip_addr , login , password ) # Connect to CVP

# Next get Container information for Tenant
result = clnt.get('/inventory/add/searchContainers.do?queryparam=Tenant&startIndex=0&endIndex=0')
# And, extract the container key or container-ID.
contnr = result['data'][0]['key']

# Get information for snapshot template: AutoSnapshot
result2=clnt.get('/snapshot/getSnapshotTemplates.do?startIndex=0&endIndex=0&queryparam=AutoSnapshot')
# And, extract it's key or template-ID
snapTemplt = result2['data'][0]['key']

# Build a dictionary which includes: "templateid" and "containerid"
parms2 = { "templateId": snapTemplt, "containerId": contnr}

# Execute a POST to the Container Snapshot module and pass the parameter dictionary: parms2
snapResult = clnt.post('/snapshot/captureContainerLevelSnapshot.do', parms2)

# Print out the returned result which should be "success"
print snapResult

Step 3 – Create a cron job (Supported in Linux/Unix/Mac OS)

Cron is a Unix utility which schedules a command or script on your server to run automatically at a specified time and date.
Using a cron job we will schedule to run our script to take snapshots periodically:

  1. The command to run script autosnap.py is python autosnap.py
  2. Create a new crontab file, first we go in to terminal and type sudo crontab -e, this will create a crontab file if you do not have one
  3. Choose when you would like the job to run. Every job is a line in your crontab file. The first 5 arguments specify the time to run the job, and the 6th argument is the command to run. As below:
    Argument 1: Minute (0 - 59)
    Argument 2: Hour (0 - 23)
    Argument 3: Day of Month (1 - 31)
    Argument 4: Month (1-12)
    Argument 5: Day of Week (0 - 6) Sunday = 0
    Argument 6: Command
  4. So in our case tthe cron job to run script every Friday 6 pm will look like
    0 18 * * 5 python ~/autosnap.py

Step 4 – Check the automated Snapshots on the GUI

Appendix 

We described an example above to take a Network-Wide snapshot, If we want to take a snapshot of a specific container, we just need to change the following GET request queryparam to the desired container, say Leaf

result=clnt.get('/inventory/add/searchContainers.do?queryparam=Leaf&startIndex=0&endIndex=0')

We described an example above to use Snapshot Template “Autosnapshot“, If we want to take a  snapshot using a different template, we just need to change the following GET request queryparam to the desired Snapshot template, say BGPsnapshot

result2=clnt.get('/snapshot/getSnapshotTemplates.do?startIndex=0&endIndex=0&queryparam=BGPSnapshot')

Further Reading

Follow

Get every new post on this blog delivered to your Inbox.

Join other followers: