Skip to content

Commit 1369263

Browse files
committed
A couple of FlashBlade scripts
1 parent 0d6885f commit 1369263

File tree

2 files changed

+404
-0
lines changed

2 files changed

+404
-0
lines changed

List_FBSnaps.py

Lines changed: 261 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,261 @@
1+
import requests
2+
from requests.packages.urllib3.exceptions import InsecureRequestWarning
3+
requests.packages.urllib3.disable_warnings(InsecureRequestWarning)
4+
from base64 import b64encode
5+
import os
6+
import sys
7+
import json
8+
import getpass
9+
from optparse import OptionParser
10+
from datetime import datetime, timedelta
11+
import time
12+
from time import gmtime, strftime, strptime
13+
from operator import itemgetter, attrgetter
14+
15+
# Global Variables
16+
VERSION = '1.0.0'
17+
HEADER = 'Pure Storage List FlashBlade Snapshots (' + VERSION + ')'
18+
BANNER = ('=' * 132)
19+
DEBUG_LEVEL = 0
20+
VERBOSE_FLAG = False
21+
XTOKEN = ''
22+
23+
def create_session(flashBlade, api_token):
24+
global XTOKEN
25+
26+
# Set-up HTTP header
27+
userAgent = 'Jakarta Commons-HttpClient/3.1'
28+
hdrs= {'Content-Type' : 'application/json', 'User-agent' : userAgent, 'api-token' : api_token}
29+
30+
data = {
31+
}
32+
33+
params = json.dumps(data)
34+
path = '/api/login'
35+
url = 'https://%s%s'%(flashBlade,path)
36+
37+
# Perform action
38+
print('Attempting to create session')
39+
40+
response = requests.post(url, params, headers=hdrs, verify=False)
41+
42+
if DEBUG_LEVEL == 2:
43+
print('URL', url)
44+
print('respose', response)
45+
print('Status', response.status_code)
46+
print('Text', response.text)
47+
print('Data', response.json)
48+
print('HTTP Header:', response.headers)
49+
print('x-auth-token:', response.headers['x-auth-token'])
50+
print('')
51+
52+
if (response):
53+
print(BANNER)
54+
XTOKEN = response.headers['x-auth-token']
55+
else:
56+
print(BANNER)
57+
sys.exit('Exiting: Unable to establish session')
58+
59+
jsonString = response.text
60+
jsonData = json.loads(jsonString)
61+
62+
if VERBOSE_FLAG:
63+
print(BANNER)
64+
print(json.dumps(jsonData, sort_keys=False, indent=4))
65+
66+
name = (jsonData['username'])
67+
welcome = 'Welcome ' + name
68+
print(welcome)
69+
70+
71+
def post_url(flashBlade,path,params):
72+
# Set-up HTTP header
73+
userAgent = 'Jakarta Commons-HttpClient/3.1'
74+
hdrs= {'Content-Type' : 'application/json', 'User-agent' : userAgent, 'x-auth-token' : XTOKEN}
75+
url = 'https://%s%s'%(flashBlade,path)
76+
77+
# Perform action
78+
response = requests.post(url, params, headers=hdrs, verify=False)
79+
80+
if DEBUG_LEVEL != 0:
81+
print('URL',url)
82+
print('Response Status:', response.status_code)
83+
print('Text', response.text)
84+
print('Data', response.json)
85+
print('HTTP Header:', response.headers)
86+
print('')
87+
88+
jsonString = response.text
89+
jsonData = json.loads(jsonString)
90+
return(jsonData)
91+
92+
93+
def get_url(flashBlade,path,params):
94+
# Set-up HTTP header
95+
userAgent = 'Jakarta Commons-HttpClient/3.1'
96+
hdrs= {'Content-Type' : 'application/json', 'User-agent' : userAgent, 'x-auth-token' : XTOKEN}
97+
url = 'https://%s%s'%(flashBlade,path)
98+
payload = params
99+
100+
# Perform action
101+
response = requests.get(url, headers=hdrs, verify=False)
102+
103+
if DEBUG_LEVEL != 0:
104+
print('URL', url)
105+
print('Response Status:', response.status_code)
106+
print('Text', response.text)
107+
print('Data', response.json)
108+
print('HTTP Header:', response.headers)
109+
110+
jsonString = response.text
111+
jsonData = json.loads(jsonString)
112+
return(jsonData)
113+
114+
115+
def list_fssnaps(flashBlade,fsname,limit):
116+
data = ''
117+
params = json.dumps(data)
118+
119+
if fsname == '':
120+
path = '/api/1.8/file-system-snapshots?sort=name&limit=%s'%(limit)
121+
else:
122+
path = '/api/1.8/file-system-snapshots?sort=created&names_or_sources=%s'%(fsname)
123+
124+
# Perform action
125+
jsonData = get_url(flashBlade,path,params)
126+
127+
r = str(jsonData)
128+
129+
if VERBOSE_FLAG:
130+
print(BANNER)
131+
print(json.dumps(jsonData, sort_keys=False, indent=4))
132+
133+
# Count of returned rows
134+
res = len(jsonData['items'])
135+
136+
if res == 0:
137+
print('No File System Snapshots found')
138+
else:
139+
print('number of snaps:', res)
140+
x = 0
141+
print(BANNER)
142+
print('{0:40} {1:60} {2:20}'.format('File System', 'File System Snapshots', 'Created'))
143+
print(BANNER)
144+
while (x<res):
145+
#
146+
source = (jsonData['items'][x]['source'])
147+
name = (jsonData['items'][x]['name'])
148+
cdate = (jsonData['items'][x]['created'])
149+
c1 = str(cdate)
150+
epoch = int(c1[0:10])
151+
created = time.strftime("%a, %d %b %Y %H:%M:%S %Z", time.localtime(epoch))
152+
153+
print('{0:40} {1:60} {2:20}'.format(source, name, created))
154+
155+
x = x + 1
156+
157+
def parsecl():
158+
usage = 'usage: %prog [options]'
159+
version = '%prog ' + VERSION
160+
description = "This program returns Snapshots for given File System. Please contact ron@purestorage.com for any assistance."
161+
162+
parser = OptionParser(usage=usage, version=version, description=description)
163+
164+
parser.add_option('-d', '--debug',
165+
type = 'int',
166+
dest = 'DEBUG_LEVEL',
167+
default = 0,
168+
help = 'Debug level, used for HTTP debugging')
169+
170+
parser.add_option('-l', '--limit',
171+
type = 'int',
172+
dest = 'limit',
173+
default = 999,
174+
help = 'Limit number of responses [default: %default]')
175+
176+
parser.add_option('-p', '--password',
177+
action = 'store',
178+
type = 'string',
179+
dest = 'password',
180+
help = 'Pure password')
181+
182+
parser.add_option('-f', '--fsname',
183+
action = 'store',
184+
type = 'string',
185+
dest = 'fsname',
186+
default = '',
187+
help = 'File System name')
188+
189+
parser.add_option('-s', '--server',
190+
action = 'store',
191+
type = 'string',
192+
dest = 'flashBlade',
193+
help = 'Pure FlashArray')
194+
195+
parser.add_option('-t', '--token',
196+
action = 'store',
197+
type = 'string',
198+
dest = 'api_token',
199+
help = 'Pure Api Token')
200+
201+
parser.add_option('-v', '--verbose',
202+
action = 'store_true',
203+
dest = 'VERBOSE_FLAG',
204+
default = False,
205+
help = 'Verbose [default: %default]')
206+
207+
(options, args) = parser.parse_args()
208+
209+
'''
210+
print("Options:", options)
211+
print("Args:", args)
212+
'''
213+
214+
return(options)
215+
216+
def main():
217+
# Setup variables
218+
global DEBUG_LEVEL
219+
global VERBOSE_FLAG
220+
exit_code = 0
221+
222+
# Check for command line parameters
223+
options = parsecl()
224+
flashBlade = options.flashBlade
225+
limit = options.limit
226+
fsname = options.fsname
227+
api_token = options.api_token
228+
DEBUG_LEVEL = options.DEBUG_LEVEL
229+
VERBOSE_FLAG = options.VERBOSE_FLAG
230+
231+
if DEBUG_LEVEL != 0:
232+
print('Flash Blade:', flashBlade)
233+
print('File System:', fsname)
234+
print('Limit:', limit)
235+
print('Api Token:', api_token)
236+
print('Debug Level:', DEBUG_LEVEL)
237+
print('Verbose Flag:', VERBOSE_FLAG)
238+
239+
if flashBlade == None:
240+
sys.exit('Exiting: You must provide FlashBlade details')
241+
242+
if api_token == None:
243+
sys.exit('Exiting: You must provide an API Token')
244+
245+
246+
print(BANNER)
247+
print(HEADER + ' - ' + flashBlade)
248+
print(strftime('%d/%m/%Y %H:%M:%S %Z', gmtime()))
249+
print(BANNER)
250+
251+
# Create session
252+
create_session(flashBlade, api_token)
253+
254+
list_fssnaps(flashBlade,fsname,limit)
255+
256+
print(BANNER)
257+
print(strftime('%d/%m/%Y %H:%M:%S %Z', gmtime()))
258+
print(BANNER)
259+
sys.exit(exit_code)
260+
261+
main()

0 commit comments

Comments
 (0)