예제 #1
0
        public override CommandResult Execute()
        {
            var result = new CommandResult();
            result.Status = 0;
            result.Output = "No checks were run";
            {
                string[] splittedArguments = ParseArguments().Split(';');
                var counterlist = getCounterlist(splittedArguments[0]);
                var parameters = ParseParameters(splittedArguments);

                var unixTimestamp = (Int32)(DateTime.UtcNow.Subtract(new DateTime(1970, 1, 1))).TotalSeconds;
                var stdout = new StringBuilder();
                var stderr = new StringBuilder();
                string schema;

                foreach (var counter in counterlist)
                {
                    if (parameters.ContainsKey("schema"))
                        schema = normalizeString(
                            parameters["schema"]
                                .Replace("{INSTANCE}", counter.InstanceName)
                                .Replace("{COUNTER}", counter.CounterName)
                                .Replace("{CATEGORY}", counter.CategoryName)
                                .Replace("%", "_PERCENT_")
                        ).Replace("_PERCENT_", "percent.");
                    else
                        schema = String.Format(CultureInfo.InvariantCulture, "{0}.{1}.{2}.{3}",
                                System.Environment.MachineName,
                                normalizeString(counter.CategoryName),
                                normalizeString(counter.CounterName.Replace('.', '_').Replace("%", "_PERCENT_")).Replace("_PERCENT_", "percent."),
                                "performance_counter");
                    schema = Regex.Replace(schema, @"_*\._*", @".");
                    try
                    {
                        var value = counter.NextValue();
                        stdout.Append(
                            String.Format(CultureInfo.InvariantCulture, "{0} {1:f2} {2}\n",
                                schema,
                                value,
                                unixTimestamp
                            )
                        );

                        if (result.Status == 0)
                        {
                            result.Status = getNewStatus(parameters, counter.ToString(), value, stderr);
                        }
                    }
                    catch (Exception e)
                    {
                        Log.Warn("Error running performance counter {0}:\n {1}", counter.CounterName, e);
                        stderr.AppendLine("# " + e.Message);
                        result.Status = 2;
                    }
                    result.Output = stderr.Append(stdout).ToString().Trim(' ', '_');
                }
            }

            return result;
        }
예제 #2
0
        public virtual CommandResult Execute()
        {
            var result = new CommandResult();
            var processstartinfo = new ProcessStartInfo()
            {
                FileName = FileName,
                Arguments = Arguments,
                WindowStyle = ProcessWindowStyle.Hidden,
                WorkingDirectory = _commandConfiguration.Plugins,
                UseShellExecute = false,
                RedirectStandardError = true,
                RedirectStandardInput = true,
                RedirectStandardOutput = true
            };
            var process = new Process { StartInfo = processstartinfo };
            try
            {
                process.Start();
                if (_commandConfiguration.TimeOut.HasValue)
                {
                    if (!process.WaitForExit(1000 * _commandConfiguration.TimeOut.Value))
                    {
                        Log.Debug("Process to be killed {0}", FileName);
                        process.Kill();
                    }
                }
                else
                {
                    process.WaitForExit();
                }

                var output = process.StandardOutput.ReadToEnd();
                var errors = process.StandardError.ReadToEnd();
                var status = process.ExitCode;
                result.Output = String.Format("{0}{1}", output, errors);
                result.Status = status;
                if (!string.IsNullOrEmpty(errors))
                {
                    Log.Error(
                        "Error when executing command: '{0}' on '{1}' \n resulted in: {2} \n",
                        _unparsedCommand,
                        _commandConfiguration.Plugins,
                        errors
                        );
                }
            }
            catch (Exception ex)
            {
                Log.Warn(
                    ex,
                    "Unexpected error when executing command: '{0}' on '{1}'",
                    _unparsedCommand,
                    _commandConfiguration.Plugins
                    );
                result.Output = String.Format("Unexpected error: {0}", ex.Message);
                result.Status = 2;
            }
            finally
            {
                process.Close();
            }
            return result;

        }
예제 #3
0
        public override CommandResult Execute()
        {
            var result = new CommandResult();
            result.Status = 2;
            result.Output = "No checks were run";

            try {
                var url = getParam("url", null);
                var timeout = Int64.Parse(getParam("timeout", "10000"));
                var uri = new Uri(url);
                var method = getParam("method", "GET");
                var schema = getParam("schema", String.Format(CultureInfo.InvariantCulture, "{0}.http.{1}.{2}", System.Environment.MachineName, uri.Host, uri.Port));
                var validStatusRaw = getParam("valid_codes", "200, 302");
                var validStatus = new List<int>();
                foreach (var code in validStatusRaw.Split(','))
                    validStatus.Add(Int32.Parse(code));

                var stopwatch = new Stopwatch();
                stopwatch.Start();
                var request = (HttpWebRequest)WebRequest.Create(url);
                request.Method = method;
                var response = (HttpWebResponse)request.GetResponse();
                stopwatch.Stop();

                if (validStatus.Contains((int)response.StatusCode))
                {
                    var unixTimestamp = (Int32)(DateTime.UtcNow.Subtract(new DateTime(1970, 1, 1))).TotalSeconds;
                    result.Output = String.Format(CultureInfo.InvariantCulture, "{0} {1:f2} {2}\n",
                                    schema,
                                    stopwatch.ElapsedMilliseconds / 1000.0,
                                    unixTimestamp
                                );
                    result.Status = 0;
                } else
                {
                    result.Output = String.Format("# Error accessing to {0} (code: {1}): {2}", url, response.StatusCode, response.StatusDescription);
                    result.Status = 1;
                }
                
            } catch (Exception e)
            {
                Log.Error(e, "There was an error accessing to an HTTP check");
                result.Output = e.Message;
                result.Status = 2;
            }
            
            return result;
        }