Sample Perl Script

001: #!/usr/bin/env perl
002: 
003: use LWP::UserAgent;
004: use LWP::Simple;
005: use File::Find;
006: use Getopt::Long;
007: 
008: use strict;
009: use warnings;
010: 
011: our $VERBOSE      = 0;
012: our $IP           = '';
013: our $FORMAT       = '';
014: our $PROTOCOL     = '';
015: our $AUTHENTICATE = '';
016: our $FAILURES     = 0;
017: our $USERNAME     = '';
018: our $PASSWORD     = '';
019: 
020: our $ua       = LWP::UserAgent->new;
021: our $rsp;
022: 
023: GetOptions ("authenticate=s" => \$AUTHENTICATE,
024:             "protocol=s"     => \$PROTOCOL,
025:             "ip=s"           => \$IP,
026:             "format=s"       => \$FORMAT,
027:             "verbose"        => \$VERBOSE)
028: or die("Error in command line arguments\n");
029: 
030: # check protocol, should be either http or https
031: #
032: unless (($PROTOCOL eq 'http') or ($PROTOCOL eq 'https')) {
033:     print "Protocol should be either 'http' or 'https'\n";
034:     exit;
035: }
036: 
037: 
038: # check format, should be json, text, or xml
039: #
040: unless (($FORMAT eq 'json') or ($FORMAT eq 'text') or ($FORMAT eq 'xml')) {
041:     print "Format should be either 'json', 'text', or 'xml'\n";
042:     exit;
043: }
044: 
045: 
046: # check the ip, standard 4 numbers '.' separated
047: #
048: if ($IP !~ /^\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}$/) {
049:     print "ip not in correct format: $IP\n";
050:     exit;
051: }
052: 
053: # check authentication
054: #
055: if ($AUTHENTICATE eq '') {
056:     ;
057: } elsif ($AUTHENTICATE =~ /\//) {
058:     my ($username, $password) = split( /\//, $AUTHENTICATE);
059:     $USERNAME = $username . '_pl';
060:     $PASSWORD = $password . '_pl';
061: } else {
062:     print 'Authenticate argument must have '/' between the username and password:\n';
063:     print "value: $AUTHENTICATE\n";
064:     exit;
065: }
066: 
067: #    $prefix = "#{$protocol}://sws:sws@#{$ip}:81/sws/v2"
068: 
069: print "VERBOSE:      $VERBOSE\n";
070: print "IP:           $IP\n";
071: print "FORMAT:       $FORMAT\n";
072: print "PROTOCOL:     $PROTOCOL\n";
073: print "AUTHENTICATE: $AUTHENTICATE\n";
074: 
075: 
076: sub check_job_status {
077:     my ($rsp,@more) = @_;
078:     print "$rsp\n";
079:     if ($rsp =~ /Job has been successfully submitted/i) {
080:         $rsp =~ /"job" : "(\d+)"/i;
081:         my $jobno = $1;
082:         my $myrsp;
083:         my $myrsp2;
084:         $myrsp2 = do_webservices_cmd( '/job/mover/info',
085:                                      "job=$jobno");
086:         print "$myrsp2\n";
087:         $myrsp = do_webservices_cmd( '/job/info',
088:                                      "job=$jobno");
089:         my $count = 0;
090:         while ($myrsp =~ /RUNNING/i) {
091:             sleep(1);
092:             $myrsp2 = do_webservices_cmd( '/job/mover/info',
093:                                          "job=$jobno");
094:             print "$myrsp2\n";
095:             $myrsp = do_webservices_cmd( '/job/info',
096:                                          "job=$jobno");
097:             print "$myrsp\n";
098:             $count += 1;
099:         }
100:         $myrsp2 = do_webservices_cmd( '/job/mover/info',
101:                                      "job=$jobno");
102:         print "$myrsp2\n";
103:         print "Checked for completion: $count times\n";
104:         print "$myrsp\n";
105:         if ($myrsp =~ /ERROR/i) {
106:             print "Job had error -----------------------------------------------------\n";
107:             $FAILURES += 1;
108:         }
109:     } else {
110:         print "Job not submitted correctly -----------------------------------------------------\n";
111:         $FAILURES += 1;
112:     }
113: }
114: 
115: 
116: sub do_webservices_cmd {
117:     my ($cmd, @pieces) = @_;
118:     #? print "cmd: $cmd\n"; #?
119:     #? print "pieces: @pieces\n"; #?
120:     #? for my $piece (@pieces) { #?
121:     #?     print "piece: $piece\n"; #?
122:     #? } #?
123: 
124:     # set up the URL prefix
125:     #
126:     my $prefix;
127:     if ($cmd =~ /wsconfig/i) {
128:         $prefix = "http://$IP:81/sws/v2";
129:     } elsif ($PROTOCOL =~ /https/i) {
130:         $prefix = "https://$IP/sws/v2";
131:     } else {
132:         $prefix = "http://$IP:81/sws/v2";
133:     }
134: 
135: 
136:     my $ws = $prefix . $cmd;
137: 
138:     if ($AUTHENTICATE ne '') {
139:         push( @pieces, "username=$USERNAME");
140:         push( @pieces, "password=$PASSWORD");
141:     }
142: 
143:     # add any passed parameters to URL
144:     #
145:     if (scalar(@pieces) > 0) {
146:         $ws .= '?' . join( '&', sort(@pieces));
147:     }
148: 
149:     # if we do not already have a format parameter
150:     #
151:     unless ($ws =~ /format=/i) {
152:         # add '?' if this is the first parameter, otherwise add '&'
153:         #
154:         if ($ws =~ /\?/) {
155:             $ws .= '&';
156:         } else {
157:             $ws .= '?';
158:         }
159:         $ws .= "format=$FORMAT";    # ask for appropriate formatting for the web-service
160:     }
161: 
162:     # add login parameters if we need to authenticate
163:     #
164:     # all web-service URLs are lowercase
165:     #
166:     $ws = lc( $ws);
167: 
168:     if ($VERBOSE) {
169:         print "\n";
170:         print "WS: $ws\n";
171:         print "=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=\n" if ($ws =~ /mover/);
172:     }
173: 
174:     my $req = new HTTP::Request GET => $ws;
175:     my $res = $ua->request($req);
176: 
177:     if ( $res->is_success ) {
178:         my $rsp = $res->content;
179:         return $rsp;
180:     }
181:     else {
182:         print "**** HTTP ERROR: " . $res->status_line . "...\n";
183:         $FAILURES += 1;
184:         return 1;
185:     }
186: }
187: 
188: my $ws_rsp;
189: 
190: #Description of Web Services sample programs:
191: 
192: # 0) Please turn on web-services using the StorNext GUI
193: #    Also choose protocol and authentication through the SN GUI
194: 
195: # 1) Do WS system info, returning TEXT, XML and JSON
196: 
197: # /sws/v2/system/info?format=text
198: $ws_rsp = do_webservices_cmd( '/system/info',
199:                               "format=text");
200: print "$ws_rsp\n";
201: 
202: # /sws/v2/system/info?format=xml
203: $ws_rsp = do_webservices_cmd( '/system/info',
204:                               "format=xml");
205: print "$ws_rsp\n";
206: 
207: # /sws/v2/system/info?format=json
208: $ws_rsp = do_webservices_cmd( '/system/info',
209:                               "format=json");
210: print "$ws_rsp\n";
211: 
212: # 2) Create a policy for a managed file system
213: 
214: # 3) 3 directories should exist:
215: # a) Directory for single-file manipulation
216: 
217: my $singles_path = '/stornext/snfs1/sample_dir_singles/pl';
218: my @singles_paths_sync = ();
219: my @singles_paths_async = ();
220: 
221: # b) Directory for directory manipulation
222: 
223: my $dirs_path_sync  = '/stornext/snfs1/sample_dir_dirs_sync/pl';
224: my @dirs_paths_sync = ();
225: my $dirs_path_async  = '/stornext/snfs1/sample_dir_dirs_async/pl';
226: my @dirs_paths_async = ();
227: 
228: # c) Directory for multi-file manipulation
229: 
230: my $multi_path = '/stornext/snfs1/sample_dir_multi/pl';
231: my @multi_paths_sync = ();
232: my @multi_paths_async = ();
233: 
234: # 4) Create arrays with path names of both files in each of those directories
235: 
236: for my $i (0,1) {
237:     my $filename = "file.$i";
238: 
239:     push @singles_paths_sync, "$singles_path/$filename";
240: 
241:     push @dirs_paths_sync,    "$dirs_path_sync/$filename";
242: 
243:     push @dirs_paths_async,   "$dirs_path_async/$filename";
244: 
245:     push @multi_paths_sync,   "$multi_path/$filename";
246: }
247: 
248: for my $i (2,3) {
249:     my $filename = "file.$i";
250: 
251:     push @singles_paths_async, "$singles_path/$filename";
252: 
253:     push @multi_paths_async,   "$multi_path/$filename";
254: }
255: 
256: 
257: # 5) Use WS fsstore to save both files from the first directory to TAPE
258: 
259: # /sws/v2/file/fsstore?file=<filepath>
260: for my $filepath (@singles_paths_sync) {
261:     $ws_rsp = do_webservices_cmd( '/file/fsstore',
262:                                   "file=$filepath");
263:     print "$ws_rsp\n";
264: }
265: 
266: # /sws/v2/file/fsfileinfo?file=<filepath>
267: for my $filepath (@singles_paths_sync) {
268:     $ws_rsp = do_webservices_cmd( '/file/fsfileinfo',
269:                                   "file=$filepath");
270:     print "$ws_rsp\n";
271: }
272: 
273: # 6) Use WS fsstore to save the second directory to TAPE
274: 
275: # /sws/v2/file/fsstore?directory=<dirpath>
276: $ws_rsp = do_webservices_cmd( '/file/fsstore',
277:                               "directory=$dirs_path_sync");
278: print "$ws_rsp\n";
279: 
280: # /sws/v2/file/fsfileinfo?directory=<dirpath>
281: $ws_rsp = do_webservices_cmd( '/file/fsfileinfo',
282:                               "directory=$dirs_path_sync");
283: print "$ws_rsp\n";
284: 
285: # 7) Use WS fsstore to save both files from the third directory to TAPE
286: 
287: # /sws/v2/file/fsstore?file=<f1>&file=<f2>
288: $ws_rsp = do_webservices_cmd( '/file/fsstore',
289:                               "file=$multi_paths_sync[0]",
290:                               "file=$multi_paths_sync[1]");
291: print "$ws_rsp\n";
292: 
293: 
294: # /sws/v2/file/fsfileinfo?file=<f1>&file=<f2>
295: $ws_rsp = do_webservices_cmd( '/file/fsfileinfo',
296:                               "file=$multi_paths_sync[0]",
297:                               "file=$multi_paths_sync[1]");
298: print "$ws_rsp\n";
299: 
300: 
301: # 8) Use WS rmdiskcopy to truncate both files in each of 3 directories
302: 
303: # /sws/v2/file/fsrmdiskcopy
304: for my $filepath (@singles_paths_sync) {
305:     $ws_rsp = do_webservices_cmd( '/file/fsrmdiskcopy',
306:                                   "file=$filepath");
307:     print "$ws_rsp\n";
308: }
309: 
310: for my $filepath (@dirs_paths_sync) {
311:     $ws_rsp = do_webservices_cmd( '/file/fsrmdiskcopy',
312:                                   "file=$filepath");
313:     print "$ws_rsp\n";
314: }
315: 
316: for my $filepath (@multi_paths_sync) {
317:     $ws_rsp = do_webservices_cmd( '/file/fsrmdiskcopy',
318:                                   "file=$filepath");
319:     print "$ws_rsp\n";
320: }
321: 
322: # 9) Use WS fsretrieve to restore both files to first directory from TAPE
323: 
324: # /sws/v2/file/fsretrieve?file=<filepath>
325: for my $filepath (@singles_paths_sync) {
326:     $ws_rsp = do_webservices_cmd( '/file/fsretrieve',
327:                                   "file=$filepath");
328:     print "$ws_rsp\n";
329: }
330: 
331: # /sws/v2/file/fsfileinfo?file=<filepath>
332: for my $filepath (@singles_paths_sync) {
333:     $ws_rsp = do_webservices_cmd( '/file/fsfileinfo',
334:                                   "file=$filepath");
335:     print "$ws_rsp\n";
336: }
337: 
338: # 10) Use WS fsretrieve to restore the second directory from TAPE
339: 
340: # /sws/v2/file/fsretrieve?directory=<dirpath>
341: $ws_rsp = do_webservices_cmd( '/file/fsretrieve',
342:                               "directory=$dirs_path_sync");
343: print "$ws_rsp\n";
344: 
345: # /sws/v2/file/fsfileinfo?directory=<dirpath>
346: $ws_rsp = do_webservices_cmd( '/file/fsfileinfo',
347:                               "directory=$dirs_path_sync");
348: print "$ws_rsp\n";
349: 
350: # 11) Use WS fsretrieve to restore both files in the third directory from TAPE
351: 
352: # /sws/v2/file/fsretrieve?file=<f1>&file=<f2>
353: $ws_rsp = do_webservices_cmd( '/file/fsretrieve',
354:                               "file=$multi_paths_sync[0]",
355:                               "file=$multi_paths_sync[1]");
356: print "$ws_rsp\n";
357: 
358: 
359: # /sws/v2/file/fsfileinfo?file=<f1>&file=<f2>
360: $ws_rsp = do_webservices_cmd( '/file/fsfileinfo',
361:                               "file=$multi_paths_sync[0]",
362:                               "file=$multi_paths_sync[1]");
363: print "$ws_rsp\n";
364: 
365: 
366: # 12-18) Repeat steps 5-11 using async mode
367: 
368: # 12) Use WS fsstore to save both files from the first directory to TAPE
369: 
370: # /sws/v2/file/fsstore?file=<filepath>
371: for my $filepath (@singles_paths_async) {
372:     $ws_rsp = do_webservices_cmd( '/file/fsstore',
373:                                   "file=$filepath",
374:                                   "mode=async");
375:     check_job_status( $ws_rsp);
376: }
377: 
378: # /sws/v2/file/fsfileinfo?file=<filepath>
379: for my $filepath (@singles_paths_async) {
380:     $ws_rsp = do_webservices_cmd( '/file/fsfileinfo',
381:                                   "file=$filepath");
382:     print "$ws_rsp\n";
383: }
384: 
385: # 13) Use WS fsstore to save the second directory to TAPE
386: 
387: # /sws/v2/file/fsstore?directory=<dirpath>
388: $ws_rsp = do_webservices_cmd( '/file/fsstore',
389:                               "directory=$dirs_path_async",
390:                               "mode=async");
391: check_job_status( $ws_rsp);
392: 
393: # /sws/v2/file/fsfileinfo?directory=<dirpath>
394: $ws_rsp = do_webservices_cmd( '/file/fsfileinfo',
395:                               "directory=$dirs_path_async");
396: print "$ws_rsp\n";
397: 
398: # 14) Use WS fsstore to save both files from the third directory to TAPE
399: 
400: # /sws/v2/file/fsstore?file=<f1>&file=<f2>
401: $ws_rsp = do_webservices_cmd( '/file/fsstore',
402:                               "file=$multi_paths_async[0]",
403:                               "file=$multi_paths_async[1]",
404:                               "mode=async");
405: check_job_status( $ws_rsp);
406: 
407: 
408: # /sws/v2/file/fsfileinfo?file=<f1>&file=<f2>
409: $ws_rsp = do_webservices_cmd( '/file/fsfileinfo',
410:                               "file=$multi_paths_async[0]",
411:                               "file=$multi_paths_async[1]");
412: print "$ws_rsp\n";
413: 
414: 
415: # 15) Use WS rmdiskcopy to truncate both files in each of 3 directories
416: 
417: # /sws/v2/file/fsrmdiskcopy
418: for my $filepath (@singles_paths_async) {
419:     $ws_rsp = do_webservices_cmd( '/file/fsrmdiskcopy',
420:                                   "file=$filepath");
421:     print "$ws_rsp\n";
422: }
423: 
424: for my $filepath (@dirs_paths_async) {
425:     $ws_rsp = do_webservices_cmd( '/file/fsrmdiskcopy',
426:                                   "file=$filepath");
427:     print "$ws_rsp\n";
428: }
429: 
430: for my $filepath (@multi_paths_async) {
431:     $ws_rsp = do_webservices_cmd( '/file/fsrmdiskcopy',
432:                                   "file=$filepath");
433:     print "$ws_rsp\n";
434: }
435: 
436: # 16) Use WS fsretrieve to restore both files to first directory from TAPE
437: 
438: # /sws/v2/file/fsretrieve?file=<filepath>
439: for my $filepath (@singles_paths_async) {
440:     $ws_rsp = do_webservices_cmd( '/file/fsretrieve',
441:                                   "file=$filepath",
442:                                   "mode=async");
443:     check_job_status( $ws_rsp);
444: }
445: 
446: # /sws/v2/file/fsfileinfo?file=<filepath>
447: for my $filepath (@singles_paths_async) {
448:     $ws_rsp = do_webservices_cmd( '/file/fsfileinfo',
449:                                   "file=$filepath");
450:     print "$ws_rsp\n";
451: }
452: 
453: # 17) Use WS fsretrieve to restore the second directory from TAPE
454: 
455: # /sws/v2/file/fsretrieve?directory=<dirpath>
456: $ws_rsp = do_webservices_cmd( '/file/fsretrieve',
457:                               "directory=$dirs_path_async",
458:                               "mode=async");
459: check_job_status( $ws_rsp);
460: 
461: # /sws/v2/file/fsfileinfo?directory=<dirpath>
462: $ws_rsp = do_webservices_cmd( '/file/fsfileinfo',
463:                               "directory=$dirs_path_async");
464: print "$ws_rsp\n";
465: 
466: # 18) Use WS fsretrieve to restore both files in the third directory from TAPE
467: 
468: # /sws/v2/file/fsretrieve?file=<f1>&file=<f2>
469: $ws_rsp = do_webservices_cmd( '/file/fsretrieve',
470:                               "file=$multi_paths_async[0]",
471:                               "file=$multi_paths_async[1]",
472:                               "mode=async");
473: check_job_status( $ws_rsp);
474: 
475: 
476: # /sws/v2/file/fsfileinfo?file=<f1>&file=<f2>
477: $ws_rsp = do_webservices_cmd( '/file/fsfileinfo',
478:                               "file=$multi_paths_async[0]",
479:                               "file=$multi_paths_async[1]");
480: print "$ws_rsp\n";
481: 
482: 
483: # 19) Get policy information
484: 
485: $ws_rsp = do_webservices_cmd( '/policy/fsdirclass',
486:                               "directory=$dirs_path_async");
487: print "$ws_rsp\n";
488: 
489: $ws_rsp = do_webservices_cmd( '/policy/fsclassinfo',
490:                               "policy=policy_min_i");
491: print "$ws_rsp\n";
492: 
493: 
494: print "\n";
495: print "Number of failures: $FAILURES\n";
496: 
497: 
498: #               Copyright 2015 Quantum Corporation