Example #1
0
        private static void HandleInfoRequest(SystemErrorsInfoRequest sys_request, SQLiteConnection conn)
        {
            Attribute attr                   = new Attribute();
            string    machine_name           = sys_request.MachineName.Replace(' ', '_').ToLower();
            string    section                = sys_request.Type.ToString().ToLower();
            string    max_record_number_path = $"{machine_name}.{section}.max_record_number";
            string    max_eventlog_path      = $"{machine_name}.{section}.max_event_log";

            // First, let's look for the "old" systems which just had the record number. If we find
            // those we'll convert them to the "new" system which has a JSON object.
            string max = attr.Get(max_record_number_path, conn);

            if (string.IsNullOrEmpty(max) == false &&
                ulong.TryParse(max, out ulong u))
            {
                // Yes, it's using the old system
                sys_request.LogData.MaxRecordNumber = u;
                sys_request.Handled();

                // Change the attribute so it's using the new one
                attr.Set(max_eventlog_path,
                         JsonConvert.SerializeObject(sys_request.LogData,
                                                     Formatting.None,
                                                     new JsonSerializerSettings {
                    NullValueHandling = NullValueHandling.Ignore
                }),
                         conn);
                attr.Clear(max_record_number_path, conn);
            }
            else
            {
                // Nope...try the new system
                max = attr.Get(max_eventlog_path, conn);
                if (string.IsNullOrEmpty(max) == false)
                {
                    try
                    {
                        EventLogData system_id = JsonConvert.DeserializeObject <EventLogData>(max);
                        if (system_id != null)
                        {
                            sys_request.LogData.Assign(system_id);
                            sys_request.Handled();
                        }
                    }
                    catch (JsonException)
                    {
                    }
                }
            }
        }
Example #2
0
        public EventLogCollector(CollectorID id, Remote remote_info, ECollectorType type)
            : base(new DataCollectorContext(id, type))
        {
            RemoteInfo = remote_info;

            m_machine_name = Context.ID.Name;
            CollectorType  = type;

            if (type != ECollectorType.SystemErrors && type != ECollectorType.ApplicationErrors)
            {
                throw new Exception($"EventLogCollector: invalid collector type {type}");
            }

            // So we don't keep getting log entries that have already been retrieved, keep track
            // of the RecordNumber, which is an ever-increasing number for each of the different
            // types of logfiles (System, or Application), and the dates.

            // We need to keep the dates because if a particular machine is rebuilt, the RecordNumber
            // will get reset and we'll stop getting any errors from the event log.
            // Originally, we just kept the record number, but that's not good enough, so we
            // switched to using the date as the primary clause.

            m_log_data = new EventLogData();

            SystemErrorsInfoRequest.EType req_type = SystemErrorsInfoRequest.EType.System;
            switch (CollectorType)
            {
            case ECollectorType.ApplicationErrors:
                req_type = SystemErrorsInfoRequest.EType.Application;
                break;

            case ECollectorType.SystemErrors:
                req_type = SystemErrorsInfoRequest.EType.System;
                break;

            default:
                throw new Exception($"EventLogCollector: failure to handle type conversion from {CollectorType}");
            }

            SystemErrorsInfoRequest request = new SystemErrorsInfoRequest(m_machine_name, req_type);

            RequestBus.Instance.MakeRequest(request);
            if (request.IsHandled)
            {
                m_log_data.Assign(request.LogData);
            }
        }
        public void ConvertProperly(string section, SystemErrorsInfoRequest.EType type)
        {
            using (FileDeleter fd = new FileDeleter(Extensions.GetTempDBFile()))
            {
                Database    db   = new Database(new Context(fd.Fi));
                Initializer init = new Initializer(null);
                init.Initialize(db);

                RequestBus        bus       = new RequestBus();
                EventLogResponder responder = new EventLogResponder()
                {
                    DB = db
                };
                bus.Subscribe(responder);

                using (SQLiteConnection conn = db.Connection)
                {
                    conn.Open();

                    string orig_path  = $"machine_name.{section}.max_record_number";
                    string new_path   = $"machine_name.{section}.max_event_log";
                    ulong  record_num = 234567;

                    Attribute attr = new Attribute();
                    attr.Set(orig_path, record_num.ToString(), conn);

                    SystemErrorsInfoRequest req = new SystemErrorsInfoRequest("machine_name", type);
                    bus.MakeRequest(req);
                    Assert.True(req.IsHandled);
                    Assert.True(req.LogData.MaxRecordNumber.HasValue);
                    Assert.Equal(record_num, req.LogData.MaxRecordNumber.Value);

                    string x = attr.Get(orig_path, conn);
                    Assert.True(string.IsNullOrEmpty(x));

                    x = attr.Get(new_path, conn);
                    Assert.False(string.IsNullOrEmpty(x));

                    EventLogData eld = JsonConvert.DeserializeObject <EventLogData>(x);
                    Assert.NotNull(eld);
                    Assert.True(eld.MaxRecordNumber.HasValue);
                    Assert.Equal(record_num, eld.MaxRecordNumber.Value);
                }
            }
        }