XMPP: A Power Tool in Your Tool Box
Extensive Message Protocol (XMPP) is an open standard protocol based on XML (Extensible Markup Language). XMPP is designed to transport instant messages (IM) between entities and to detect online presence. It supports authentication of IM application and secure transport of messages over SSL/TLS. In XMPP entities can be bots, physical users, servers, devices or components. It’s really a powerful tool that has great potential for system administrators to add to their toolbox because:
- XMPP is powerful
- XMPP with Python is only 12 lines of code – trust me, it’s easy!
- XMPP only requires a single query for multiple nodes
- Status message can be used to track host presence
The Power of XMPP
For those of you that are not familiar with XMPP, it not only supports one-to-one messaging between entities but it also supports multi-party messaging (which enables an entity to join a chat room for the exchange of messages with several participants). The messages can be text messages embedded in XML format but XML can also be used to send control messages between entities as we will see with the presence stanza in a bit.
XMPP is widely used; Google uses it (for its Hangout application – formerly google chat) and so does Yahoo and MSN. At Cisco, we use Cisco Jabber extensively to communicate internally. The XMPP client function is now integrated in the Cisco Nexus 5000 series with the release 5.2(1)N1(7) and the Nexus 6000 series with the release of 7.0(0)N1(1). XMPP is an integral part of the single console access for Dynamic Fabric Automation (DFA) which is a powerful framework described in my previous blog.
The new Data Center Network Manager (DCNM) 7.0(1) is delivered as an OVA file that can be deployed quickly on an existing VMware-enabled server. Although DCNM comes with a lot of features that simplify the deployment of the Data Center fabric, we can pick and choose any service we want to use independently – which is great since DCNM comes with Cisco Jabber XCP and is license free. If you already have a XMPP service installed (like Openfire or ejabberd), it will not be a problem because everything discussed here is valid on any standard XMPP implementation.
On NX-OS devices, the XMPP feature is activated by configuring ‘feature fabric access’ and is part of the Enhanced L2 license (ENHANCED_LAYER2_PKG). Once activated, the switch becomes a XMPP client that needs to be registered on the server. In order to register it, XMPP requires the use of fully qualified domain names (FQDNs) to identify the domain server. If the switch does not have access to a DNS service, I recommend that you use the switch management network for messaging and a static host–to–IP address mapping in the switch configuration.
The switch will use its hostname to login to the XMPP service. If your XMPP server does not support auto-registration, you will need to register the switch and the rooms in the XMPP database beforehand. The DCNM OVA requires users and groups to be created via the CLI, and example of this user and group creation is:
[root@dcnm-ova ~]# appmgr add_user xmpp -u leaf0 -p cisco123
[root@dcnm-ova ~]# appmgr add_user xmpp -u leaf1 -p cisco123
[root@dcnm-ova ~]# appmgr add_user xmpp -u python -p cisco123
[root@dcnm-ova ~]# appmgr add_group xmpp -u admin -p C1sc0123 -g all-nodes
[root@dcnm-ova ~]# appmgr add_group xmpp -u admin -p C1sc0123 -g leaf-nodes
Here is an example switch CLI that connects the XMPP server as well as includes the switch in two different chat rooms.
I can now use any XMPP client and have a chat with my switches! I use Pidgin since it is implemented on multiple desktop OS including Linux. You can request response messages to be formatted in XML to ease the breakdown of the various fields (e.g.: show interface e2/1 status | xml) but keep in mind that Pidgin may not display switch CLI output in XML format correctly. In order to resolve this issue, you need to enable the Conversation Colors plugin.
Once configured, the benefits are immediate! As soon as I joined the chat room all-nodes; I can issue the show command to all of my switches or I can apply the same configuration to the group in one single command since XMPP supports privilege 15. As you can see below, a reply will always end with a return value that notifies the sender of the status of the response, a value of 0 means success. The reply includes the switch name in jid format and the host-id, which could be very convenient to verify licenses for example.
Some important return value are described below:
|2||Invalid component name|
|7||Command name not found|
To prevent CPU hogging, switches will not return output that are bigger than 64kB, they will in place write it on the bootflash and send a XMPP message describing the file name.
There are more advantages in using XMPP on NX-OS, as Python can be enhanced with the xmpppy library (http://xmpppy.sourceforge.net) and I can quickly write a script that will send the infamous “Hello world” message to the console of the switch.
Although this example is not very secure nor does it take in account connection and authentication problem, it demonstrates just how easy it is to interact with a switch using XMPP and Python. This program does not wait or expect an answer from the client, it just send a command message and the switch display it on the console.
Are You in the Room?
Multi User Chat room (MUC) offer several advantages, the most obvious is the capability to configure all switches participating in the chat in one shot. If you configure all the switches to join a common room, it also offer the possibility to monitor their presence inside that room. That means, you do not need to query the individual nodes or the room because, as soon as the script signal its presence to the room, all the participants (including the bot itself) will automatically respond with a presence stanza. Receiving message is a little bit more complex because, you need an event loop and a callback handler for incoming messages, here is the example:
node = str(iq_node)
if “http://jabber.org/protocol/muc#user” in node:
if (str(nrole) == "none"):
print "%s %s left the room" %(time.strftime('%X'),str(njid))
print "%s %s is %s" %(time.strftime('%X'),str(njid), str(nrole))
The script loop endlessly in the event handler routine until the user presses “ctrl + c”. Thanks to the endless loop you dynamically monitor the change in the nodes presence inside the room. I am including the output of the script below; you can observe that I rebooted leaf0 after I launched the script.
15:35:47 firstname.lastname@example.org is participant
15:35:47 email@example.com is participant
15:35:47 firstname.lastname@example.org is participant
15:35:47 email@example.com is participant
15:35:47 firstname.lastname@example.org is participant
15:35:47 email@example.com is moderator
15:35:47 firstname.lastname@example.org is participant
15:35:47 email@example.com is participant
15:35:47 firstname.lastname@example.org is participant
15:36:40 email@example.com left the room</span>
The user firstname.lastname@example.org is the jid of our python bot, it is important to note that jid are unique so while the script is running no other script using the same jid can be executed.
Dude, Where is My VM?
One of the key advantages for Dynamic Fabric Automation (DFA) is to provide workload automation, this capability guides configuration of fabric components based on the requirements and location of a physical or virtual server. This functionality enables the deployment of any workload, anywhere, dynamically. Now that VMs can moved freely inside the infrastructure, we may need to locate the payload quickly for troubleshooting purposes, DCNM provides this function via the DFA Dashboard but with a simple script I also can identify the DFA leaf where the VM is located.
The example below use the “groupchat” capability and query the “leaf-nodes” group for a specific Virtual Machine and interprets the result. With a little bit of parsing I can quickly return the jid of the leaf hosting the VM and display the result.
cmd="show evb host name " + vm
if msg.getType() == "groupchat":
result=re.findall("No. of Hosts: [1-9]", str(msg.getBody()))
print vm + " was found on:\n"
print sw + "\n"
print str(msg.getBody()) + "\n"
print "connection failed"
print "authentication failed"
message = xmpp.Message(room, cmd)
The result message can be divided into 2 main fields, the first contain the source jid and the second contain the output from the CLI. Here is the output:
The graphic below illustrate the result we collected from the leaf.
I hope you can see that XMPP is a powerful addition to the NX-OS toolbox. It has the capability to support end-to-end secure transport (TLS will be supported in an upcoming release), one-to-many communication, server query, and is using the standard NX-OS CLI. You can also increase your efficiency with the Python library and get away from expect/tcl scripts and move to a more modern efficient and feature rich scripting language. Python is well supported on the NX-OS switches; they already possess a Python interpreter and can automate their configuration using a Python Power On Auto Provisioning (POAP) script.
Have you written a script to share? Do not hesitate to send me your comments!