-
Notifications
You must be signed in to change notification settings - Fork 9
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
61ac238
commit 74db9ca
Showing
5 changed files
with
130 additions
and
41 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
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
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,27 @@ | ||
package me.melijn.bot.utils | ||
|
||
import com.sun.management.OperatingSystemMXBean | ||
import java.lang.management.ManagementFactory | ||
|
||
data class JvmUsage(val totalMem: Long, val usedMem: Long, val totalJVMMem: Long, val usedJVMMem: Long) { | ||
|
||
companion object { | ||
fun current(bean: OperatingSystemMXBean): JvmUsage { | ||
val totalMem: Long | ||
val usedMem: Long | ||
if (SystemUtil.isUnix) { | ||
totalMem = SystemUtil.getTotalMBUnixRam() | ||
val available = SystemUtil.getAvailableMBUnixRam() | ||
usedMem = totalMem - available | ||
} else { | ||
totalMem = bean.totalMemorySize shr 20 | ||
usedMem = totalMem - (bean.freeSwapSpaceSize shr 20) | ||
} | ||
|
||
val totalJVMMem = ManagementFactory.getMemoryMXBean().heapMemoryUsage.max shr 20 | ||
val usedJVMMem = ManagementFactory.getMemoryMXBean().heapMemoryUsage.used shr 20 | ||
|
||
return JvmUsage(totalMem, usedMem, totalJVMMem, usedJVMMem) | ||
} | ||
} | ||
} |
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,65 @@ | ||
package me.melijn.bot.utils | ||
|
||
import java.io.File | ||
import java.util.regex.Pattern | ||
|
||
object SystemUtil { | ||
|
||
val isUnix: Boolean = os == OS.UNIX | ||
|
||
// 11105353.49 239988480.98 | ||
val linuxUptimePattern: Pattern = Pattern.compile("([0-9]+)(?:\\.[0-9]+)? [0-9]+(?:\\.[0-9]+)?") | ||
|
||
val linuxMemTotalPattern: Pattern = Pattern.compile("MemTotal:\\s+([0-9]+) kB") | ||
val linuxMemAvailPattern: Pattern = Pattern.compile("MemAvailable:\\s+([0-9]+) kB") | ||
|
||
val os: OS | ||
get() { | ||
val s = System.getProperty("os.name").lowercase() | ||
return when { | ||
s.contains("mac") || s.contains("nix") || s.contains("nux") || s.contains("aix") -> OS.UNIX | ||
else -> OS.OTHER | ||
} | ||
} | ||
|
||
enum class OS { | ||
UNIX, OTHER | ||
} | ||
|
||
fun getSystemUptime(): Long { | ||
return try { | ||
when (os) { | ||
OS.UNIX -> getUnixUptime() | ||
OS.OTHER -> -1 | ||
} | ||
} catch (e: Exception) { | ||
-1 | ||
} | ||
} | ||
|
||
fun getUnixUptime(): Long { | ||
val uptimeStr = File("/proc/uptime").readText() | ||
val matcher = linuxUptimePattern.matcher(uptimeStr) | ||
|
||
if (!matcher.find()) return -1 // Extract ints out of groups | ||
return matcher.group(1).toLong() | ||
} | ||
|
||
fun getTotalMBUnixRam(): Long = File("/proc/meminfo").readLines().firstNotNullOf { line -> | ||
val matcher = linuxMemTotalPattern.matcher(line) | ||
|
||
if (!matcher.find()) return@firstNotNullOf null | ||
val group = matcher.group(1) | ||
group.toLong() / 1024 | ||
} | ||
|
||
|
||
fun getAvailableMBUnixRam(): Long = File("/proc/meminfo").readLines().firstNotNullOf { line -> | ||
val matcher = linuxMemAvailPattern.matcher(line) | ||
|
||
if (!matcher.find()) return@firstNotNullOf null | ||
val group = matcher.group(1) | ||
group.toLong() / 1024 | ||
} | ||
|
||
} |