Exemple #1
0
        private void CheckResource(string name, string type, dynamic jObject)
        {
            try
            {
                long result    = -1;
                bool?state     = null;
                var  metadatas = new Dictionary <string, object>()
                {
                    ["Type"] = type
                };
                switch (type.ToLower())
                {
                case "ping":
                    metadatas.Add("Hostname", jObject.Hostname.Value);
                    PingReply reply = this.PingSafe(jObject.Hostname.Value);
                    if (reply != null && reply.Status == IPStatus.Success)
                    {
                        result = reply.RoundtripTime;
                    }
                    break;

                case "tcp":
                    result = CheckPort(jObject.Hostname.Value, (int)jObject.Port.Value);
                    metadatas.Add("Hostname", jObject.Hostname.Value);
                    metadatas.Add("Port", jObject.Port.Value);
                    break;

                case "http":
                    metadatas.Add("Address", jObject.Address.Value);
                    var client = new ExtendedWebClient()
                    {
                        Encoding = Encoding.UTF8,
                        Timeout  = (jObject["Timeout"] != null ? jObject.Timeout.Value : ExtendedWebClient.DEFAULT_TIMEOUT)
                    };
                    try
                    {
                        var sw       = Stopwatch.StartNew();
                        var response = client.DownloadString(jObject.Address.Value);
                        sw.Stop();
                        result = sw.ElapsedMilliseconds;
                        if (jObject["Regex"] != null)
                        {
                            metadatas.Add("Regex", jObject.Regex.Value);
                            var rex = new Regex(jObject.Regex.Value);
                            state = rex.IsMatch(response);
                        }
                    }
                    catch { }
                    break;

                default:
                    PackageHost.WriteWarn("Unknow type : " + type);
                    return;
                }
                PackageHost.PushStateObject <MonitoringResult>(name,
                                                               new MonitoringResult
                {
                    ResponseTime = result,
                    State        = state.HasValue ? state.Value : result >= 0
                }, metadatas: metadatas);
            }
            catch (Exception ex)
            {
                PackageHost.WriteError("Unable to monitor '{0}' ({1}) : {2}", name, type, ex.Message);
            }
        }