Class OSSystemInfo<INFO>

java.lang.Object
com.iizix.os.OSSystemInfo<INFO>

public class OSSystemInfo<INFO> extends Object
Package to retrieve system information from the OS.

The history gatherer (startHistory(long)) samples at the live interval (DEFAULT_LIVE_INTERVAL, one second) into a live buffer of the last DEFAULT_LIVE_SIZE samples, and every history interval's sample into the history (getHistory(long)). A listener (addLiveListener(ILiveListener)) is told of every live sample on the gatherer's timer thread, inside the gatherer's monitor: it must be quick and never throw (CC-SERVERADMIN-LIVE-DASHBOARD).

Author:
Christopher Mindus
  • Field Details

    • DEFAULT_INTERVAL

      public static final long DEFAULT_INTERVAL
      The default history gatherer interval: 5 seconds.
      See Also:
    • DEFAULT_LIVE_INTERVAL

      public static final long DEFAULT_LIVE_INTERVAL
      The default live sampling interval: 1 second (CC-SERVERADMIN-LIVE-DASHBOARD, LM-D-3).
      See Also:
    • DEFAULT_LIVE_SIZE

      public static final int DEFAULT_LIVE_SIZE
      The default size of the live buffer: 5 minutes at the default live interval (5 * 60 = 300 entries).
      See Also:
  • Constructor Details

    • OSSystemInfo

      public OSSystemInfo(IAdditonalInformation<INFO> informationProvider)
      The constructor.
      Parameters:
      informationProvider - The additional information provider to be stored with the history data, or null for none.
  • Method Details

    • getBasic

      public static OSSystemInfo.Basic getBasic()
      Gets the basic information.

      If a security manager prohibits access to the OS information, a warning message is logged in the console.

      Returns:
      The basic information, or null if the OS management bean failed loading or a security manager restricts access to the OS information.
    • getSystemLoadAverage

      public static double getSystemLoadAverage()
      Gets the average load during the last minute.
      Returns:
      The load between 0.0d to 1.0d, negative if not available.
    • getAvailableProcessors

      public static int getAvailableProcessors()
      Gets the number of processors available to the Java virtual machine. Note that this value may change during the JVM lifetime.
      Returns:
      The number of processors, never smaller than 1.
    • getDetails

      public static OSSystemInfo.Details getDetails()
      Gets detailed information of the OS and process memory/swap space usage and CPU load.
      Returns:
      The details instance or null if not supported.
    • startHistory

      public boolean startHistory(long interval)
      Starts the history gatherer. This clears the history. If the gathering information is not available, a warning message is logged, otherwise an information message is logged about the start.
      Parameters:
      interval - In milliseconds.
      Returns:
      true for OK, false if already started or history not available due to security restrictions.
    • setMaxHistorySize

      public void setMaxHistorySize(int maxSize)
      Sets the maximum number of entries in the history.
      Parameters:
      maxSize - The maximum size, between 100 and 100_000.
      Throws:
      IllegalArgumentException - If range is invalid.
    • setMaxLiveSize

      public void setMaxLiveSize(int maxLiveSize)
      Sets the maximum number of entries in the live buffer (CC-SERVERADMIN-LIVE-DASHBOARD).
      Parameters:
      maxLiveSize - The maximum size, between 10 and 100_000.
      Throws:
      IllegalArgumentException - If range is invalid.
    • setLiveInterval

      public void setLiveInterval(long liveInterval)
      Sets the live interval, the timer's tick: in effect at the next startHistory(long).
      Parameters:
      liveInterval - In milliseconds, at least 10.
      Throws:
      IllegalArgumentException - If the interval is below 10 ms.
    • getLiveInterval

      public long getLiveInterval()
      Gets the live interval.
      Returns:
      The interval in milliseconds, the timer's tick when the history is gathered.
    • getHistoryInterval

      public long getHistoryInterval()
      Gets the interval used for gathering of history entries.
      Returns:
      Interval in milliseconds, zero if never started.
    • isGatheringHistory

      public boolean isGatheringHistory()
      Checks if history is being gathered.
      Returns:
      true if timer is running, false otherwise.
    • stopHistory

      public boolean stopHistory()
      Stops the history gatherer.
      Returns:
      true for OK, false if already stopped or never started.
    • getHistory

      public OSSystemInfo.HistoryEntry<INFO>[] getHistory(long start)
      Gets the history entries gathered between a date up to the last available entry. The history entries are gathered at specific interval defined at start of history.
      Parameters:
      start - The start time for the events in milliseconds since the Epoch, or zero for all history entries.
      Returns:
      The entries, possibly empty array.
    • getLiveHistory

      public OSSystemInfo.HistoryEntry<INFO>[] getLiveHistory()
      Gets the live buffer: the last entries sampled at the live interval, oldest first.
      Returns:
      The entries, possibly empty array.
    • getLatest

      public OSSystemInfo.HistoryEntry<INFO> getLatest()
      Gets the newest entry of the live buffer.
      Returns:
      The entry, or null when the history gatherer was never started.
    • addLiveListener

      public OSSystemInfo.HistoryEntry<INFO>[] addLiveListener(OSSystemInfo.ILiveListener<INFO> listener)
      Adds a listener of the live samples and gets the live buffer in the same breath: every entry the listener is told of afterwards is newer than the last one returned. Adding the same listener twice has no effect.
      Parameters:
      listener - The listener.
      Returns:
      The live buffer, oldest first.
      Throws:
      NullPointerException - If the listener is null.
    • removeLiveListener

      public boolean removeLiveListener(OSSystemInfo.ILiveListener<INFO> listener)
      Removes a listener of the live samples.
      Parameters:
      listener - The listener.
      Returns:
      true if the listener was removed, false if it was not added.
    • hasLiveListeners

      public boolean hasLiveListeners()
      Checks if a listener of the live samples is present.
      Returns:
      true for at least one listener, false for none: nothing is told to anyone at a tick.
    • osNameOf

      public static String osNameOf(WindowsVersion windows, String javaName)
      Gets the name of the operating system for a page that shows the name and the version in two places (OSSystemInfo.Basic.osName, ServerAdmin's Dashboard). On Windows it is the name part of the description of the Windows version (WindowsVersion.getName(), e.g. "Windows 11"): the description that the server's program info, the start line of its log and the header of iiziGo's log files print. On any other operating system it is the Java VM's os.name, which those print there too.
      Parameters:
      windows - The Windows version, com.iizix.SystemConfig.windowsVersion: null when not on Windows.
      javaName - The Java VM's os.name.
      Returns:
      The name.
    • osVersionOf

      public static String osVersionOf(WindowsVersion windows, String javaVersion)
      Gets the version of the operating system for a page that shows the name and the version in two places (OSSystemInfo.Basic.osVersion). On Windows it is the version part of the description of the Windows version (WindowsVersion.getVersionText(), e.g. "25H2 (build 26200.7171)"), so that the name, a blank and the version are that description: the Java VM's os.version says "10.0" on Windows 10, on Windows 11 and on every Windows Server since 2016 alike. On any other operating system, and on a Windows whose registry could not be read (no version part), it is the Java VM's os.version.
      Parameters:
      windows - The Windows version, com.iizix.SystemConfig.windowsVersion: null when not on Windows.
      javaVersion - The Java VM's os.version.
      Returns:
      The version.