Thursday, May 18, 2006

EventLogCatcher

In the organization I'm currently working there was a demand to check whether the Sharepoint backups were being done properly. To do this I had to face some challenges : 1. Impersonate on the fly because we have one intranet Sharepoint environment en one extranet environment. Each with a different domain, so one user account was not enough to check if the backups were complete. With some Googling I found a class which did just the trick. So kudos to NetoMatix (http://www.netomatix.com/ImpersonateUser.aspx) 2. Fortunatly to check if the backups are being done, the process SPSBackpup writes logs to the eventlog. So I need to check the eventlog.. with some googling again I found the following piece of code http://www.aspheute.com/english/20000811.asp 3. Write logs to a Sharepoint list on the intranet environment. I used the listswebservice for this part. Some examples are in the WSS SDK and I rewrote some code to make it work. This was the most tricky part I might add, because if you don't put in the proper data in a field, the whole listitem won't be uploaded (and no bloody error is given!!). 4. Show KPI icons (eg. red stoplight if backup has failed) I uploaded some icons into a document library (coud be also picture library, would make more sense to be honest) and I linked to these when I inserted a listitem. So without further or due here is the code ;) public static void GetAllLogs(string username, string password, string domain, string servername) { WindowsImpersonationContext impContext = null; try { impContext = NetworkSecurity.ImpersonateUser( domain, username, password, LogonType.LOGON32_LOGON_NEW_CREDENTIALS, LogonProvider.LOGON32_PROVIDER_DEFAULT); } catch (ApplicationException ex) { Console.WriteLine(ex.Message); } if (null != impContext) { try { EventLog aLog = new EventLog(); aLog.Log = "Application"; aLog.MachineName = servername; foreach (EventLogEntry entry in aLog.Entries) { if (entry.Source == "SharePoint Portal Server Backup") { if (entry.TimeWritten.Date == System.DateTime.Now.AddDays(-1).Date) { InsertTask(entry); } } } } catch (Exception ex) { Console.WriteLine(ex.Message); } finally { // Finally we have to revert the impersonation. impContext.Undo(); } } } private static void InsertTask(EventLogEntry entry) { Lists.Lists listService = new Lists.Lists(); listService.Credentials = new System.Net.NetworkCredential(username, password, domain); listService.Url = "http://servername/_vti_bin/lists.asmx"; //Met deze functie gaan we XML sturen naar de webservice zodat deze gegevens in een list kan zetten XmlDocument doc = new XmlDocument(); XmlElement updates = doc.CreateElement("Batch"); int x = entry.Message.IndexOf("successfully"); int y = entry.Message.IndexOf("started"); int z = entry.Message.IndexOf("fail"); if (y > -1) { } else { StringBuilder sb = new StringBuilder(); sb.Append(""); sb.Append("New"); sb.Append(""+entry.MachineName+""); sb.Append(""+entry.TimeGenerated.ToShortDateString()+""); sb.Append(""+entry.Message.ToString()+""); // if (y > -1) // {sb.Append("http://servername/Document%20Library2/KPINORMAL1.GIF, http://servername/Document%20Library2/KPINORMAL1.GIF/KPINORMAL1.GIF");} // if (x > -1) { //sb.Append("http://servername/Document%20Library2/KPINORMAL0.GIF, http://servername/Document%20Library2/KPINORMAL1.GIF"); sb.Append("http://servername/Document%20Library2/KPINORMAL0.GIF, http://servername/Document%20Library2/KPINORMAL1.GIF"); } else if (z > -1) {sb.Append("http://servername/Document%20Library2/KPINORMAL2.GIF, http://servername/Document%20Library2/KPINORMAL2.GIF");} sb.Append(""); updates.InnerXml = sb.ToString(); Console.WriteLine("Inserting..."); XmlNode node = null; //De daadwerkelijke insert van het xmldocument in de list try { node = listService.UpdateListItems("Backups",updates); } catch (Exception err) { if (err.Message.StartsWith("The underlying connection was closed")) Console.WriteLine("The feedback service is currently unavailable. Please try again later"); else Console.WriteLine(err.Message); } } } So just call the GetAllLogs function and you're done ;)

2 comments:

Tim said...

Good Job! :)

digital signature PDF said...

Hi there! glad to drop by your page and found these very interesting and informative stuff. Thanks for sharing, keep it up!