diff --git a/ServMon/javasysmon-0.3.5.jar b/ServMon/javasysmon-0.3.5.jar new file mode 100644 index 0000000..f18715b Binary files /dev/null and b/ServMon/javasysmon-0.3.5.jar differ diff --git a/ServMon/src/CpuLoad.java b/ServMon/src/CpuLoad.java new file mode 100644 index 0000000..43d3163 --- /dev/null +++ b/ServMon/src/CpuLoad.java @@ -0,0 +1,90 @@ +import com.jezhumble.javasysmon.CpuTimes; +import com.jezhumble.javasysmon.JavaSysMon; + + +public class CpuLoad { + JavaSysMon monitor = new JavaSysMon(); + Integer seccounter; + String path; + CpuTimes Cpu1sec; + CpuTimes Cpu1min; + CpuTimes Cpu5min; + CpuTimes Cpu10min; + + float Avg1min; + float Avg5min; + float Avg10min; + + public CpuLoad(String path) { + this.path = path; + seccounter = 0; + Avg1min = -1; + Avg5min = -1; + Avg10min = -1; + } + + public float CalcAvg1sec() { + + CpuTimes Avg1sec = monitor.cpuTimes(); + + try { + Thread.sleep(1000); + } catch (InterruptedException e) { + e.printStackTrace(); + } + + if(seccounter == 0) { + Cpu1min = monitor.cpuTimes(); + Cpu5min = monitor.cpuTimes(); + Cpu10min = monitor.cpuTimes(); + seccounter++; + return (monitor.cpuTimes().getCpuUsage(Avg1sec) * 100); + } + + if((seccounter % 60) == 0) { + Avg1min = monitor.cpuTimes().getCpuUsage(Cpu1min) * 100; + Cpu1min = monitor.cpuTimes(); + + // calculate Uptime + UpTime ut = new UpTime(); + + // calculate Ramload + RamLoad rm = new RamLoad(); + + // Write new data to file + Writer mywriter = new Writer(path, (int)Avg1min, (int)Avg5min, (int)Avg10min, ut.getUpTime(), rm.getFreemem(), rm.getTotmem()); + } + + if((seccounter % (60 * 5)) == 0) { + Avg5min = monitor.cpuTimes().getCpuUsage(Cpu5min) * 100; + Cpu5min = monitor.cpuTimes(); + } + + if(seccounter == 60 * 10) { + Avg10min = monitor.cpuTimes().getCpuUsage(Cpu10min) * 100; + Cpu10min = monitor.cpuTimes(); + seccounter = 0; + return (monitor.cpuTimes().getCpuUsage(Avg1sec) * 100); + } + + seccounter++; + return (monitor.cpuTimes().getCpuUsage(Avg1sec) * 100); + + } + + + public float getAvg1min() { + return Avg1min; + } + + public float getAvg5min() { + return Avg5min; + } + + public float getAvg10min() { + return Avg10min; + } + + + +} diff --git a/ServMon/src/RamLoad.java b/ServMon/src/RamLoad.java new file mode 100644 index 0000000..236f5c4 --- /dev/null +++ b/ServMon/src/RamLoad.java @@ -0,0 +1,33 @@ +import com.jezhumble.javasysmon.CpuTimes; +import com.jezhumble.javasysmon.JavaSysMon; +import com.jezhumble.javasysmon.MemoryStats; + + +public class RamLoad { + JavaSysMon monitor = new JavaSysMon(); + long freemem; + long totmem; + + + public RamLoad() { + + MemoryStats mem = monitor.physical(); + + freemem = mem.getFreeBytes() / 1000; + totmem = mem.getTotalBytes() / 1000; + + } + + + public long getFreemem() { + return freemem; + } + + + + public long getTotmem() { + return totmem; + } + + +} diff --git a/ServMon/src/ServMon.java b/ServMon/src/ServMon.java new file mode 100644 index 0000000..d53d5a8 --- /dev/null +++ b/ServMon/src/ServMon.java @@ -0,0 +1,54 @@ +import java.util.Scanner; +public class ServMon { + + public static void main(String[] args) { + + String path; + + System.out.println("--- Welcome to ServMon ---"); + System.out.println("To start, please enter absolute path to Monyt-file:"); + Scanner input = new Scanner(System.in); + path = input.next(); + input.close(); + + CpuLoad cl = new CpuLoad(path); + + while(1 == 1) { + + ClearConsole(); // Does not work on every Window-Machine + + System.out.println("Average 1sec: " + (int)cl.CalcAvg1sec() + " %"); + System.out.println("Average 1min: " + (int)cl.getAvg1min() + " %"); + System.out.println("Average 5min: " + (int)cl.getAvg5min() + " %"); + System.out.println("Average 10min: " + (int)cl.getAvg10min() + " %"); + System.out.println("-------------------- " + cl.seccounter + " ---"); + + } + + } + + public static void ClearConsole() { + + try + { + final String os = System.getProperty("os.name"); + + if (os.contains("Windows")) + { + Runtime.getRuntime().exec("cls"); + } + else + { + Runtime.getRuntime().exec("clear"); + } + } + catch (final Exception e) + { + // Handle any exceptions. + } + } + + +} + + diff --git a/ServMon/src/UpTime.java b/ServMon/src/UpTime.java new file mode 100644 index 0000000..51821f6 --- /dev/null +++ b/ServMon/src/UpTime.java @@ -0,0 +1,14 @@ +import com.jezhumble.javasysmon.JavaSysMon; + + +public class UpTime { + + public long getUpTime() { + + JavaSysMon monitor = new JavaSysMon(); + + long uptime = monitor.uptimeInSeconds(); + return uptime; + + } +} diff --git a/ServMon/src/Writer.java b/ServMon/src/Writer.java new file mode 100644 index 0000000..89140d3 --- /dev/null +++ b/ServMon/src/Writer.java @@ -0,0 +1,69 @@ + +import java.io.BufferedWriter; +import java.io.File; +import java.io.FileWriter; + + +public class Writer { + + public Writer(String path, Integer Avg1min, Integer Avg5min, Integer Avg15min, long uptime, long freemem, long totmem) { + + BufferedWriter writer = null; + try { + + File file = new File(path); + + // This will output the full path where the file will be written to... + System.out.println(file.getCanonicalPath()); + + writer = new BufferedWriter(new FileWriter(file)); + + + writer.write(" MONYT_SCRIPT_VERSION," + + "'distro' => 'not available yet'," + + "'kernel' => 'not available yet'," + + "'cpu' => 'not available yet'," + + "'cores' => 1 );"); + writer.newLine(); + writer.write("$sDistroName = '';"); + writer.newLine(); + writer.write("$sDistroVer = '';"); + writer.newLine(); + writer.write("die( json_encode($aCheck) );"); + writer.newLine(); + writer.write("}"); + writer.newLine(); + writer.write("die (\"{\\\"monyt\\\":\\\"1.0.1\\\",\\\"uptime\\\":\\\"" + uptime + " 00.00\\\",\\\"load\\\":\\\"" + Avg1min + ","+ Avg5min + "," + Avg15min + "\\\",\\\"total_memory\\\":\\\"" + totmem + "\\\",\\\"free_memory\\\":\\\"" + freemem + "\\\",\\\"hd\\\":[],\\\"net_tx\\\":0,\\\"net_rx\\\":0}\");"); + writer.newLine(); + writer.write("?>"); + + + } catch (Exception e) { + e.printStackTrace(); + } finally { + try { + + writer.close(); + } catch (Exception e) { + } + } + + + } +} \ No newline at end of file