Sending vCenter Alarm Notifications to Jabber

If you are a person like me who uses Jabber to communicate with other people and you administrator a VMware environment the following post might be useful for you. If you don’t use your own Jabber/XMPP server, you can use your Gmail account and connect to the Google XMPP servers.

VMware vCenter comes with a standard set of alarms which can be configured with a few options:
– Send a notification email
– Send a notification trap
– Run a command

It is with the last option “Run a command” that we will push the vCenter Alarms!

What do you need?

  1. 1 Jabber account (your own)
  2. 1 Jabber “monitoring” account
  3. Windows server with the VMware vCenter service
  4. PHP 5.3 installed on the Windows server
  5. XMPPHP: The PHP XMPP Library

Making a Jabber account

You can create 2 free accounts using Gmail. If you are on Google+ you can just use that account. If wanted you can use one of the Google Talk clients and receive the messages on your desktop.

Add your “monitoring” account to your friends list (and make sure both accounts have accepted each other).

PHP installation

First you will need to install PHP 5.3 on your Windows server running the vCenter service. You can download the setup file from PHP.net. Make sure you download the “Installer” version. The install is mostly hitting next. There is no need to configure a webserver as seen on the screenshot below.

After the installation is finished we will configure XMPHP and setup the directory where the bat script will be placed which we will need later on.

XMPPHP Configuration

We will use the XMPPHP library for sending the alarms which you will have to download on your Windows server running the vCenter service. You can download it from their project page at Google XMPPHP: The PHP XMPP Library.

When the download is finished, extract the library to a directory on your hard disk (you can use 7zip or WinRar for this). In this case I have placed the library in “c:\Monitoring”.

Now create a new PHP file in the “monitoring” directory called “sendmessage.php” with the following code:

<?php
include 'XMPPHP/XMPP.php';

$fromuser = 'username';
$frompass = 'password';
$sendto = 'your.name@gmail.com';

$conn = new XMPPHP_XMPP('talk.google.com', 5222, $fromuser, $frompass, 'xmpphp', 'gmail.com', $printlog=false, $loglevel=XMPPHP_Log::LEVEL_INFO);

$msg = 'ALARM:';
$tot = count($argv);
for ($i = 1; $i <= $tot; $i++) {
	$msg = $msg . ' ' . $argv[$i];
}

try {
    $conn->connect();
    $conn->processUntil('session_start');
    $conn->presence();
    $conn->message($sendto, $msg);
    $conn->disconnect();
} catch(XMPPHP_Exception $e) {
    die($e->getMessage());
}

In the above code/file you will have to replace 3 items if you use Gmail:

  • $fromuser = ‘username’; – This is the username from the “monitoring” address.
  • $frompass = ‘password’; – This is the password from the “monitoring” address.
  • $sendto = ‘your.adres@gmail.com’; – This is contact you want to receive the information.

If you use your own server you will have to replace “talk.google.com” and “gmail.com” in the 8th line with your own parameters (for example: xmpp.company.com and company.com).

That’s it for the XMPPHP configuration, let’s configure our alarms!

vCenter Alarm Configuration

Now open up a vSphere client and connect to your vCenter and head to the alarm management. Go to the “Definitions” tab under Alarms.

In this example I have used the default “Virtual machine cpu usage” alarm. Select the “Virtual machine cpu usage” alarm, right-click on it and choose “Edit Settings…“. Navigate to the “Actions” tab and click the “Add” button and configure a “Run a command” action with the following configuration value:

"c:\Program Files (x86)\PHP\php.exe" "c:\Monitoring\sendmessage.php" {targetName} {eventDescription} 

You can configure the state in which you want to receive an alarm (green to yellow, yellow to red, red to yellow and/or yellow to green) and either once or repeat it. Below is a screenshot which will send an alarm once it goes from yellow to red.

Whenever the alarm/trigger is hit you will get an announcement via Jabber from your “monitoring” account.

And that is it, you are done for this alarm. You can configure more alarms with the same action or use PowerCLI and configure more alarms at once. šŸ™‚

Niels Engelen
Working as a pre-sales with an interest for anything virtual and cloud. Certified as a VMware Certified Professional 4 and 5. Niels also gained the VMware vExpert award (2012-2016) and is an ex-PernixPro.