Jun 272013

I’ve been building a “Stats Grabber” for one of the managers at work. The ServiceNow instance that I’m using only has the XML interface enabled at the moment so I’ll be using that.

This will just be a quick overview of what I’ve used to build this, won’t go into the details too much as each configuration will differ.
The stats that I needed were to be sorted by day, so I’ve defined a function that will return an array that each day will have.
I’ve cut down what I actually had but this gives you a good example.

function CreateArray() {
return array(

Next up is defining the URLs that are used to retrieve data.
The $instance variable will need to be defined.

// Request Queries
$requrl = "https://".$instance.".service-now.com/u_request_list.do?XML&sysparm_query=u_requestor!%3D9faaf5b90a0a7813005cc0d4347713bc%5Esys_created_onONDateRange%40javascript%3Ags.dateGenerate('".$_POST['startdate']."'%2C'start')%40javascript%3Ags.dateGenerate('".$_POST['enddate']."'%2C'end')";
$resolvedrequrl = "https://".$instance.".service-now.com/u_request_list.do?XML&sysparm_query=u_requestor!%3D9faaf5b90a0a7813005cc0d4347713bc%5Eu_resolvedONDateRange%40javascript%3Ags.dateGenerate('".$_POST['startdate']."'%2C'start')%40javascript%3Ags.dateGenerate('".$_POST['enddate']."'%2C'end')";

// Incident Queries
$incurl = "https://".$instance.".service-now.com/incident_list.do?XML&sysparm_query=sys_created_onONDateRange%40javascript%3Ags.dateGenerate('".$_POST['startdate']."'%2C'start')%40javascript%3Ags.dateGenerate('".$_POST['enddate']."'%2C'end')";
$resolvedincurl = "https://".$instance.".service-now.com/incident_list.do?XML&sysparm_query=u_resolvedONDateRange%40javascript%3Ags.dateGenerate('".$_POST['startdate']."'%2C'start')%40javascript%3Ags.dateGenerate('".$_POST['enddate']."'%2C'end')";

You may also notice the $_POST variables in the url, these will need to be passed to this script in the YYYY-MM-DD format. These will also be used later to construct the array for the days.

I’ve used CURL to retrieve the data, but there are a few different ways to do this
This snippet will create the $ch handle;

$ch = curl_init();
curl_setopt($ch, CURLOPT_USERPWD, $un .":". $pw);

Next we’ll start getting the data.

// Get Requsts
curl_setopt($ch, CURLOPT_URL, $requrl);
$reqreturn = curl_exec($ch);

// Get Incidents
curl_setopt($ch, CURLOPT_URL, $incurl);
$increturn = curl_exec($ch);

// Get Resolved Requests
curl_setopt($ch, CURLOPT_URL, $resolvedrequrl);
$resreqreturn = curl_exec($ch);

// Get Resolved Incidents
curl_setopt($ch, CURLOPT_URL, $resolvedincurl);
$resincreturn = curl_exec($ch);

// Curl End

I haven’t put in any error capturing but that is something that should be there to catch any errors when trying to retrieve the data.

Now to create the array that will contain all of our data.
As I wanted the data sorted by days, I’ve used a for loop with the start and end dates to create the array.

$DateArray = array();
if(isset($_POST['startdate']) && isset($_POST['enddate'])) {
$start = new DateTime($_POST['startdate']);
$end = new DateTime($_POST['enddate']);
$diff = $start->diff($end)->format("%a");
for($x = 0; $x < $diff+1; $x++) { $datestamp = date("Y-m-d",strtotime("+$x days",strtotime($start->format("Y-m-d"))));
$DateArray[$datestamp] = CreateArray();

Now we’ll start processing the data.
I’ll only post one of the four blocks for brevity as they’re esssentially the same with minor differences when it comes down to the data.
SimpleXML is used here to parse the xml output of ServiceNow

// Load Incidents XML
$xml = simplexml_load_string($increturn);
// Get Total Incidents
$incTotal = $xml->incident->count();
// Parse incidents for date
foreach($xml->incident as $item) {
$datestamp = date("Y-m-d",strtotime("+8 hours",strtotime($item->opened_at)));
if($item->priority < 3) { $DateArray[$datestamp]["incMajor"]++; $totalMajorInc++; } switch($item->contact_type) {
case "Email":
case "phone":
case "Auto Monitoring":

Once all four of those blocks have been executed, you now have the data all in a nice array.
You can then use a foreach loop on the DateArray array to get your data out