Zerto Virtual Replication APIs : Resources Report API : Perl Code Example: Retrieving All the Records
  
Perl Code Example: Retrieving All the Records
The following example uses Perl and Json1 and can be run on both Windows and Linux operating systems.
#!/usr/bin/perl -w
use strict;
use LWP::UserAgent;
use JSON;
main();
sub main {
my $zvm = "1.2.3.4";
  my $startIndex = 0;
  my $count = 500;
  my $url;
  my $last_timestamp = "";
 
  my $json = new JSON;
  my $userAgent = LWP::UserAgent->new
  my $num_results_from_last_query = 0;
 
  do {
    $url = "https://$zvm:9669/ZvmService/ResourcesReport/getSamples?fromTimeString=2013-01-01&toTimeString=2015-01-01&startIndex=$startIndex&count=$count";
    print "Retrieving $url\n";
    my $req = HTTP::Request->new(GET => $url);
    $req->header(Accept => 'application/json');
    my $res = $userAgent->request($req);
 
    if ($res->is_success) {
      my @json_array = @{$json->decode($res->content)}
      $num_results_from_last_query = @json_array;
      for my $elem (@json_array) {
        my $timestamp = jsonDateToString($elem->{Timestamp});
        if ($timestamp ne $last_timestamp) {
          print "\nTime: $timestamp\n";
          $last_timestamp = $timestamp;
        }
        print "VPG: $elem->{VpgName}, VM: $elem->{VmName}, Bandwidth (Bps): $elem->{BandwidthInBytes}\n";
      }
      $startIndex += $count;
    }    else {
      print "Error: ", $res->status_line, "\n";
      last;
    }
  } while ($num_results_from_last_query == $count);
}
sub jsonDateToString($) {
  my $json_date = shift;
  if ($json_date =~ m{ \b (\d+) \b ([+-]\d\d\d\d\b)? }x ) {
    my ( $epoch_milliseconds, $time_zone ) = ( $1, $2 );
    return localtime($epoch_milliseconds / 1000);
  }
  return $json_date;
}
Code Notes
The example assumes Json and Crypt::SSLeay Perl packages are installed.2
The API returns all the first records from the specified date to the specified date. When the toTimeString is a date in the future all the records from the specified date are returned.
The code uses the default SSL_VERIFY_NONE verification mode for clients. The most recent Perl versions will still run this code although they will report the verification as an error.
jsonDateToString($) converts the date from its epoch format to a readable date.

1 If you prefer to use XML, you can convert the Json to XML.

2 On a Windows operating system, ActiveState Perl was used.