Skip to content
This repository has been archived by the owner on Jan 11, 2020. It is now read-only.

Documentation

Nick Miyake edited this page Dec 6, 2016 · 3 revisions

Using as standalone daemon

  1. Unzip zip file
  2. java -jar ptoss-sysmon/ptoss-sysmon.jar
  3. Connect to the daemon using your favorite JMX querying tool. Here are instructions on [Using jconsole to read data from the Sysmon Daemon](Using jconsole to read data from the Sysmon Daemon)

Using as a library in your project

SimpleSysmonExample illustrates using Sysmon inside another program:

    public class SimpleSysmonExample {

        public static void main(String[] args) throws Exception {

            // empty config picks up the defaults
            SysmonDaemon daemon = new SysmonDaemon(null);
   
            // daemon is now running
            final MBeanServer server = ManagementFactory.getPlatformMBeanServer();
            ObjectName allSysmonObjectsPattern = new ObjectName(SystemMonitor.DEFAULT_JMX_BEAN_PATH + ".*");
   
            // take data every two seconds, ten times
            for (int i = 0; i < 10; i++){
                Thread.sleep(2000);
                String timestamp = getTimestamp();
                System.out.println("------------------- START " + timestamp + " -------------------");
                Set<ObjectName> sysmonObjects = server.queryNames(allSysmonObjectsPattern, null);
                for(ObjectName mbean : sysmonObjects) {
                    JMXUtils.prettyPrintMbean(mbean);
                }
                System.out.println("-------------------- END " + timestamp + " --------------------\n\n");
            }
   
            daemon.shutdown();
        }


        static String getTimestamp(){
            DateFormat df = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ssZ");
            return df.format(new Date());
        }

    }

[Sample output](SimpleSysmonExample sample output) from the above example.

Using just a single Monitor inside your program

IndividualMonitorExample illustrates using just a single monitor rather than the entire SysmonDaemon as an embedded component inside a larger program:

 public class IndividualMonitorExample {
 
 	/**
 	 * Path to JMX bean where data will be published.
 	 */
 	static final String JMX_BEAN_PATH = LinuxMonitor.DEFAULT_JMX_BEAN_PATH + 
 										LinuxLoadAverageJMXWrapper.OBJECT_NAME;
 
 	public static void main(String[] args) throws Exception {
 		
 		SysmonDaemon.configureDefaultLogging();
 		
 		// configure the monitor
 		Properties config = new Properties();
 		config.setProperty(LinuxLoadAverageJMXWrapper.CONFIG_KEY_UPTIME_PERIOD, "1");
 		
 		// instantiate
 		Monitor monitor = new LinuxLoadAverageJMXWrapper(config);
 		
 		// fire up the monitor
 		monitor.startMonitoring();
 		
 		// wait two periods for startup and reading
 		Thread.sleep(2000);
 		
 		// read data
 		for(int i = 0; i < 10; i++){
 			Thread.sleep(1000);
 			String timestamp = getTimestamp();
 			System.out.println("------------------- START " + timestamp + " -------------------");
 			JMXUtils.prettyPrintMbean(new ObjectName(JMX_BEAN_PATH));
 			System.out.println();
 			System.out.println("-------------------- END " + timestamp + " --------------------\n\n");
 		}
 
 		System.out.println("Took ten samples across 10 seconds.");
 		
 		// shut it down
 		System.out.println("Shutting down monitor");
 		long shutdownStart = System.currentTimeMillis();
 		monitor.stopMonitoring();
 		long shutdownDuration = System.currentTimeMillis() - shutdownStart;
 		System.out.println("Shutdown completed in " + shutdownDuration + "ms");
 	}
 	
 	static String getTimestamp(){
 		DateFormat df = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ssZ");
 		return df.format(new Date());
 	}
 
 }

[Sample output](IndividualMonitorExample sample output) from the above example.

Config files

Sysmon uses a Properties object for configuration. All config values start with the prefix sysmon with all other values being ignored, so it's entirely possible to embed your sysmon configuration inside a larger config file.

Passed on the command line

The daemon takes a single argument: the config file to use. If you do not specify one, it will start up with a sane set of defaults. Look in the API docs for each monitor to see what config keys it supports.

Via System Property

You can set the Java System Property sysmon.configPath, either via the command line or at runtime using System.setProperty(). The value should be the filesystem path to the config file you'd like to use to configure Sysmon.

Passed to constructor

When using it embedded in another program, you'll may manage the loading of the Properties object that contains the config information and pass it to the SysmonDaemon constructor.