Network MAP PHP
Problem this snippet solves:
Hi all,
Here is a PHP script to use if you want to present you network map to a NOC or to the Application Support & Maintenance Services.
This replicate Network Map of F5 web interface without the need to give any credentials to them.
How to use this snippet:
You are going to need to change your credentials (comments //changethis), and to download and change the names of the images for the vs,members,nodes status. Place the images on images folder.
Then just run the php script in your web browser and it will pull the information and present it on html.
Code :
f5 monitoring page F5 server - $f5server
"; // funtion to display images. you can download them for F5 web interface. Place them into ./images folder function getgif($status) { $img_path = "/images"; switch($status) { case 'up': $img = $img_path."/status_circle_green.png"; break; case 'down': $img = $img_path."/status_diamond_red.png"; break; case 'user-down': $img = $img_path."/status_diamond_black.png"; break; case 'unchecked'; $img = $img_path."/status_square_blue.png"; break; default: $img = $img_path."/status_circle_outline.gif"; } return $img; } $ch = curl_init(); curl_setopt($ch, CURLOPT_URL, "https://$user:$password@$f5server/mgmt/tm/ltm/pool/"); curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false); curl_setopt($ch, CURLOPT_VERBOSE, true); $pools = json_decode(curl_exec($ch)); curl_setopt($ch, CURLOPT_URL, "https://$user:$password@$f5server/mgmt/tm/ltm/virtual/"); $vservers = json_decode(curl_exec($ch)); curl_setopt($ch, CURLOPT_URL, "https://$user:$password@$f5server/mgmt/tm/ltm/node/"); $nodes = json_decode(curl_exec($ch)); curl_close($ch); //NETWORK MAP presentation echo "
VIRTUAL SERVERS
"; foreach ($vservers->{'items'} as $vs){ echo strtolower($vs->{'name'}); echo " ".$vs->{'destination'}; if ($vs->{'rules'}[1] != NULL){ echo "
"; echo $vs->{'rules'}[1]; } foreach ($pools->{'items'} as $item ){ if ($vs->{'pool'} == $item->{'fullPath'}){ echo "
"; $stateurl = $item->{'membersReference'}; echo " "; echo strtolower($item->{'name'}); echo " - (".$item->{'loadBalancingMode'}.")"; echo "
"; $rewrited_url = str_replace('localhost',$user.":".$password."@".$f5server,$stateurl->{'link'}); $ch = curl_init(); curl_setopt($ch, CURLOPT_URL, $rewrited_url); curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false); curl_setopt($ch, CURLOPT_VERBOSE, true); $members = json_decode(curl_exec($ch)); curl_close($ch); foreach ($members->{'items'} as $member) { $img_path = getgif($member->{'state'}); echo " "; echo strtolower($member->{'name'}); echo " - (".$member->{'address'}.")"; foreach ($nodes->{'items'} as $node){ if ($member->{'address'} === $node->{'address'}){ $img_path = getgif($node->{'state'}); echo " "; echo strtolower($node->{'name'}); } } echo "
"; } } } echo "
"; } } catch (Exception $e) { echo "Error: " .$e->getMessage(); } ?>
- Christopher_YouNimbostratus
I was able to get this working after a little effort. Thank you!
There must have been an issue with the copy and paste. The code above is missing a top portion where the php begins
all of the > should be replaced with a greater than sign.
I had some other issues I think with missing code - I deleted lines 86 - 90 after making the fixes I stated above and it all worked.
Thanks again.
Chris
- Donamato_01_150Nimbostratus
Chris, Could you please post your updated code
I did make the changes mentioned above, couldn't make it working. Please advise whats wrong.
f5 monitoring page "; // funtion to display images. you can download them for F5 web interface. Place them into ./images folder function getgif($status) { $img_path = "/images"; switch($status) { case 'up': $img = $img_path."/status_circle_green.png"; break; case 'down': $img = $img_path."/status_diamond_red.png"; break; case 'user-down': $img = $img_path."/status_diamond_black.png"; break; case 'unchecked'; $img = $img_path."/status_square_blue.png"; break; default: $img = $img_path."/status_circle_outline.gif"; } return $img; } $ch = curl_init(); curl_setopt($ch, CURLOPT_URL, "https://$user:$password@$f5server/mgmt/tm/ltm/pool/"); curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false); curl_setopt($ch, CURLOPT_VERBOSE, true); $pools = json_decode(curl_exec($ch)); curl_setopt($ch, CURLOPT_URL, "https://$user:$password@$f5server/mgmt/tm/ltm/virtual/"); $vservers = json_decode(curl_exec($ch)); curl_setopt($ch, CURLOPT_URL, "https://$user:$password@$f5server/mgmt/tm/ltm/node/"); $nodes = json_decode(curl_exec($ch)); curl_close($ch); //NETWORK MAP presentation echo " VIRTUAL SERVERS "; foreach ($vservers->{'items'} as $vs){ echo strtolower($vs->{'name'}); echo " ".$vs->{'destination'}; if ($vs->{'rules'}[1] != NULL){ echo " "; echo $vs->{'rules'}[1]; } foreach ($pools->{'items'} as $item ){ if ($vs->{'pool'} == $item->{'fullPath'}){ echo " "; $stateurl = $item->{'membersReference'}; echo " "; echo strtolower($item->{'name'}); echo " - (".$item->{'loadBalancingMode'}.")"; echo " "; $rewrited_url = str_replace('localhost',$user.":".$password."@".$f5server,$stateurl->{'link'}); $ch = curl_init(); curl_setopt($ch, CURLOPT_URL, $rewrited_url); curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false); curl_setopt($ch, CURLOPT_VERBOSE, true); $members = json_decode(curl_exec($ch)); curl_close($ch); foreach ($members->{'items'} as $member) { $img_path = getgif($member->{'state'}); echo " "; echo strtolower($member->{'name'}); echo " - (".$member->{'address'}.")"; foreach ($nodes->{'items'} as $node){ if ($member->{'address'} === $node->{'address'}){ $img_path = getgif($node->{'state'}); echo " "; echo strtolower($node->{'name'}); } } echo " "; } } } echo " "; } ?>
- Christopher_YouNimbostratus
You should have this on line 9:
echo "F5 server - $f5server ";
- Christopher_YouNimbostratus
This is a work in progress for me. I am modifying the original script. Not completed yet for me - but here is my current version. I am just hacking my way along. Not a coder by any means. I don't have a lot of time right now to put a lot of effort into this.
f5 monitoring page $f5server "; // funtion to display images. you can download them for F5 web interface. Place them into ./images folder function getgif($status) { $img_path = "/images"; switch($status) { case 'up': $img = $img_path."/status_circle_green.png"; break; case 'down': $img = $img_path."/status_diamond_red.png"; break; case 'user-down': $img = $img_path."/status_diamond_black.png"; break; case 'unchecked'; $img = $img_path."/status_square_blue.png"; break; case 'available'; $img = $img_path."/status_circle_green.png"; break; case 'offline': $img = $img_path."/status_diamond_red.png"; break; case 'unknown'; $img = $img_path."/status_square_blue.png"; break; case 'unavailable'; $img = $img_path."/status_triangle_yellow.png"; break; default: $img = $img_path."/status_circle_outline.gif"; } return $img; } $ch = curl_init(); curl_setopt($ch, CURLOPT_URL, "https://$user:$password@$f5server/mgmt/tm/ltm/pool/"); curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false); curl_setopt($ch, CURLOPT_VERBOSE, true); $pools = json_decode(curl_exec($ch)); curl_setopt($ch, CURLOPT_URL, "https://$user:$password@$f5server/mgmt/tm/ltm/virtual/"); $vservers = json_decode(curl_exec($ch)); curl_setopt($ch, CURLOPT_URL, "https://$user:$password@$f5server/mgmt/tm/ltm/node/"); $nodes = json_decode(curl_exec($ch)); curl_close($ch); //NETWORK MAP presentation // echo " VIRTUAL SERVERS "; foreach ($vservers->{'items'} as $vs){ $ch = curl_init(); curl_setopt($ch, CURLOPT_URL, "https://$user:$password@$f5server/mgmt/tm/ltm/virtual/".$vs->{'name'}."/stats/"); curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false); curl_setopt($ch, CURLOPT_VERBOSE, true); $virtual_stats = json_decode(curl_exec($ch)); curl_close($ch); $virtual_entries = $virtual_stats->{'entries'}; $virtual_availabilityState = $virtual_entries->{'status.availabilityState'}; $virtual_stateDescription = $virtual_availabilityState->{'description'}; $img_path = getgif($virtual_stateDescription); echo "VS:![\](.$img_path.)"; echo strtolower($vs->{'name'}); // echo " ".$vs->{'destination'}; if ($vs->{'rules'}[1] != NULL){ // echo " "; echo $vs->{'rules'}[1]; } foreach ($pools->{'items'} as $item ){ if ($vs->{'pool'} == $item->{'fullPath'}){ // echo " "; echo " Pool:"; $stateurl = $item->{'membersReference'}; // echo "fullPath: ".$item->{'name'}; $ch = curl_init(); curl_setopt($ch, CURLOPT_URL, "https://$user:$password@$f5server/mgmt/tm/ltm/pool/".$item->{'name'}."/stats/"); curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false); curl_setopt($ch, CURLOPT_VERBOSE, true); $pool_stats = json_decode(curl_exec($ch)); curl_close($ch); $pool_entries = $pool_stats->{'entries'}; $pool_availabilityState = $pool_entries->{'status.availabilityState'}; $pool_stateDescription = $pool_availabilityState->{'description'}; $img_path = getgif($pool_stateDescription); echo "![\](.$img_path.)"; echo strtolower($item->{'name'}); // echo " "; $rewrited_url = str_replace('localhost',$user.":".$password."@".$f5server,$stateurl->{'link'}); $ch = curl_init(); curl_setopt($ch, CURLOPT_URL, $rewrited_url); curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false); curl_setopt($ch, CURLOPT_VERBOSE, true); $members = json_decode(curl_exec($ch)); curl_close($ch); foreach ($members->{'items'} as $member) { $img_path = getgif($member->{'state'}); echo " Member:![\](.$img_path.)"; echo strtolower($member->{'name'}); echo " - (".$member->{'address'}.")"; // foreach ($nodes->{'items'} as $node){ // if ($member->{'address'} === $node->{'address'}){ // $img_path = getgif($node->{'state'}); // echo "![\](.$img_path.)"; // echo strtolower($node->{'name'}); // } // } //echo " "; } //echo " "; } } echo " "; } // catch (Exception $e) { // echo "Error: " .$e->;getMessage(); // } ?>
- Maneesh_72711Cirrostratus
In order to run this what all do we need ?
1) Tomcat running on a Unix platform (considering curl commands) ? 2) What all PHP libraries installed ? 3) Anything more ?
- renato_martins_Nimbostratus
Hi Maneesh,
I have used Ubuntu with Apache and PHP. sudo apt-get install apache2 php5 libapache2-mod-php5
Configure the virtual server on apache, make sure that the service is running.
Create a .php file and paste the script code and save it. Copy the file to the Apache folder (the default is /var/www/html if am not wrong).
Access it in server host by using http://172.0.0.1/.php
I hope that this information helps.
- Maneesh_72711Cirrostratus
It definitely does, can you share the sample how does the view look like from end user perspective.
- Donamato_01_150Nimbostratus
Great script, I've got it working with the exception of the images - example - vs:![](./images/status_circle_outline.gif.)web1_dc1_80_vs/Common/loggerv1 Pool:![](./images/status_circle_outline.gif.)web1_dc1_80_pool Member:![](./images/status_circle_green.png.)web1_dc1:80 - (10.101.0.31)
Any idea what i'm missing ?
- renato_martins_Nimbostratus
Hi Donamato01,
Are the images using the same name as the code above and on the same directory(/images) ? I have also noticed that your images have a dot (.) at the end and different extensions.
The folder images needs to be at the same level as the php script. Or you need to change the variable $img_path to suit your case.
Let us know if you were able to fix it.
Best regards
Renato Martins