Windows Home Server Twitter Notification Plugin
A while back, the very wise Brendan Grant sent me some sample code that would use Twitter to report the health of one's Windows Home Server. I always meant to update the code to use TweetSharp to talk to Twitter, as well as add some robustness for connected/disconnected scenarios, but I'm just never going to get around to it. Instead, here it is as he sent to me.
There's a REALLY vibrant community around Windows Home Server plugins and if you've got a WHS and you want it to do something that it doesn't do, I'd encourage you to jump in.
Even as I'm posting this, I'm sure there are better and more interesting implementations. However, I like what Brendan has done to abstract away the core COM-based API of WHS for use in managed code.
Here's the full program...note again that the PostTweet() method is hacked together and should use a more robust technique:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Net;
using System.IO;
using System.Xml.Serialization;
using Microsoft.HomeServer.SDK.Interop.v1;
namespace Twitter_Test
{
class Program
{
static string username = "";
static string password = "";
static void Main(string[] args)
{
IWHSInfo info = new WHSInfoClass();
//Register application name
info.Init("WHS Twitter Client");
NotificationCallbackClass notificationClass = new NotificationCallbackClass();
//Register notification callback class
info.RegisterForNotifications(notificationClass);
//Check current state
Console.WriteLine("Current State: " + notificationClass.GetHealthState().ToString());
notificationClass.HealthChanged += new EventHandler(notificationClass_HealthChanged);
Console.WriteLine("Monitoring for health changes. Pressto exit.");
Console.ReadLine();
}
static void notificationClass_HealthChanged(object sender, HealthChangedEventArgs e)
{
Console.WriteLine("Current State " + e.Health.ToString());
PostTweet(username, password, "Your Windows Home Server's health is now: " + e.Health.ToString());
}
private static Status PostTweet(string username, string password, string message)
{
string user = Convert.ToBase64String(System.Text.Encoding.UTF8.GetBytes(username + ":" + password));
// determine what we want to upload as a status
byte[] bytes = System.Text.Encoding.ASCII.GetBytes("status=" + message);
// connect with the update page
HttpWebRequest request = (HttpWebRequest)WebRequest.Create("http://twitter.com/statuses/update.xml");
// set the method to POST
request.Method = "POST";
// set the authorisation levels
request.Headers.Add("Authorization", "Basic " + user);
request.ContentType = "application/x-www-form-urlencoded";
// set the length of the content
request.ContentLength = bytes.Length;
request.ServicePoint.Expect100Continue = false;
// set up the stream
Stream reqStream = request.GetRequestStream();
// write to the stream
reqStream.Write(bytes, 0, bytes.Length);
// close the stream
reqStream.Close();
HttpWebResponse response = request.GetResponse() as HttpWebResponse;
StreamReader sr = new StreamReader(response.GetResponseStream());
string s = sr.ReadToEnd();
XmlSerializer ser = new XmlSerializer(typeof(Status));
object o = ser.Deserialize(new StringReader(s));
Status status = o as Status;
return status;
}
}
}
There interesting part is the Eventing part where he makes changes in your Home Server turn into .NET Events via callbacks. Check the code for details. You can get events when Physical Disks are changed, when Backup States change, or when basically anything happens. There's a number of folks on Twitter already who have their Windows Home Servers tweeting.
If you've got, or you're using a plugin to report your Home Server status on Twitter (or SMS or whatever) leave a comment and I'll update the post! I'm sure there are better solutions than this little sample.
Here's the code if you want it, and remember, it may kill your pets. If so, don't blame me as I'll deny everything. It's a sample you found on the Internet, what did you expect?
Download Twitter WHS Test Project
About Scott
Scott Hanselman is a former professor, former Chief Architect in finance, now speaker, consultant, father, diabetic, and Microsoft employee. He is a failed stand-up comic, a cornrower, and a book author.
About Newsletter
request.Credentials = new NetworkCredential(username, password);
Comments are closed.
@WHSTweet is a spiffy ready-to-install plugin for WHS...