Skip to content

Commit

Permalink
implemented basic functions to read CPU-Load, RAM-Load and Uptime and…
Browse files Browse the repository at this point in the history
… write it to a Monyt-compatible Script based on Version 1.0.1
  • Loading branch information
Zappelphilipp committed Feb 23, 2015
0 parents commit 1520fc3
Show file tree
Hide file tree
Showing 6 changed files with 260 additions and 0 deletions.
Binary file added ServMon/javasysmon-0.3.5.jar
Binary file not shown.
90 changes: 90 additions & 0 deletions ServMon/src/CpuLoad.java
Original file line number Diff line number Diff line change
@@ -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;
}



}
33 changes: 33 additions & 0 deletions ServMon/src/RamLoad.java
Original file line number Diff line number Diff line change
@@ -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;
}


}
54 changes: 54 additions & 0 deletions ServMon/src/ServMon.java
Original file line number Diff line number Diff line change
@@ -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.
}
}


}


14 changes: 14 additions & 0 deletions ServMon/src/UpTime.java
Original file line number Diff line number Diff line change
@@ -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;

}
}
69 changes: 69 additions & 0 deletions ServMon/src/Writer.java
Original file line number Diff line number Diff line change
@@ -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("<?php");
writer.newLine();
writer.write("define(\"MONYT_SCRIPT_VERSION\", \"1.0.1\");");
writer.newLine();
writer.write("if( isset($_GET['version'] ) )");
writer.newLine();
writer.write("{");
writer.newLine();
writer.write("die( MONYT_SCRIPT_VERSION );");
writer.newLine();
writer.write("} else if( isset($_GET['check']) )");
writer.newLine();
writer.write("{");
writer.newLine();
writer.write("$aCheck = array");
writer.newLine();
writer.write("( 'monyt' => 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) {
}
}


}
}

0 comments on commit 1520fc3

Please sign in to comment.