Class OsCommandService
java.lang.Object
org.sentrysoftware.metricshub.extension.oscommand.OsCommandService
Os Command Service that handles OS commands, including local and remote execution.
-
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionstatic Optional<char[]> getPassword(IConfiguration protocolConfiguration) Retrieves the password associated with an SSH configuration.static longgetTimeout(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 timeoutgetUsername(IConfiguration configuration) Retrieves the username associated with an SSH configuration.static StringrunLocalCommand(@NonNull String command, long timeout, String noPasswordCommand) Run the given command on the localhost machine.static OsCommandResultrunOsCommand(@NonNull String commandLine, @NonNull TelemetryManager telemetryManager, Long commandTimeout, boolean isExecuteLocally, boolean isLocalhost, @NonNull Map<Integer, EmbeddedFile> connectorEmbeddedFiles) Run the OS Command on: Local (use java Process) Remote Linux (use SSH)static StringrunSshCommand(@NonNull String command, @NonNull String hostname, @NonNull SshConfiguration sshConfiguration, long timeout, List<File> localFiles, String noPasswordCommand) Run an SSH command, checking if it can be executed on localhost or remotely.
-
Constructor Details
-
OsCommandService
public OsCommandService()
-
-
Method Details
-
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.
-
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 generalSshConfigurationconfiguration.defaultTimeout- The default timeout in seconds.- Returns:
- The timeout in seconds.
-
getUsername
Retrieves the username associated with an SSH configuration.- Parameters:
configuration- The configuration object of typeIConfiguration.- Returns:
- An
Optionalcontaining the username if the configuration is of typeSshConfigurationand has a username set; otherwise, an empty optional.
-
getPassword
Retrieves the password associated with an SSH configuration.- Parameters:
protocolConfiguration- The configuration object of typeIConfiguration.- Returns:
- An
Optionalcontaining the password as a char array if the configuration is of typeSshConfigurationand has a password set; otherwise, an empty optional.
-
runOsCommand
public static OsCommandResult runOsCommand(@NonNull @NonNull String commandLine, @NonNull @NonNull TelemetryManager telemetryManager, Long commandTimeout, boolean isExecuteLocally, boolean isLocalhost, @NonNull @NonNull Map<Integer, EmbeddedFile> connectorEmbeddedFiles) throws IOException, ClientException, InterruptedException, TimeoutException, NoCredentialProvidedException, ControlledSshExceptionRun the OS Command on:- Local (use java Process)
- 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.connectorEmbeddedFiles- All the embedded files map defined in the Connector instance.- 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.
-