|
| 1 | +#!/usr/bin/env perl |
| 2 | + |
| 3 | +package eventlogs; |
| 4 | + |
| 5 | +use JSON; |
| 6 | +use warnings; |
| 7 | +use Text::Table; |
| 8 | +use LWP::UserAgent; |
| 9 | +use LWP::Protocol::https; |
| 10 | + |
| 11 | +$ENV{PERL_LWP_SSL_VERIFY_HOSTNAME} = 0; |
| 12 | +$CONTENT_TYPE_V4 = "application/vnd.netbackup+json; version=4.0"; |
| 13 | +$NB_PORT = 1556; |
| 14 | + |
| 15 | +# |
| 16 | +# This function displays data in a tabular form. It takes table title array and |
| 17 | +# table data (2-d matrix) as inputs and renders it in a tabular form with border |
| 18 | +# |
| 19 | +sub displayDataInTable { |
| 20 | + |
| 21 | + my $arguments_count = scalar(@_); |
| 22 | + if ($arguments_count != 2) { |
| 23 | + print "ERROR :: Incorrect number of arguments passed to displayDataInTable()\n"; |
| 24 | + print "Usage : displayDataInTable( <Array of Table Column Titles>, <Matrix of table data> ) \n"; |
| 25 | + return; |
| 26 | + } |
| 27 | + |
| 28 | + my @titletext = @{$_[0]}; |
| 29 | + my @data = @{$_[1]}; |
| 30 | + |
| 31 | + my @tabletitle; |
| 32 | + my $val; |
| 33 | + foreach $val (@titletext) { |
| 34 | + push @tabletitle, {is_sep => 1, title => '| ', body => '| '}; |
| 35 | + push @tabletitle, $val; |
| 36 | + } |
| 37 | + push @tabletitle, {is_sep => 1, title => '| ', body => '| '}; |
| 38 | + |
| 39 | + my $tb = Text::Table->new( @tabletitle ); |
| 40 | + $tb->load(@data); |
| 41 | + |
| 42 | + print $tb->rule('-', '+'); |
| 43 | + for (0 .. @data) { |
| 44 | + print $tb->table($_); |
| 45 | + print $tb->rule('-', '+'); |
| 46 | + } |
| 47 | + |
| 48 | +} |
| 49 | + |
| 50 | + |
| 51 | +# |
| 52 | +# This function returns a list of notifications based on |
| 53 | +# a filter parameter |
| 54 | +# |
| 55 | + |
| 56 | +sub getNotificationsByFilter { |
| 57 | + |
| 58 | + my $arguments_count = scalar(@_); |
| 59 | + my $fqdn_hostname; |
| 60 | + my $token; |
| 61 | + my $filter; |
| 62 | + |
| 63 | + if ($arguments_count == 2) { |
| 64 | + $fqdn_hostname = $_[0]; |
| 65 | + $token = $_[1]; |
| 66 | + } elsif ($arguments_count == 3) { |
| 67 | + $fqdn_hostname = $_[0]; |
| 68 | + $token = $_[1]; |
| 69 | + $filter = $_[2]; |
| 70 | + } else { |
| 71 | + print "ERROR :: Incorrect number of arguments passed to getNotificationsByFilter()\n"; |
| 72 | + print "Usage : getNotificationsByFilter( <FQDN Hostname>, <Token>. (optional) <filter> ) \n"; |
| 73 | + return; |
| 74 | + } |
| 75 | + |
| 76 | + my $url; |
| 77 | + if ($filter) { |
| 78 | + $url = "https://$fqdn_hostname:$NB_PORT/netbackup/eventlog/notifications?filter=$filter"; |
| 79 | + } else { |
| 80 | + $url = "https://$fqdn_hostname:$NB_PORT/netbackup/eventlog/notifications"; |
| 81 | + } |
| 82 | + |
| 83 | + my $notifications_req = HTTP::Request->new(GET => $url); |
| 84 | + $notifications_req->header('Authorization' => $token); |
| 85 | + |
| 86 | + my $ua = LWP::UserAgent->new( |
| 87 | + timeout => 1000, |
| 88 | + ssl_opts => { verify_hostname => 0, SSL_verify_mode => IO::Socket::SSL::SSL_VERIFY_NONE }, |
| 89 | + ); |
| 90 | + |
| 91 | + print "Performing Get Notifications Request on $url\n"; |
| 92 | + my $response = $ua->request($notifications_req); |
| 93 | + if ($response->is_success) { |
| 94 | + print "Successfully completed Get Notifications Request.\n"; |
| 95 | + |
| 96 | + $data = decode_json($response->content); |
| 97 | + my $pretty = JSON->new->pretty->encode($data); |
| 98 | + return $pretty; |
| 99 | + } else { |
| 100 | + print "ERROR :: Get Notifications Request Failed!\n"; |
| 101 | + print "HTTP GET error code: ", $response->code, "\n"; |
| 102 | + print "HTTP GET error message: ", $response->message, "\n"; |
| 103 | + } |
| 104 | + |
| 105 | +} |
| 106 | + |
| 107 | +# |
| 108 | +## This function displays the JSON content returned from GET Notifications API |
| 109 | +## using query filter in a tabular format |
| 110 | +## |
| 111 | +sub displayNotifications { |
| 112 | + |
| 113 | + my $arguments_count = scalar(@_); |
| 114 | + if ($arguments_count != 1) { |
| 115 | + print "ERROR :: Incorrect number of arguments passed to displayNotifications()\n"; |
| 116 | + print "Usage : displayNotifications( <JSON content returned from GET Notifications API> ) \n"; |
| 117 | + return; |
| 118 | + } |
| 119 | + |
| 120 | + my $jsonstring = $_[0]; |
| 121 | + my $json = decode_json($jsonstring); |
| 122 | + my @notifications = @{$json->{'data'}}; |
| 123 | + |
| 124 | + my @tablerows; |
| 125 | + |
| 126 | + foreach (@notifications) { |
| 127 | + my $notification = $_; |
| 128 | + |
| 129 | + my $type = $notification->{'type'}; |
| 130 | + my $id = $notification->{'id'}; |
| 131 | + my $version = $notification->{'attributes'}->{'version'}; |
| 132 | + my $priority = $notification->{'attributes'}->{'priority'}; |
| 133 | + my $severity = $notification->{'attributes'}->{'severity'}; |
| 134 | + my $createdDateTime = $notification->{'attributes'}->{'createdDateTime'}; |
| 135 | + my $insertionDateTime = $notification->{'attributes'}->{'insertionDateTime'}; |
| 136 | + my $displayString = $notification->{'attributes'}->{'displayString'}; |
| 137 | + my $notificationType = $notification->{'attributes'}->{'notificationType'}; |
| 138 | + my $producerName = $notification->{'attributes'}->{'producerName'}; |
| 139 | + my $producerId = $notification->{'attributes'}->{'producerId'}; |
| 140 | + my $producerType = $notification->{'attributes'}->{'producerType'}; |
| 141 | + my $producerSubType = $notification->{'attributes'}->{'producerSubType'}; |
| 142 | + my $namespace = $notification->{'attributes'}->{'namespace'}; |
| 143 | + |
| 144 | + my @tablerow = ($type, $id, $version, $priority, $severity, $createdDateTime, $insertionDateTime, $displayString, |
| 145 | + $notificationType, $producerName, $producerId, $producerType, |
| 146 | + $producerSubType, $namespace); |
| 147 | + push @tablerows, \@tablerow; |
| 148 | + } |
| 149 | + |
| 150 | + my @title = ("Type", "ID", "Version", "Priority", "Severity", "Created Date Time", "Insertion Date Time", |
| 151 | + "Display String", "Notification Type", "Producer Name", "Producer ID", "Producer Type", |
| 152 | + "Producer Sub Type", "Namespace"); |
| 153 | + print "\n"; |
| 154 | + displayDataInTable(\@title, \@tablerows); |
| 155 | + print "\n"; |
| 156 | + |
| 157 | +} |
| 158 | + |
| 159 | +# Post notifications |
| 160 | +sub postNotifications { |
| 161 | + my $arguments_count = scalar(@_); |
| 162 | + if ($arguments_count != 3) { |
| 163 | + print "ERROR :: Incorrect number of arguments passed to postNotifications()\n"; |
| 164 | + print "Usage : postNotifications( <Master Server Hostname>, <Token>, <Payload>) \n"; |
| 165 | + return; |
| 166 | + } |
| 167 | + |
| 168 | + my $master_server = $_[0]; |
| 169 | + my $token = $_[1]; |
| 170 | + my $filename = $_[2]; |
| 171 | + my $url = "https://$master_server:$NB_PORT/netbackup/eventlog/notifications"; |
| 172 | + open(my $fh, '<:encoding(UTF-8)', $filename) |
| 173 | + or die "Could not open file '$filename' $!"; |
| 174 | + |
| 175 | + my $payload = ""; |
| 176 | + while (my $row = <$fh>) { |
| 177 | + chomp $row; |
| 178 | + $payload .= $row; |
| 179 | + } |
| 180 | + print "payload: $payload\n"; |
| 181 | + |
| 182 | + my $json = common::send_http_request($url, "POST", $token, $payload, undef, $CONTENT_TYPE_V4); |
| 183 | + |
| 184 | + if (defined $json) { |
| 185 | + print "Successfully completed POST Notifications.\n"; |
| 186 | + |
| 187 | + my $pretty = JSON->new->pretty->encode($json); |
| 188 | + return $pretty; |
| 189 | + } |
| 190 | + else { |
| 191 | + print "ERROR :: POST Notifications Request Failed!\n"; |
| 192 | + } |
| 193 | +} |
| 194 | + |
| 195 | +1; |
0 commit comments