##!/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_attribute[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_attribute[normalize-space(name)='Service Name']/value", $node); $service_name=~ s/\n//; my $ssrid = $xp->find("./custom_attribute_list/custom_attribute[normalize-space(name)='SSRID']/value", $node); $ssrid=~ s/\n//; my $remedyqueue = $xp->find("./custom_attribute_list/custom_attribute[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',pattern => '%Y-%m-%d %H:%M:%S', time_zone=>'UTC'); my $dtevent = $strp->parse_datetime($timereceived); my $diff_sec = $dtnow->subtract_datetime_absolute($dtevent)->in_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 output 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 );