Skip to content

Instantly share code, notes, and snippets.

@shoestring
Created December 4, 2012 04:48
Show Gist options
  • Save shoestring/4200649 to your computer and use it in GitHub Desktop.
Save shoestring/4200649 to your computer and use it in GitHub Desktop.
get and display system info with java and SIGAR
import java.io.BufferedWriter;
import java.io.FileWriter;
import java.lang.reflect.Array;
import java.util.Arrays;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import org.hyperic.sigar.CpuPerc;
import org.hyperic.sigar.Mem;
import org.hyperic.sigar.Sigar;
import org.hyperic.sigar.SigarException;
import org.hyperic.sigar.Who;
public class whatsupj {
ublic static void main(String[] args) throws SigarException {
// New instance of SIGAR to do our work and get stuff
Sigar sigar = new Sigar();
// Output our collected information to an html file
StringBuilder builder = new StringBuilder();
builder.append("<!DOCTYPE html>");
builder.append("<html lang=\"en\">");
builder.append("<head><title>State of All Systems in Statistics</title></head>");
builder.append("<body><h3>" + machineName(sigar) + "</h3>" + "<b>CPU Usage:</b>" + cleanCpuPerc(sigar) +
"<br>" + "<b>Memory Usage:</b> " + "Total: " + cleanMem(sigar) + "<br>" + who(sigar));
builder.append("</body>");
builder.append("</html>");
String html = builder.toString();
writeTofile(html);
}
public static void writeTofile(String str) {
try {
Sigar sigar = new Sigar();
// Full path to /p/stat/drupal most likely needed for the .html file
FileWriter fstream = new FileWriter("/p/stat/drupal/systems_reports/" + machineName(sigar) + ".html");
BufferedWriter out = new BufferedWriter(fstream);
out.write(str);
out.close();
}
catch(Exception e) {
System.err.println("Error: " + e.getMessage());
}
}
public static String machineName(Sigar sigar) throws SigarException {
String hostName = new String();
hostName = sigar.getFQDN();
return hostName;
}
public static String cleanCpuPerc(Sigar sigar) throws SigarException {
CpuPerc cpu = sigar.getCpuPerc();
String cpu1 = cpu.toString();
// Remove unwanted information from the output of SIGAR, stripping it down to just the numbers
//cpu1 = cpu1.replaceAll("[ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz:,]","");
// Remove the first 11 characters and then
// everything after the letter "t" in "system"from the // we want to style these headings ourselves via HTML/CSS. Theres probably a better way.
int length = cpu1.length();
cpu1 = cpu1.substring(11, length);
int spaceIndex = cpu1.indexOf("t");
cpu1 = cpu1.substring(0, spaceIndex);
return cpu1;
}
public static double cleanMem(Sigar sigar) throws SigarException {
Mem mem = sigar.getMem();
String mem1 = mem.toString();
int length = mem1.indexOf("K");
mem1 = mem1.substring(5, length);
// Convert mem1 result from K to GB
double kilobytes = Double.parseDouble(mem1);
double megabytes = (kilobytes / 1024);
double gigabytes = (megabytes / 1024);
// For quick math testing to Eclipse console
System.out.println("gigabytes : " + gigabytes);
return gigabytes;
}
public static String loadAvg(Sigar sigar) throws SigarException {
double[] loadAvg = sigar.getLoadAverage();
// Convert loadAvg from an array to a string so that we can print it
return Arrays.toString(loadAvg);
}
public static StringBuilder who(Sigar sigar) throws SigarException {
Who[] who = sigar.getWhoList();
String str = new String();
StringBuilder nameList = new StringBuilder();
for(int i = 0; i < who.length; i++)
{
int a = who[i].toString().indexOf("=");
int b = who[i].toString().indexOf(",");
str = who[i].toString().substring(a+1, b);
nameList.append(str + "<br>");
}
return (nameList);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment