|
| 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 Protection Group Snapshot Replication (' + VERSION + ')' |
| 18 | +BANNER = ('=' * 132) |
| 19 | +DEBUG_LEVEL = 0 |
| 20 | +VERBOSE_FLAG = False |
| 21 | +COOKIE = '' |
| 22 | + |
| 23 | +def create_session(flashArray, user, password, api_token): |
| 24 | + global COOKIE |
| 25 | + |
| 26 | + # Set-up HTTP header |
| 27 | + userAgent = 'Jakarta Commons-HttpClient/3.1' |
| 28 | + hdrs= {'Content-Type' : 'application/json', 'User-agent' : userAgent, 'Cookie' : COOKIE} |
| 29 | + |
| 30 | + #Establish Session, if no token provide need to create an API token first |
| 31 | + |
| 32 | + if user: |
| 33 | + data = { |
| 34 | + 'password': user, |
| 35 | + 'username': password |
| 36 | + } |
| 37 | + params = json.dumps(data) |
| 38 | + path = '/api/1.12/auth/apitoken' |
| 39 | + url = 'https://%s%s'%(flashArray,path) |
| 40 | + |
| 41 | + # Perform action |
| 42 | + response = requests.post(url, params, headers=hdrs, verify=False) |
| 43 | + |
| 44 | + COOKIE = response.cookies |
| 45 | + |
| 46 | + if DEBUG_LEVEL == 2: |
| 47 | + print('Status', response.status_code) |
| 48 | + print('Reason', response.reason) |
| 49 | + print('Text', response.text) |
| 50 | + print('Data', response.json) |
| 51 | + print('HTTP Header:', response.headers) |
| 52 | + print('Cookie', COOKIE) |
| 53 | + print('') |
| 54 | + |
| 55 | + if (response.reason) != 'OK': |
| 56 | + print(BANNER) |
| 57 | + sys.exit('Exiting: invalid username / password combination') |
| 58 | + |
| 59 | + jsonString = response.text |
| 60 | + jsonData = json.loads(jsonString) |
| 61 | + |
| 62 | + api_token = (jsonData['api_token']) |
| 63 | + |
| 64 | + data = { |
| 65 | + 'api_token': api_token |
| 66 | + } |
| 67 | + |
| 68 | + params = json.dumps(data) |
| 69 | + path = '/api/1.12/auth/session' |
| 70 | + url = 'https://%s%s'%(flashArray,path) |
| 71 | + |
| 72 | + # Perform action |
| 73 | + print('Attempting to create session') |
| 74 | + |
| 75 | + response = requests.post(url, params, headers=hdrs, verify=False) |
| 76 | + |
| 77 | + COOKIE = response.cookies |
| 78 | + |
| 79 | + if DEBUG_LEVEL == 2: |
| 80 | + print('Status', response.status_code) |
| 81 | + print('Reason', response.reason) |
| 82 | + print('Text', response.text) |
| 83 | + print('Data', response.json) |
| 84 | + print('HTTP Header:', response.headers) |
| 85 | + print('Cookie', COOKIE) |
| 86 | + print('') |
| 87 | + |
| 88 | + if (response.reason) != 'OK': |
| 89 | + print(BANNER) |
| 90 | + sys.exit('Exiting: Unable to establish session') |
| 91 | + |
| 92 | + jsonString = response.text |
| 93 | + jsonData = json.loads(jsonString) |
| 94 | + |
| 95 | + if VERBOSE_FLAG: |
| 96 | + print(BANNER) |
| 97 | + print(json.dumps(jsonData, sort_keys=False, indent=4)) |
| 98 | + |
| 99 | + name = (jsonData['username']) |
| 100 | + welcome = 'Welcome ' + name |
| 101 | + print(welcome) |
| 102 | + |
| 103 | + |
| 104 | +def post_url(flashArray,path,params): |
| 105 | + # Set-up HTTP header |
| 106 | + userAgent = 'Jakarta Commons-HttpClient/3.1' |
| 107 | + hdrs= {'Content-Type' : 'application/json', 'User-agent' : userAgent} |
| 108 | + url = 'https://%s%s'%(flashArray,path) |
| 109 | + |
| 110 | + # Perform action |
| 111 | + response = requests.post(url, params, headers=hdrs, cookie=COOKIE, verify=False) |
| 112 | + |
| 113 | + if DEBUG_LEVEL != 0: |
| 114 | + print('Response Status:', response.status_code) |
| 115 | + print('Reason:', response.reason) |
| 116 | + print('Text', response.text) |
| 117 | + print('Data', response.json) |
| 118 | + print('HTTP Header:', response.headers) |
| 119 | + print('Cookie', COOKIE) |
| 120 | + print('') |
| 121 | + |
| 122 | + jsonString = response.text |
| 123 | + jsonData = json.loads(jsonString) |
| 124 | + return(jsonData) |
| 125 | + |
| 126 | + |
| 127 | +def get_url(flashArray,path,params): |
| 128 | + # Set-up HTTP header |
| 129 | + userAgent = 'Jakarta Commons-HttpClient/3.1' |
| 130 | + hdrs= {'Content-Type' : 'application/json', 'User-agent' : userAgent} |
| 131 | + url = 'https://%s%s'%(flashArray,path) |
| 132 | + payload = params |
| 133 | + |
| 134 | + # Perform action |
| 135 | + response = requests.get(url, headers=hdrs, cookies=COOKIE, verify=False) |
| 136 | + |
| 137 | + if DEBUG_LEVEL != 0: |
| 138 | + print('Response Status:', response.status_code) |
| 139 | + print('Reason:', response.reason) |
| 140 | + print('Text', response.text) |
| 141 | + print('Data', response.json) |
| 142 | + print('HTTP Header:', response.headers) |
| 143 | + print('Cookie:', COOKIE) |
| 144 | + |
| 145 | + jsonString = response.text |
| 146 | + jsonData = json.loads(jsonString) |
| 147 | + return(jsonData) |
| 148 | + |
| 149 | + |
| 150 | +def list_pgsnaps(flashArray,pgroup,limit): |
| 151 | + data = '' |
| 152 | + params = json.dumps(data) |
| 153 | + |
| 154 | + if pgroup != '': |
| 155 | + path = '/api/1.12/pgroup?names=%s&snap=true&transfer=true&sort=created-&limit=%s'%(pgroup,limit) |
| 156 | + else: |
| 157 | + path = '/api/1.12/pgroup?snap=true&transfer=true&sort=created-&limit=%s'%(limit) |
| 158 | + |
| 159 | + # Perform action |
| 160 | + jsonData = get_url(flashArray,path,params) |
| 161 | + |
| 162 | + r = str(jsonData) |
| 163 | + |
| 164 | + if (r[3:15]) == 'pure_err_key': |
| 165 | + pure_err_code = jsonData[0]['pure_err_code'] |
| 166 | + msg = 'Exiting: ' + pgroup + ' ' + jsonData[0]['msg'] |
| 167 | + print(BANNER) |
| 168 | + sys.exit(msg) |
| 169 | + |
| 170 | + if VERBOSE_FLAG: |
| 171 | + print(BANNER) |
| 172 | + print(json.dumps(jsonData, sort_keys=False, indent=4)) |
| 173 | + |
| 174 | + # Count of returned rows |
| 175 | + res = len(jsonData) |
| 176 | + |
| 177 | + if res == 0: |
| 178 | + print('No Snaps found') |
| 179 | + else: |
| 180 | + x = 0 |
| 181 | + print(BANNER) |
| 182 | + print('{0:40} {1:60} {2:20} {3:10}'.format('Source', 'Snap Name', 'Created', 'Progress')) |
| 183 | + print(BANNER) |
| 184 | + while (x<res): |
| 185 | + # |
| 186 | + source = (jsonData[x]['source']) |
| 187 | + name = (jsonData[x]['name']) |
| 188 | + progress = (jsonData[x]['progress']) |
| 189 | + physical = (jsonData[x]['physical_bytes_written']) |
| 190 | + cdate = (jsonData[x]['created']) |
| 191 | + c1 = cdate[0:10] |
| 192 | + c2 = cdate[11:19] |
| 193 | + c3 = c1 + ' ' + c2 |
| 194 | + |
| 195 | + c4 = strptime(c3,'%Y-%m-%d %H:%M:%S') |
| 196 | + created = strftime('%d/%m/%Y %H:%M:%S', c4) |
| 197 | + |
| 198 | + print('{0:40} {1:60} {2:20} {3:10}'.format(source, name, created, progress)) |
| 199 | + |
| 200 | + x = x + 1 |
| 201 | + |
| 202 | +def parsecl(): |
| 203 | + usage = 'usage: %prog [options]' |
| 204 | + version = '%prog ' + VERSION |
| 205 | + description = "This program returns Snapshots for given Protection Group. Please contact ron@purestorage.com for any assistance." |
| 206 | + |
| 207 | + parser = OptionParser(usage=usage, version=version, description=description) |
| 208 | + |
| 209 | + parser.add_option('-d', '--debug', |
| 210 | + type = 'int', |
| 211 | + dest = 'DEBUG_LEVEL', |
| 212 | + default = 0, |
| 213 | + help = 'Debug level, used for HTTP debugging') |
| 214 | + |
| 215 | + parser.add_option('-l', '--limit', |
| 216 | + type = 'int', |
| 217 | + dest = 'limit', |
| 218 | + default = 999, |
| 219 | + help = 'Limit number of responses [default: %default]') |
| 220 | + |
| 221 | + parser.add_option('-p', '--password', |
| 222 | + action = 'store', |
| 223 | + type = 'string', |
| 224 | + dest = 'password', |
| 225 | + help = 'Pure password') |
| 226 | + |
| 227 | + parser.add_option('-P', '--pgroup', |
| 228 | + action = 'store', |
| 229 | + type = 'string', |
| 230 | + dest = 'pgroup', |
| 231 | + default = '', |
| 232 | + help = 'Protection Group') |
| 233 | + |
| 234 | + parser.add_option('-s', '--server', |
| 235 | + action = 'store', |
| 236 | + type = 'string', |
| 237 | + dest = 'flashArray', |
| 238 | + help = 'Pure FlashArray') |
| 239 | + |
| 240 | + parser.add_option('-t', '--token', |
| 241 | + action = 'store', |
| 242 | + type = 'string', |
| 243 | + dest = 'api_token', |
| 244 | + help = 'Pure Api Token') |
| 245 | + |
| 246 | + parser.add_option('-u', '--user', |
| 247 | + action = 'store', |
| 248 | + type = 'string', |
| 249 | + dest = 'user', |
| 250 | + help = 'Pure user name') |
| 251 | + |
| 252 | + parser.add_option('-v', '--verbose', |
| 253 | + action = 'store_true', |
| 254 | + dest = 'VERBOSE_FLAG', |
| 255 | + default = False, |
| 256 | + help = 'Verbose [default: %default]') |
| 257 | + |
| 258 | + (options, args) = parser.parse_args() |
| 259 | + |
| 260 | + ''' |
| 261 | + print("Options:", options) |
| 262 | + print("Args:", args) |
| 263 | + ''' |
| 264 | + |
| 265 | + if options.api_token and options.user: |
| 266 | + parser.error('options --token and --user are mutually exclusive') |
| 267 | + |
| 268 | + return(options) |
| 269 | + |
| 270 | +def main(): |
| 271 | + # Setup variables |
| 272 | + global DEBUG_LEVEL |
| 273 | + global VERBOSE_FLAG |
| 274 | + exit_code = 0 |
| 275 | + |
| 276 | + # Check for command line parameters |
| 277 | + options = parsecl() |
| 278 | + password = options.password |
| 279 | + user = options.user |
| 280 | + flashArray = options.flashArray |
| 281 | + limit = options.limit |
| 282 | + pgroup = options.pgroup |
| 283 | + api_token = options.api_token |
| 284 | + DEBUG_LEVEL = options.DEBUG_LEVEL |
| 285 | + VERBOSE_FLAG = options.VERBOSE_FLAG |
| 286 | + |
| 287 | + if DEBUG_LEVEL != 0: |
| 288 | + print('Password', password) |
| 289 | + print('User', user) |
| 290 | + print('Flash Array', flashArray) |
| 291 | + print('Protection Group', pgroup) |
| 292 | + print('Limit', limit) |
| 293 | + print('Api Token', api_token) |
| 294 | + print('Debug Level:', DEBUG_LEVEL) |
| 295 | + print('Verbose Flag:', VERBOSE_FLAG) |
| 296 | + |
| 297 | + if flashArray == None: |
| 298 | + sys.exit('Exiting: You must provide FlashArray details') |
| 299 | + |
| 300 | + if api_token == None and user == None: |
| 301 | + sys.exit('Exiting: You must provide either API Token details or username and password') |
| 302 | + |
| 303 | + if user and password == None: |
| 304 | + sys.exit('Exiting: You must provide password if using username') |
| 305 | + |
| 306 | + print(BANNER) |
| 307 | + print(HEADER + ' - ' + flashArray) |
| 308 | + print(strftime('%d/%m/%Y %H:%M:%S %Z', gmtime())) |
| 309 | + print(BANNER) |
| 310 | + |
| 311 | + # Create session |
| 312 | + create_session(flashArray, user, password, api_token) |
| 313 | + |
| 314 | + list_pgsnaps(flashArray,pgroup,limit) |
| 315 | + |
| 316 | + print(BANNER) |
| 317 | + print(strftime('%d/%m/%Y %H:%M:%S %Z', gmtime())) |
| 318 | + print(BANNER) |
| 319 | + sys.exit(exit_code) |
| 320 | + |
| 321 | +main() |
0 commit comments