public static bool IsServerInDatabase(int id)
        {
            HttpClient client = new HttpClient();

            APISetup.AddHeaders(client);
            HttpResponseMessage response = null;

            try
            {
                Console.WriteLine("getting server...");
                response = client.GetAsync("api/Servers/GetServer/" + id).Result;

                if (response.StatusCode == HttpStatusCode.OK)
                {
                    Console.WriteLine("Successfully");
                }
                else
                {
                    Console.WriteLine("getServerAsync - status code " + response.StatusCode);
                    Program.log.Info("getServerAsync - status code " + response.StatusCode);
                }
            }
            catch (Exception e)
            {
                Console.WriteLine("getServerAsync - Error " + e);
                Program.log.Info("getServerAsync - Error " + e);
            }
            if (response == null || response.StatusCode == HttpStatusCode.NotFound)
            {
                return(false);
            }
            return(true);
        }
        public static void SetupServer()
        {
            HttpClient client = new HttpClient();

            APISetup.AddHeaders(client);

            try
            {
                var    name   = GetServerName();
                Server server = new Server()
                {
                    ServerName = name
                };
                var s = CreateServerAsync(server, client).Result;
                if (s != null && s.Id > 0)
                {
                    Settings.Default.ServerId = s.Id;
                    Settings.Default.Save();
                    Program.ServerId = s.Id;
                    try
                    {
                        var pathtoid     = System.IO.Path.GetDirectoryName(System.Reflection.Assembly.GetEntryAssembly().Location);
                        var filenametoid = @"\MonitorServiceId.txt";
                        pathtoid += filenametoid;

                        if (File.Exists(pathtoid))
                        {
                            File.Delete(pathtoid);
                        }

                        File.WriteAllText(pathtoid, s.Id + "");

                        Settings.Default.ServerId = s.Id;
                        Settings.Default.Save();
                    }
                    catch (Exception e)
                    {
                        Console.WriteLine("Id error: " + e);
                        Program.log.Error("id error: " + e);
                    }
                }
            }
            catch (Exception e)
            {
                Program.log.Info("SendServerSetup - Error: " + e);
                Environment.Exit(0);
            }
        }
Exemple #3
0
        public static void SendServerInfo()
        {
            HttpClient client = new HttpClient();

            APISetup.AddHeaders(client);
            try
            {
                var          temp          = Temperature.Temperatures;
                ServerDetail serverDetails = new ServerDetail
                {
                    Created            = DateTime.Now,
                    CpuUtilization     = Convert.ToInt32(Program.cpuCounter.NextValue()),
                    Processes          = GetProcesses(),
                    UpTime             = Convert.ToInt32(Program.upTime.NextValue()),
                    RamAvailable       = Convert.ToInt32(Program.ramCounter.NextValue()),
                    RamTotal           = Convert.ToInt32(GetTotalMemoryInBytes()),
                    BytesReceived      = Program.bytesReceived,
                    BytesSent          = Program.bytesSent,
                    ServerId           = Program.ServerId,
                    Temperature        = temp.HasValue ? Convert.ToDecimal(temp.GetValueOrDefault()) : 0,
                    NetworkUtilization = Convert.ToDecimal(Network.GetNetworkUtilization()),
                    HarddiskUsedSpace  = Convert.ToDecimal(HarddiskStatus.UsedDiskSpace()),
                    HarddiskTotalSpace = Convert.ToDecimal(HarddiskStatus.TotalDiskSpace()),
                    Handles            = Convert.ToDecimal(GetHandles()),
                    Threads            = Convert.ToDecimal(GetThreads())
                };
                Network.ResetNetwork();
                Program.bytesSent     = 0;
                Program.bytesReceived = 0;
                var result = CreateServerDetailAsync(serverDetails, client).Result;
            }
            catch (Exception e)
            {
                Program.log.Info("SendServerInfo - Error: " + e);
                Environment.Exit(0);
            }
        }