Пример #1
0
        public void Interpret(Data d, SQLiteConnection conn)
        {
            if (d.Type != ECollectorType.Memory)
            {
                return;
            }

            if (d is GenericData <MemoryUsage> )
            {
                GenericData <MemoryUsage> data = d as GenericData <MemoryUsage>;

                MemoryLowAlert           low_level            = new MemoryLowAlert();
                MemoryCriticallyLowAlert critically_low_level = new MemoryCriticallyLowAlert();
                double low            = low_level.GetValueAsDouble(conn) ?? 80.0;
                double critically_low = critically_low_level.GetValueAsDouble(conn) ?? 90.0;

                ulong  capacity     = data.Generic.CapacityNum;
                ulong  free         = data.Generic.FreeNum;
                ulong  percent_used = (ulong)((double)free / (double)capacity * 100.0 + 0.5);
                string message      = $"{percent_used} %";

                MemoryStatus mem_status = new MemoryStatus(low, critically_low);
                EStatusType  status     = mem_status.GetStatus(data.Generic.UsedNum, capacity);

                long device_id = GetDeviceID(d, conn);
                if (device_id >= 0)
                {
                    SetDeviceStatus(device_id, status, MemoryStatus.Types, message, conn);
                }
            }
            else
            {
                throw new Exception("MemoryInterpreter: data type is wrong");
            }
        }
Пример #2
0
 public HistoryItemVM(EStatusType statusType, int statusCode, DateTime dt, TimeSpan timeout)
 {
     StatusCode  = statusCode;
     StatusType  = statusType;
     Timestamp   = dt;
     Timeout     = timeout;
     StatusColor = GetColor(statusType);
 }
Пример #3
0
        public void Interpret(Data d, SQLiteConnection conn)
        {
            if (d.Type != ECollectorType.Disk)
            {
                return;
            }

            if (d is GenericDictionaryData <DiskUsage> )
            {
                Dictionary <string, DiskUsage> value = ((GenericDictionaryData <DiskUsage>)d).Data;

                // Don't alert on drives that aren't being monitored
                MonitoredDrivesRequest request = new MonitoredDrivesRequest(d.Name);
                RequestBus.Instance.MakeRequest(request);

                DiskSpaceLowAlert           low_level            = new DiskSpaceLowAlert();
                DiskSpaceCriticallyLowAlert critically_low_level = new DiskSpaceCriticallyLowAlert();
                double low            = low_level.GetValueAsDouble(conn) ?? 80.0;
                double critically_low = critically_low_level.GetValueAsDouble(conn) ?? 90.0;

                EStatusType     status    = EStatusType.AdequateDiskSpace;
                List <string>   messages  = new List <string>();
                DiskSpaceStatus ds_status = new DiskSpaceStatus(low, critically_low);

                foreach (string drive in value.Keys)
                {
                    if (request.IsHandled && request.DriveManager.IsDriveMonitored(drive) == false)
                    {
                        continue;
                    }

                    DiskUsage data = value[drive];
                    Tuple <EStatusType, double> drive_status = ds_status.GetStatus(data.UsedNum, data.CapacityNum);
                    status = status.DiskSpaceCompare(drive_status.Item1);

                    if (drive_status.Item1 == EStatusType.CriticallyLowOnDiskSpace || drive_status.Item1 == EStatusType.LowOnDiskSpace)
                    {
                        messages.Add($"{drive} -- {drive_status.Item2:0.0} %");
                    }
                }

                string message   = messages.JoinStrings(", ");
                long   device_id = GetDeviceID(d, conn);
                if (device_id >= 0)
                {
                    SetDeviceStatus(device_id, status, DiskSpaceStatus.Types, message, conn);
                }
                else
                {
                    ApplicationEventLog log = new ApplicationEventLog();
                    log.LogError($"DiskSpaceInterpreter: unable to get device id for {d.Context.Name}");
                }
            }
            else
            {
                throw new Exception("DiskSpaceInterpreter: data type is wrong");
            }
        }
Пример #4
0
        public void HandleLargeNumbersOK()
        {
            MemoryStatus mem_status = new MemoryStatus(80.0, 90.0);

            // Handle large #s OK
            EStatusType status = mem_status.GetStatus(10_000_000_000_000, 10_500_000_000_000);

            Assert.Equal(EStatusType.CriticallyLowFreeMemory, status);
        }
Пример #5
0
 public void SetAchievementPoint(EStatusType statusType, int curPoint)
 {
     foreach (IUiAchievementModule item in _achievementItem)
     {
         if (item.GetStatusType() == statusType)
         {
             item.SetCounter(curPoint);
         }
     }
 }
Пример #6
0
        public void Interpret(Data d, SQLiteConnection conn)
        {
            if (d.Type == ECollectorType.CPUUsage)
            {
                CPUUsageAlert       cpu_alert_level = new CPUUsageAlert();
                double              cpu_alert       = cpu_alert_level.GetValueAsDouble(conn) ?? 80.0;
                CPUUsageAlertCounts alert_counts    = new CPUUsageAlertCounts();
                // Make sure we get at least 1
                int a_counts = Math.Max(alert_counts.GetValueAsInt(conn) ?? 1, 1);

                // The interpretation should be done after the insertion, so the value that was just collected should be in the database already

                // Because we're averaging over the last x readings, we can't just use the single Data record d and have to do a query
                // to get the last y readings

                long device_id = GetDeviceID(d, conn);
                if (device_id >= 0)
                {
                    List <int> all_cpu = new List <int>();

                    // To see how the TimeStamp clause works, check out:
                    // https://www.sqlite.org/lang_datefunc.html
                    //string sql = string.Format("SELECT D.Value FROM Data AS D INNER JOIN Collectors AS C ON D.CollectorID = C.CollectorID WHERE C.DeviceID = {0} AND C.CollectorType = {1} AND TimeStamp >= date('now', '-1 day');",
                    //    device_id, (int)CollectorType.CPUUsage);
                    string sql = $"SELECT D.Value FROM Data AS D INNER JOIN Collectors AS C ON D.CollectorID = C.CollectorID WHERE C.DeviceID = {device_id} AND C.CollectorType = {(int)ECollectorType.CPUUsage} ORDER BY TimeStamp DESC LIMIT {a_counts};";
                    using (SQLiteCommand command = new SQLiteCommand(sql, conn))
                        using (SQLiteDataReader reader = command.ExecuteReader())
                        {
                            while (reader.Read())
                            {
                                //var definition = new { Value = string.Empty };
                                //var val = JsonConvert.DeserializeAnonymousType(reader.GetString(0), definition);
                                var val = JsonConvert.DeserializeObject <ValueClass <string> >(reader.GetString(0));
                                if (int.TryParse(val.Value, out int cpu_usage))
                                {
                                    all_cpu.Add(cpu_usage);
                                }
                            }
                        }

                    // When all_cpu doesn't have anything in it, Average() throws an exception, so we
                    // need to make sure there's something in it. And, we don't want to generate an alert
                    // unless we've gotten all the readings we should have. Given both of those, the
                    // average shouldn't even be calculated unless we have everything we want.
                    if (all_cpu.Count == a_counts)
                    {
                        CPUStatus   cpu_status   = new CPUStatus(cpu_alert);
                        EStatusType alert_status = cpu_status.GetStatus(all_cpu);
                        string      message      = $"{(int)(cpu_status.Average + 0.5)} %";

                        SetDeviceStatus(device_id, alert_status, CPUStatus.Types, message, conn);
                    }
                }
            }
        }
Пример #7
0
        private Brush GetColor(EStatusType statusType)
        {
            var color = statusType switch
            {
                EStatusType.Error => Colors.DarkRed,
                EStatusType.Success => Colors.DarkGreen,
                EStatusType.Warning => Colors.Gold,
                _ => Colors.White,
            };

            return(new SolidColorBrush(color));
        }
Пример #8
0
        public void HandleBatteryStatusesProperly()
        {
            EUPSBatteryStatus battery_status = EUPSBatteryStatus.Charging;
            UPSStatus         ups_status     = new UPSStatus();

            EStatusType status = ups_status.GetStatus(battery_status);

            Assert.Equal(EStatusType.UPSNotProvidingPower, status);

            battery_status = EUPSBatteryStatus.Other;
            status         = ups_status.GetStatus(battery_status);
            Assert.Equal(EStatusType.UPSProvidingPower, status);
        }
Пример #9
0
        public void HandleFailingDrivesProperly()
        {
            List <string> failing_drive_letters = new List <string>();
            SmartStatus   smart_status          = new SmartStatus();

            EStatusType status = smart_status.GetStatus(failing_drive_letters);

            Assert.Equal(EStatusType.DiskFailureNotPredicted, status);

            failing_drive_letters.Add("C:");
            status = smart_status.GetStatus(failing_drive_letters);
            Assert.Equal(EStatusType.DiskFailurePredicted, status);
        }
Пример #10
0
        public void GenerateStatusTypesProperly()
        {
            CPUStatus  cpu_status = new CPUStatus(90.0);
            List <int> cpu        = new List <int>(new int[] { 1, 1, 1, 1, 1 });

            EStatusType status = cpu_status.GetStatus(cpu);

            Assert.Equal(EStatusType.NormalCPU, status);
            Assert.Equal(1.0, cpu_status.Average, 1);

            cpu    = new List <int>(new int[] { 90, 90, 90, 90, 90 });
            status = cpu_status.GetStatus(cpu);
            Assert.Equal(EStatusType.ExcessiveCPU, status);
        }
Пример #11
0
        /// <summary>
        /// Make sure we only drive the status "up". Critically low should be >
        /// low, and low should be > adequate
        /// </summary>
        /// <param name="a">The first status to compare</param>
        /// <param name="b">The second status to compare</param>
        /// <returns>CriticallyLowOnDiskSpace, LowOnDiskSpace, or AdequateDiskSpace depending upon which is greater: a or b</returns>
        public static EStatusType DiskSpaceCompare(this EStatusType a, EStatusType b)
        {
            if (a == EStatusType.CriticallyLowOnDiskSpace || b == EStatusType.CriticallyLowOnDiskSpace)
            {
                return(EStatusType.CriticallyLowOnDiskSpace);
            }

            if (a == EStatusType.LowOnDiskSpace || b == EStatusType.LowOnDiskSpace)
            {
                return(EStatusType.LowOnDiskSpace);
            }

            return(EStatusType.AdequateDiskSpace);
        }
Пример #12
0
        public void DiskSpaceCompareIsOK()
        {
            EStatusType status = EStatusType.AdequateDiskSpace;

            Assert.Equal(EStatusType.LowOnDiskSpace, status.DiskSpaceCompare(EStatusType.LowOnDiskSpace));
            Assert.Equal(EStatusType.CriticallyLowOnDiskSpace, status.DiskSpaceCompare(EStatusType.CriticallyLowOnDiskSpace));

            status = EStatusType.LowOnDiskSpace;
            Assert.Equal(EStatusType.LowOnDiskSpace, status.DiskSpaceCompare(EStatusType.AdequateDiskSpace));
            Assert.Equal(EStatusType.CriticallyLowOnDiskSpace, status.DiskSpaceCompare(EStatusType.CriticallyLowOnDiskSpace));

            status = EStatusType.CriticallyLowOnDiskSpace;
            Assert.Equal(EStatusType.CriticallyLowOnDiskSpace, status.DiskSpaceCompare(EStatusType.AdequateDiskSpace));
            Assert.Equal(EStatusType.CriticallyLowOnDiskSpace, status.DiskSpaceCompare(EStatusType.LowOnDiskSpace));
        }
Пример #13
0
        public void HandleMaxThresholdsProperly()
        {
            CPUStatus  cpu_status = new CPUStatus(0.0);
            List <int> cpu        = new List <int>(new int[] { 0, 0, 0, 0, 0 });

            EStatusType status = cpu_status.GetStatus(cpu);

            Assert.Equal(EStatusType.ExcessiveCPU, status);

            cpu_status = new CPUStatus(100.0);
            status     = cpu_status.GetStatus(cpu);
            Assert.Equal(EStatusType.NormalCPU, status);
            cpu    = new List <int>(new int[] { 100, 100, 100, 100, 100 });
            status = cpu_status.GetStatus(cpu);
            Assert.Equal(EStatusType.ExcessiveCPU, status);
        }
Пример #14
0
        public void HandleMaxThresholdsProperly()
        {
            MemoryStatus mem_status = new MemoryStatus(0.0, 100.0);

            EStatusType status = mem_status.GetStatus(0, 1000);

            Assert.Equal(EStatusType.LowFreeMemory, status);

            status = mem_status.GetStatus(1, 1000);
            Assert.Equal(EStatusType.LowFreeMemory, status);

            status = mem_status.GetStatus(999, 1000);
            Assert.Equal(EStatusType.LowFreeMemory, status);

            status = mem_status.GetStatus(1000, 1000);
            Assert.Equal(EStatusType.CriticallyLowFreeMemory, status);
        }
Пример #15
0
        public void HandleThresholdTransitionsProperly()
        {
            MemoryStatus mem_status = new MemoryStatus(80.0, 90.0);

            EStatusType status = mem_status.GetStatus(799, 1000);

            Assert.Equal(EStatusType.AdequateFreeMemory, status);

            status = mem_status.GetStatus(800, 1000);
            Assert.Equal(EStatusType.LowFreeMemory, status);

            status = mem_status.GetStatus(899, 1000);
            Assert.Equal(EStatusType.LowFreeMemory, status);

            status = mem_status.GetStatus(900, 1000);
            Assert.Equal(EStatusType.CriticallyLowFreeMemory, status);
        }
Пример #16
0
        public void HandleThresholdTransitionsProperly()
        {
            CPUStatus  cpu_status = new CPUStatus(90.0);
            List <int> cpu        = new List <int>(new int[] { 89, 89, 89, 89, 89 });

            EStatusType status = cpu_status.GetStatus(cpu);

            Assert.Equal(EStatusType.NormalCPU, status);
            Assert.Equal(89.0, cpu_status.Average, 1);

            cpu    = new List <int>(new int[] { 89, 90, 90, 90, 90 });
            status = cpu_status.GetStatus(cpu);
            Assert.Equal(EStatusType.NormalCPU, status);
            Assert.Equal(89.8, cpu_status.Average, 1);

            cpu[0] = 90;
            status = cpu_status.GetStatus(cpu);
            Assert.Equal(EStatusType.ExcessiveCPU, status);
            Assert.Equal(90.0, cpu_status.Average, 1);
        }
Пример #17
0
        public void GenerateStatusTypesProperly()
        {
            MemoryStatus mem_status = new MemoryStatus(80.0, 90.0);

            EStatusType status = mem_status.GetStatus(70, 100);

            Assert.Equal(EStatusType.AdequateFreeMemory, status);

            status = mem_status.GetStatus(80, 100);
            Assert.Equal(EStatusType.LowFreeMemory, status);

            status = mem_status.GetStatus(90, 100);
            Assert.Equal(EStatusType.CriticallyLowFreeMemory, status);

            status = mem_status.GetStatus(99, 100);
            Assert.Equal(EStatusType.CriticallyLowFreeMemory, status);

            status = mem_status.GetStatus(0, 100);
            Assert.Equal(EStatusType.AdequateFreeMemory, status);
        }
Пример #18
0
        public void Interpret(Data data, SQLiteConnection conn)
        {
            if (data.Type == ECollectorType.SMART)
            {
                ListData <HardDisk> disks = data as ListData <HardDisk>;
                if (disks == null)
                {
                    return;
                }

                long device_id = GetDeviceID(data, conn);
                if (device_id >= 0)
                {
                    List <string> drive_letter_list = HardDisk.FailingDrives(disks.Data);
                    SmartStatus   smart_status      = new SmartStatus();
                    EStatusType   status            = smart_status.GetStatus(drive_letter_list);

                    SetDeviceStatus(device_id, status, SmartStatus.Types, drive_letter_list.JoinStrings(", "), conn);
                }
            }
        }
 public void onChange(EStatusType _status, ref string _channelURI, bool _proximal)
 {
 }
 public static string status2string(EStatusType _inStatus)
 {
     return _inStatus.ToString();
 }
Пример #21
0
 public void DisableBranch() => Status  = EStatusType.Inactive;
Пример #22
0
        /// <summary>
        /// Updates a device's status. If the new status is null, the existing status should be invalidated and no new
        /// entry added. If the new status is non-null, see it the existing status matches the new status. If the new
        /// and old status are identical, do nothing. If the new and old status are different somehow (the status itself,
        /// the is-alarm flag or the message are different), invalidate the old status and insert a new status.
        ///
        /// A single device may have different statuses, so we need to restrict our decisions to a set of types, which
        /// is included in the 3rd parameter. For example, the memory statuses are adequate, low, or critically low. Only
        /// one of these should be 'active' at a given time.
        /// </summary>
        /// <param name="device_id">The ID of the device we're checking</param>
        /// <param name="type">The new type. Can be null to indicate the status should be cleared</param>
        /// <param name="statuses">The full set of statuses we should be considering. Will be a subset of all
        /// the statuses a device can have.</param>
        /// <param name="message">A message regarding the status</param>
        /// <param name="conn">The DB connection to use</param>
        protected static void SetDeviceStatus(long device_id, EStatusType?type, List <EStatusType> statuses, string message, SQLiteConnection conn)
        {
            if (type.HasValue)
            {
                Debug.Assert(statuses.Contains(type.Value));
            }

            EAlertLevel alert_level = (type.HasValue ? type.Value.GetAlertLevel() : null) ?? EAlertLevel.Normal;
            string      in_clause   = statuses.Join();
            string      clause      = $"DeviceID = {device_id} AND StatusType IN ({in_clause}) AND IsValid = 1";
            string      sql         = $"SELECT StatusType, IsAlarm, Message FROM DeviceStatus WHERE {clause};";
            bool        insert      = type.HasValue;
            bool        remove      = type.HasValue == false;

            if (type.HasValue)
            {
                // We may be inserting a new type. We need to see if the current value for the device/status is the
                // same, or has changed. If it's the same we don't need to do anything. If it's changed, we'll want to
                // mark the old value as invalid, and insert the new value.
                using (SQLiteCommand command = new SQLiteCommand(sql, conn))
                    using (SQLiteDataReader reader = command.ExecuteReader())
                    {
                        if (reader.Read())
                        {
                            EStatusType existing_type        = (EStatusType)reader.GetInt32(0);
                            EAlertLevel existing_alert_level = (EAlertLevel)reader.GetInt32(1);
                            string      existing_message     = reader.IsDBNull(2) ? string.Empty : reader.GetString(2);

                            // An existing record exists, so insert the new one and update the old one if something's changed.
                            // If nothing changed, we'll leave the old one alone
                            bool something_changed =
                                (type.Value != existing_type) ||
                                (alert_level != existing_alert_level) ||
                                (string.Compare(message, existing_message) != 0);

                            // If something has changed, we'll want to remove the old version, and insert the new
                            // version. If nothing changed, we don't want to do either.
                            insert = remove = something_changed;
                        }
                        // If it wasn't found, just insert, which is the default
                    }
            }
            // In this case there's no status value, so that means were clearing out the old one, if it exists.

            if (insert || remove)
            {
                if (remove)
                {
                    Updater updater = new Updater("DeviceStatus", clause, conn);
                    updater.Set("IsValid", 0);
                    updater.Execute();
                }

                if (insert)
                {
                    Inserter inserter = new Inserter("DeviceStatus", conn);
                    inserter.Set("DeviceID", device_id);
                    inserter.Set("StatusType", (int)type);
                    inserter.Set("IsAlarm", (int)alert_level);
                    inserter.Set("Date", DateTimeOffset.Now);
                    inserter.Set("Message", message, false);
                    inserter.Set("IsValid", 1);
                    inserter.Execute();
                }
            }
            // else, no change
        }
Пример #23
0
 public void SetStatusType(EStatusType statusType)
 {
     _statusType = statusType;
 }
Пример #24
0
 private void OnAsteroidTerminated(EStatusType state)
 {
     _uiController.SetAchievementPoint(state, _config.AddStatusCount(state.ToString(), 1));
 }
Пример #25
0
 private void OnPlayerDefeated(EStatusType state)
 {
     _uiController.SetAchievementPoint(state, _config.AddStatusCount(state.ToString(), 1));
     _enemiesController.ClearArea();
     _playerController.Respawn();
 }
Пример #26
0
        static void HandleData(Data d)
        {
            WriteLine(JsonConvert.SerializeObject(d, Newtonsoft.Json.Formatting.Indented,
                                                  new Newtonsoft.Json.JsonSerializerSettings {
                StringEscapeHandling = StringEscapeHandling.EscapeHtml
            }));

            switch (d.Type)
            {
            case ECollectorType.Memory:
                break;

            case ECollectorType.Disk:
                if (d is GenericDictionaryData <DiskUsage> )
                {
                    Dictionary <string, DiskUsage> value = ((GenericDictionaryData <DiskUsage>)d).Data;
                    DiskSpaceStatus ds_status            = new DiskSpaceStatus(80, 90);
                    EStatusType     status = EStatusType.AdequateDiskSpace;
                    foreach (string drive in value.Keys)
                    {
                        DiskUsage data = value[drive];
                        Tuple <EStatusType, double> drive_status = ds_status.GetStatus(data.UsedNum, data.CapacityNum);
                        status = status.DiskSpaceCompare(drive_status.Item1);
                        WriteLine($"{drive} -- {drive_status.Item2:0.0} %");
                    }
                    WriteLine($"Status: {status}");
                }
                break;

            case ECollectorType.CPUUsage:
                break;

            case ECollectorType.NICUsage:
                break;

            case ECollectorType.Uptime:
                break;

            case ECollectorType.LastBootTime:
                break;

            case ECollectorType.Processes:
                GenericDictionaryData <gov.sandia.sld.common.data.wmi.Process> processes = d as GenericDictionaryData <gov.sandia.sld.common.data.wmi.Process>;
                ulong working_set         = 0;
                ulong working_set_private = 0;
                ulong private_bytes       = 0;
                ulong memory = 0;
                foreach (gov.sandia.sld.common.data.wmi.Process process in processes.Data.Values)
                {
                    memory              += process.MemoryNum;
                    working_set         += process.WorkingSetNum;
                    working_set_private += process.WorkingSetPrivateNum;
                    private_bytes       += process.PrivateBytesNum;
                }

                WriteLine($"Total memory: {memory} bytes, {memory / (float)(1024 * 1024)} MB");
                WriteLine($"Total working set: {working_set} bytes, {working_set / (float)(1024 * 1024)} MB");
                WriteLine($"Total private working set: {working_set_private} bytes, {working_set_private / (float)(1024 * 1024)} MB");
                WriteLine($"Total private bytes: {private_bytes} bytes, {private_bytes / (float)(1024 * 1024)} MB");

                break;

            case ECollectorType.Ping:
                break;

            case ECollectorType.InstalledApplications:
                break;

            case ECollectorType.Services:
                break;

            case ECollectorType.SystemErrors:
                break;

            case ECollectorType.ApplicationErrors:
                break;

            case ECollectorType.DatabaseSize:
                break;

            case ECollectorType.UPS:
                break;

            case ECollectorType.DiskSpeed:
                break;

            case ECollectorType.Configuration:
                break;

            case ECollectorType.SMART:
                break;

            //case CollectorType.AntiVirus:
            //    break;
            //case CollectorType.Firewall:
            //    break;
            case ECollectorType.Unknown:
                break;

            default:
                break;
            }
        }
Пример #27
0
 public void SetAchievementItem(int index, string achieveName, string description, EStatusType statusType,
                                string rewardType, int rewardAmount, int curLevel, int maxPoint, int curPoint)
 {
     _achievementItem[index].SetName(achieveName, curLevel);
     _achievementItem[index].SetDescription(description, maxPoint);
     _achievementItem[index].SetReward(rewardType, rewardAmount);
     _achievementItem[index].SetMaxPoint(maxPoint);
     _achievementItem[index].SetStatusType(statusType);
     _achievementItem[index].SetCounter(curPoint);
 }
Пример #28
0
 public void ActivateBranch() => Status = EStatusType.Active;