java.lang.Object
org.sentrysoftware.metricshub.engine.strategy.utils.OsCommandHelper

public class OsCommandHelper extends Object
Utility class for handling OS commands, including local and remote execution.
  • Constructor Details

    • OsCommandHelper

      public OsCommandHelper()
  • Method Details

    • createOsCommandEmbeddedFiles

      public static Map<String,File> createOsCommandEmbeddedFiles(@NonNull @NonNull String commandLine, OsCommandConfiguration osCommandConfiguration, @NonNull @NonNull Map<String,EmbeddedFile> commandLineEmbeddedFiles, Function<String,File> tempFileCreator) throws IOException
      Create the temporary embedded files in the given command line.
      Parameters:
      commandLine - The command line to process.
      osCommandConfiguration - The OS Command Configuration.
      commandLineEmbeddedFiles - A map of embedded files referenced in the command line.
      tempFileCreator - The function that creates a temporary file.
      Returns:
      A map with EmbeddedFile tags as keys and corresponding temporary File objects.
      Throws:
      IOException - If an error occurs during temp file creation.
    • runLocalCommand

      public static String runLocalCommand(@NonNull @NonNull String command, long timeout, String noPasswordCommand) throws InterruptedException, IOException, TimeoutException
      Run the given command on the localhost machine.
      Parameters:
      command - The command to be executed.
      timeout - The timeout for the command execution in seconds.
      noPasswordCommand - The command with the password masked (if present).
      Returns:
      The result of the local command execution.
      Throws:
      InterruptedException - When the thread is interrupted during execution.
      IOException - When an I/O error occurs during command execution.
      TimeoutException - When the command execution times out.
    • runSshCommand

      public static String runSshCommand(@NonNull @NonNull String command, @NonNull @NonNull String hostname, @NonNull @NonNull SshConfiguration sshConfiguration, long timeout, List<File> localFiles, String noPasswordCommand) throws ClientException, InterruptedException, ControlledSshException
      Run an SSH command, checking if it can be executed on localhost or remotely.
      Parameters:
      command - The SSH command to be executed.
      hostname - The hostname of the remote machine.
      sshConfiguration - The SSH configuration including username, password, and private key.
      timeout - The timeout for the command execution in seconds.
      localFiles - List of local files required for the remote execution.
      noPasswordCommand - The command with password masked (if present).
      Returns:
      The result of the SSH command execution.
      Throws:
      ClientException - When an error occurs during Client execution.
      InterruptedException - When the thread is interrupted during execution.
      ControlledSshException - When there's an issue with controlled SSH execution.
    • toCaseInsensitiveRegex

      public static String toCaseInsensitiveRegex(String host)
      Convert a string to be searched in a case insensitive regex.
      Parameters:
      host - The string to searched. (mandatory)
      Returns:
      The case insensitive regex for this string.
    • getTimeout

      public static long getTimeout(Long commandTimeout, OsCommandConfiguration osCommandConfiguration, IConfiguration configuration, long defaultTimeout)
      Get the timeout from:
      • First, the command timeout
      • Then, the command configuration timeout
      • Then, the protocol configuration timeout
      • Finally, the default timeout
      Parameters:
      commandTimeout - The OS command timeout in seconds.
      osCommandConfiguration - The configuration specific to OS command execution.
      configuration - The general configuration, either an instance of IWinConfiguration or SshConfiguration.
      defaultTimeout - The default timeout in seconds.
      Returns:
      The timeout in seconds.
    • getUsername

      public static Optional<String> getUsername(IConfiguration configuration)
      Get the username from the IWinProtocol or the SSHProtocol.
      Parameters:
      configuration - IWinProtocol or SSHProtocol.
      Returns:
      An optional with the username if found. An empty optional otherwise.
    • getPassword

      public static Optional<char[]> getPassword(IConfiguration protocolConfiguration)
      Get the password from the IWinProtocol or the SSHProtocol.
      Parameters:
      protocolConfiguration - IWinProtocol or SSHProtocol.
      Returns:
      An optional with the password if found. An empty optional otherwise.
    • runOsCommand

      public static OsCommandResult runOsCommand(@NonNull @NonNull String commandLine, @NonNull @NonNull TelemetryManager telemetryManager, Long commandTimeout, boolean isExecuteLocally, boolean isLocalhost) throws IOException, ClientException, InterruptedException, TimeoutException, NoCredentialProvidedException, ControlledSshException
      Run the OS Command on:
      • Local (use java Process)
      • Remote windows (use WMI/WinRm command)
      • Remote Linux (use SSH)

      It replaces Host name, User name, Password, Sudo, Embedded files macros in the command line.

      If necessary, it creates embedded files and deletes them after the command execution.

      Parameters:
      commandLine - The command Line. (mandatory)
      telemetryManager - The engine configuration and host properties. (mandatory)
      commandTimeout - The OS command parameter for the timeout.
      isExecuteLocally - The OS command parameter to indicate if the command should be executed locally.
      isLocalhost - The parameter in Host Monitoring to indicate if the command is executed locally.
      Returns:
      The command execution return and the command with password masked (if present).
      Throws:
      IOException - When an I/O error occurred on local command execution or embedded file creation.
      ClientException - When an error occurred on remote execution.
      InterruptedException - When the local command execution is interrupted.
      TimeoutException - When the local command execution ends in timeout.
      NoCredentialProvidedException - When there's no user provided for a remote command.
      ControlledSshException - When an error occurs during controlled SSH execution.