public static void Run(Model.Monitor monitor) { var db = new PetaPoco.Database("LocalSQLite"); // check the monitor should run if (monitor.State == Model.MonitorState.Running.ToString() && monitor.RunMultiple == 0) { log.InfoFormat("Monitor [{0}] is in a running state - not running a multiple version", monitor.MonitorID); return; } monitor.State = Model.MonitorState.Running.ToString(); db.Update(monitor); log.InfoFormat("Monitor (ID: {2}) running on Thread Id {0} with the sql {1}", Thread.CurrentThread.ManagedThreadId.ToString(), monitor.SQL, monitor.MonitorID); // Sleeping to test the web interface update //Thread.Sleep(10000); // start the job var job = new Model.Job(); job.MonitorID = monitor.MonitorID; job.StartTime = DateTime.Now.ToString(); db.Insert(job); // set the local directory string monitorFolder = System.AppDomain.CurrentDomain.BaseDirectory + "\\" + monitor.MonitorID; try { if (File.Exists(monitorFolder)) { log.InfoFormat("Folder ({0}) exists, setting current location to that", monitorFolder); Directory.SetCurrentDirectory(monitorFolder); } else { log.InfoFormat("Folder ({0}) doesn't exist, creating the folder", monitorFolder); DirectoryInfo di = Directory.CreateDirectory(monitorFolder); log.InfoFormat("setting current location to that {0}", monitorFolder); Directory.SetCurrentDirectory(monitorFolder); } } catch (Exception ex) { log.ErrorFormat(@"Unable to create and\or set directory {0}", monitorFolder); log.Error(ex.ToString()); } int RowCount = 0; try { DataTable dt; // switch on the type of monitor switch (monitor.Type.ToUpper()) { case "EVENTVWR": //case Model.MonitorType.Eventvwr: if (monitor.isBathStatement()) { if (monitor.intoFileName() == null) { throw new Exception("filename (as parsed from the SQL) is null - shouldn't be"); } string fileName = @".\" + monitor.intoFileName(); string newFileName = @".\" + job.JobID + "." + monitor.intoFileName(); try { if (String.IsNullOrEmpty(monitor.Checkpoint)) { LogParser.BatchEventLog(monitor.SQL); } else { LogParser.BatchEventLog(monitor.SQL, monitor.Checkpoint); } //rename the output to include the jobId if (File.Exists(fileName)) { File.Move(fileName, newFileName); RowCount = -1; } } catch (Exception ex) { throw new Exception("Error running batch execute against Event Log", ex); } job.Batch = 1; job.Info = newFileName; job.FinalState = "Complete"; } else { try { if (String.IsNullOrEmpty(monitor.Checkpoint)) { dt = LogParser.ParseEventLog(monitor.SQL); } else { dt = LogParser.ParseEventLog(monitor.SQL, monitor.Checkpoint); } } catch (Exception ex) { throw new Exception("Error parsing Event Log", ex); } RowCount = dt.Rows.Count; // convert the DataTable into a list of Model.Eventvwr (with the JobId) and insert into the DB try { foreach (DataRow row in dt.Rows) { db.Insert(new Model.Eventvwr(row, job.JobID)); } } catch (Exception ex) { throw new Exception("Error saving Event Viewer data", ex); } job.Info = "Job returned " + RowCount.ToString() + " rows"; job.FinalState = "Complete"; } break; case "IIS": if (monitor.isBathStatement()) { if (monitor.intoFileName() == null) { throw new Exception("filename (as parsed from the SQL) is null - shouldn't be"); } string fileName = @".\" + monitor.intoFileName(); string newFileName = @".\" + job.JobID + "." + monitor.intoFileName(); try { if (String.IsNullOrEmpty(monitor.Checkpoint)) { LogParser.BatchIISLog(monitor.SQL); } else { LogParser.BatchIISLog(monitor.SQL, monitor.Checkpoint); } //rename the output to include the jobId if (File.Exists(fileName)) { File.Move(fileName, newFileName); RowCount = -1; } } catch (Exception ex) { throw new Exception("Error running batch execute against Event Log", ex); } job.Batch = 1; job.Info = newFileName; job.FinalState = "Complete"; } else { try { if (String.IsNullOrEmpty(monitor.Checkpoint)) { dt = LogParser.ParseIISLog(monitor.SQL); } else { dt = LogParser.ParseIISLog(monitor.SQL, monitor.Checkpoint); } } catch (Exception ex) { throw new Exception("Error parsing IIS Log", ex); } RowCount = dt.Rows.Count; // convert the DataTable into a list of Model.Eventvwr (with the JobId) and insert into the DB try { foreach (DataRow row in dt.Rows) { db.Insert(new Model.IIS(row, job.JobID)); } } catch (Exception ex) { throw new Exception("Error saving IIS Log data", ex); } job.Info = "Job returned " + RowCount.ToString() + " rows"; job.FinalState = "Complete"; } break; default: job.Info = "Monitor type [" + monitor.Type + "] not implemented"; job.FinalState = "Error"; db.Update(job); throw new Exception(job.Info); } } catch (Exception ex) { job.Info = ex.ToString(); job.FinalState = "Error"; } finally { if (monitor.Alert == 1) { if (job.FinalState == "Error") { log.Error("Sending error email"); log.ErrorFormat("{0}", job.Info); // send email saying job errored Helpers.Email.Send("*****@*****.**", monitor.EmailAddresses, "Error on Monitor [" + monitor.Name + "] ", job.Info); monitor.State = Model.MonitorState.Alert.ToString(); } else if (RowCount > 0) { log.Info("Sending alert email"); monitor.State = Model.MonitorState.Alert.ToString(); // send email saying rowcount is greater than 0, include link to data Helpers.Email.Send("*****@*****.**", monitor.EmailAddresses, "Alert on Monitor [" + monitor.Name + "] ", "The monitor returned " + RowCount.ToString() + " rows. </br> More information here: <a href='http://*****:*****@".\" + job.JobID + "." + monitor.intoFileName() log.Info("Sending alert email"); monitor.State = Model.MonitorState.Alert.ToString(); // send email saying rowcount is greater than 0, include link to data Helpers.Email.AttachAndSend("*****@*****.**", monitor.EmailAddresses, "Alert on Monitor [" + monitor.Name + "] ", "The monitor returned the attached file. </br> More information here: <a href='http://*****:*****@".\" + job.JobID + "." + monitor.intoFileName()); } } else { monitor.State = Model.MonitorState.Idle.ToString(); } } // catch monitor state still in running if (monitor.State == Model.MonitorState.Running.ToString()) { monitor.State = Model.MonitorState.Idle.ToString(); } job.EndTime = DateTime.Now.ToString(); db.Update(job); db.Update(monitor); }