Created
May 6, 2014 03:15
-
-
Save benjholla/81f841ff347f23db9e3b to your computer and use it in GitHub Desktop.
Grabbing system profile metrics via Java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.awt.GraphicsDevice; | |
import java.awt.GraphicsEnvironment; | |
import java.io.File; | |
import java.lang.management.ManagementFactory; | |
import java.net.InetAddress; | |
import java.net.NetworkInterface; | |
import java.util.ArrayList; | |
import java.util.Enumeration; | |
import java.util.Locale; | |
import java.util.TimeZone; | |
public class SystemProfiler { | |
public static void main(String[] args) throws Exception { | |
// system hardware | |
System.out.println("Hardware: "); | |
System.out.println("Available processors (cores): " + Runtime.getRuntime().availableProcessors()); | |
System.out.println("Free memory (bytes): " + Runtime.getRuntime().freeMemory()); | |
long maxMemory = Runtime.getRuntime().maxMemory(); | |
System.out.println("Maximum memory (bytes): " + (maxMemory == Long.MAX_VALUE ? "no limit" : maxMemory)); | |
System.out.println("Total memory available to JVM (bytes): " + Runtime.getRuntime().totalMemory()); | |
// for each filesystem root, print info | |
System.out.println("\nFile Systems: "); | |
for (File root : File.listRoots()) { | |
System.out.println("File system root: " + root.getAbsolutePath()); | |
System.out.println("Total space (bytes): " + root.getTotalSpace()); | |
System.out.println("Free space (bytes): " + root.getFreeSpace()); | |
System.out.println("Usable space (bytes): " + root.getUsableSpace()); | |
} | |
// for each screen device print | |
System.out.println("\nScreen Devices: "); | |
int screenNumber = 0; | |
for(GraphicsDevice gd : GraphicsEnvironment.getLocalGraphicsEnvironment().getScreenDevices()){ | |
int width = gd.getDisplayMode().getWidth(); | |
int height = gd.getDisplayMode().getHeight(); | |
System.out.println("Screen " + screenNumber + ": " + width + "w X " + height + "h"); | |
} | |
// print supported font families | |
System.out.println("\nFont Families: "); | |
ArrayList<String> fontFamilies = new ArrayList<String>(); | |
for(String font : GraphicsEnvironment.getLocalGraphicsEnvironment().getAvailableFontFamilyNames()){ | |
fontFamilies.add(font); | |
} | |
System.out.println("Supported Fonts: " + fontFamilies); | |
// print system properties | |
System.out.println("\nSystem Properties: "); | |
for(Object propertyKey : System.getProperties().keySet()){ | |
String propertyValue = System.getProperty(propertyKey.toString()); | |
if(propertyKey.equals("line.separator")){ | |
propertyValue = propertyValue.replaceAll("\f", "\\f").replaceAll("\r", "\\r").replaceAll("\n", "\\n"); | |
} | |
System.out.println(propertyKey.toString() + ": " + propertyValue); | |
} | |
// network interfaces | |
System.out.println("\nNetwork Interfaces: "); | |
Enumeration<NetworkInterface> e = NetworkInterface.getNetworkInterfaces(); | |
while (e.hasMoreElements()) | |
{ | |
NetworkInterface n = e.nextElement(); | |
System.out.print(n.getName()); | |
Enumeration<InetAddress> ee = n.getInetAddresses(); | |
while (ee.hasMoreElements()) { | |
InetAddress i = ee.nextElement(); | |
System.out.print(", " + i.getHostAddress()); | |
} | |
System.out.println(); | |
} | |
// hostname | |
System.out.println("\nHostname: " + InetAddress.getLocalHost().getHostName()); | |
// timezone | |
System.out.println("\nTimezone: " + TimeZone.getDefault().getDisplayName()); | |
// language | |
Locale locale = Locale.getDefault(); | |
System.out.println("\nLocale: "); | |
System.out.println("Language: " + locale.getDisplayLanguage()); | |
System.out.println("Country: " + locale.getDisplayCountry()); | |
// JVM Uptime | |
System.out.println("\nJVM Uptime: " + ManagementFactory.getRuntimeMXBean().getUptime()); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment