{"id":3981,"date":"2012-03-30T02:08:10","date_gmt":"2012-03-30T08:08:10","guid":{"rendered":"http:\/\/humairahmed.com\/blog\/?p=3981"},"modified":"2012-04-05T21:12:08","modified_gmt":"2012-04-06T03:12:08","slug":"coding-a-xenserver-client-monitoring-multiple-vms-via-multiple-threads","status":"publish","type":"post","link":"https:\/\/humairahmed.com\/blog\/?p=3981","title":{"rendered":"Coding a XenServer Client in Python &#8211; Monitoring Multiple VMs via Multiple Threads"},"content":{"rendered":"<div class=\"twttr_buttons\"><div class=\"twttr_twitter\">\n\t\t\t\t\t<a href=\"http:\/\/twitter.com\/share?text=Coding+a+XenServer+Client+in+Python+-+Monitoring+Multiple+VMs+via+Multiple+Threads\" class=\"twitter-share-button\" data-via=\"\" data-hashtags=\"\"  data-size=\"default\" data-url=\"https:\/\/humairahmed.com\/blog\/?p=3981\"  data-related=\"\" target=\"_blank\">Tweet<\/a>\n\t\t\t\t<\/div><\/div><p><div id=\"attachment_4048\" style=\"width: 251px\" class=\"wp-caption alignleft\"><a href=\"http:\/\/humairahmed.com\/blog\/wp-content\/uploads\/2012\/03\/xen_api_test_3.png\"><img loading=\"lazy\" decoding=\"async\" aria-describedby=\"caption-attachment-4048\" src=\"http:\/\/humairahmed.com\/blog\/wp-content\/uploads\/2012\/03\/xen_api_test_3-241x300.png\" alt=\"HueXenClient - XenServer client written in Python using the Citrix XenAPI\" title=\"HueXenClient - XenServer client written in Python using the Citrix XenAPI\" width=\"241\" height=\"300\" class=\"size-medium wp-image-4048\" srcset=\"https:\/\/humairahmed.com\/blog\/wp-content\/uploads\/2012\/03\/xen_api_test_3-241x300.png 241w, https:\/\/humairahmed.com\/blog\/wp-content\/uploads\/2012\/03\/xen_api_test_3.png 411w\" sizes=\"auto, (max-width: 241px) 100vw, 241px\" \/><\/a><p id=\"caption-attachment-4048\" class=\"wp-caption-text\">HueXenClient - XenServer client written in Python using the Citrix XenAPI<\/p><\/div>In some <a href=\"http:\/\/humairahmed.com\/blog\/?p=3851\">prior blogs<\/a> 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. <!--more--><br \/>\n<br \/>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.<br \/>\n<br \/>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 <a href=\"https:\/\/github.com\/HumairAhmed\/HueXenClient\">GitHub<\/a> and on the <a href=\"http:\/\/humairahmed.com\/#Downloads\">download section<\/a> of this website. For the latest code, you should download from <a href=\"https:\/\/github.com\/HumairAhmed\/HueXenClient\">GitHub<\/a>.<\/p>\n<p><strong><u>Development Environment Setup:<\/u><\/strong><br \/>\n<br \/>HueXenClient is written in Python and uses the included Tkinter GUI library in addition to the <a href=\"http:\/\/pmw.sourceforge.net\/\">Python megawidgets library<\/a> and the <a href=\"http:\/\/community.citrix.com\/display\/xs\/Download+SDKs\">Citrix XenAPI<\/a> for XenServer 6.0.<br \/>\n<br \/>You can use whatever editor you like. I used the Eclipse program and the <a href=\"http:\/\/pydev.org\/\">PyDev plug-in<\/a>. 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.<br \/>\n<br \/>If you download the program from <a href=\"https:\/\/github.com\/HumairAhmed\/HueXenClient\">GitHub<\/a>, you will have to download the <a href=\"http:\/\/pmw.sourceforge.net\/\">Python megawidgets library<\/a> separately and appropriately link to all the libraries. If you download the code from <a href=\"http:\/\/humairahmed.com\/#Downloads\">my personal website<\/a>, I have included the entire Eclipse project with respective libraries within the download package. However, you will still need to install Eclipse and the <a href=\"http:\/\/pydev.org\/\">PyDev plug-in<\/a> if that\u2019s what you choose to use.<\/p>\n<p><strong><u>Testing Environment Setup:<\/u><\/strong><br \/>\n<div id=\"attachment_3985\" style=\"width: 210px\" class=\"wp-caption alignleft\"><a href=\"http:\/\/humairahmed.com\/blog\/wp-content\/uploads\/2012\/03\/xen_server_pool-e1333028181508.png\"><img loading=\"lazy\" decoding=\"async\" aria-describedby=\"caption-attachment-3985\" src=\"http:\/\/humairahmed.com\/blog\/wp-content\/uploads\/2012\/03\/xen_server_pool-e1333028181508.png\" alt=\"Citrix XenServer Pool Setup\" title=\"Citrix XenServer Pool Setup\" width=\"200\" height=\"273\" class=\"size-full wp-image-3985\" \/><\/a><p id=\"caption-attachment-3985\" class=\"wp-caption-text\">Citrix XenServer Pool Setup<\/p><\/div><br \/>\nThis 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. <\/p>\n<p><strong><u>Running the Program:<\/u><\/strong><br \/>\n<br \/>I used Windows 7 as the operating system during the development process. To run the program from Eclipse, make sure you have the <a href=\"http:\/\/pydev.org\/\">Pydev Eclipse plugin<\/a> installed. Right click Eclipse and \u201cRun as administrator\u201d. 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 <a href=\"http:\/\/humairahmed.com\/#Downloads\">my website<\/a>, I have included all the libraries you need in the zip file with the entire Eclipse project.<br \/>\n<br \/><strong>Connecting to the master and slave host of the XenServer pool:<\/strong><br \/>\n<br \/>You must enter the IP address of the XenServer pool master host before attempting to connect, otherwise, you must select the \u2018Login to slave host option\u2019 from the \u2018Options\u2019 menu as shown below before connecting. Note, you can only perform operations when connecting to the master host. You can only view VMs and \u201cFilter powered-on VMs by host\u201d when connecting to a slave host. <div id=\"attachment_4037\" style=\"width: 420px\" class=\"wp-caption aligncenter\"><a href=\"http:\/\/humairahmed.com\/blog\/wp-content\/uploads\/2012\/03\/xen_api_test_1.png\"><img loading=\"lazy\" decoding=\"async\" aria-describedby=\"caption-attachment-4037\" src=\"http:\/\/humairahmed.com\/blog\/wp-content\/uploads\/2012\/03\/xen_api_test_1.png\" alt=\"HueXenCHueXenClient - connecting to a slave host of a XenServer Pool\" title=\"HueXenClient - connecting to a slave host of a XenServer Pool\" width=\"410\" height=\"131\" class=\"size-full wp-image-4037\" srcset=\"https:\/\/humairahmed.com\/blog\/wp-content\/uploads\/2012\/03\/xen_api_test_1.png 410w, https:\/\/humairahmed.com\/blog\/wp-content\/uploads\/2012\/03\/xen_api_test_1-300x95.png 300w\" sizes=\"auto, (max-width: 410px) 100vw, 410px\" \/><\/a><p id=\"caption-attachment-4037\" class=\"wp-caption-text\">HueXenClient - connecting to a slave host of a XenServer Pool<\/p><\/div><br \/>\n<br \/><strong><u>VM Network Monitoring Feature:<\/u><\/strong><br \/>\n<br \/>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 \u201cOptions&#8221; menu in the toolbar. It will be grayed-out until you select a VM from the dropdown box. Once you select the \u201cStart network monitoring on VM\u201d 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\u2019s adapter so that the pings are intentionally missed. <\/p>\n<p>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.<br \/>\n<br \/><strong>1.<\/strong> <div id=\"attachment_4042\" style=\"width: 421px\" class=\"wp-caption aligncenter\"><a href=\"http:\/\/humairahmed.com\/blog\/wp-content\/uploads\/2012\/03\/xen_api_test_4.png\"><img loading=\"lazy\" decoding=\"async\" aria-describedby=\"caption-attachment-4042\" src=\"http:\/\/humairahmed.com\/blog\/wp-content\/uploads\/2012\/03\/xen_api_test_4.png\" alt=\"HueXenClient - enabling network monitoring on a VM\" title=\"HueXenClient - enabling network monitoring on a VM\" width=\"411\" height=\"512\" class=\"size-full wp-image-4042\" srcset=\"https:\/\/humairahmed.com\/blog\/wp-content\/uploads\/2012\/03\/xen_api_test_4.png 411w, https:\/\/humairahmed.com\/blog\/wp-content\/uploads\/2012\/03\/xen_api_test_4-240x300.png 240w\" sizes=\"auto, (max-width: 411px) 100vw, 411px\" \/><\/a><p id=\"caption-attachment-4042\" class=\"wp-caption-text\">HueXenClient - enabling network monitoring on a VM<\/p><\/div><\/p>\n<p><strong>2.<\/strong><div id=\"attachment_4025\" style=\"width: 564px\" class=\"wp-caption alignleft\"><a href=\"http:\/\/humairahmed.com\/blog\/wp-content\/uploads\/2012\/03\/xen_server_pool2.png\"><img loading=\"lazy\" decoding=\"async\" aria-describedby=\"caption-attachment-4025\" src=\"http:\/\/humairahmed.com\/blog\/wp-content\/uploads\/2012\/03\/xen_server_pool2-e1333157708873.png\" alt=\"XenCenter - disabling the network adapter of the VM being monitored\" title=\"XenCenter - disabling the network adapter of the VM being monitored\" width=\"554\" height=\"392\" class=\"size-full wp-image-4025\" \/><\/a><p id=\"caption-attachment-4025\" class=\"wp-caption-text\">XenCenter - disabling the network adapter of the VM being monitored<\/p><\/div><\/p>\n<p><strong>3.<\/strong><div id=\"attachment_4034\" style=\"width: 585px\" class=\"wp-caption aligncenter\"><a href=\"http:\/\/humairahmed.com\/blog\/wp-content\/uploads\/2012\/03\/xen_server_snap-e1333031457408.png\"><img loading=\"lazy\" decoding=\"async\" aria-describedby=\"caption-attachment-4034\" src=\"http:\/\/humairahmed.com\/blog\/wp-content\/uploads\/2012\/03\/xen_server_snap-e1333031457408.png\" alt=\"XenCenter - snapshot of VM automatically created after two consecutive pings are missed\" title=\"XenCenter - snapshot of VM automatically created after two consecutive pings are missed\" width=\"575\" height=\"379\" class=\"size-full wp-image-4034\" \/><\/a><p id=\"caption-attachment-4034\" class=\"wp-caption-text\">XenCenter - snapshot of VM automatically created after two consecutive pings are missed<\/p><\/div><\/p>\n<p><strong><u>VM Network Monitoring on Multiple VMs Simultaneously:<\/u><\/strong><br \/>\n<br \/>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 <a href=\"https:\/\/github.com\/jedie\/python-ping\">open source python ping implementation<\/a> 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.<br \/>\n<br \/>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 &#8216;Disconnect&#8217; button and the &#8216;Exit&#8217; 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! <div id=\"attachment_4054\" style=\"width: 585px\" class=\"wp-caption alignleft\"><a href=\"http:\/\/humairahmed.com\/blog\/wp-content\/uploads\/2012\/03\/xen_shut_monitoring-e1333033984230.png\"><img loading=\"lazy\" decoding=\"async\" aria-describedby=\"caption-attachment-4054\" src=\"http:\/\/humairahmed.com\/blog\/wp-content\/uploads\/2012\/03\/xen_shut_monitoring-e1333033984230.png\" alt=\"HueXenClient code - upon exiting app, shuts down any VM monitoring threads\" title=\"HueXenClient code - upon exiting app, shuts down any VM monitoring threads\" width=\"575\" height=\"204\" class=\"size-full wp-image-4054\" \/><\/a><p id=\"caption-attachment-4054\" class=\"wp-caption-text\">HueXenClient code - upon exiting app, shuts down any VM monitoring threads<\/p><\/div><br \/>\n<br \/>As you can see this app can be pretty useful and can easily be expanded. That&#8217;s it for this tutorial; feel free to dive into the <a href=\"https:\/\/github.com\/HumairAhmed\/HueXenClient\">code<\/a>!<br \/><\/p>\n","protected":false},"excerpt":{"rendered":"<p>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 &hellip; <a href=\"https:\/\/humairahmed.com\/blog\/?p=3981\">Continue reading <span class=\"meta-nav\">&rarr;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[295,5,747,262,3,210],"tags":[4367,1187,1189,1190,1188,1198,1201,1202,1207,1175,1196,1210,1211,1205,1204,4352,1195,1194,1197,770,771,1193,1192,1208,1206,1134,1191,1209,1199,1141,1203,1200],"class_list":["post-3981","post","type-post","status-publish","format-standard","hentry","category-networking","category-programming","category-protocols","category-python","category-technology","category-virtualization-and-cloud-computing","tag-citrix","tag-citrix-xen","tag-citrix-xen-api","tag-citrix-xenapi","tag-citrix-xenserver","tag-huexenclient","tag-icmp","tag-icmp-packets","tag-network-monitoring","tag-ping","tag-pmw","tag-port","tag-ports","tag-pydev","tag-pydev-plug-in","tag-python","tag-python-megawidgets","tag-python-megawidgets-library","tag-python-pmw","tag-thread","tag-threads","tag-tkinter","tag-tkinter-gui-library","tag-vm-monitoring","tag-vm-network-monitoring","tag-xen","tag-xenap","tag-xencenter","tag-xenclient","tag-xenserver","tag-xenserver-6-0","tag-xenserver-pool"],"_links":{"self":[{"href":"https:\/\/humairahmed.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/3981","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/humairahmed.com\/blog\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/humairahmed.com\/blog\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/humairahmed.com\/blog\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/humairahmed.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=3981"}],"version-history":[{"count":116,"href":"https:\/\/humairahmed.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/3981\/revisions"}],"predecessor-version":[{"id":4108,"href":"https:\/\/humairahmed.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/3981\/revisions\/4108"}],"wp:attachment":[{"href":"https:\/\/humairahmed.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=3981"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/humairahmed.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=3981"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/humairahmed.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=3981"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}