-
Notifications
You must be signed in to change notification settings - Fork 20
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Prototypical functionality added for #22.
- Loading branch information
Showing
4 changed files
with
299 additions
and
2 deletions.
There are no files selected for viewing
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,111 @@ | ||
package io.honeybadger.reporter.dto; | ||
|
||
import java.io.File; | ||
import java.io.IOException; | ||
import java.io.Serializable; | ||
import java.lang.management.ManagementFactory; | ||
import java.lang.management.OperatingSystemMXBean; | ||
import java.util.Scanner; | ||
|
||
/** | ||
* Class containing statistics about the host system's load average. | ||
* | ||
* @author <a href="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/dekobon">Elijah Zupancic</a> | ||
* @since 1.0.11 | ||
*/ | ||
public class Load implements Serializable { | ||
private static final long serialVersionUID = 3398000045209329774L; | ||
|
||
public final Number one; | ||
public final Number five; | ||
public final Number fifteen; | ||
|
||
public Load() { | ||
Number[] loadAverages = findLoadAverages(); | ||
this.one = loadAverages[0]; | ||
this.five = loadAverages[1]; | ||
this.fifteen = loadAverages[2]; | ||
} | ||
|
||
public Load(Number one, Number five, Number fifteen) { | ||
this.one = one; | ||
this.five = five; | ||
this.fifteen = fifteen; | ||
} | ||
|
||
/** | ||
* Attempts to find all three load values in a way that is safe for an in-process | ||
* operation. This leads to platform specific code. We attempt to avoid forking to | ||
* call external processes because this would be a bad thing to do on every error | ||
* that came into the system. | ||
* | ||
* @return an array containing all three load averages (1, 5, 15) in that order | ||
*/ | ||
static Number[] findLoadAverages() { | ||
OperatingSystemMXBean osBean = ManagementFactory.getOperatingSystemMXBean(); | ||
String os = osBean.getName(); | ||
|
||
if (os.equals("Linux")) { | ||
File loadavg = new File("/proc/loadavg"); | ||
|
||
if (loadavg.exists() && loadavg.isFile() && loadavg.canRead()) { | ||
try (Scanner scanner = new Scanner(loadavg)) { | ||
if (!scanner.hasNext()) { | ||
return defaultLoadAverages(osBean); | ||
} | ||
|
||
String line = scanner.nextLine(); | ||
String[] values = line.split(" ", 3); | ||
|
||
return new Number[]{ | ||
Double.parseDouble(values[0]), | ||
Double.parseDouble(values[1]), | ||
Double.parseDouble(values[2]) | ||
}; | ||
|
||
} catch (Exception e) { | ||
return defaultLoadAverages(osBean); | ||
} | ||
} else { | ||
return defaultLoadAverages(osBean); | ||
} | ||
|
||
} else { | ||
return defaultLoadAverages(osBean); | ||
} | ||
} | ||
|
||
static Number[] defaultLoadAverages(OperatingSystemMXBean osBean) { | ||
return new Number[] { osBean.getSystemLoadAverage(), -1, -1 }; | ||
} | ||
|
||
@Override | ||
public boolean equals(Object o) { | ||
if (this == o) return true; | ||
if (o == null || getClass() != o.getClass()) return false; | ||
|
||
Load load = (Load) o; | ||
|
||
if (one != null ? !one.equals(load.one) : load.one != null) return false; | ||
if (five != null ? !five.equals(load.five) : load.five != null) return false; | ||
return !(fifteen != null ? !fifteen.equals(load.fifteen) : load.fifteen != null); | ||
|
||
} | ||
|
||
@Override | ||
public int hashCode() { | ||
int result = one != null ? one.hashCode() : 0; | ||
result = 31 * result + (five != null ? five.hashCode() : 0); | ||
result = 31 * result + (fifteen != null ? fifteen.hashCode() : 0); | ||
return result; | ||
} | ||
|
||
@Override | ||
public String toString() { | ||
return "Load{" + | ||
"one=" + one + | ||
", five=" + five + | ||
", fifteen=" + fifteen + | ||
'}'; | ||
} | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
package io.honeybadger.reporter.dto; | ||
|
||
import java.io.Serializable; | ||
|
||
/** | ||
* Class containing the current state of memory on the running JVM. | ||
* | ||
* @author <a href="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/dekobon">Elijah Zupancic</a> | ||
* @since 1.0.11 | ||
*/ | ||
public class Memory implements Serializable { | ||
private static final long serialVersionUID = -8799953046383217102L; | ||
|
||
public final Number total; | ||
public final Number free; | ||
public final Number buffers; | ||
public final Number cached; | ||
public final Number free_total; | ||
|
||
public Memory() { | ||
final Runtime r = Runtime.getRuntime(); | ||
this.total = r.totalMemory(); | ||
this.free = r.freeMemory(); | ||
this.buffers = -1; | ||
this.cached = -1; | ||
this.free_total = r.maxMemory() - (r.totalMemory() - r.freeMemory()); | ||
} | ||
|
||
public Memory(Number total, Number free, Number buffers, Number cached, Number free_total) { | ||
this.total = total; | ||
this.free = free; | ||
this.buffers = buffers; | ||
this.cached = cached; | ||
this.free_total = free_total; | ||
} | ||
|
||
@Override | ||
public boolean equals(Object o) { | ||
if (this == o) return true; | ||
if (o == null || getClass() != o.getClass()) return false; | ||
|
||
Memory memory = (Memory) o; | ||
|
||
if (total != null ? !total.equals(memory.total) : memory.total != null) return false; | ||
if (free != null ? !free.equals(memory.free) : memory.free != null) return false; | ||
if (buffers != null ? !buffers.equals(memory.buffers) : memory.buffers != null) return false; | ||
if (cached != null ? !cached.equals(memory.cached) : memory.cached != null) return false; | ||
return !(free_total != null ? !free_total.equals(memory.free_total) : memory.free_total != null); | ||
|
||
} | ||
|
||
@Override | ||
public int hashCode() { | ||
int result = total != null ? total.hashCode() : 0; | ||
result = 31 * result + (free != null ? free.hashCode() : 0); | ||
result = 31 * result + (buffers != null ? buffers.hashCode() : 0); | ||
result = 31 * result + (cached != null ? cached.hashCode() : 0); | ||
result = 31 * result + (free_total != null ? free_total.hashCode() : 0); | ||
return result; | ||
} | ||
|
||
@Override | ||
public String toString() { | ||
return "Memory{" + | ||
"total=" + total + | ||
", free=" + free + | ||
", buffers=" + buffers + | ||
", cached=" + cached + | ||
", free_total=" + free_total + | ||
'}'; | ||
} | ||
} |
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
package io.honeybadger.reporter.dto; | ||
|
||
import java.io.Serializable; | ||
|
||
/** | ||
* Class containing the statistics about the running JVM. | ||
* | ||
* @author <a href="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/dekobon">Elijah Zupancic</a> | ||
* @since 1.0.11 | ||
*/ | ||
public class Stats implements Serializable { | ||
private static final long serialVersionUID = 4563609532018909058L; | ||
|
||
public final Memory mem; | ||
public final Load load; | ||
|
||
public Stats() { | ||
this.mem = new Memory(); | ||
this.load = new Load(); | ||
} | ||
|
||
public Stats(Memory mem, Load load) { | ||
this.mem = mem; | ||
this.load = load; | ||
} | ||
|
||
@Override | ||
public boolean equals(Object o) { | ||
if (this == o) return true; | ||
if (o == null || getClass() != o.getClass()) return false; | ||
|
||
Stats stats = (Stats) o; | ||
|
||
if (mem != null ? !mem.equals(stats.mem) : stats.mem != null) return false; | ||
return !(load != null ? !load.equals(stats.load) : stats.load != null); | ||
|
||
} | ||
|
||
@Override | ||
public int hashCode() { | ||
int result = mem != null ? mem.hashCode() : 0; | ||
result = 31 * result + (load != null ? load.hashCode() : 0); | ||
return result; | ||
} | ||
|
||
@Override | ||
public String toString() { | ||
return "Stats{" + | ||
"mem=" + mem + | ||
", load=" + load + | ||
'}'; | ||
} | ||
} |