Options

xAPI getstatus- PHP

Posted by JackofallTrades

Below is a sample of code for accessing the xAPI via PHP. I am using xPath and pear HTTP/REQUEST2. I am not a coder so ignore my slop.

First I define the login information and the xAPI path.
The code snippet is ax_main.php


Code:



<?php //pear install HTTP_Request2 require 'HTTP/Request2.php'; //define username,password and host $username = 'admin'; $password = 'a10'; $host="172.21.21.50"; //define the xAPI define("AX_REST", "http://".$host."/services/rest/V1/"); ?>


From there I have a ax_session_id.php. This is where we grab the session_id we will be using for the rest of the session.


Code:



<?php $sid = 1; function getSid() { require 'ax_main.php'; $args = array( 'method' => 'authenticate', 'username' => $username, 'password' => $password); //http_build_query is only used here because I ran into a bug. $url = AX_REST . "?" . http_build_query($args); $Req = new HTTP_Request2(); $Req->setUrl($url); $res = $Req->send(); $sid_xml= $res->getBody(); //echo $sid_xml; $sid_local_scope = new SimpleXMLElement($sid_xml); $sid = $sid_local_scope->xpath('//session_id'); //I am using globals because I am lazy. $GLOBALS['sid'] = $sid[0]; } getSid(); //echo $GLOBALS['sid']; ?>


Here is the bit of code to get the status of the vips


Code:



<?php //require_once 'ax_main.php'; $vip = ''; function getVip_status() { include 'ax_session_id.php'; //getSid(); //I ran into a bug so I am not using http_build_query $url = AX_REST . "?session_id=" .$GLOBALS['sid']."&method=slb.vip.getALL" ; //echo $url; $Req = new HTTP_Request2(); $Req->setUrl($url); $res = $Req->send(); $vip_xml= $res->getBody(); //echo $vip_xml; $vip_local_scope = new SimpleXMLElement($vip_xml); $virtual = $vip_local_scope->xpath('//virtual-server'); foreach ($virtual as $list) { if ($list->attributes()->status == 0) { $vip_status = "<font color=\"black\">disabled</font>"; } elseif ($list->attributes()->status == 1) { $vip_status = "<font color=\"green\">All Up</font>"; } elseif ($list->attributes()->status == 2) { $vip_status = "<font color=\"yellow\">Part Up</font>"; } elseif ($list->attributes()->status == 3) { $vip_status = "<font color=\"orange\">Func Up</font>"; } elseif ($list->attributes()->status == 4) { $vip_status = "<font color=\"red\">Down</font>"; } else { $vip_status = "<font color=\"blue\">Unknown</font>"; } echo "VIP: ".$list->attributes()->name." Status: ". $vip_status."<br>"; } } getVip_status(); ?>


Here is the code to get server status.


Code:



<?php //get session ID $server = ''; function getServer_status() { include 'ax_session_id.php'; //getSid(); $url = AX_REST . "?" . "session_id=" .$GLOBALS['sid']."&method=slb.server.getALL"; $Req = new HTTP_Request2(); $Req->setUrl($url); $res = $Req->send(); $server_xml= $res->getBody(); //echo "server_xml = ".$server_xml."<br>"; $server_local_scope = new SimpleXMLElement($server_xml); $server_list = $server_local_scope->xpath('//server'); foreach ($server_list as $list) { if ($list->attributes()->status == 0) { $server_status = "<font color=\"black\">disabled</font>"; } elseif ($list->attributes()->status == 1) { $server_status = "<font color=\"green\">Up</font>"; } elseif ($list->attributes()->status == 4) { $server_status = "<font color=\"red\">Down</font>"; } else { $server_status = "<font color=\"blue\">Unknown</font>"; } echo "Server Name: ".$list->attributes()->name." Status is: ". $server_status."<br>"; } } getServer_status(); ?>


Enjoy!
Sign In or Register to comment.