lunedì 16 giugno 2014

VMware: alarm notifications using PowerCLI and IFTTT

IFTTT is the short name for If-This-Than-That, a website and an Android/iOS app, that automates task based on the triggering of a certain condition. The idea behind that is really simple yet really powerful: if a <condition> is verified then <do something>.
In this post we are gonna use PowerCLI and IFTTT to provide push notifications in case of an alarm or a warning is triggered in vSphere.

What we are up to do is basically:

-Use a PowerCLI script, intended to run as a scheduled task, to check if alarms or warnings are triggered in our vSphere environment.
-Generate an RSS Atom feed with all alarms/warnings (if any). The feed file must be reachable by the internet via an IP address/DNS name.
-Create a new recipe in IFTTT to check for our RSS feed updates.

If a new alarm or warning is triggered in vSphere the RSS feed will be updated, IFTTT will notify the change and will provide a push notification to the user informing that something that needs to be addressed is happening in vSphere.

Let's start with the PowerCLI script: it checks for any warning/alarm and generates as output an RSS Atom feed which will be monitored by IFTTT in search of updates. The idea is to add this script to a scheduled task which runs automatically and possibly frequently. The $OutputFile variable is where the RSS feed is generated. IFTTT would require an internet accessible feed so in my case I point $OutputFile to the root of one website running on IIS on the same machine that runs the script. Of course the website is publicly accessible.

This is the code, as usual you can also find it in my GitHub Page. IFTTT_Alarms.ps1



The output RSS feed is generated by concatenating three parts: the header which has the xml declaration and the opening tags, the body, which is the main part that is dynamically generated based on any alarm/warning triggered in vSphere, and the footer which closes the feed.

If no alarms or warnings are (hopefully) triggered in your vSphere environment the resulting feed will be like this:

 <?xml version="1.0" encoding="utf-8"?>  
 <feed xmlns="http://www.w3.org/2005/Atom">  
   <title>Triggered Alarms Feed</title>  
   <subtitle>This feed contains all triggered alarms and warnings</subtitle>  
   <link href="http://hostilecoding.blogspot.com" rel="self" />  
   <link href="http://hostilecoding.blogspot.com/" />  
   <updated>2014-06-13T22:38:45.0020183+02:00</updated>  
 </feed>  



If there is any alarm the resulting feed will be similar to this:

 <?xml version="1.0" encoding="utf-8"?>  
 <feed xmlns="http://www.w3.org/2005/Atom">  
      <title>Triggered Alarms Feed</title>  
      <subtitle>This feed contains all triggered alarms and warnings</subtitle>  
      <link href="http://hostilecoding.blogspot.com" rel="self" />  
      <link href="http://hostilecoding.blogspot.com/" />  
      <updated>2014-06-13T22:38:45.0020183+02:00</updated>  
 <entry>  
      <title>Alarm: 192.168.243.144</title>  
                <link href="http://hostilecoding.blogspot.com/" />  
                <link rel="alternate" type="text/html" href="http://hostilecoding.blogspot.com/"/>  
                <link rel="edit" href="http://hostilecoding.blogspot.com/"/>  
      <updated>2014-06-13T22:38:45.0020183+02:00</updated>  
      <summary>Alarm Triggered.</summary>  
                <content type="xhtml">  
                  <div xmlns="http://www.w3.org/1999/xhtml">  
                      <p>Network uplink redundancy lost</p>  
                  </div>  
                </content>  
                <author>  
                      <name>Administrator@vsphere.local</name>  
             </author>  
 </entry>  
 </feed>  



The <entry> part contains the informations about the alarm, which host the alarm refers to, description, and some other infos. Links are present because IFTTT was a bit sensitive to the feed format, if no links are in the feed IFTTT would not recognize it as having a correct syntax and will report an error during recipe creation.

Let's now jump to IFTTT recipe creation, I used Android version but I guess  the iOS counterpart would be identical.

Create a new recipe:



Look for RSS feed -> Neew Feed Item



Insert the URL of the RSS feed. I used feeburner.com to map the public IP address of my IIS server to a URL. I guess you could simply use your IP address.



Android Notifications -> Send Notification



This is the look of the complete recipe:



So, when an alarm/warning will be detected the PowerCLI script will update the feed, IFTTT will notice the new feed entry and will generate a push notification similar to this:



That's all!!

Nessun commento:

Posta un commento