[Working Blog] ONTAP Snapshot Reporting in Python - Part 1 - The Problem

**This is a working blog were I record ideas and notes as I'm trying to solve a challenge/problem/task**

Problem: Generate a snapshot report, to be run on a daily basis, which includes the following:

1) Snapshot Name, 2) Snapshot ID (if any), 3) Backup Schedule, 4) Volume Name/Path, 5) Backup Results, 6) Time, 7) Success/Failed

Thoughts on the Problem

My reckoning is that we can do this task using the ONTAP Python client library*. We need to construct the following flow:

  1. Connect to ONTAP Cluster
  2. Acquire the Snapshot Policies and Snapshot Policy Schedules
  3. Acquire the cron schedules (these are referenced in the Snapshot Policy Schedules)
  4. Acquire the Data SVMs
  5. Acquire the volumes and applied snapshot policy
  6. Acquire the list of snapshots for the last 24 hours (or longer)
  7. Work out expected snapshot, based on the volumes and their applied snapshot policies
  8. Compare expected and actual snapshots, do we have all the snapshots we think we should have?
  9. Output to CSV 

*Other useful links off the link above:


(Trying to) Solve the Problem:

Note: I use the Python command line to get to where I need to be, then will go back and turn it into a script.

Firstly, we learn how to get to the data we want.

1) Connect to ONTAP Cluster:

Firstly, we need to install the NetApp ONTAP Python Client Library:

CMD> pip3 install netapp-ontap

Then enter the Python command line:

CMD> python

To connect to the cluster, modify the following and paste to the Python CLI >>>

  • from netapp_ontap import config, HostConnection
  • from netapp_ontap.resources import Cluster

  • conn = HostConnection(
  •   host="CLUSTERNAME",
  •   username="LOGIN_USER",
  •   password="LOGIN_PASSWORD",
  •   verify=False # Set to True if you have valid SSL cert
  • )

  • config.CONNECTION = conn

  • clus = Cluster()
  • clus.get()
  • print(clus)

2) Acquire the Snapshot Policies and Snapshot Policy Schedules

Note: I found it initially very hard to understand how to use the various classes. Then I found the dir(CLASS) command. I saved an object in variable b. type(b) was class 'netapp_ontap.models.snapshot_policy_copies.SnapshotPolicyCopies'. dir(b) showed me all the keys.

from netapp_ontap.resources import SnapshotPolicy
allSnapshotPolicies = SnapshotPolicy.get_collection()

## Display snapshot policy name and uuid ##
def display_snapshot_policies():
  snapshotPolicies = SnapshotPolicy.get_collection()
  for policy in snapshotPolicies:
    policy.get()
    print(policy.name,policy.uuid)

display_snapshot_policies()

## Display a snapshot policy ##
def display_A_snapshot_policy(UUID):
  resource = SnapshotPolicy(uuid=UUID)
  resource.get()
  return resource
  
display_A_snapshot_policy("UUID")

## Lets inspect the snapshot policy ##
aSnapshotPolicy = display_A_snapshot_policy("UUID")

aSnapshotPolicy
dir(aSnapshotPolicy) # <-- returns all the properties and methods

aSnapshotPolicy.copies
for copy in aSnapshotPolicy.copies:
   z = copy
   print(z)

dir(z) # Running from the Python CLI we keep the final copy output from the for loop so we can inspect it.

## Looking at a snapshot schedule

z.count
z.prefix
z.schedule

dir(z.schedule)

z.schedule.uuid # <-- The cron schedule UUID

3) Acquire the cron schedules

Now we know how to find the cron schedules used by the Snapshot Policies, we need to get these cron schedules.

  • from netapp_ontap.resources import Schedule

  • def cronPeriod(x,period):
  •   try:
  •     if period == "days":
  •       return x.cron.days
  •     elif period == "hours":
  •       return x.cron.hours
  •     elif period == "minutes":
  •       return x.cron.minutes
  •     else:
  •       return ['X']
  •   except AttributeError:
  •     return ['X']

  • schedules = Schedule.get_collection(type="cron") # <-- Only interested in cron schedules
  • for s in schedules:
  •   print("Cron:",s.name, cronPeriod(s,"days"),"days,", cronPeriod(s,"hours"),"hours,", cronPeriod(s, "minutes"),"minutes,")

4) Acquire the Data SVMs

from netapp_ontap.resources import Svm

# Retrieve and print information about all SVMs
def get_svms():
    try:
        for svm in Svm.get_collection():
            svm.get()
            print(f"SVM Name: {svm.name}")
            print(f"SVM UUID: {svm.uuid}")
            print(f"SVM State: {svm.state}")
            print(f"SVM Subtype: {svm.subtype}")
            print("-" * 40)
    except Exception as e:
        print(f"An error occurred: {e}")

get_svms()

5) Acquire the volumes and applied snapshot policy

from netapp_ontap.resources import Volume

def getVols():
  for vol in Volume.get_collection():
    vol.get()
    print("Name:", vol.name, "SnapPol:", vol.snapshot_policy.name)

getVols()

6) Acquire the list of snapshots for the last 24 hours (or longer)

To list the snaphots we need the value volume.uuid.

from netapp_ontap.resources import Snapshot

print(list(Snapshot.get_collection("{volume.uuid}")))

With a known volume UUID, for example:

... To be continued ...

Comments