Coding a XenServer Client in Python – Monitoring Multiple VMs via Multiple Threads

HueXenClient - XenServer client written in Python using the Citrix XenAPI

HueXenClient - XenServer client written in Python using the Citrix XenAPI

In some prior blogs I had written about developing an ESXi client in Java which could connect to a VMware ESXi server and perform some management tasks. In this blog, I will describe developing a Cirtrix XenServer Client in Python that connects to a Citrix XenServer or XenServer pool; the client can also perform management tasks and provides some advanced monitoring features.

HueXenClient is the name of the program; it was written to demonstrate the possibilities of what can be done using the Citrix XenAPI. The most advanced feature of the program is the capability to monitor multiple VMs simultaneously for loss of network connectivity. The program acts as a management station and pings the VMs via internal mechanism by creating its own ICMP packets and sending the ping request.

This program was written in a short time-span; even-so, I have included proper error/exception handling for all issues I came across during testing. I will be adding on to this application over the next few releases which will be available on GitHub and on the download section of this website. For the latest code, you should download from GitHub.

Development Environment Setup:

HueXenClient is written in Python and uses the included Tkinter GUI library in addition to the Python megawidgets library and the Citrix XenAPI for XenServer 6.0.

You can use whatever editor you like. I used the Eclipse program and the PyDev plug-in. It should also be mentioned that this program was developed, run, and tested on a Windows 7 machine. Although, it should theoretically work with Linux, this has not been tested with Linux and some of the libraries/code may behave differently.

If you download the program from GitHub, you will have to download the Python megawidgets library separately and appropriately link to all the libraries. If you download the code from my personal website, I have included the entire Eclipse project with respective libraries within the download package. However, you will still need to install Eclipse and the PyDev plug-in if that’s what you choose to use.

Testing Environment Setup:

Citrix XenServer Pool Setup

Citrix XenServer Pool Setup


This program was developed, run, and tested on a Windows 7 machine. It was tested against a XenServer setup which consisted of two XenServers in a XenServer pool. Nine VMs existed across both servers combined. Each VM only had one NIC adapter and IP assigned to it. Currently the VM Network Monitoring feature of the application only supports monitoring a VM with one NIC and IP assigned. Also, XenTools must be installed on the respective VM for many of the application functions to work. Additionally, in my setup, all hosts and VMs were part of the same network.

Running the Program:

I used Windows 7 as the operating system during the development process. To run the program from Eclipse, make sure you have the Pydev Eclipse plugin installed. Right click Eclipse and “Run as administrator”. You must run as administrator or root as the application opens sockets and sends ICMP packets without depending on the operating system. If you downloaded the zip file from my website, I have included all the libraries you need in the zip file with the entire Eclipse project.

Connecting to the master and slave host of the XenServer pool:

You must enter the IP address of the XenServer pool master host before attempting to connect, otherwise, you must select the ‘Login to slave host option’ from the ‘Options’ menu as shown below before connecting. Note, you can only perform operations when connecting to the master host. You can only view VMs and “Filter powered-on VMs by host” when connecting to a slave host.

HueXenCHueXenClient - connecting to a slave host of a XenServer Pool

HueXenClient - connecting to a slave host of a XenServer Pool



VM Network Monitoring Feature:

Currently the VM Network Monitoring feature of the application only supports monitoring a VM with one NIC and IP assigned. XenTools must also be installed on the respective VM. The VM Network monitoring feature is a check mark option under the “Options” menu in the toolbar. It will be grayed-out until you select a VM from the dropdown box. Once you select the “Start network monitoring on VM” option from the menu, the network connectivity to the VM is monitored by the application via ICMP ping packets. Two pings are sent around every 5 seconds. If two consecutive pings are missed, a snapshot of the VM is automatically created and the VM stops being monitored. This is demonstrated below using a single VM and by disabling the VM’s adapter so that the pings are intentionally missed.

It is important to remember that that the application is monitoring the VM via external pings, so you must ensure that the application has IP reach-ability to the VM you would like to monitor.

1.

HueXenClient - enabling network monitoring on a VM

HueXenClient - enabling network monitoring on a VM

2.

XenCenter - disabling the network adapter of the VM being monitored

XenCenter - disabling the network adapter of the VM being monitored

3.

XenCenter - snapshot of VM automatically created after two consecutive pings are missed

XenCenter - snapshot of VM automatically created after two consecutive pings are missed

VM Network Monitoring on Multiple VMs Simultaneously:

Since the application is using threads, simultaneous VM network monitoring is supported. This feature is pretty cool and implementing it was a bit of a challenge. I used an open source python ping implementation which had to be modified to work correctly with my application. Since ICMP does not innately have a concept of ports, I had to implement a way for the correct VM to receive the response, otherwise messages are received by all listeners. I accomplished this by creating a unique hash for each thread that associates it with the respective VM.

One important thing to remember is that in an app like this where multiple threads are running in the background, you want to make sure you implement a solution where the threads are killed when the application is exited. I implemented the below code in both the ‘Disconnect’ button and the ‘Exit’ option in the menu. Upon exit, it loops through all the VMs and if needed gets its monitoring object and shuts it down, which in turn shuts down the associated thread for that VM which is sending out the pings. Pretty cool!

HueXenClient code - upon exiting app, shuts down any VM monitoring threads

HueXenClient code - upon exiting app, shuts down any VM monitoring threads



As you can see this app can be pretty useful and can easily be expanded. That’s it for this tutorial; feel free to dive into the code!

This entry was posted in Networking, Programming Languages, Protocols, Python, Technology, Virtualization and Cloud Computing and tagged , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , . Bookmark the permalink.

Leave a Reply

Your email address will not be published. Required fields are marked *


2 + = eight