WP-XenServerStats – WordPress Plugin for XenServer 1/2

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

I got this idea when I saw Nick (Show And Tell : First vSphere Plugin for WordPress!) doing something I wanted to do for Citrix XenServer. Then before trying to do myself (I’m a very bad dev….) I’ve try to find someone who did it better than I would ever do… And no one did it(yet)…

So here I am trying to make something nice and learning again something new (php / mysql / function etc…) just to show off my XenServer @ Home hypervisor stats. I got everything working, but I’m sure someone, better dev than me would make a really cleaner and efficient job.

I’ve study Citrix XenServer 5.6 FP1 SDK documentation / references and this is hard (for me) to try to find a good way to handle and find values I’m looking for. So my first step was to list everything I wanted to see on my widget bar (the bar on the right on this blog)

Here is the list :

  • Show Hostname
  • Show Number of CPU
  • Show Memory amount
  • Show Number of VMs
  • Show Number of VMs On
  • Show Number of VMs Off
  • Show Number of VMs Paused
  • Show Last data refresh update time
  • Automatic information refresh every hour
  • Storing these data in my MySQL database

At first I needed to find a mechanism to get all those data the simplest way possible. As I told you, I’m not very comfortable yet with MySQL stuffs, so I’ll do it the old way because there is really not much data and I think this is a good start.

To get the amount of VMs running on my host it came very quicky a simple way to get it :

[root@suomixen log]# xe vm-list | grep -c power-state
18

And then using the same way, it was be very easy to get the Powered, Halted and Suspended ones :

[root@suomixen log]# xe vm-list | grep -c running
13
[root@suomixen log]# xe vm-list | grep -c halted
5
[root@suomixen log]# xe vm-list | grep -c suspended
0

As you might have understand I’m listing VM with their status and I’m using the grep -c option to count the number of line in the result. Why I’m doing it that way ? Because I already know how to read a value from a text file in php and I won’t burst my mind yet to try something else. If I’m able to transport this information from my XenServer to my Web Host, I’ll be able to display it.

I’m still missing some data, the number of CPU and amount of memory, for the CPUs I used the same method as before :

[root@suomixen log]# xe host-cpu-list | grep -c uuid
4

And for the memory I chose to use the xeninfo, heritage from XenSource :

[root@suomixen log]# xeninfo host-total-mem
16776284

and I got the total memory ! I needed some more data, the hostname and the last data refresh update. The best way to get the hostname is still under work but I figure out how to get the last update refresh time : As I will create one file for each value (no MySQL yet) I just need to get the time stamp of one of these file.

[root@suomixen statxenwp]# date -r vms_total.txt -R
Sat, 05 Feb 2011 20:51:18 +0100

vm_total.txt is the text file generated and containing the total VMs number. Now I have all the information I need to go forward, I can just create a script to generate all the files with the information I wanted to use.

Here is what I’ve done, this is very very simple and very basic, but it did the trick :

#!/bin/bash
#Made to use with WP-XenServerStat plugin for WordPress
#Script by Stephane Thirion http://www.archy.net
#
# 05 Feb 2011 - Script Creation
# 06 Feb 2011 - Hostname added
 
xe vm-list | grep -c power-state > /tmp/statxenwp/vms_total.txt
xe vm-list | grep -c running > /tmp/statxenwp/vms_powered.txt
xe vm-list | grep -c halted > /tmp/statxenwp/vms_halted.txt
xe vm-list | grep -c suspended > /tmp/statxenwp/vms_suspended.txt
xe host-cpu-list | grep -c uuid > /tmp/statxenwp/host_cpus.txt
xeninfo host-total-mem > /tmp/statxenwp/host_mem.txt
hostname > /tmp/statxenwp/hostname.txt
 
cd /tmp/statxenwp/
 
date -r vms_total.txt -R > last_update.txt
 
wput ftp://login:password@ftp.yourftpaddress.com/www/wp-content/plugins/wp-xenserverstats/ *.txt
 
rm *.txt -f

Now I have 8 text files with information inside :

[root@suomixen statxenwp]# ll
total 28
-rw-r--r-- 1 root root 2 Feb  6 16:51 host_cpus.txt
-rw-r--r-- 1 root root 9 Feb  6 16:51 host_mem.txt
-rw-r--r-- 1 root root 9 Feb  6 16:51 hostname.txt
-rw-r--r-- 1 root root 2 Feb  6 16:51 vms_halted.txt
-rw-r--r-- 1 root root 3 Feb  6 16:51 vms_powered.txt
-rw-r--r-- 1 root root 2 Feb  6 16:51 vms_suspended.txt
-rw-r--r-- 1 root root 3 Feb  6 16:51 vms_total.txt

That’s cool ! but another problem is coming… How I can transfer these files to my Web Host, my XenServer is at my place, and my Web Host is outside… I don’t know how to send file by FTP without ftp, because ftp is not present on a XenServer (maybe I just didn’t found it…) I through maybe I could use the wget command line tool, but you can only get with wget (obviously) So I looked over the internet for a tool able to send file over ftp without have to install ftp client on XenServer. I found wput (http://wput.sourceforge.net/) which do exactly what I was looking for. Then another issue pop up, I had to compile wput source, so I did it in my Citrix XenServer 5.6 FP1 SDK VM and then just copy the wput binary to my XenServer. (If you need / want more information about that, just ask) The script needed to be modify to include the file sending with wput :

#!/bin/bash
#Made to use with WP-XenServerStat plugin for WordPress
#Script by Stephane Thirion http://www.archy.net
#
# 05 Feb 2011 - Script Creation
 
xe vm-list | grep -c power-state > /tmp/statxenwp/vms_total.txt
xe vm-list | grep -c running > /tmp/statxenwp/vms_powered.txt
xe vm-list | grep -c halted > /tmp/statxenwp/vms_halted.txt
xe vm-list | grep -c suspended > /tmp/statxenwp/vms_suspended.txt
xe host-cpu-list | grep -c uuid > /tmp/statxenwp/host_cpus.txt
xeninfo host-total-mem > /tmp/statxenwp/host_mem.txt
 
cd /tmp/statxenwp/
 
date -r vms_total.txt -R > last_update.txt
 
wput ftp://user:pass@ftp.address.com/wp-content/plugins/wp-xenserver/ *.txt
 
rm *.txt -f

Yes! I have the data, I can send it over the internet but how I can display it on my blog ??? This is the next blog to come.

Post author