示例#1
0
 public ShellCommand(CommandConfiguration commandConfiguration, string unparsedCommand) : base(commandConfiguration, unparsedCommand)
 {
     var cmd = unparsedCommand.Split(new char[] { ' ' }, 2);
     _filename = cmd[0];
     if (cmd.Length > 1)
         _arguments = cmd[1];
 }
示例#2
0
        public static Command Create(CommandConfiguration commandConfiguration, string command)
        {
            var command_lower = command.ToLower();
            if (command_lower.StartsWith(PerformanceCounterCommand.PREFIX)) return new PerformanceCounterCommand(commandConfiguration, command);
            if (command_lower.StartsWith(HTTPCommand.PREFIX)) return new HTTPCommand(commandConfiguration, command);
            if (command_lower.Contains(".ps1")) return new PowerShellCommand(commandConfiguration, command);
            if (command_lower.Contains(".rb")) return new RubyCommand(commandConfiguration, command);

            return new ShellCommand(commandConfiguration, command);
        }
示例#3
0
        public bool ExecuteUpdateCheck(CommandConfiguration configuration, JObject check)
        {
            UpdateFile file = new Update.UpdateFile(configuration.Plugins, check);

            if (File.Exists(file.filepath))
            {
                try
                {
                    file = ReadUpdateFile(file);
                    bool performUpdate = CompareTimeForUpdate(file);

                    if (performUpdate)
                    {
                        file.FileTimestamp = DateTime.Today;
                        DownloadUpdate(file, check);
                        WriteUpdateFile(file);
                        return true;
                    }
                }
                catch (Exception e)
                {
                    if (e is IOException)
                    {
                        throw new Exception("IOException: " + e.StackTrace);
                    } 
                    else if (e is UnauthorizedAccessException)
                    { 
                        FileAttributes attr = (new FileInfo(file.filepath)).Attributes;
                        string exception = "UnAuthorizedAccesException: Unable to access file. ";
                        if ((attr & FileAttributes.ReadOnly) > 0)
                        {
                            exception = exception + "File is readonly";
                        }
                        throw new Exception(exception + " Stacktrace: " + e.StackTrace);
                    }
               }
            }
            else if (!File.Exists(file.filepath) || !File.Exists(file.checkFilePath))
            {
                file.FileTimestamp = DateTime.Today;
                DownloadUpdate(file, check);
                createUpdateFile(file);
                return true;
            }
            return false;
        }
示例#4
0
        public void ExecuteCheckCommand(JObject check, bool withUpdateCheck)
        {
            Log.Debug("Attempting to execute check command {0}", JsonConvert.SerializeObject(check, SerializerSettings));
            if (check["name"] == null)
            {
                CheckDidNotHaveValidName(check);
                return;
            }
            var checkName = check["name"].ToString();

            if (!checksInProgress.Lock(checkName))
                return;

            try {
                var commandParseErrors = "";
                check["command"] = SensuClientHelper.SubstitueCommandTokens(
                    check, out commandParseErrors, (JObject)_sensuClientConfigurationReader.Configuration.Config["client"]);

                if (!String.IsNullOrEmpty(commandParseErrors))
                {
                    Log.Warn("Errors parsing the command: {0}", commandParseErrors);
                    CheckDidNotHaveValidParameters(check, commandParseErrors);
                    throw new Exception(String.Format("Errors parsing the command {0}", commandParseErrors));
                }

                Log.Debug("Preparing check to be launched: {0}", checkName);

                int? timeout = null;
                if (check["timeout"] != null)
                    timeout = SensuClientHelper.TryParseNullable(check["timeout"].ToString());

                CommandConfiguration configuration = new CommandConfiguration();
                configuration.Plugins = _sensuClientConfigurationReader.SensuClientConfig.Client.Plugins;
                configuration.TimeOut = timeout;

                var commandToExcecute = CommandFactory.Create(configuration
                                                        , check["command"].ToString());
                
                if (commandToExcecute is Command.RemoteCommand)
                {
                    try
                    {
                        Log.Debug("About to run update: " + checkName);
                        UpdateScheduler scheduler = new UpdateScheduler();
                        scheduler.ExecuteUpdateCheck(configuration, check);
                    }
                    catch (Exception e)
                    {
                        Log.Warn(e, "Error preparing update {0}", checkName);
                    }
                }

                Log.Debug("About to run command: " + checkName);
                var executingTask = ExecuteCheck(check, commandToExcecute);
                checksInProgress.SetTask(checkName, executingTask);
                executingTask.ContinueWith<JObject>(ReportCheckResultAfterCompletion).ContinueWith(CheckCompleted);
            } catch (Exception e)
            {
                Log.Error(e, "Error preparing check {0}", checkName);
                checksInProgress.UnlockAnyway(checkName);
            }
        }
示例#5
0
 /*RemoteCommand that allows the sensu client to execute a powershell script from a specific URL*/
 public RemoteCommand(CommandConfiguration commandConfiguration, string unparsedCommand)
     : base(commandConfiguration, unparsedCommand)
 {
 }
示例#6
0
 protected Command(CommandConfiguration commandConfiguration, string unparsedCommand)
 {
     _commandConfiguration = commandConfiguration;
     _unparsedCommand      = unparsedCommand;
 }
示例#7
0
 public PerformanceCounterCommand(CommandConfiguration commandConfiguration, string unparsedCommand)
     : base(commandConfiguration, unparsedCommand)
 {
 }
示例#8
0
 public HTTPCommand(CommandConfiguration commandConfiguration, string unparsedCommand)
     : base(commandConfiguration, unparsedCommand)
 {
     ParseArguments();
 }
示例#9
0
 public PowerShellCommand(CommandConfiguration commandConfiguration, string unparsedCommand)
     : base(commandConfiguration, unparsedCommand)
 {
 }
示例#10
0
 protected Command(CommandConfiguration commandConfiguration, string unparsedCommand)
 {
     _commandConfiguration = commandConfiguration;
     _unparsedCommand = unparsedCommand;
 }
示例#11
0
 public PerformanceCounterCommand(CommandConfiguration commandConfiguration, string unparsedCommand) : base(commandConfiguration, unparsedCommand)
 {
 }
示例#12
0
 public HTTPCommand(CommandConfiguration commandConfiguration, string unparsedCommand) : base(commandConfiguration, unparsedCommand)
 {
     ParseArguments();
 }
示例#13
0
 public RubyCommand(CommandConfiguration commandConfiguration, string unparsedCommand)
     : base(commandConfiguration, unparsedCommand)
 {
 }
示例#14
0
 public PowerShellCommand(CommandConfiguration commandConfiguration, string unparsedCommand) : base(commandConfiguration, unparsedCommand)
 {
 }
示例#15
0
 public void Init()
 {
     _configuration = new CommandConfiguration { Plugins = @"c:\etc\plugins" };
 }