It may seem as though the method names VM.get_name_label() and VM.get_is_a_template() from the previous example Hello XAPI were pulled out of a hat. Especially since the python help system can't tell you the names.
However take a look at: Xen Enterprise Management API . It's automatically generated from the source code, and as such is both an accurate source of useful information, and largely unreadable. On the first page are the 'Class relationship diagram' and the list of fields and methods, represented by a clickable tree. Keep them in front of you whilst reading the rest of this article.
If you look at the tree, you'll find at API/Classes/VM/Fields, that there are fields in a VM object called name_label, and is_a_template. These imply the existence of corresponding get methods.
The key abstraction with XAPI is the representation of everything as an object in database. The objects in the database are related to one another by one-to-one and one-to-many relations. These are represented on the diagram by funny looking arrows.
Take a look at the ring of squares in the Class relationship diagram. Let's go exploring...
(It can help to have a copy of XenCenter, our GUI, open, so you can see that view of the XenServer and its VMs, networks and disks, and also to have an ssh session to the server open, so that you can explore with the command line tool xe. However neither is necessary if you're not familiar with them.)
I'm going to describe the results of interrogating my server 'ivory'. Try the same commands on your server, mutatis mutandis, and see what you get.
Firstly we need to start python, import XenAPI, and log in. See [Xen Server API Test Page] if you're not sure how. In summary:
Once we have the session object, let's see how many hosts we have in our pool of servers.
My server ivory has a friend, sapphire, in its pool, so I've got back two 'OpaqueRef:' strings, each of which identifies one of them. Your server may be part of a larger pool, or just a single box on its own in a pool of one.
The host object is one of the orange squares (the one on the left) in the diagram in Xen Enterprise Management API , and it represents a physical server box. If we click on the orange square 'host', we should get a big index of fields and methods. Since 'name_label' is one of the fields, there will be a get_name_label method for the host object.
Try it using the strings returned by host.get_all()
Let's make them their own variables:
giving us the entire database record as a dictionary. We can make this a little more readable with the pretty-printer:
There's also the nuclear option, which returns all the host records in a dictionary, with one entry per host. The keys are the host's 'OpaqueRef', the values are the records.
As well as examining the host-specific data, we can also chase round the ring of objects. The networking direction is easiest to understand, so let's go anti-clockwise on the diagram:
We've asked for the list of PIFs (physical interfaces) on ivory. In this case there's only one, the single ethernet card, so the list has only one element. We'll name that element ivorypif. Again, it's an 'OpaqueRef:', a unique ID for that element in the database.
Again, the PIF object has a get_record() method (we see from Xen Enterprise Management API), but the record structure is much simpler than that for a host. I've deleted some of the output for readability:
There's not a great deal of useful info here, but we can tell that the host's ip address is set dynamically rather than being forced, and we have further links to a metrics and a network object, which corresponds with our orange diagram.
It is a convention not universally acknowledged that data read from things goes in separate 'metrics' objects, which have their own sets of fields and methods. So here we are taking the 'metrics' opaque reference from the PIF, and using it as the argument in a call to PIF_metrics.get_record(). Our reward is to discover that ivory's network card has recently been reading data at 9.5 kb/s and sending it at 3.1 kb/s. Since its top speed is 100Mb/s, it's not terribly busy. We could get the same information from XenCenter's performance tab. Indeed, this is how XenCenter gets it.
We can also see that sapphire's PIF is connected to the same network object as ivory's:
You should carry on, chasing the various links about, figuring out what each object is for.
In another article I'll describe how to get the computer to produce a nice overview of your server's current database. This is the diagram I wish I'd had when I was trying to figure it all out. See Drawing a graph of the database