snehit.ar has asked for the wisdom of the Perl Monks concerning the following question:
##!/usr/bin/perl use warnings; use strict; use XML::XPath; use Data::Dumper; use LWP::UserAgent; use JSON; use DateTime; use Math::Round; use DateTime::Duration; use DateTime::Format::Duration; use DateTime::Format::Strptime; use Cpanel::JSON::XS qw(encode_json); my $xml = 'events.xml'; my $xp = XML::XPath->new(filename => $xml); my $nodeset = $xp->findnodes('//event'); my $records = []; my $json; my $count=0; # Counter to set the Line variable foreach my $node ($nodeset->get_nodelist) { my $severity = $xp->find('./severity', $node); $severity =~ s/\n//; # Remove prefix newline \n character my $ticketnum = $xp->find("./custom_attribute_list/custom_att +ribute[normalize-space(name)='SLB_RemedyIncident']/value", $node); $ticketnum=~ s/\n//; my $timereceived = $xp->find('./time_first_received', $node); $timereceived=~ s/\n//; my $service_name = $xp->find("./custom_attribute_list/custom_a +ttribute[normalize-space(name)='Service Name']/value", $node); $service_name=~ s/\n//; my $ssrid = $xp->find("./custom_attribute_list/custom_attribut +e[normalize-space(name)='SLB_SSRID']/value", $node); $ssrid=~ s/\n//; my $remedyqueue = $xp->find("./custom_attribute_list/custo +m_attribute[normalize-space(name)='SLB_RemedyQueue']/value", $node); $remedyqueue = [ map $_->string_value =~ s/\n//r, $remedyqueue +->get_nodelist ]; #Format Time received $timereceived =~ s/T/ /; #removing the T $timereceived =~ s/\.\d+Z//; #removing the Z #Calculate the time difference my $dtnow = DateTime->now; my $strp = DateTime::Format::Strptime->new(on_error=>'croak',p +attern => '%Y-%m-%d %H:%M:%S', time_zone=>'UTC'); my $dtevent = $strp->parse_datetime($timereceived); my $diff_sec = $dtnow->subtract_datetime_absolute($dtevent)->i +n_units('seconds'); my $diff_hours = $diff_sec/(60*60); #print "$diff_sec s / $diff_hours h\n"; if ($diff_hours>2000) { # Get the event details which are more + then 2 hours $count ++; push @$records, { line => $count, severity => $severity, ticketnum => $ticketnum, appname => $service_name . "(" . $ssrid . ")", remedy_queue => $remedyqueue, event_age => round($diff_hours) . " h", } } } print Dumper(@$records); # Convert into JSON format $json = encode_json \@$records; # print $json; # my $uri = 'http://test/dims/line/tags/availability'; # my $jsondata = $json; # my $req = HTTP::Request->new( 'POST', $uri ); # $req->header( 'Content-Type' => 'application/json'); # $req->content( $json ); # my $lwp = LWP::UserAgent->new; # $lwp->request( $req ); print "Data send to Dashboard";
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Sort the hash array
by haukex (Archbishop) on Jul 06, 2017 at 10:30 UTC | |
by snehit.ar (Beadle) on Jul 06, 2017 at 10:56 UTC | |
by haukex (Archbishop) on Jul 06, 2017 at 11:26 UTC | |
by snehit.ar (Beadle) on Jul 06, 2017 at 12:09 UTC | |
by haukex (Archbishop) on Jul 06, 2017 at 12:38 UTC | |
|
Re: Sort the hash array
by Laurent_R (Canon) on Jul 06, 2017 at 10:29 UTC | |
by snehit.ar (Beadle) on Jul 06, 2017 at 11:04 UTC | |
by Laurent_R (Canon) on Jul 06, 2017 at 12:26 UTC |