2021-04-25 13:25:20 +00:00
/ *
* This Java source file was generated by the Gradle ' init ' task .
* /
2021-05-21 09:08:43 +00:00
package sysmon.client ;
2021-04-25 13:25:20 +00:00
2021-05-04 12:23:34 +00:00
import org.apache.camel.main.Main ;
2022-09-23 12:49:13 +00:00
import org.slf4j.simple.SimpleLogger ;
2021-05-07 15:53:47 +00:00
import picocli.CommandLine ;
2021-04-25 13:25:20 +00:00
2021-09-10 10:15:33 +00:00
import java.io.File ;
2021-05-07 15:53:47 +00:00
import java.net.URL ;
2021-12-05 20:17:16 +00:00
import java.net.InetAddress ;
2021-05-10 14:56:56 +00:00
import java.net.UnknownHostException ;
2021-05-07 15:53:47 +00:00
import java.util.concurrent.Callable ;
2021-04-25 13:25:20 +00:00
2021-05-07 15:53:47 +00:00
@CommandLine.Command ( name = " sysmon-client " , mixinStandardHelpOptions = true )
public class Application implements Callable < Integer > {
2021-05-01 12:44:55 +00:00
2021-05-08 16:55:37 +00:00
@CommandLine.Option ( names = { " -s " , " --server-url " } , description = " Server URL (default: ${DEFAULT-VALUE}). " , defaultValue = " http://127.0.0.1:9925/metrics " , paramLabel = " <url> " )
2021-05-07 15:53:47 +00:00
private URL serverUrl ;
2021-05-01 12:44:55 +00:00
2021-05-11 13:37:23 +00:00
@CommandLine.Option ( names = { " -n " , " --hostname " } , description = " Client hostname (default: <hostname>). " , paramLabel = " <name> " )
2021-05-08 16:55:37 +00:00
private String hostname ;
2021-05-13 17:28:31 +00:00
@CommandLine.Option ( names = { " -p " , " --plugin-dir " } , description = " Plugin jar path (default: ${DEFAULT-VALUE}). " , paramLabel = " <path> " , defaultValue = " /opt/sysmon/plugins " )
private String pluginPath ;
2021-04-25 13:25:20 +00:00
2021-09-10 10:15:33 +00:00
@CommandLine.Option ( names = { " -c " , " --conf " } , description = " Configuration file [default: '/etc/sysmon-client.toml']. " , paramLabel = " <file> " , defaultValue = " /etc/sysmon-client.toml " )
private File configurationFile ;
2022-03-01 19:07:43 +00:00
//@CommandLine.Option(names = { "-d", "--debug" }, description = "Enable debugging (default: ${DEFAULT_VALUE}).")
//private boolean enableDebug = false;
2021-09-11 19:55:48 +00:00
2022-03-01 19:07:43 +00:00
@CommandLine.Option ( names = { " -d " , " --debug " } , description = " Enable debugging (default: ${DEFAULT_VALUE}). " )
private boolean [ ] enableDebug = new boolean [ 0 ] ;
2021-09-11 19:55:48 +00:00
2021-05-07 15:53:47 +00:00
public static void main ( String . . . args ) {
int exitCode = new CommandLine ( new Application ( ) ) . execute ( args ) ;
System . exit ( exitCode ) ;
2021-05-04 12:23:34 +00:00
}
2021-04-25 13:25:20 +00:00
2021-05-07 15:53:47 +00:00
@Override
2021-12-05 20:17:16 +00:00
public Integer call ( ) {
2021-05-01 12:44:55 +00:00
2021-09-17 09:53:57 +00:00
String sysmonDebug = System . getProperty ( " sysmon.debug " ) ;
2022-03-01 19:07:43 +00:00
if ( sysmonDebug ! = null ) {
2021-09-13 15:13:58 +00:00
System . setProperty ( SimpleLogger . DEFAULT_LOG_LEVEL_KEY , " INFO " ) ;
2021-09-11 19:55:48 +00:00
}
2022-03-01 19:07:43 +00:00
switch ( enableDebug . length ) {
case 1 :
System . setProperty ( SimpleLogger . DEFAULT_LOG_LEVEL_KEY , " INFO " ) ;
break ;
case 2 :
System . setProperty ( SimpleLogger . DEFAULT_LOG_LEVEL_KEY , " DEBUG " ) ;
break ;
case 3 :
System . setProperty ( SimpleLogger . DEFAULT_LOG_LEVEL_KEY , " TRACE " ) ;
break ;
}
2021-09-17 09:53:57 +00:00
String sysmonCfgFile = System . getProperty ( " sysmon.cfgFile " ) ;
if ( sysmonCfgFile ! = null ) {
configurationFile = new File ( sysmonCfgFile ) ;
}
String sysmonPluginsDir = System . getProperty ( " sysmon.pluginsDir " ) ;
if ( sysmonPluginsDir ! = null ) {
pluginPath = sysmonPluginsDir ;
}
2021-09-13 15:13:58 +00:00
2021-05-08 16:55:37 +00:00
if ( hostname = = null | | hostname . isEmpty ( ) ) {
2021-05-10 14:56:56 +00:00
try {
hostname = InetAddress . getLocalHost ( ) . getHostName ( ) ;
} catch ( UnknownHostException e ) {
2021-12-05 20:17:16 +00:00
System . err . println ( " Could not detect hostname. Use the '-n' or '--hostname' option to specify. " ) ;
2021-06-14 15:57:18 +00:00
return - 1 ;
2021-05-10 14:56:56 +00:00
}
2021-05-08 16:55:37 +00:00
}
2021-09-17 09:53:57 +00:00
Configuration configuration = new Configuration ( ) ;
2021-09-10 10:15:33 +00:00
if ( configurationFile . exists ( ) ) {
try {
configuration . parse ( configurationFile . toPath ( ) ) ;
} catch ( Exception e ) {
2023-01-22 10:07:15 +00:00
System . err . println ( " Could not parse configuration file: " + e . getMessage ( ) ) ;
2021-09-10 10:15:33 +00:00
return 1 ;
}
}
2021-05-07 15:53:47 +00:00
Main main = new Main ( ) ;
2021-05-13 17:28:31 +00:00
main . bind ( " pluginPath " , pluginPath ) ;
2021-05-07 15:53:47 +00:00
main . bind ( " myServerUrl " , serverUrl . toString ( ) ) ;
2021-05-08 16:55:37 +00:00
main . bind ( " myHostname " , hostname ) ;
2021-09-10 10:15:33 +00:00
main . bind ( " configuration " , configuration ) ;
2021-05-07 15:53:47 +00:00
main . configure ( ) . addRoutesBuilder ( ClientRouteBuilder . class ) ;
2021-05-01 12:44:55 +00:00
2021-05-07 15:53:47 +00:00
// now keep the application running until the JVM is terminated (ctrl + c or sigterm)
try {
main . run ( ) ;
} catch ( Exception e ) {
System . err . println ( e . getMessage ( ) ) ;
2021-09-10 10:15:33 +00:00
return 1 ;
2021-04-25 13:25:20 +00:00
}
2021-05-07 15:53:47 +00:00
return 0 ;
2021-05-01 12:44:55 +00:00
2021-04-25 13:25:20 +00:00
}
2021-05-01 12:44:55 +00:00
2021-04-25 13:25:20 +00:00
}
2021-05-07 15:53:47 +00:00