示例#1
0
        /// <summary>
        ///     Adapt the value specified in the context
        /// </summary>
        /// <param name="context">Context information</param>
        /// <param name="currentValue">Value which might have been adapted</param>
        /// <returns>The new value (or same as the current value if no modification has been made)</returns>
        public object Adapt(ValueAdapterContext context, object currentValue)
        {
            if (context == null)
            {
                throw new ArgumentNullException("context");
            }
            if (currentValue == null || context.ContextName != "ApplicationInfo")
            {
                return(currentValue);
            }

            if (context.PropertyName == "MainModule")
            {
                if (currentValue.ToString().StartsWith("System.Diagnostics.ProcessModule"))
                {
                    return(currentValue.ToString()
                           .Substring("System.Diagnostics.ProcessModule".Length)
                           .Trim(' ', '(', ')'));
                }
            }

            if (MemoryProperties.Any(x => x.Equals(context.PropertyName, StringComparison.OrdinalIgnoreCase)))
            {
                var value = 0;
                if (!int.TryParse(currentValue.ToString(), out value))
                {
                    return(currentValue);
                }

                value = value / 1000000;
                return(MemoryNormalizer.Divide(value, context.TypeOfApplication == "Mobile" ? 32 : 512));
            }

            if (context.PropertyName == "ThreadCount")
            {
                return(NumberNormalizer.Normalize(currentValue.ToString(), 10, 50));
            }
            if (context.PropertyName == "HandleCount")
            {
                return(NumberNormalizer.Normalize(currentValue.ToString(), 100, 5000));
            }

            if (context.PropertyName == "StartTime")
            {
                DateTime dt;
                if (!DateTime.TryParse(currentValue.ToString(), out dt))
                {
                    return(currentValue);
                }

                context.IgnoreProperty = true;
                context.AddCustomField("ApplicationInfo", "StartTime.Hour", dt.Hour);
                context.AddCustomField("ApplicationInfo", "StartTime.DayOfWeek", dt.DayOfWeek.ToString());
            }

            context.IgnoreProperty = true;
            return(currentValue);
        }
示例#2
0
        public object Adapt(ValueAdapterContext context, object currentValue)
        {
            if (context == null)
            {
                throw new ArgumentNullException("context");
            }
            var val = currentValue as string;

            if (string.IsNullOrEmpty(val))
            {
                return(currentValue);
            }
            if (!context.ContextName.Equals("OperatingSystem", StringComparison.OrdinalIgnoreCase))
            {
                return(currentValue);
            }

            context.IgnoreProperty = true;
            if (context.PropertyName.Equals("OperatingSystemSKU", StringComparison.OrdinalIgnoreCase))
            {
                var name = OperatingSystemSku.GetName(currentValue.ToString()) ?? currentValue;
                context.AddCustomField("OS.Metadata", "Edition", name);
            }

            if (context.PropertyName.Equals("CSDVersion", StringComparison.OrdinalIgnoreCase))
            {
                context.AddCustomField("OS.Metadata", "ServicePack", currentValue.ToString());
                return(currentValue);
            }

            if (context.PropertyName.Equals("OSProductSuite", StringComparison.OrdinalIgnoreCase))
            {
                var value = OsProductSuite.GetNames(currentValue.ToString()) ?? currentValue;
                context.AddCustomField("OS.Metadata", "ProductSuite", value);
                return(currentValue);
            }

            if (context.PropertyName.Equals("ProductType", StringComparison.OrdinalIgnoreCase))
            {
                switch (currentValue.ToString())
                {
                case "1":
                    currentValue = "Work Station";
                    break;

                case "2":
                    currentValue = "Domain Controller";
                    break;

                case "3":
                    currentValue = "Server";
                    break;
                }
                context.AddCustomField("OS.Metadata", "ProductType", currentValue);
                return(currentValue);
            }


            if (context.PropertyName.Equals("SuiteMask", StringComparison.OrdinalIgnoreCase))
            {
                var bitMask = 0;
                var value   = "";
                if (int.TryParse(currentValue.ToString(), out bitMask))
                {
                    if ((bitMask & 1) != 0)
                    {
                        value += "Small Business, ";
                    }
                    if ((bitMask & 2) != 0)
                    {
                        value += "Enterprise, ";
                    }
                    if ((bitMask & 4) != 0)
                    {
                        value += "BackOffice, ";
                    }
                    if ((bitMask & 8) != 0)
                    {
                        value += "Communications, ";
                    }
                    if ((bitMask & 16) != 0)
                    {
                        value += "Terminal Services, ";
                    }
                    if ((bitMask & 32) != 0)
                    {
                        value += "Small Business Restricted, ";
                    }
                    if ((bitMask & 64) != 0)
                    {
                        value += "Embedded Edition, ";
                    }
                    if ((bitMask & 128) != 0)
                    {
                        value += "Datacenter Edition, ";
                    }
                    if ((bitMask & 256) != 0)
                    {
                        value += "Single User, ";
                    }
                    if ((bitMask & 512) != 0)
                    {
                        value += "Home Edition, ";
                    }
                    if ((bitMask & 1024) != 0)
                    {
                        value += "Web Server Edition, ";
                    }
                }
                if (value != "")
                {
                    currentValue = value.Remove(value.Length - 2, 2);
                }
                context.AddCustomField("OS.Metadata", "Suite2", currentValue);
                return(currentValue);
            }
            if (context.PropertyName.Equals("QuantumLength", StringComparison.OrdinalIgnoreCase))
            {
                switch (currentValue.ToString())
                {
                case "0":
                    currentValue = "Unknown ";
                    break;

                case "1":
                    currentValue = "One tick";
                    break;

                case "2":
                    currentValue = "Two ticks";
                    break;
                }
                context.AddCustomField("OS.Metadata", "QuantumLength", currentValue);
                return(currentValue);
            }

            if (context.PropertyName.Equals("QuantumType", StringComparison.OrdinalIgnoreCase))
            {
                switch (currentValue.ToString())
                {
                case "0":
                    currentValue = "Unknown ";
                    break;

                case "1":
                    currentValue = "Fixed";
                    break;

                case "2":
                    currentValue = "Variable";
                    break;
                }
                context.AddCustomField("OS.Metadata", "QuantumType", currentValue);
                return(currentValue);
            }


            if (context.PropertyName == "InstallDate")
            {
                return(AdaptInstallDate(context, currentValue));
            }


            if (context.PropertyName == "LastBootUpTime")
            {
                DateTime time;
                if (WmiDateConverter.TryParse(context.Value.ToString(), out time))
                {
                    context.AddCustomField("OS.Environment", "LastBootup.Hour", time.Hour);
                    context.AddCustomField("OS.Environment", "LastBootup.DayOfWeek", time.DayOfWeek.ToString());
                }
            }


            if (context.PropertyName == "LocalDateTime")
            {
                return(AdaptLocalTime(context, currentValue));
            }

            if (context.PropertyName.StartsWith("Free", StringComparison.OrdinalIgnoreCase) ||
                context.PropertyName.StartsWith("Total", StringComparison.OrdinalIgnoreCase) ||
                context.PropertyName.Equals("SizeStoredInPagingFiles", StringComparison.OrdinalIgnoreCase))
            {
                var divisor = context.TypeOfApplication == "Server" ? 512 : 256;
                var value   = MemoryNormalizer.Divide(currentValue as string, divisor);
                if (!string.IsNullOrEmpty(value))
                {
                    context.AddCustomField("OS.Environment", context.PropertyName, value);
                }

                return(currentValue);
            }

            //allow as-is
            if (context.PropertyName.StartsWith("DataExecutionPrevention", StringComparison.OrdinalIgnoreCase))
            {
                context.AddCustomField("OS.Metadata", context.PropertyName, currentValue);
                return(currentValue);
            }

            if (LocalizationProperties.Any(x => x.Equals(context.PropertyName, StringComparison.OrdinalIgnoreCase)))
            {
                if (context.PropertyName == "Locale")
                {
                    int lcid;
                    if (int.TryParse(currentValue.ToString(), NumberStyles.HexNumber, NumberFormatInfo.InvariantInfo,
                                     out lcid))
                    {
                        currentValue = CultureInfo.GetCultureInfo(lcid).Name;
                    }
                }

                context.AddCustomField("OS.Localization", context.PropertyName, currentValue);
                return(null);
            }

            if (OsEnvironment.Any(x => x.Equals(context.PropertyName, StringComparison.OrdinalIgnoreCase)))
            {
                if (context.PropertyName == "NumberOfUsers")
                {
                    if ("0".Equals(currentValue))
                    {
                        currentValue = "0";
                    }
                    else if ("1".Equals(currentValue))
                    {
                        currentValue = "1";
                    }
                    else
                    {
                        currentValue = "> 1";
                    }
                    context.AddCustomField("OS.Environment", "NumberOfLoggedInUsers", currentValue);
                    return(currentValue);
                }

                if (context.PropertyName == "NumberOfProcesses")
                {
                    currentValue = NumberNormalizer.Normalize(currentValue as string, 20, 1000);
                }

                context.AddCustomField("OS.Environment", context.PropertyName, currentValue);
                return(currentValue);
            }

            return(currentValue);
        }