Exemple #1
0
        public async Task Stats()
        {
            long temp;
            var  owner    = (await Context.Client.GetApplicationInfoAsync()).Owner;
            var  cpuUsage = (int)await CommandUtils.GetCpuUsageForProcessAsync();

            string cpu             = null;
            long?  totalRamUsage   = null;
            long   processRamUsage = 0;
            long?  totalRam        = null;
            string os = RuntimeInformation.OSDescription;

            if (RuntimeInformation.IsOSPlatform(OSPlatform.Linux))
            {
                // CPU Name
                if (File.Exists("/proc/cpuinfo"))
                {
                    var cpuinfo = File.ReadAllLines("/proc/cpuinfo");
                    cpu = cpuinfo.ElementAtOrDefault(4)?.Split(':').ElementAtOrDefault(1);
                }

                // OS Name
                if (File.Exists("/etc/lsb-release"))
                {
                    var distroInfo = File.ReadAllLines("/etc/lsb-release");
                    os = distroInfo.ElementAtOrDefault(3)?.Split('=').ElementAtOrDefault(1)?.Trim('\"');
                }

                // Total RAM & total RAM usage
                var output = CommandUtils.RunCommand("free -m")?.Split(Environment.NewLine);
                var memory = output?.ElementAtOrDefault(1)?.Split(' ', StringSplitOptions.RemoveEmptyEntries);

                if (long.TryParse(memory?.ElementAtOrDefault(1), out temp))
                {
                    totalRam = temp;
                }
                if (long.TryParse(memory?.ElementAtOrDefault(2), out temp))
                {
                    totalRamUsage = temp;
                }

                // Process RAM usage
                processRamUsage = Process.GetCurrentProcess().WorkingSet64 / 1024 / 1024;
            }
            else if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows))
            {
                // CPU Name
                cpu = CommandUtils.RunCommand("wmic cpu get name")
                      ?.Trim()
                      .Split(Environment.NewLine, StringSplitOptions.RemoveEmptyEntries)
                      .ElementAtOrDefault(1);

                // Total RAM & total RAM usage
                var output = CommandUtils.RunCommand("wmic OS get FreePhysicalMemory,TotalVisibleMemorySize /Value")
                             ?.Trim()
                             .Split(Environment.NewLine, StringSplitOptions.RemoveEmptyEntries);

                if (output?.Length > 1)
                {
                    long freeRam = 0;
                    var  split   = output[0].Split('=', StringSplitOptions.RemoveEmptyEntries);
                    if (split.Length > 1 && long.TryParse(split[1], out temp))
                    {
                        freeRam = temp / 1024;
                    }

                    split = output[1].Split('=', StringSplitOptions.RemoveEmptyEntries);
                    if (split.Length > 1 && long.TryParse(split[1], out temp))
                    {
                        totalRam = temp / 1024;
                    }

                    if (totalRam != null && freeRam != 0)
                    {
                        totalRamUsage = totalRam - freeRam;
                    }
                }

                // Process RAM usage
                processRamUsage = Process.GetCurrentProcess().PrivateMemorySize64 / 1024 / 1024;
            }
            else
            {
                // TODO: Get system info from the remaining platforms
            }

            int totalUsers = 0;

            foreach (var guild in Context.Client.Guilds)
            {
                totalUsers += guild.MemberCount;
            }
            string version = $"v{Constants.Version}";

            if (FergunClient.IsDebugMode)
            {
                version += "-dev";
            }
            var elapsed = DateTimeOffset.UtcNow - FergunClient.Uptime;

            var builder = new EmbedBuilder()
                          .WithTitle("Fergun Stats")

                          .AddField(Locate("OperatingSystem"), os, true)
                          .AddField("\u200b", "\u200b", true)
                          .AddField("CPU", cpu ?? "?", true)

                          .AddField(Locate("CPUUsage"), cpuUsage + "%", true)
                          .AddField("\u200b", "\u200b", true)
                          .AddField(Locate("RAMUsage"),
                                    $"{processRamUsage}MB ({(totalRam == null ? 0 : Math.Round((double)processRamUsage / totalRam.Value * 100, 2))}%) " +
                                    $"/ {(totalRamUsage == null || totalRam == null ? "?MB" : $"{totalRamUsage}MB ({Math.Round((double)totalRamUsage.Value / totalRam.Value * 100, 2)}%)")} " +