using System; using System.Collections.Generic; using System.Linq; using System.Text; using Microsoft.UpdateServices.Administration; using Microsoft.UpdateServices.Internal; namespace WsusNagiosReporter { class Program { static void Main(string[] args) { List listResults = new List(); IUpdateServer objUpdateServer = AdminProxy.GetUpdateServer(); ComputerTargetGroupCollection ctc = objUpdateServer.GetComputerTargetGroups(); foreach (IComputerTargetGroup tg in ctc) { foreach (IComputerTarget ictTarget in tg.GetComputerTargets(true)) { String[] strResults = new String[5]; strResults[0] = String.Format("[{0}] PROCESS_SERVICE_CHECK_RESULT", UnixTimeStamp()); strResults[1] = ictTarget.FullDomainName; strResults[2] = "System - WSUS Reporttime"; if (ictTarget.LastReportedStatusTime > DateTime.Now.AddDays(-14)) { strResults[3] = "0"; } else { strResults[3] = "1"; } strResults[4] = ictTarget.LastReportedStatusTime.ToString(); listResults.Add(strResults); // Prüfung auf Installierte Updates Int32 UpdatesNeeded = ictTarget.GetUpdateInstallationSummary().NotInstalledCount; Int32 UpdatesFailed = ictTarget.GetUpdateInstallationSummary().FailedCount; Int32 UpdatesInstalled = ictTarget.GetUpdateInstallationSummary().InstalledCount; strResults = new String[5]; strResults[0] = String.Format("[{0}] PROCESS_SERVICE_CHECK_RESULT", UnixTimeStamp()); strResults[1] = ictTarget.FullDomainName; strResults[2] = "System - WSUS Status"; if (UpdatesFailed > 0) { strResults[3] = "2"; } else if (UpdatesNeeded > 0) { strResults[3] = "1"; } else { strResults[3] = "0"; } strResults[4] = String.Format("Needed: {0}, Failed: {1}, Installed: {2}", UpdatesNeeded.ToString(), UpdatesFailed.ToString(), UpdatesInstalled.ToString()); listResults.Add(strResults); } } foreach (String[] stringarray in listResults) { Console.WriteLine(String.Join(";", stringarray)); } } static Int32 UnixTimeStamp() { DateTime dtUnixEpoch = new DateTime(1970, 01, 01, 0, 0, 0); TimeSpan Difference = DateTime.UtcNow - dtUnixEpoch; return Convert.ToInt32(Difference.TotalSeconds); } } }