Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- $site = "http://insert/your/site";
- function print_layout()
- {
- global $site;
- $ch = curl_init("$site/api.php?method=get_rackspace");
- curl_setopt($ch, CURLOPT_HEADER, 0);
- curl_setopt($ch, CURLOPT_RETURNTRANSFER,true);
- $data = curl_exec($ch);
- curl_close($ch);
- $d = json_decode($data, true );
- $result = $d['response'];
- foreach ( $result as $location ) {
- echo $location['location_name']."\n";
- echo " - ".$location['row_name']."\n";
- foreach ( $location['racks'] as $row ) {
- echo " - ".$row['name']."\n";
- $rack_curl = curl_init("$site/api.php?method=get_rack&rack_id=".$row['id']);
- curl_setopt($rack_curl, CURLOPT_HEADER, 0);
- curl_setopt($rack_curl, CURLOPT_RETURNTRANSFER,true);
- $rackdata = curl_exec($rack_curl);
- curl_close($rack_curl);
- $rack_json = json_decode($rackdata, true );
- $rack = $rack_json['response'];
- foreach( $rack['mountedObjects'] as $obj ) {
- $obj_curl = curl_init("$site/racktables/api.php?method=get_object&object_id=$obj");
- curl_setopt($obj_curl, CURLOPT_HEADER, 0);
- curl_setopt($obj_curl, CURLOPT_RETURNTRANSFER,true);
- $objdata = curl_exec($obj_curl);
- curl_close($obj_curl);
- $obj_json = json_decode($objdata, true );
- $object = $obj_json['response'];
- echo " - ".$object['name']."\n";
- }
- //}
- }
- }
- }
- /* Print only objects with type 4 for servers, may need to come back when virtualization is figured out more */
- /* Example output:
- hostname, Model, OS, S/N, App, Tier, Role, Rack, Postition, IPs with interface, Port Connections, Admin IP, Serial Connection, Admin Connection
- test1, HP ProLiant DL140, Ubuntu 11.10, 1234567abc, MGT, Webserver, TEST, Rack 1, U28-29, net0/192.168.0.25, e1000g0->test2/e1000g0, 192.168.0.254, test-serial:7001, netmgmt->test2/netmgmt
- */
- function print_servers() {
- global $site;
- $ch = curl_init("$site/api.php?method=get_depot");
- curl_setopt($ch, CURLOPT_HEADER, 0);
- curl_setopt($ch, CURLOPT_RETURNTRANSFER,true);
- $data = curl_exec($ch);
- curl_close($ch);
- $d = json_decode($data, true );
- $data = $d['response'];
- echo "hostname, Model, OS, S/N, App, Tier, Role, Rack, Postition, IPs with interface, Port Connections, Admin IP, Serial Connection, Admin Connection\n";
- foreach( $data as $id => $obj )
- {
- /* Set/Reset variables */
- $rack_name = NULL;
- $high = ""; $low = "";
- $u_size = "";
- $chassis = '';
- $sn = '';
- $application = '';
- $role = '';
- $app = '';
- $os = '';
- $netmgmt = '';
- $netmgmt_conn = '';
- $ip_addr = '';
- $conn_info = '';
- $serial = '';
- $obj_curl = curl_init("$site/api.php?method=get_object&object_id=$id&include_attrs=1&include_unset_attrs=1");
- curl_setopt($obj_curl, CURLOPT_HEADER, 0);
- curl_setopt($obj_curl, CURLOPT_RETURNTRANSFER,true);
- $objdata = curl_exec($obj_curl);
- curl_close($obj_curl);
- $obj_json = json_decode($objdata, true );
- $object = $obj_json['response'];
- if ( $object['objtype_id'] != 4 ) {
- continue;
- }
- /* Used to retrieve one host only */
- /* if ( $obj['name'] == 'object-to-filter on' ) {
- print_r( $obj );
- } else {
- continue;
- } */
- /* Retrieve additional attributes */
- $attrs = $object['attrs'];
- foreach ( $attrs as $attr ) {
- switch ( $attr['id'] ) {
- case 1:
- $sn = $attr['a_value'];
- break;
- case 2:
- $chassis = $attr['a_value'];
- break;
- case 4:
- $os = $attr['a_value'];
- break;
- default:
- break;
- }
- }
- /* Retrieve explicit tags */
- $etags = $object['etags'];
- foreach ( $etags as $tag ) {
- switch( $tag['parent_id'] ) {
- case 31:
- $application = $tag['tag'];
- break;
- case 33:
- $role = strtoupper($tag['tag']);
- break;
- case 32:
- $app = strtoupper(substr($tag['tag'],0, 3 ));
- break;
- default:
- break;
- }
- }
- /* Retrieve IPv4 addresses */
- $device = array();
- $ipv4 = $object['ipv4'];
- foreach( $ipv4 as $ip ) {
- if ( $ip['osif'] == "netmgmt" ) {
- $netmgmt = $ip['addrinfo']['ip'];
- continue;
- }
- $device[] = $ip['osif'];
- $addr[] = $ip['addrinfo']['ip'];
- }
- /* Build IP string */
- for ( $i = 0; $i <sizeof($device); $i++ ) {
- if ( isset($device[$i]) ) {
- $ip_addr = $ip_addr.$device[$i]."/".$addr[$i];
- }
- if ( $i + 1 < sizeof($device) ) { $ip_addr = $ip_addr."; "; }
- }
- unset($device); unset($addr);
- /* Retrieve port connections */
- $ports = $object['ports'];
- $remote_nname = array();
- foreach ( $ports as $port ) {
- switch($port['oif_id']) {
- case 24:
- if ( $port['name'] == "netmgmt" && $port['remote_name'] != "" ) {
- $netmgmt_conn = "netmgmt->".$port['remote_object_name']."/".$port['remote_name'];
- continue;
- }
- if ( isset( $port['remote_name'] ) && $port['remote_name'] != "" ) {
- $remote_nname[] = $port['remote_name'];
- $remote_name[] = $port['name'];
- $remote_oname[] = $port['remote_object_name'];
- }
- break;
- case 29:
- if ( $port['remote_object_name'] != "" ) {
- $port_num = trim($port['remote_name'], "tty") + 7000;
- $serial = $port['remote_object_name'].":$port_num";
- }
- default:
- /* Recover wwn info here */
- break;
- }
- }
- for ( $i = 0; $i < sizeof( $remote_nname ); $i++ ) {
- if ( isset($remote_nname[$i]) ) {
- $conn_info = $conn_info.$remote_name[$i]."->".$remote_oname[$i]."/".$remote_nname[$i];
- }
- if ( $i + 1 < sizeof($remote_nname) ) { $conn_info = $conn_info."; "; }
- }
- unset($remote_name); unset($renote_oname); unset($remote_nname);
- /* Check object allocation */
- $rack_curl = curl_init("$site/api.php?method=get_object_allocation&object_id=$id");
- curl_setopt($rack_curl, CURLOPT_HEADER, 0);
- curl_setopt($rack_curl, CURLOPT_RETURNTRANSFER,true);
- $rackdata = curl_exec($rack_curl);
- curl_close($rack_curl);
- $rack_json = json_decode($rackdata, true );
- $racks = $rack_json['response']['racks'];
- //print_r($racks);
- foreach ( $racks as $rack )
- {
- $rack_name = $rack['name'];
- $low = 100; $high = 0;
- /* Kludge together a basic server height */
- for ( $i = 1; $i <= $rack['height'] ; $i++ )
- {
- if ( $rack[$i][2]['state'] == 'T' && $rack[$i][2]['object_id'] == $id )
- {
- // echo "We're mounted!";
- if ( $i > $high ) { $high = $i; }
- if ( $i < $low ) { $low = $i; }
- }
- }
- if ( $high == "" || $low == "" ) { $high = ""; $low = ""; } else { $u_size = "U$low-$high"; }
- }
- unset($racks);
- echo $obj['name'].", $chassis, $os, $sn, $app, $application, $role, $rack_name, $u_size, $ip_addr, $conn_info, $netmgmt, $serial, $netmgmt_conn";
- echo "\n";
- }
- // print_r($data);
- }
- print_servers();
- ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement