public static void Initialize() { if (initialized) { return; } try { foreach (string name in RepositoryNames) { EEventRepository repos = (EEventRepository)Enum.Parse(typeof(EEventRepository), name); ReadRepositoryEvents(repos); } fsw.Path = InstallPath; fsw.Filter = "*.xml"; fsw.NotifyFilter = NotifyFilters.LastWrite; fsw.Changed += new FileSystemEventHandler(OnChanged); fsw.EnableRaisingEvents = true; // start monitoring } catch { MessageBox.Show("There is a problem monitoring for new events - the Crystal registry is likely corrupt. Please reinstall the product."); } initialized = true; }
private static Guid?ReadRepositoryEvents(EEventRepository repos) { string xmlFile = InstallPath + repos.ToString() + ".xml"; Guid? lastEvent = null; if (!File.Exists(xmlFile)) { return(null); } bool mutexHeld = false; try { eventWriterMutex.WaitOne(); mutexHeld = true; using (FileStream fs = new FileStream(xmlFile, FileMode.Open)) { using (XmlReader xml = XmlReader.Create(fs)) { lastEvent = ReadAndAddDifference(repos, xml); } } } catch (Exception ex) { // not a valid repository //MessageBox.Show(ex.ToString()); } finally { if (mutexHeld) { eventWriterMutex.ReleaseMutex(); } } return(lastEvent); }
private static Guid?ReadAndAddDifference(EEventRepository repos, XmlReader xml) { Guid?firstNewEvent = null; bool foundLastEvent = true; List <EventItem> eventItems = null; EventItem lastEvent = null, newEvent = null; if (!events.TryGetValue(repos, out eventItems)) { eventItems = new List <EventItem>(); events.Add(repos, eventItems); } if (eventItems.Count != 0) { lastEvent = eventItems[eventItems.Count - 1]; foundLastEvent = false; } while (xml.Read()) { if (String.Compare(xml.Name, "event", true) != 0 && newEvent == null) { continue; } try { switch (xml.NodeType) { case XmlNodeType.Element: string strId = xml.GetAttribute("eventid"); if (strId == null) { continue; } Guid eventId = new Guid(strId); if (foundLastEvent == false) { if (lastEvent.EventGuid.Equals(eventId)) { foundLastEvent = true; } break; } if (!firstNewEvent.HasValue) { firstNewEvent = eventId; } newEvent = new EventItem(); newEvent.EventRepository = repos; DateTime.TryParse(xml.GetAttribute("date"), null, System.Globalization.DateTimeStyles.AdjustToUniversal, out newEvent.EventDate); //date="12/01/2012 22:27:16" (important to parse exact otherwise US dates will only parse 1/3rd of the month!! DateTime.TryParseExact(xml.GetAttribute("date"), "dd/MM/yyyy HH:mm:ss", new System.Globalization.CultureInfo("en-GB").DateTimeFormat, System.Globalization.DateTimeStyles.None, out newEvent.EventDate); newEvent.EventGuid = eventId; newEvent.EventTitle = xml.GetAttribute("title"); newEvent.EventRisk = xml.GetAttribute("risk"); newEvent.EventProcess = xml.GetAttribute("procid"); if (newEvent.EventProcess != null) { int idx = newEvent.EventProcess.IndexOf(']'); if (idx != -1 && newEvent.EventProcess[0] == '[') { newEvent.EventProcess = newEvent.EventProcess.Substring(1, idx - 1); } } break; case XmlNodeType.Text: if (newEvent != null) { newEvent.EventMessage = xml.Value; if (newEvent.Valid) { eventItems.Add(newEvent); } newEvent = null; } break; } } catch { continue; } } return(firstNewEvent); }
private void DisplayNewEvent(EEventRepository r, EventItem e, bool isRealtime) { try { if (this.lvLatestAlerts.InvokeRequired) { lvLatestAlerts.Invoke(new DisplayNewEventDelegate(DisplayNewEvent), new object[] { r, e, isRealtime }); } else { lock (displaylock) { EThreatRiskRating risk = (EThreatRiskRating)Enum.Parse(typeof(EThreatRiskRating), e.EventRisk, true); Color riskColor = Color.LightGray; if (displayHighRiskOnly) { if (risk != EThreatRiskRating.HighRisk && risk != EThreatRiskRating.MediumRisk) { return; } } switch (risk) { case EThreatRiskRating.Information: riskColor = Color.LightGray; break; case EThreatRiskRating.LowRisk: riskColor = Color.LightGreen; break; case EThreatRiskRating.MediumRisk: riskColor = Color.Orange; break; case EThreatRiskRating.HighRisk: riskColor = Color.OrangeRed; break; } string date = e.EventDate.ToString().Substring(0, e.EventDate.ToString().IndexOf(' ')); string time = e.EventDate.ToString().Substring(e.EventDate.ToString().IndexOf(' ') + 1); ListViewItem lvi = new ListViewItem(new string[] { date, time, e.EventTitle }); lvi.BackColor = riskColor; lvi.Tag = e; lvi.ToolTipText = string.Format("Date/Time: {0} - {1}\nRisk: {2}\nTitle: {3}\nMessage: {4}\n", date, time, e.EventRisk, e.EventTitle, e.EventMessage); lvLatestAlerts.Items.Insert(0, lvi); if (lvLatestAlerts.Items.Count > 250) { lvLatestAlerts.Items.RemoveAt(250); } if (isRealtime && risk == EThreatRiskRating.HighRisk) { IndividualAlert ia = new IndividualAlert(); ia.ShowEvent(e); ia.Show(); } } } } catch (Exception ex) { MessageBox.Show(ex.ToString()); } }
private void DisplayNewEvent(EEventRepository r, EventItem e, bool isRealtime) { try { if (this.lvAlerts.InvokeRequired) { lvAlerts.Invoke(new DisplayNewEventDelegate(DisplayNewEvent), new object[] { r, e, isRealtime }); } else { if (String.Compare(cmbRepos.Text, r.ToString(), true) != 0 && String.Compare(cmbRepos.Text, "All", true) != 0) { return; } lock (displaylock) { EThreatRiskRating risk = (EThreatRiskRating)Enum.Parse(typeof(EThreatRiskRating), e.EventRisk, true); Color riskColor = Color.LightGray; switch (risk) { case EThreatRiskRating.Information: riskColor = Color.LightGray; break; case EThreatRiskRating.LowRisk: riskColor = Color.LightGreen; break; case EThreatRiskRating.MediumRisk: riskColor = Color.Orange; break; case EThreatRiskRating.HighRisk: riskColor = Color.OrangeRed; break; } string date = e.EventDate.ToString().Substring(0, e.EventDate.ToString().IndexOf(' ')); string time = e.EventDate.ToString().Substring(e.EventDate.ToString().IndexOf(' ') + 1); ListViewItem lvi = new ListViewItem(new string[] { date, time, e.EventProcess, e.EventRisk, e.EventTitle }); lvi.BackColor = riskColor; lvi.Tag = e; bool showAllCount = false; int showMax = 0; if (!GetFormItemsCount(out showMax, out showAllCount)) { showAllCount = true; } if (!showAllCount && lvAlerts.Items.Count == showMax) { lvAlerts.Items.RemoveAt(showMax - 1); } lvAlerts.Items.Insert(0, lvi); } } } catch (Exception ex) { MessageBox.Show(ex.ToString()); } }