示例#1
0
        private static void Main()
        {
            {
                var config = Path.Combine(Environment.CurrentDirectory, "settings.json");

                if (File.Exists(config))
                {
                    Configuration = JsonConvert.DeserializeObject <Configuration>(File.ReadAllText(config));
                }
                else
                {
                    File.WriteAllText(config, JsonConvert.SerializeObject(Configuration.Default, Formatting.Indented));

                    Console.WriteLine($"Written a new instance of the configuration to: {config}");
                    Console.ReadKey();
                    Environment.Exit(0);
                }
            }

            Log.Configure(Configuration);

            Cachet = new CachetClient(Configuration.ApiBase.OriginalString, Configuration.ApiKey);

            MainAsync().GetAwaiter().GetResult();
        }
示例#2
0
        private static async Task DoMonitorCheck(CachetClient Cachet, Monitor monitor)
        {
            ComponentStatus componentStatus = 0;

            switch (monitor.Type)
            {
            case MonitorType.PORT:
            {
                using TcpClient tcpClient = new TcpClient();
                try
                {
                    tcpClient.Connect(monitor.Target, monitor.Settings.Port);
                    try
                    {
                        componentStatus = ComponentStatus.Operational;
                        await Cachet.UpdateComponentAsync(monitor.ComponentId, new PutComponent
                            {
                                Status = componentStatus,
                            });

                        Log.Debug("PortMonitorCheck", "Sent to Cachet successfully");
                    }
                    catch (Exception ex)
                    {
                        Log.Error("PortMonitorCheck", ex.Message, ex);
                    }
                }
                catch (Exception ex)
                {
                    Log.Error("PortMonitorCheck", ex.Message, ex);

                    try
                    {
                        componentStatus = ComponentStatus.MajorOutage;
                        await Cachet.UpdateComponentAsync(monitor.ComponentId, new PutComponent
                            {
                                Status = componentStatus,
                            });

                        Log.Debug("PortMonitorCheck", "Sent to Cachet successfully");
                    }
                    catch (Exception ex2)
                    {
                        Log.Error("PortMonitorCheck", ex2.Message, ex2);
                    }
                }
            }
            break;

            case MonitorType.ICMP:
            {
                Ping        ping    = new Ping();
                PingOptions options = new PingOptions
                {
                    DontFragment = true,
                    Ttl          = monitor.Settings.TTL
                };

                PingReply reply = ping.Send(monitor.Target, monitor.Timeout, null, options);

                try
                {
                    await Cachet.AddMetricPointAsync(monitor.MetricId, new PostMetricPoint
                        {
                            Value = (int)reply.RoundtripTime
                        });

                    Log.Debug("IPMonitorCheck", "Sent to Cachet successfully");
                }
                catch (Exception ex)
                {
                    Log.Error("IPMonitorCheck", ex.Message, ex);
                }

                if (reply.Status == IPStatus.Success)
                {
                    try
                    {
                        componentStatus = ComponentStatus.Operational;
                        await Cachet.UpdateComponentAsync(monitor.ComponentId, new PutComponent
                            {
                                Status = componentStatus,
                            });

                        Log.Debug("IPMonitorCheck", "Sent to Cachet successfully");
                    }
                    catch (Exception ex)
                    {
                        Log.Error("IPMonitorCheck", ex.Message, ex);
                    }
                }

                if (reply.RoundtripTime >= monitor.Timeout)
                {
                    try
                    {
                        componentStatus = ComponentStatus.PerformanceIssues;
                        await Cachet.UpdateComponentAsync(monitor.ComponentId, new PutComponent
                            {
                                Status = componentStatus,
                            });

                        Log.Debug("IPMonitorCheck", "Sent to Cachet successfully");
                    }
                    catch (Exception ex)
                    {
                        Log.Error("IPMonitorCheck", ex.Message, ex);
                    }
                }

                if (reply.Status != IPStatus.Success)
                {
                    if (reply.Status == IPStatus.TimedOut)
                    {
                        try
                        {
                            componentStatus = ComponentStatus.PartialOutage;
                            await Cachet.UpdateComponentAsync(monitor.ComponentId, new PutComponent
                                {
                                    Status = componentStatus,
                                });

                            Log.Debug("IPMonitorCheck", "Sent to Cachet successfully");
                        }
                        catch (Exception ex)
                        {
                            Log.Error("IPMonitorCheck", ex.Message, ex);
                        }
                    }
                    else
                    {
                        try
                        {
                            componentStatus = ComponentStatus.MajorOutage;
                            await Cachet.UpdateComponentAsync(monitor.ComponentId, new PutComponent
                                {
                                    Status = componentStatus,
                                });

                            Log.Debug("IPMonitorCheck", "Sent to Cachet successfully");
                        }
                        catch (Exception ex)
                        {
                            Log.Error("IPMonitorCheck", ex.Message, ex);
                        }
                    }
                }
            }
            break;

            case MonitorType.HTTP:
            {
                HttpWebRequest request = (HttpWebRequest)WebRequest.Create(monitor.Target);
                request.Timeout = monitor.Timeout * 1000;

                Stopwatch timer = new Stopwatch();

                timer.Start();
                try
                {
                    HttpWebResponse response = (HttpWebResponse)request.GetResponse();
                    timer.Stop();

                    if (response.StatusCode == (HttpStatusCode)monitor.Settings.ExpectedStatusCode)
                    {
                        try
                        {
                            componentStatus = ComponentStatus.Operational;
                            await Cachet.UpdateComponentAsync(monitor.ComponentId, new PutComponent
                                {
                                    Status = componentStatus,
                                });

                            Log.Debug("WebMonitorCheck", "Sent to Cachet successfully");
                        }
                        catch (Exception ex)
                        {
                            Log.Error("WebMonitorCheck", ex.Message, ex);
                        }
                    }
                    else
                    {
                        try
                        {
                            componentStatus = ComponentStatus.PartialOutage;
                            await Cachet.UpdateComponentAsync(monitor.ComponentId, new PutComponent
                                {
                                    Status = componentStatus,
                                });

                            Log.Debug("WebMonitorCheck", "Sent to Cachet successfully");
                        }
                        catch (Exception ex)
                        {
                            Log.Error("WebMonitorCheck", ex.Message, ex);
                        }
                    }

                    response.Close();
                }
                catch (WebException ex)
                {
                    timer.Stop();
                    Log.Warning("WebMonitorChcek", ex.Message, ex);

                    if (ex.Status == WebExceptionStatus.ProtocolError)
                    {
                        if (ex.Response is HttpWebResponse response)
                        {
                            if (response.StatusCode == (HttpStatusCode)monitor.Settings.ExpectedStatusCode)
                            {
                                try
                                {
                                    componentStatus = ComponentStatus.Operational;
                                    await Cachet.UpdateComponentAsync(monitor.ComponentId, new PutComponent
                                        {
                                            Status = componentStatus,
                                        });

                                    Log.Debug("WebMonitorCheck", "Sent to Cachet successfully");
                                }
                                catch (Exception ex2)
                                {
                                    Log.Error("WebMonitorCheck", ex2.Message, ex2);
                                }
                            }
                        }
                    }
                    else
                    {
                        if (ex.Status == WebExceptionStatus.Timeout)
                        {
                            try
                            {
                                componentStatus = ComponentStatus.PerformanceIssues;
                                await Cachet.UpdateComponentAsync(monitor.ComponentId, new PutComponent
                                    {
                                        Status = componentStatus,
                                    });

                                Log.Debug("WebMonitorCheck", "Sent to Cachet successfully");
                            }
                            catch (Exception ex2)
                            {
                                Log.Error("WebMonitorCheck", ex2.Message, ex2);
                            }
                        }
                        else
                        {
                            try
                            {
                                componentStatus = ComponentStatus.MajorOutage;
                                await Cachet.UpdateComponentAsync(monitor.ComponentId, new PutComponent
                                    {
                                        Status = componentStatus,
                                    });

                                Log.Debug("WebMonitorCheck", "Sent to Cachet successfully");
                            }
                            catch (Exception ex2)
                            {
                                Log.Error("WebMonitorCheck", ex2.Message, ex2);
                            }
                        }
                    }
                }

                await Cachet.AddMetricPointAsync(monitor.MetricId, new PostMetricPoint
                    {
                        Value = (int)timer.ElapsedMilliseconds
                    });
            }
            break;
            }

            Log.Verbose("DoMonitorCheck", $"Ran check on \"{monitor.Name}\" Status: {componentStatus.ToString()}");
        }