|
| 1 | +from purestorage import purestorage |
| 2 | +import sys |
| 3 | +import csv |
| 4 | +import requests |
| 5 | +import time |
| 6 | +import os |
| 7 | +import argparse |
| 8 | + |
| 9 | +# Disable certificate warnings |
| 10 | + |
| 11 | +requests.packages.urllib3.disable_warnings() |
| 12 | + |
| 13 | +starttime = time.time() |
| 14 | + |
| 15 | +# Set array variables |
| 16 | + |
| 17 | +flasharray = '' |
| 18 | +api_token = '’ |
| 19 | + |
| 20 | +# Set script variables |
| 21 | + |
| 22 | +time_stamp = (time.strftime('%m-%d-%y')) |
| 23 | +report_file = 'space-report-' + time_stamp + '.csv' |
| 24 | +interval_options = '1h, 3h, 24h, 7d, 30d, 90d, 1y' |
| 25 | + |
| 26 | +def fa_connect(flasharray, api_token): |
| 27 | + |
| 28 | + global array |
| 29 | + |
| 30 | + try: |
| 31 | + array = purestorage.FlashArray(flasharray, api_token=api_token) |
| 32 | + print('Successfully connected to ', flasharray) |
| 33 | + except Exception as e: |
| 34 | + print(e) |
| 35 | + sys.exit('Exiting: Unable to establish session') |
| 36 | + |
| 37 | +def write_output(interval): |
| 38 | + with open(report_file, 'w') as csvfile: |
| 39 | + fieldnames = ['Volume Name', 'Current Data Reduction', 'Data Reduction ' + interval, 'Current Size(GB)', |
| 40 | + 'Size ' + interval + ' Ago(GB)', interval + ' Growth(GB)'] |
| 41 | + writer = csv.DictWriter(csvfile, fieldnames=fieldnames) |
| 42 | + writer.writeheader() |
| 43 | + |
| 44 | + print('Parsing volume data.') |
| 45 | + |
| 46 | + # Loop through all volumes to get historical space data |
| 47 | + |
| 48 | + for currentvol in allvolumes: |
| 49 | + thisvol = array.get_volume(currentvol['name'], space='True', historical=interval) |
| 50 | + volname = thisvol[0]['name'] |
| 51 | + volcurdr = round(thisvol[0]['data_reduction'], 2) |
| 52 | + volstartdr = round(thisvol[len(thisvol) - 1]['data_reduction'], 2) |
| 53 | + volstartsize = round(thisvol[0]['volumes'] / 1024 / 1024 / 1024, 2) |
| 54 | + volcursize = round(thisvol[len(thisvol) - 1]['volumes'] / 1024 / 1024 / 1024, 2) |
| 55 | + volsizedif = volcursize - volstartsize |
| 56 | + volsizedif = round(volsizedif, 2) |
| 57 | + writer.writerow( |
| 58 | + {'Volume Name': volname, 'Current Data Reduction': volcurdr, 'Data Reduction ' + interval: volstartdr, |
| 59 | + 'Current Size(GB)': volcursize, 'Size ' + interval + ' Ago(GB)': volstartsize, interval + ' Growth(GB)': volsizedif}) |
| 60 | + |
| 61 | +def arg_parser(): |
| 62 | + |
| 63 | + global interval |
| 64 | + |
| 65 | + parser = argparse.ArgumentParser('Add interval') |
| 66 | + parser.add_argument('interval', type=str, help='(' + interval_options + ')') |
| 67 | + |
| 68 | + args = parser.parse_args() |
| 69 | + interval = args.interval |
| 70 | + |
| 71 | + if interval not in interval_options: |
| 72 | + print('Please enter a valid interval: ' + interval_options) |
| 73 | + sys.exit() |
| 74 | + |
| 75 | + return interval |
| 76 | + |
| 77 | +def main(): |
| 78 | + |
| 79 | + global allvolumes, interval |
| 80 | + |
| 81 | + # Check to see if FlashArray and API key have been set |
| 82 | + |
| 83 | + if flasharray == "": |
| 84 | + sys.exit('Please edit this file and set the IP or DNS name for the variable: flasharray') |
| 85 | + |
| 86 | + if api_token == "": |
| 87 | + sys.exit('Please edit this file and assign an API token for the variable: api_token for the FlashArray ' + flasharray) |
| 88 | + |
| 89 | + # Call argument parser to check for command line options |
| 90 | + |
| 91 | + arg_parser() |
| 92 | + |
| 93 | + # Call function to connect to the FlashArray |
| 94 | + |
| 95 | + fa_connect(flasharray, api_token) |
| 96 | + |
| 97 | + # Gather all volumes on the array |
| 98 | + |
| 99 | + allvolumes = array.list_volumes() |
| 100 | + print('Gathering all volumes.') |
| 101 | + |
| 102 | + # Call function to parse and write output to CSV file |
| 103 | + |
| 104 | + write_output(interval) |
| 105 | + |
| 106 | + print('Script completed in ', round(time.time()-starttime,2), ' seconds.') |
| 107 | + print('Output file: ', report_file, ' located at: ', os.getcwd()) |
| 108 | + |
| 109 | +main() |
0 commit comments