Perl Code: Please do optimized below CODE if required ..$VAR1 = { 'appname' => 'IPT(957)', 'event_time' => '2017-06-06 15:37:17', 'line' => 1, 'ticketnum' => 'INC000015345', 'remedy_queue' => 'Coll@borate-L3', 'severity' => 'critical' }; $VAR2 = { 'appname' => 'Instant Messaging - Lync External(2667)', 'remedy_queue' => bless( [], 'XML::XPath::NodeSet' ), 'event_time' => '2017-04-07 13:54:51', 'line' => 2, 'ticketnum' => 'INC0000134513', 'severity' => 'critical' }; $VAR3 = { 'severity' => 'critical', 'event_time' => '2017-04-07 13:54:50', 'line' => 3, 'ticketnum' => 'INC00001847651', 'remedy_queue' => 'Global Service Desk', 'appname' => 'Instant Messaging - Lync External(2667)' };
##!/usr/bin/perl use warnings; use strict; use XML::XPath; use Data::Dumper; use LWP::UserAgent; use JSON; use DateTime; 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 $count=0; foreach my $node ($nodeset->get_nodelist) { $count ++; my $severity = $xp->find('./severity', $node); $severity =~ s/\n//; my $ticketnum = $xp->find("./custom_attribute_list/custom_att +ribute[normalize-space(name)='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)='SSRID']/value", $node); $ssrid=~ s/\n//; my $remedyqueue = $xp->find("./custom_attribute_list/custom_at +tribute[normalize-space(name)='RemedyQueue']/value", $node); $remedyqueue=~ s/\n//; #Format $timereceived $timereceived =~ s/T/ /; #removing the T $timereceived =~ s/\.\d+Z//; #removing the Z 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>4000) { ##//TODO:delete after testing the out +put if ($diff_hours>2) { push @$records, { line => $count, severity => $severity, ticketnum => $ticketnum, appname => $service_name . "(" . $ssrid . ")", remedy_queue => $remedyqueue, event_time => $timereceived, } } } print Dumper(@$records); # my $uri = 'http://testing/dims/line/tags/availability'; # my $json = '{ # "line":"4", # "status":"Critical", # "appname":"aaa", # "ticketnumber":"INC0012312312312", # "eventage":"0.5 h", # "specialist":"Guru" # }'; # my $req = HTTP::Request->new( 'POST', $uri ); # $req->header( 'Content-Type' => 'application/json'); # $req->content( $json ); # my $lwp = LWP::UserAgent->new; # $lwp->request( $req );
In reply to array data to json by snehit.ar
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |