コード例 #1
0
ファイル: Check.cs プロジェクト: pcGitHub1/GHOSTS
        private void RunEx()
        {
            var c      = new ConfigHealth(ApplicationDetails.ConfigurationFiles.Health);
            var config = c.Load();

            while (true)
            {
                try
                {
                    var r = HealthManager.Check(config);

                    var o = JsonConvert.SerializeObject(r,
                                                        Formatting.None,
                                                        new JsonSerializerSettings
                    {
                        NullValueHandling = NullValueHandling.Ignore
                    });

                    _healthLog.Info($"HEALTH|{DateTime.UtcNow}|{o}");


                    Thread.Sleep(config.Sleep);
                }
                catch (Exception e)
                {
                    _log.Debug(e);
                }
            }
        }
コード例 #2
0
        public static ResultHealth Check(ConfigHealth config)
        {
            var r = new ResultHealth();

            //connectivity (internet)
            foreach (var url in config.CheckUrls)
            {
                var request = WebRequest.Create(url);

                var watch = System.Diagnostics.Stopwatch.StartNew();

                //TODO - this only gets running user, there may be other users on the box
                if (!r.LoggedOnUsers.Contains(Environment.UserName))
                {
                    r.LoggedOnUsers.Add(Environment.UserName);
                }

                try
                {
                    using (var response = (HttpWebResponse)request.GetResponse())
                    {
                        //log.Debug(response.StatusCode);
                        using (var stream = response.GetResponseStream())
                        {
                            using (var reader = new StreamReader(stream))
                            {
                                //html = reader.ReadToEnd(); //if we wanted to read html
                            }
                        }
                    }
                }
                catch (WebException e)
                {
                    r.Errors.Add($"Connection error - web exception: {e.Message} to {request.RequestUri}");
                }
                catch (Exception e)
                {
                    r.Errors.Add($"Connection error - general exception: {e.Message} to {request.RequestUri}");
                }

                watch.Stop();

                r.ExecutionTime = watch.ElapsedMilliseconds;
                r.Internet      = (r.Errors.Count == 0);
                r.Stats         = MachineHealth.Run();

                //log.Debug(r.ExecutionTime);
                //log.Debug(html);
            }



            // permissions
            // can run x

            return(r);
        }