WP-XenServerStats – WordPress Plugin for XenServer 2/2

WP-XenServerStats – WordPress Plugin for XenServer 1/2
WP-XenServerStats – WordPress Plugin for XenServer 2/2

In the first part of this blog, I made a list about what I wanted to do, how to get the data and how to transport it from my XenServer (home) to my Web Host (Datacenter). Now I’ll explain how I create one WordPress plug-in (my first one) to show everything :

It looks good, and even if it’s really low level on the dev side in PHP and scripting, it works well. In WordPress, there is this sidebar, on the right, if you want to add something you can chose to do it as widget, it’s clean and not hard coded in the main WordPress code, then you can easily keep this plug-in and keep it working without changing anything from a theme to another.

I created a file named : wp-xenserverstats.php in a new directory : http://www.domainname.com/wp-content/plugins/wp-xenserverstats/wp-xenserverstats.php this is the main plug-in file and as you can read bellow, this is very very basic.

  1. <?php
  2. /*
  3. Plugin Name: WP-XenServerStats
  4. Plugin URI: http://www.archy.net
  5. Description: Citrix XenServer statistics
  6. Version: 0.2
  7. Author: Stephane Thirion
  8. Author URI: http://www.archy.net
  9. */
  10.  
  11. function xenserverhostname()
  12. {
  13. $myFile = "/homez.35/archy/www/wp-content/plugins/wp-xenserverstats/hostname.txt";
  14. $fh = fopen($myFile, 'r');
  15. $theData = fread($fh, 12);
  16. fclose($fh);
  17. echo $theData;
  18. }
  19.  
  20. function hostcpu()
  21. {
  22. $myFile = "/homez.35/archy/www/wp-content/plugins/wp-xenserverstats/host_cpus.txt";
  23. $fh = fopen($myFile, 'r');
  24. $theData = fread($fh, 2);
  25. fclose($fh);
  26. echo $theData;
  27. }
  28.  
  29. function hostmem()
  30. {
  31. $myFile = "/homez.35/archy/www/wp-content/plugins/wp-xenserverstats/host_mem.txt";
  32. $fh = fopen($myFile, 'r');
  33. $theData = fread($fh, 10);
  34. fclose($fh);
  35. echo $theData;
  36. }
  37.  
  38. function totalvmscount()
  39. {
  40. $myFile = "/homez.35/archy/www/wp-content/plugins/wp-xenserverstats/vms_total.txt";
  41. $fh = fopen($myFile, 'r');
  42. $theData = fread($fh, 2);
  43. fclose($fh);
  44. echo $theData;
  45. }
  46.  
  47. function totalpoweredvmscount()
  48. {
  49. $myFile = "/homez.35/archy/www/wp-content/plugins/wp-xenserverstats/vms_powered.txt";
  50. $fh = fopen($myFile, 'r');
  51. $theData = fread($fh, 2);
  52. fclose($fh);
  53. echo $theData;
  54. }
  55.  
  56. function totalhaltedvmscount()
  57. {
  58. $myFile = "/homez.35/archy/www/wp-content/plugins/wp-xenserverstats/vms_halted.txt";
  59. $fh = fopen($myFile, 'r');
  60. $theData = fread($fh, 2);
  61. fclose($fh);
  62. echo $theData;
  63. }
  64.  
  65. function totalsuspendedvmscount()
  66. {
  67. $myFile = "/homez.35/archy/www/wp-content/plugins/wp-xenserverstats/vms_suspended.txt";
  68. $fh = fopen($myFile, 'r');
  69. $theData = fread($fh, 2);
  70. fclose($fh);
  71. echo $theData;
  72. }
  73.  
  74. function lastupdate()
  75. {
  76. $myFile = "/homez.35/archy/www/wp-content/plugins/wp-xenserverstats/last_update.txt";
  77. $fh = fopen($myFile, 'r');
  78. $theData = fread($fh, 26);
  79. fclose($fh);
  80. echo $theData;
  81. }
  82.  
  83. function widget_wpxenserver($args)
  84. {
  85. extract($args);
  86. echo $before_widget;
  87. echo $before_title;
  88. echo "XenServer Stats";
  89. echo $after_title;
  90. echo "<div style='text-align: left; padding-left: 10px;'><b><u><img src='".$baseurl."/wp-content/plugins/wp-xenserverstats/images/host.png'> Host : ";
  91. xenserverhostname();
  92.  
  93. echo "</u></b><br>Cpu Nbr : ";
  94. hostcpu();
  95.  
  96. echo "/ Memory : ";
  97. hostmem();
  98.  
  99. echo "Mb<br><br><img src='".$baseurl."/wp-content/plugins/wp-xenserverstats/images/vmstotal.png'> Virtual Machines : ";
  100. totalvmscount();
  101.  
  102. echo "<br><img src='".$baseurl."/wp-content/plugins/wp-xenserverstats/images/powered.png'> Powered On VMs : ";
  103. totalpoweredvmscount();
  104.  
  105. echo "<br><img src='".$baseurl."/wp-content/plugins/wp-xenserverstats/images/halted.png'> Halted VMs : ";
  106. totalhaltedvmscount();
  107.  
  108. echo "<br><img src='".$baseurl."/wp-content/plugins/wp-xenserverstats/images/paused.png'> Suspended VMs : ";
  109. totalsuspendedvmscount();
  110.  
  111. echo "<br><br><i>Last Update : ";
  112. lastupdate();
  113.  
  114. echo "</i></div>";
  115. echo $after_widget;
  116. }
  117.  
  118. function wpxenserver_init()
  119. {
  120. register_sidebar_widget(__('XenServer Widget'), 'widget_wpxenserver');
  121. }
  122. add_action("plugins_loaded", "wpxenserver_init");
  123. ?>

So as you see, there is still some hard coded path… I try to get rid of that this week, and if someone can give me the tip, it will save me some time…

I think you have everything to try to make it work on your side with some change. Of course if you want the information refresh automatically on your blog you need to setup a cron, I chose hourly for me, to run the script describe in the first part of this blog.

You can download a zip file containing the plug-in directory with the bash script to gather all the data on your XenServer here :

  WP-XenServer-Stats (106.5 KiB, 1,626 hits)

For some of you more skilled than me, maybe you would like to help, you can send me your modification or post comment here, I’ll modify the blog entries or create a new blog if needed 🙂

Here is the list about things I want to do to make this plugin easy to setup and use :

  • Export all data in MySQL instead of files
  • Use path variable for everything
  • Adding more information : XenServer host version, Disk usage, Network usage, Memory usage
  • Adding a graphic performance for the host and for each VMs

I hope some of you will test that out and share the www their XenServer stats !

Post author