init repo
This commit is contained in:
18
.editorconfig
Normal file
18
.editorconfig
Normal file
@@ -0,0 +1,18 @@
|
|||||||
|
root = true
|
||||||
|
|
||||||
|
[*]
|
||||||
|
end_of_line = lf
|
||||||
|
insert_final_newline = true
|
||||||
|
|
||||||
|
[*.bat]
|
||||||
|
end_of_line = crlf
|
||||||
|
|
||||||
|
[*.{java,gradle,groovy}]
|
||||||
|
charset = utf-8
|
||||||
|
indent_style = space
|
||||||
|
indent_size = 4
|
||||||
|
|
||||||
|
[*.{yml,yaml}]
|
||||||
|
charset = utf-8
|
||||||
|
indent_style = space
|
||||||
|
indent_size = 2
|
||||||
3
.gitattributes
vendored
Normal file
3
.gitattributes
vendored
Normal file
@@ -0,0 +1,3 @@
|
|||||||
|
gradlew text eol=lf
|
||||||
|
gradlew.bat -text
|
||||||
|
gradle/wrapper/gradle-wrapper.properties text eol=lf
|
||||||
12
.gitignore
vendored
Normal file
12
.gitignore
vendored
Normal file
@@ -0,0 +1,12 @@
|
|||||||
|
# IDEA #
|
||||||
|
.idea/
|
||||||
|
out/
|
||||||
|
*.iml
|
||||||
|
*.ipr
|
||||||
|
*.iws
|
||||||
|
*.ids
|
||||||
|
|
||||||
|
# GRADLE #
|
||||||
|
.gradle/
|
||||||
|
build/
|
||||||
|
publish.gradle
|
||||||
40
bukkit/build.gradle
Normal file
40
bukkit/build.gradle
Normal file
@@ -0,0 +1,40 @@
|
|||||||
|
plugins {
|
||||||
|
id 'java'
|
||||||
|
id 'com.github.johnrengelman.shadow' version '7.0.0'
|
||||||
|
}
|
||||||
|
|
||||||
|
group 'ru.dmitriymx'
|
||||||
|
version '1.0-SNAPSHOT'
|
||||||
|
jar.archiveBaseName.set(project.name)
|
||||||
|
|
||||||
|
compileJava {
|
||||||
|
sourceCompatibility = targetCompatibility = JavaVersion.VERSION_1_8
|
||||||
|
options.encoding = 'UTF-8'
|
||||||
|
}
|
||||||
|
|
||||||
|
repositories {
|
||||||
|
mavenCentral()
|
||||||
|
maven { url 'https://hub.spigotmc.org/nexus/content/repositories/snapshots' }
|
||||||
|
}
|
||||||
|
|
||||||
|
dependencies {
|
||||||
|
implementation(project(':core'))
|
||||||
|
|
||||||
|
annotationProcessor('org.projectlombok:lombok:1.18.20')
|
||||||
|
compileOnly('org.projectlombok:lombok:1.18.20')
|
||||||
|
|
||||||
|
compileOnly('org.spigotmc:spigot-api:1.12-R0.1-SNAPSHOT') {
|
||||||
|
exclude(module: 'bungeecord-chat')
|
||||||
|
exclude(module: 'commons-lang')
|
||||||
|
exclude(module: 'gson')
|
||||||
|
exclude(module: 'guava')
|
||||||
|
exclude(module: 'json-simple')
|
||||||
|
exclude(module: 'snakeyaml')
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
shadowJar {
|
||||||
|
archiveBaseName.set('mc-mectics-bukkit')
|
||||||
|
archiveVersion.set(project.version.toString())
|
||||||
|
archiveClassifier.set('')
|
||||||
|
}
|
||||||
@@ -0,0 +1,44 @@
|
|||||||
|
package ru.dmitriymx.minecraft.metrics.bukkit;
|
||||||
|
|
||||||
|
import com.typesafe.config.Config;
|
||||||
|
import com.typesafe.config.ConfigFactory;
|
||||||
|
import lombok.RequiredArgsConstructor;
|
||||||
|
import lombok.SneakyThrows;
|
||||||
|
import org.bukkit.plugin.java.JavaPlugin;
|
||||||
|
import ru.dmitriymx.minecraft.config.ConfigProvider;
|
||||||
|
|
||||||
|
import java.io.BufferedReader;
|
||||||
|
import java.io.InputStream;
|
||||||
|
import java.io.InputStreamReader;
|
||||||
|
import java.io.Reader;
|
||||||
|
import java.nio.file.Files;
|
||||||
|
import java.nio.file.Path;
|
||||||
|
|
||||||
|
@RequiredArgsConstructor
|
||||||
|
public class BukkitConfigProvider implements ConfigProvider {
|
||||||
|
|
||||||
|
private final JavaPlugin plugin;
|
||||||
|
|
||||||
|
@Override
|
||||||
|
@SneakyThrows
|
||||||
|
public Config get() {
|
||||||
|
InputStream inputStream = plugin.getClass().getResourceAsStream("/assets/config-default.conf");
|
||||||
|
if (inputStream == null) {
|
||||||
|
throw new RuntimeException("Where is 'config-default.conf'!!??");
|
||||||
|
}
|
||||||
|
|
||||||
|
Reader reader = new InputStreamReader(inputStream);
|
||||||
|
Config defaultConfig = ConfigFactory.parseReader(reader);
|
||||||
|
Config config;
|
||||||
|
Path userConfigPath = plugin.getDataFolder().toPath().resolve("config.conf");
|
||||||
|
if (Files.exists(userConfigPath)) {
|
||||||
|
BufferedReader reader1 = Files.newBufferedReader(userConfigPath);
|
||||||
|
Config userConfig = ConfigFactory.parseReader(reader1);
|
||||||
|
config = userConfig.withFallback(defaultConfig).resolve();
|
||||||
|
} else {
|
||||||
|
config = defaultConfig.resolve();
|
||||||
|
}
|
||||||
|
|
||||||
|
return config;
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,32 @@
|
|||||||
|
package ru.dmitriymx.minecraft.metrics.bukkit;
|
||||||
|
|
||||||
|
import lombok.RequiredArgsConstructor;
|
||||||
|
import lombok.SneakyThrows;
|
||||||
|
import org.bukkit.Server;
|
||||||
|
import ru.dmitriymx.minecraft.metrics.MinecraftInfoProvider;
|
||||||
|
|
||||||
|
import java.lang.reflect.Field;
|
||||||
|
|
||||||
|
@RequiredArgsConstructor
|
||||||
|
public class BukkitInfoProvider implements MinecraftInfoProvider {
|
||||||
|
|
||||||
|
private final Server bukkitServer;
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public int playersOnline() {
|
||||||
|
return bukkitServer.getOnlinePlayers().size();
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
@SneakyThrows
|
||||||
|
public double tps() {
|
||||||
|
Field consoleField = bukkitServer.getClass().getDeclaredField("console");
|
||||||
|
consoleField.setAccessible(true);
|
||||||
|
Object minecraftServer = consoleField.get(bukkitServer);
|
||||||
|
Field recentTps = minecraftServer.getClass().getSuperclass().getDeclaredField("recentTps");
|
||||||
|
recentTps.setAccessible(true);
|
||||||
|
|
||||||
|
double tps = ((double[]) recentTps.get(minecraftServer))[0];
|
||||||
|
return Math.min(tps, 20.0d);
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,43 @@
|
|||||||
|
package ru.dmitriymx.minecraft.metrics.bukkit;
|
||||||
|
|
||||||
|
import lombok.RequiredArgsConstructor;
|
||||||
|
import ru.dmitriymx.minecraft.logger.LoggerAdapter;
|
||||||
|
|
||||||
|
import java.util.logging.Level;
|
||||||
|
import java.util.logging.Logger;
|
||||||
|
|
||||||
|
@RequiredArgsConstructor
|
||||||
|
public class BukkitLogger extends LoggerAdapter {
|
||||||
|
|
||||||
|
private final Logger originallLogger;
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void debug(String message) {
|
||||||
|
originallLogger.log(Level.CONFIG, message);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void debug(String message, Throwable throwable) {
|
||||||
|
originallLogger.log(Level.CONFIG, message, throwable);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void info(String message) {
|
||||||
|
originallLogger.log(Level.INFO, message);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void warn(String message) {
|
||||||
|
originallLogger.log(Level.WARNING, message);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void error(String message) {
|
||||||
|
originallLogger.log(Level.SEVERE, message);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void error(String message, Throwable throwable) {
|
||||||
|
originallLogger.log(Level.SEVERE, message, throwable);
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,51 @@
|
|||||||
|
package ru.dmitriymx.minecraft.metrics.bukkit;
|
||||||
|
|
||||||
|
import com.typesafe.config.Config;
|
||||||
|
import lombok.SneakyThrows;
|
||||||
|
import org.bukkit.plugin.java.JavaPlugin;
|
||||||
|
import ru.dmitriymx.minecraft.config.ConfigProvider;
|
||||||
|
import ru.dmitriymx.minecraft.metrics.MetricsServer;
|
||||||
|
|
||||||
|
import java.nio.file.Files;
|
||||||
|
import java.nio.file.Path;
|
||||||
|
|
||||||
|
@SuppressWarnings("unused")
|
||||||
|
public class MetricsPlugin extends JavaPlugin {
|
||||||
|
|
||||||
|
private MetricsServer metricsServer;
|
||||||
|
|
||||||
|
@Override
|
||||||
|
@SneakyThrows
|
||||||
|
public void onEnable() {
|
||||||
|
Path pluginDataDirPath = getDataFolder().toPath();
|
||||||
|
if (Files.notExists(pluginDataDirPath)) {
|
||||||
|
getLogger().info("Create data dir: " + pluginDataDirPath);
|
||||||
|
Files.createDirectories(pluginDataDirPath);
|
||||||
|
}
|
||||||
|
|
||||||
|
ConfigProvider configProvider = new BukkitConfigProvider(this);
|
||||||
|
Config config = configProvider.get();
|
||||||
|
|
||||||
|
BukkitLogger logger = new BukkitLogger(getLogger());
|
||||||
|
metricsServer = new MetricsServer(
|
||||||
|
config.getString("server.host"),
|
||||||
|
config.getInt("server.port"),
|
||||||
|
config.getString("server.endpoint"),
|
||||||
|
logger,
|
||||||
|
new BukkitInfoProvider(getServer())
|
||||||
|
);
|
||||||
|
|
||||||
|
//noinspection HttpUrlsUsage
|
||||||
|
logger.info("Start metrics server: http://{}:{}{}",
|
||||||
|
config.getString("server.host"),
|
||||||
|
config.getInt("server.port"),
|
||||||
|
config.getString("server.endpoint"));
|
||||||
|
metricsServer.start();
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
@SneakyThrows
|
||||||
|
public void onDisable() {
|
||||||
|
metricsServer.stop();
|
||||||
|
}
|
||||||
|
}
|
||||||
5
bukkit/src/main/resources/plugin.yml
Normal file
5
bukkit/src/main/resources/plugin.yml
Normal file
@@ -0,0 +1,5 @@
|
|||||||
|
name: MinecraftMetrics
|
||||||
|
version: 1.0
|
||||||
|
description: 'Minecraft Prometheus Exporter'
|
||||||
|
author: DmitriyMX
|
||||||
|
main: ru.dmitriymx.minecraft.metrics.bukkit.MetricsPlugin
|
||||||
29
core/build.gradle
Normal file
29
core/build.gradle
Normal file
@@ -0,0 +1,29 @@
|
|||||||
|
plugins {
|
||||||
|
id 'java'
|
||||||
|
id 'java-library'
|
||||||
|
}
|
||||||
|
|
||||||
|
group 'ru.dmitriymx'
|
||||||
|
version '1.0-SNAPSHOT'
|
||||||
|
jar.archiveBaseName.set(project.name)
|
||||||
|
|
||||||
|
compileJava {
|
||||||
|
sourceCompatibility = targetCompatibility = JavaVersion.VERSION_1_8
|
||||||
|
options.encoding = 'UTF-8'
|
||||||
|
}
|
||||||
|
|
||||||
|
repositories {
|
||||||
|
mavenCentral()
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
dependencies {
|
||||||
|
annotationProcessor('org.projectlombok:lombok:1.18.20')
|
||||||
|
compileOnly('org.projectlombok:lombok:1.18.20')
|
||||||
|
|
||||||
|
compileOnly('io.netty:netty-transport-native-epoll:4.1.24.Final')
|
||||||
|
compileOnly('io.netty:netty-codec-http:4.1.24.Final')
|
||||||
|
// compileOnly('org.slf4j:slf4j-api:1.7.32')
|
||||||
|
|
||||||
|
api('com.typesafe:config:1.4.1')
|
||||||
|
}
|
||||||
@@ -0,0 +1,8 @@
|
|||||||
|
package ru.dmitriymx.minecraft.config;
|
||||||
|
|
||||||
|
import com.typesafe.config.Config;
|
||||||
|
|
||||||
|
public interface ConfigProvider {
|
||||||
|
|
||||||
|
Config get();
|
||||||
|
}
|
||||||
@@ -0,0 +1,19 @@
|
|||||||
|
package ru.dmitriymx.minecraft.logger;
|
||||||
|
|
||||||
|
import lombok.Getter;
|
||||||
|
import lombok.RequiredArgsConstructor;
|
||||||
|
import lombok.ToString;
|
||||||
|
|
||||||
|
@RequiredArgsConstructor
|
||||||
|
@Getter
|
||||||
|
@ToString
|
||||||
|
public class FormattingPair {
|
||||||
|
|
||||||
|
private final String message;
|
||||||
|
private final Throwable throwable;
|
||||||
|
|
||||||
|
public FormattingPair(String message) {
|
||||||
|
this.message = message;
|
||||||
|
this.throwable = null;
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,39 @@
|
|||||||
|
package ru.dmitriymx.minecraft.logger;
|
||||||
|
|
||||||
|
import ru.dmitriymx.minecraft.utils.StringFormatter;
|
||||||
|
|
||||||
|
public abstract class LoggerAdapter {
|
||||||
|
|
||||||
|
public abstract void debug(String message);
|
||||||
|
public abstract void debug(String message, Throwable throwable);
|
||||||
|
public abstract void info(String message);
|
||||||
|
public abstract void warn(String message);
|
||||||
|
public abstract void error(String message);
|
||||||
|
public abstract void error(String message, Throwable throwable);
|
||||||
|
|
||||||
|
public void debug(String pattern, Object... objects) {
|
||||||
|
FormattingPair formattingPair = LoggerFormatter.arrayFormat(pattern, objects);
|
||||||
|
if (formattingPair.getThrowable() != null) {
|
||||||
|
debug(formattingPair.getMessage(), formattingPair.getThrowable());
|
||||||
|
} else {
|
||||||
|
debug(formattingPair.getMessage());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public void info(String pattern, Object... objects) {
|
||||||
|
info(StringFormatter.arrayFormat(pattern, objects));
|
||||||
|
}
|
||||||
|
|
||||||
|
public void warn(String pattern, Object... objects) {
|
||||||
|
warn(StringFormatter.arrayFormat(pattern, objects));
|
||||||
|
}
|
||||||
|
|
||||||
|
public void error(String pattern, Object... objects) {
|
||||||
|
FormattingPair formattingPair = LoggerFormatter.arrayFormat(pattern, objects);
|
||||||
|
if (formattingPair.getThrowable() != null) {
|
||||||
|
error(formattingPair.getMessage(), formattingPair.getThrowable());
|
||||||
|
} else {
|
||||||
|
error(formattingPair.getMessage());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,61 @@
|
|||||||
|
package ru.dmitriymx.minecraft.logger;
|
||||||
|
|
||||||
|
import ru.dmitriymx.minecraft.utils.StringFormatter;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Copy-Paste from org.slf4j.helpers.MessageFormatter
|
||||||
|
*/
|
||||||
|
public final class LoggerFormatter {
|
||||||
|
|
||||||
|
public static FormattingPair arrayFormat(String messagePattern, Object[] argArray) {
|
||||||
|
Object[] args;
|
||||||
|
Throwable throwableCandidate = getThrowableCandidate(argArray);
|
||||||
|
if (throwableCandidate != null) {
|
||||||
|
args = trimmedCopy(argArray);
|
||||||
|
} else {
|
||||||
|
args = argArray;
|
||||||
|
}
|
||||||
|
|
||||||
|
return arrayFormat(messagePattern, args, throwableCandidate);
|
||||||
|
}
|
||||||
|
|
||||||
|
public static FormattingPair arrayFormat(String messagePattern, Object[] argArray, Throwable throwable) {
|
||||||
|
if (messagePattern == null) {
|
||||||
|
return new FormattingPair(null, throwable);
|
||||||
|
}
|
||||||
|
|
||||||
|
if (argArray == null) {
|
||||||
|
return new FormattingPair(messagePattern);
|
||||||
|
}
|
||||||
|
|
||||||
|
return new FormattingPair(StringFormatter.arrayFormat(messagePattern, argArray), throwable);
|
||||||
|
}
|
||||||
|
|
||||||
|
private static Throwable getThrowableCandidate(Object[] argArray) {
|
||||||
|
if (argArray == null || argArray.length == 0) {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
Object lastEntry = argArray[argArray.length - 1];
|
||||||
|
if (lastEntry instanceof Throwable) {
|
||||||
|
return (Throwable) lastEntry;
|
||||||
|
}
|
||||||
|
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
private static Object[] trimmedCopy(final Object[] argArray) {
|
||||||
|
if (argArray == null || argArray.length == 0) {
|
||||||
|
throw new IllegalStateException("non-sensical empty or null argument array");
|
||||||
|
}
|
||||||
|
|
||||||
|
int trimmedLen = argArray.length - 1;
|
||||||
|
Object[] trimmed = new Object[trimmedLen];
|
||||||
|
|
||||||
|
if (trimmedLen > 0) {
|
||||||
|
System.arraycopy(argArray, 0, trimmed, 0, trimmedLen);
|
||||||
|
}
|
||||||
|
|
||||||
|
return trimmed;
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,89 @@
|
|||||||
|
package ru.dmitriymx.minecraft.metrics;
|
||||||
|
|
||||||
|
import io.netty.buffer.ByteBuf;
|
||||||
|
import io.netty.buffer.Unpooled;
|
||||||
|
import io.netty.channel.ChannelFutureListener;
|
||||||
|
import io.netty.channel.ChannelHandlerContext;
|
||||||
|
import io.netty.channel.ChannelInboundHandlerAdapter;
|
||||||
|
import io.netty.handler.codec.http.*;
|
||||||
|
import ru.dmitriymx.minecraft.logger.LoggerAdapter;
|
||||||
|
|
||||||
|
import java.nio.charset.StandardCharsets;
|
||||||
|
|
||||||
|
public class HttpHandler extends ChannelInboundHandlerAdapter {
|
||||||
|
|
||||||
|
private final FullHttpResponse responseNotFound;
|
||||||
|
|
||||||
|
private final String endpoint;
|
||||||
|
private final LoggerAdapter logger;
|
||||||
|
private final MinecraftInfoProvider minecraftInfoProvider;
|
||||||
|
|
||||||
|
public HttpHandler(String endpoint, LoggerAdapter logger, MinecraftInfoProvider minecraftInfoProvider) {
|
||||||
|
this.endpoint = endpoint;
|
||||||
|
this.logger = logger;
|
||||||
|
this.minecraftInfoProvider = minecraftInfoProvider;
|
||||||
|
|
||||||
|
ByteBuf byteBuf = Unpooled.wrappedBuffer("404: Not Found".getBytes(StandardCharsets.UTF_8));
|
||||||
|
this.responseNotFound = new DefaultFullHttpResponse(HttpVersion.HTTP_1_1, HttpResponseStatus.NOT_FOUND, byteBuf);
|
||||||
|
this.responseNotFound.headers().set(HttpHeaderNames.CONTENT_TYPE, HttpHeaderValues.TEXT_PLAIN);
|
||||||
|
this.responseNotFound.headers().setInt(HttpHeaderNames.CONTENT_LENGTH, byteBuf.readableBytes());
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void channelRead(ChannelHandlerContext ctx, Object msg) {
|
||||||
|
if (!(msg instanceof HttpRequest)) {
|
||||||
|
if (!(msg instanceof LastHttpContent)) {
|
||||||
|
logger.debug("Incoming request is unknown");
|
||||||
|
logger.debug("request class: {}", msg.getClass());
|
||||||
|
}
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
channelRead1(ctx, (HttpRequest) msg);
|
||||||
|
}
|
||||||
|
|
||||||
|
private void channelRead1(ChannelHandlerContext ctx, HttpRequest request) {
|
||||||
|
HttpResponse response;
|
||||||
|
|
||||||
|
if (request.method() != HttpMethod.GET) {
|
||||||
|
logger.warn("Incoming request method \"{}\" not allowed", request.method());
|
||||||
|
response = new DefaultFullHttpResponse(HttpVersion.HTTP_1_1, HttpResponseStatus.METHOD_NOT_ALLOWED);
|
||||||
|
} else {
|
||||||
|
QueryStringDecoder queryString = new QueryStringDecoder(request.uri());
|
||||||
|
logger.debug("uri.path: {}", queryString.path());
|
||||||
|
|
||||||
|
if (!queryString.path().equals(endpoint)) {
|
||||||
|
response = responseNotFound;
|
||||||
|
} else {
|
||||||
|
ByteBuf buffer = Unpooled.wrappedBuffer(metrics().getBytes(StandardCharsets.UTF_8));
|
||||||
|
|
||||||
|
response = new DefaultFullHttpResponse(HttpVersion.HTTP_1_1, HttpResponseStatus.OK, buffer);
|
||||||
|
response.headers().set(HttpHeaderNames.CONTENT_TYPE, HttpHeaderValues.TEXT_PLAIN);
|
||||||
|
response.headers().setInt(HttpHeaderNames.CONTENT_LENGTH, buffer.readableBytes());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
ctx.writeAndFlush(response).addListener(ChannelFutureListener.CLOSE);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void channelReadComplete(ChannelHandlerContext ctx) {
|
||||||
|
ctx.flush();
|
||||||
|
}
|
||||||
|
|
||||||
|
private String metrics() {
|
||||||
|
return players() + tps();
|
||||||
|
}
|
||||||
|
|
||||||
|
private String players() {
|
||||||
|
return "# HELP mc_players_online Online players\n" +
|
||||||
|
"# TYPE mc_players_online gauge\n" +
|
||||||
|
"mc_players_online " + minecraftInfoProvider.playersOnline() + '\n';
|
||||||
|
}
|
||||||
|
|
||||||
|
private String tps() {
|
||||||
|
return "# HELP mc_tps TPS\n" +
|
||||||
|
"# TYPE mc_tps gauge\n" +
|
||||||
|
"mc_tps " + minecraftInfoProvider.tps() + '\n';
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,68 @@
|
|||||||
|
package ru.dmitriymx.minecraft.metrics;
|
||||||
|
|
||||||
|
import io.netty.bootstrap.ServerBootstrap;
|
||||||
|
import io.netty.channel.ChannelInitializer;
|
||||||
|
import io.netty.channel.EventLoopGroup;
|
||||||
|
import io.netty.channel.epoll.Epoll;
|
||||||
|
import io.netty.channel.epoll.EpollEventLoopGroup;
|
||||||
|
import io.netty.channel.epoll.EpollServerSocketChannel;
|
||||||
|
import io.netty.channel.nio.NioEventLoopGroup;
|
||||||
|
import io.netty.channel.socket.SocketChannel;
|
||||||
|
import io.netty.channel.socket.nio.NioServerSocketChannel;
|
||||||
|
import io.netty.handler.codec.http.HttpServerCodec;
|
||||||
|
import lombok.RequiredArgsConstructor;
|
||||||
|
import lombok.SneakyThrows;
|
||||||
|
import ru.dmitriymx.minecraft.logger.LoggerAdapter;
|
||||||
|
|
||||||
|
@RequiredArgsConstructor
|
||||||
|
public class MetricsServer {
|
||||||
|
|
||||||
|
private final String host;
|
||||||
|
private final int port;
|
||||||
|
private final String endpoint;
|
||||||
|
private final LoggerAdapter loggerAdapter;
|
||||||
|
private final MinecraftInfoProvider minecraftInfoProvider;
|
||||||
|
|
||||||
|
private EventLoopGroup bossGroup;
|
||||||
|
private EventLoopGroup workerGroup;
|
||||||
|
|
||||||
|
@SneakyThrows
|
||||||
|
public void start() {
|
||||||
|
bossGroup = createEventLoopGroup();
|
||||||
|
workerGroup = createEventLoopGroup();
|
||||||
|
|
||||||
|
buildServerBootstrap().bind(host, port)
|
||||||
|
.sync().channel().closeFuture();
|
||||||
|
}
|
||||||
|
|
||||||
|
@SneakyThrows
|
||||||
|
public void stop() {
|
||||||
|
workerGroup.shutdownGracefully().sync();
|
||||||
|
bossGroup.shutdownGracefully().sync();
|
||||||
|
}
|
||||||
|
|
||||||
|
private EventLoopGroup createEventLoopGroup() {
|
||||||
|
if (Epoll.isAvailable()) {
|
||||||
|
return new EpollEventLoopGroup(1);
|
||||||
|
} else {
|
||||||
|
return new NioEventLoopGroup(1);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private ServerBootstrap buildServerBootstrap() {
|
||||||
|
ServerBootstrap bootstrap = new ServerBootstrap();
|
||||||
|
|
||||||
|
bootstrap.group(bossGroup, workerGroup)
|
||||||
|
.channel(Epoll.isAvailable() ? EpollServerSocketChannel.class : NioServerSocketChannel.class)
|
||||||
|
.childHandler(new ChannelInitializer<SocketChannel>() {
|
||||||
|
@Override
|
||||||
|
protected void initChannel(SocketChannel ch) {
|
||||||
|
ch.pipeline()
|
||||||
|
.addLast(new HttpServerCodec())
|
||||||
|
.addLast(new HttpHandler(endpoint, loggerAdapter, minecraftInfoProvider));
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
return bootstrap;
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,8 @@
|
|||||||
|
package ru.dmitriymx.minecraft.metrics;
|
||||||
|
|
||||||
|
public interface MinecraftInfoProvider {
|
||||||
|
|
||||||
|
int playersOnline();
|
||||||
|
|
||||||
|
double tps();
|
||||||
|
}
|
||||||
@@ -0,0 +1,240 @@
|
|||||||
|
package ru.dmitriymx.minecraft.utils;
|
||||||
|
|
||||||
|
import java.util.HashMap;
|
||||||
|
import java.util.Map;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Copy-Paste from org.slf4j.helpers.MessageFormatter
|
||||||
|
*/
|
||||||
|
public final class StringFormatter {
|
||||||
|
private static final String EMPTY = "";
|
||||||
|
private static final char DELIM_START = '{';
|
||||||
|
private static final String DELIM_STR = "{}";
|
||||||
|
private static final char ESCAPE_CHAR = '\\';
|
||||||
|
|
||||||
|
public static String arrayFormat(String messagePattern, Object[] argArray) {
|
||||||
|
if (messagePattern == null) {
|
||||||
|
return EMPTY;
|
||||||
|
} else if (argArray == null) {
|
||||||
|
return messagePattern;
|
||||||
|
}
|
||||||
|
|
||||||
|
StringBuilder sb = new StringBuilder(messagePattern.length() + 50);
|
||||||
|
|
||||||
|
int k = 0;
|
||||||
|
for (int i = 0; i < argArray.length; i++) {
|
||||||
|
int idx = messagePattern.indexOf(DELIM_STR, k);
|
||||||
|
|
||||||
|
if (idx == -1) {
|
||||||
|
// no more variables
|
||||||
|
if (k == 0) { // this is a simple string
|
||||||
|
return messagePattern;
|
||||||
|
} else { // add the tail string which contains no variables and return
|
||||||
|
// the result.
|
||||||
|
sb.append(messagePattern, k, messagePattern.length());
|
||||||
|
return sb.toString();
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
if (isEscapedDelimeter(messagePattern, idx)) {
|
||||||
|
if (!isDoubleEscaped(messagePattern, idx)) {
|
||||||
|
i--; // DELIM_START was escaped, thus should not be incremented
|
||||||
|
sb.append(messagePattern, k, idx - 1);
|
||||||
|
sb.append(DELIM_START);
|
||||||
|
k = idx + 1;
|
||||||
|
} else {
|
||||||
|
// The escape character preceding the delimiter start is
|
||||||
|
// itself escaped: "abc x:\\{}"
|
||||||
|
// we have to consume one backward slash
|
||||||
|
sb.append(messagePattern, k, idx - 1);
|
||||||
|
deeplyAppendParameter(sb, argArray[i], new HashMap<>());
|
||||||
|
k = idx + 2;
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
sb.append(messagePattern, k, idx);
|
||||||
|
deeplyAppendParameter(sb, argArray[i], new HashMap<>());
|
||||||
|
k = idx + 2;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
// append the characters following the last {} pair.
|
||||||
|
sb.append(messagePattern, k, messagePattern.length());
|
||||||
|
return sb.toString();
|
||||||
|
}
|
||||||
|
|
||||||
|
private static boolean isEscapedDelimeter(String messagePattern, int delimeterStartIndex) {
|
||||||
|
if (delimeterStartIndex == 0) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
char potentialEscape = messagePattern.charAt(delimeterStartIndex - 1);
|
||||||
|
return potentialEscape == ESCAPE_CHAR;
|
||||||
|
}
|
||||||
|
|
||||||
|
private static boolean isDoubleEscaped(String messagePattern, int delimeterStartIndex) {
|
||||||
|
return delimeterStartIndex >= 2 && messagePattern.charAt(delimeterStartIndex - 2) == ESCAPE_CHAR;
|
||||||
|
}
|
||||||
|
|
||||||
|
// special treatment of array values was suggested by 'lizongbo'
|
||||||
|
private static void deeplyAppendParameter(StringBuilder sbuf, Object o, Map<Object[], Object> seenMap) {
|
||||||
|
if (o == null) {
|
||||||
|
sbuf.append("null");
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
if (!o.getClass().isArray()) {
|
||||||
|
safeObjectAppend(sbuf, o);
|
||||||
|
} else {
|
||||||
|
// check for primitive array types because they
|
||||||
|
// unfortunately cannot be cast to Object[]
|
||||||
|
if (o instanceof boolean[]) {
|
||||||
|
booleanArrayAppend(sbuf, (boolean[]) o);
|
||||||
|
} else if (o instanceof byte[]) {
|
||||||
|
byteArrayAppend(sbuf, (byte[]) o);
|
||||||
|
} else if (o instanceof char[]) {
|
||||||
|
charArrayAppend(sbuf, (char[]) o);
|
||||||
|
} else if (o instanceof short[]) {
|
||||||
|
shortArrayAppend(sbuf, (short[]) o);
|
||||||
|
} else if (o instanceof int[]) {
|
||||||
|
intArrayAppend(sbuf, (int[]) o);
|
||||||
|
} else if (o instanceof long[]) {
|
||||||
|
longArrayAppend(sbuf, (long[]) o);
|
||||||
|
} else if (o instanceof float[]) {
|
||||||
|
floatArrayAppend(sbuf, (float[]) o);
|
||||||
|
} else if (o instanceof double[]) {
|
||||||
|
doubleArrayAppend(sbuf, (double[]) o);
|
||||||
|
} else {
|
||||||
|
objectArrayAppend(sbuf, (Object[]) o, seenMap);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private static void safeObjectAppend(StringBuilder sbuf, Object o) {
|
||||||
|
try {
|
||||||
|
String oAsString = o.toString();
|
||||||
|
sbuf.append(oAsString);
|
||||||
|
} catch (Throwable t) {
|
||||||
|
throw new RuntimeException("Failed toString() invocation on an object of type [" + o.getClass().getName() + "]", t);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
@SuppressWarnings("DuplicatedCode")
|
||||||
|
private static void booleanArrayAppend(StringBuilder sbuf, boolean[] a) {
|
||||||
|
sbuf.append('[');
|
||||||
|
int len = a.length;
|
||||||
|
for (int i = 0; i < len; i++) {
|
||||||
|
sbuf.append(a[i]);
|
||||||
|
if (i != len - 1) {
|
||||||
|
sbuf.append(", ");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
sbuf.append(']');
|
||||||
|
}
|
||||||
|
|
||||||
|
@SuppressWarnings("DuplicatedCode")
|
||||||
|
private static void byteArrayAppend(StringBuilder sbuf, byte[] a) {
|
||||||
|
sbuf.append('[');
|
||||||
|
int len = a.length;
|
||||||
|
for (int i = 0; i < len; i++) {
|
||||||
|
sbuf.append(a[i]);
|
||||||
|
if (i != len - 1) {
|
||||||
|
sbuf.append(", ");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
sbuf.append(']');
|
||||||
|
}
|
||||||
|
|
||||||
|
@SuppressWarnings("DuplicatedCode")
|
||||||
|
private static void charArrayAppend(StringBuilder sbuf, char[] a) {
|
||||||
|
sbuf.append('[');
|
||||||
|
int len = a.length;
|
||||||
|
for (int i = 0; i < len; i++) {
|
||||||
|
sbuf.append(a[i]);
|
||||||
|
if (i != len - 1) {
|
||||||
|
sbuf.append(", ");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
sbuf.append(']');
|
||||||
|
}
|
||||||
|
|
||||||
|
@SuppressWarnings("DuplicatedCode")
|
||||||
|
private static void shortArrayAppend(StringBuilder sbuf, short[] a) {
|
||||||
|
sbuf.append('[');
|
||||||
|
int len = a.length;
|
||||||
|
for (int i = 0; i < len; i++) {
|
||||||
|
sbuf.append(a[i]);
|
||||||
|
if (i != len - 1) {
|
||||||
|
sbuf.append(", ");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
sbuf.append(']');
|
||||||
|
}
|
||||||
|
|
||||||
|
@SuppressWarnings("DuplicatedCode")
|
||||||
|
private static void intArrayAppend(StringBuilder sbuf, int[] a) {
|
||||||
|
sbuf.append('[');
|
||||||
|
int len = a.length;
|
||||||
|
for (int i = 0; i < len; i++) {
|
||||||
|
sbuf.append(a[i]);
|
||||||
|
if (i != len - 1) {
|
||||||
|
sbuf.append(", ");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
sbuf.append(']');
|
||||||
|
}
|
||||||
|
|
||||||
|
@SuppressWarnings("DuplicatedCode")
|
||||||
|
private static void longArrayAppend(StringBuilder sbuf, long[] a) {
|
||||||
|
sbuf.append('[');
|
||||||
|
int len = a.length;
|
||||||
|
for (int i = 0; i < len; i++) {
|
||||||
|
sbuf.append(a[i]);
|
||||||
|
if (i != len - 1) {
|
||||||
|
sbuf.append(", ");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
sbuf.append(']');
|
||||||
|
}
|
||||||
|
|
||||||
|
@SuppressWarnings("DuplicatedCode")
|
||||||
|
private static void floatArrayAppend(StringBuilder sbuf, float[] a) {
|
||||||
|
sbuf.append('[');
|
||||||
|
int len = a.length;
|
||||||
|
for (int i = 0; i < len; i++) {
|
||||||
|
sbuf.append(a[i]);
|
||||||
|
if (i != len - 1) {
|
||||||
|
sbuf.append(", ");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
sbuf.append(']');
|
||||||
|
}
|
||||||
|
|
||||||
|
@SuppressWarnings("DuplicatedCode")
|
||||||
|
private static void doubleArrayAppend(StringBuilder sbuf, double[] a) {
|
||||||
|
sbuf.append('[');
|
||||||
|
int len = a.length;
|
||||||
|
for (int i = 0; i < len; i++) {
|
||||||
|
sbuf.append(a[i]);
|
||||||
|
if (i != len - 1) {
|
||||||
|
sbuf.append(", ");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
sbuf.append(']');
|
||||||
|
}
|
||||||
|
|
||||||
|
private static void objectArrayAppend(StringBuilder sbuf, Object[] a, Map<Object[], Object> seenMap) {
|
||||||
|
sbuf.append('[');
|
||||||
|
if (!seenMap.containsKey(a)) {
|
||||||
|
seenMap.put(a, null);
|
||||||
|
int len = a.length;
|
||||||
|
for (int i = 0; i < len; i++) {
|
||||||
|
deeplyAppendParameter(sbuf, a[i], seenMap);
|
||||||
|
if (i != len - 1) {
|
||||||
|
sbuf.append(", ");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
// allow repeats in siblings
|
||||||
|
seenMap.remove(a);
|
||||||
|
} else {
|
||||||
|
sbuf.append("...");
|
||||||
|
}
|
||||||
|
sbuf.append(']');
|
||||||
|
}
|
||||||
|
}
|
||||||
5
core/src/main/resources/assets/config-default.conf
Normal file
5
core/src/main/resources/assets/config-default.conf
Normal file
@@ -0,0 +1,5 @@
|
|||||||
|
server {
|
||||||
|
host: "0.0.0.0"
|
||||||
|
port: 9225
|
||||||
|
endpoint: "/metrics"
|
||||||
|
}
|
||||||
2
gradle.properties
Normal file
2
gradle.properties
Normal file
@@ -0,0 +1,2 @@
|
|||||||
|
org.gradle.console=plain
|
||||||
|
#org.gradle.warning.mode=all
|
||||||
BIN
gradle/wrapper/gradle-wrapper.jar
vendored
Normal file
BIN
gradle/wrapper/gradle-wrapper.jar
vendored
Normal file
Binary file not shown.
5
gradle/wrapper/gradle-wrapper.properties
vendored
Normal file
5
gradle/wrapper/gradle-wrapper.properties
vendored
Normal file
@@ -0,0 +1,5 @@
|
|||||||
|
distributionBase=GRADLE_USER_HOME
|
||||||
|
distributionPath=wrapper/dists
|
||||||
|
distributionUrl=https\://services.gradle.org/distributions/gradle-7.2-bin.zip
|
||||||
|
zipStoreBase=GRADLE_USER_HOME
|
||||||
|
zipStorePath=wrapper/dists
|
||||||
185
gradlew
vendored
Normal file
185
gradlew
vendored
Normal file
@@ -0,0 +1,185 @@
|
|||||||
|
#!/usr/bin/env sh
|
||||||
|
|
||||||
|
#
|
||||||
|
# Copyright 2015 the original author or authors.
|
||||||
|
#
|
||||||
|
# Licensed under the Apache License, Version 2.0 (the "License");
|
||||||
|
# you may not use this file except in compliance with the License.
|
||||||
|
# You may obtain a copy of the License at
|
||||||
|
#
|
||||||
|
# https://www.apache.org/licenses/LICENSE-2.0
|
||||||
|
#
|
||||||
|
# Unless required by applicable law or agreed to in writing, software
|
||||||
|
# distributed under the License is distributed on an "AS IS" BASIS,
|
||||||
|
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||||
|
# See the License for the specific language governing permissions and
|
||||||
|
# limitations under the License.
|
||||||
|
#
|
||||||
|
|
||||||
|
##############################################################################
|
||||||
|
##
|
||||||
|
## Gradle start up script for UN*X
|
||||||
|
##
|
||||||
|
##############################################################################
|
||||||
|
|
||||||
|
# Attempt to set APP_HOME
|
||||||
|
# Resolve links: $0 may be a link
|
||||||
|
PRG="$0"
|
||||||
|
# Need this for relative symlinks.
|
||||||
|
while [ -h "$PRG" ] ; do
|
||||||
|
ls=`ls -ld "$PRG"`
|
||||||
|
link=`expr "$ls" : '.*-> \(.*\)$'`
|
||||||
|
if expr "$link" : '/.*' > /dev/null; then
|
||||||
|
PRG="$link"
|
||||||
|
else
|
||||||
|
PRG=`dirname "$PRG"`"/$link"
|
||||||
|
fi
|
||||||
|
done
|
||||||
|
SAVED="`pwd`"
|
||||||
|
cd "`dirname \"$PRG\"`/" >/dev/null
|
||||||
|
APP_HOME="`pwd -P`"
|
||||||
|
cd "$SAVED" >/dev/null
|
||||||
|
|
||||||
|
APP_NAME="Gradle"
|
||||||
|
APP_BASE_NAME=`basename "$0"`
|
||||||
|
|
||||||
|
# Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script.
|
||||||
|
DEFAULT_JVM_OPTS='"-Xmx64m" "-Xms64m"'
|
||||||
|
|
||||||
|
# Use the maximum available, or set MAX_FD != -1 to use that value.
|
||||||
|
MAX_FD="maximum"
|
||||||
|
|
||||||
|
warn () {
|
||||||
|
echo "$*"
|
||||||
|
}
|
||||||
|
|
||||||
|
die () {
|
||||||
|
echo
|
||||||
|
echo "$*"
|
||||||
|
echo
|
||||||
|
exit 1
|
||||||
|
}
|
||||||
|
|
||||||
|
# OS specific support (must be 'true' or 'false').
|
||||||
|
cygwin=false
|
||||||
|
msys=false
|
||||||
|
darwin=false
|
||||||
|
nonstop=false
|
||||||
|
case "`uname`" in
|
||||||
|
CYGWIN* )
|
||||||
|
cygwin=true
|
||||||
|
;;
|
||||||
|
Darwin* )
|
||||||
|
darwin=true
|
||||||
|
;;
|
||||||
|
MSYS* | MINGW* )
|
||||||
|
msys=true
|
||||||
|
;;
|
||||||
|
NONSTOP* )
|
||||||
|
nonstop=true
|
||||||
|
;;
|
||||||
|
esac
|
||||||
|
|
||||||
|
CLASSPATH=$APP_HOME/gradle/wrapper/gradle-wrapper.jar
|
||||||
|
|
||||||
|
|
||||||
|
# Determine the Java command to use to start the JVM.
|
||||||
|
if [ -n "$JAVA_HOME" ] ; then
|
||||||
|
if [ -x "$JAVA_HOME/jre/sh/java" ] ; then
|
||||||
|
# IBM's JDK on AIX uses strange locations for the executables
|
||||||
|
JAVACMD="$JAVA_HOME/jre/sh/java"
|
||||||
|
else
|
||||||
|
JAVACMD="$JAVA_HOME/bin/java"
|
||||||
|
fi
|
||||||
|
if [ ! -x "$JAVACMD" ] ; then
|
||||||
|
die "ERROR: JAVA_HOME is set to an invalid directory: $JAVA_HOME
|
||||||
|
|
||||||
|
Please set the JAVA_HOME variable in your environment to match the
|
||||||
|
location of your Java installation."
|
||||||
|
fi
|
||||||
|
else
|
||||||
|
JAVACMD="java"
|
||||||
|
which java >/dev/null 2>&1 || die "ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH.
|
||||||
|
|
||||||
|
Please set the JAVA_HOME variable in your environment to match the
|
||||||
|
location of your Java installation."
|
||||||
|
fi
|
||||||
|
|
||||||
|
# Increase the maximum file descriptors if we can.
|
||||||
|
if [ "$cygwin" = "false" -a "$darwin" = "false" -a "$nonstop" = "false" ] ; then
|
||||||
|
MAX_FD_LIMIT=`ulimit -H -n`
|
||||||
|
if [ $? -eq 0 ] ; then
|
||||||
|
if [ "$MAX_FD" = "maximum" -o "$MAX_FD" = "max" ] ; then
|
||||||
|
MAX_FD="$MAX_FD_LIMIT"
|
||||||
|
fi
|
||||||
|
ulimit -n $MAX_FD
|
||||||
|
if [ $? -ne 0 ] ; then
|
||||||
|
warn "Could not set maximum file descriptor limit: $MAX_FD"
|
||||||
|
fi
|
||||||
|
else
|
||||||
|
warn "Could not query maximum file descriptor limit: $MAX_FD_LIMIT"
|
||||||
|
fi
|
||||||
|
fi
|
||||||
|
|
||||||
|
# For Darwin, add options to specify how the application appears in the dock
|
||||||
|
if $darwin; then
|
||||||
|
GRADLE_OPTS="$GRADLE_OPTS \"-Xdock:name=$APP_NAME\" \"-Xdock:icon=$APP_HOME/media/gradle.icns\""
|
||||||
|
fi
|
||||||
|
|
||||||
|
# For Cygwin or MSYS, switch paths to Windows format before running java
|
||||||
|
if [ "$cygwin" = "true" -o "$msys" = "true" ] ; then
|
||||||
|
APP_HOME=`cygpath --path --mixed "$APP_HOME"`
|
||||||
|
CLASSPATH=`cygpath --path --mixed "$CLASSPATH"`
|
||||||
|
|
||||||
|
JAVACMD=`cygpath --unix "$JAVACMD"`
|
||||||
|
|
||||||
|
# We build the pattern for arguments to be converted via cygpath
|
||||||
|
ROOTDIRSRAW=`find -L / -maxdepth 1 -mindepth 1 -type d 2>/dev/null`
|
||||||
|
SEP=""
|
||||||
|
for dir in $ROOTDIRSRAW ; do
|
||||||
|
ROOTDIRS="$ROOTDIRS$SEP$dir"
|
||||||
|
SEP="|"
|
||||||
|
done
|
||||||
|
OURCYGPATTERN="(^($ROOTDIRS))"
|
||||||
|
# Add a user-defined pattern to the cygpath arguments
|
||||||
|
if [ "$GRADLE_CYGPATTERN" != "" ] ; then
|
||||||
|
OURCYGPATTERN="$OURCYGPATTERN|($GRADLE_CYGPATTERN)"
|
||||||
|
fi
|
||||||
|
# Now convert the arguments - kludge to limit ourselves to /bin/sh
|
||||||
|
i=0
|
||||||
|
for arg in "$@" ; do
|
||||||
|
CHECK=`echo "$arg"|egrep -c "$OURCYGPATTERN" -`
|
||||||
|
CHECK2=`echo "$arg"|egrep -c "^-"` ### Determine if an option
|
||||||
|
|
||||||
|
if [ $CHECK -ne 0 ] && [ $CHECK2 -eq 0 ] ; then ### Added a condition
|
||||||
|
eval `echo args$i`=`cygpath --path --ignore --mixed "$arg"`
|
||||||
|
else
|
||||||
|
eval `echo args$i`="\"$arg\""
|
||||||
|
fi
|
||||||
|
i=`expr $i + 1`
|
||||||
|
done
|
||||||
|
case $i in
|
||||||
|
0) set -- ;;
|
||||||
|
1) set -- "$args0" ;;
|
||||||
|
2) set -- "$args0" "$args1" ;;
|
||||||
|
3) set -- "$args0" "$args1" "$args2" ;;
|
||||||
|
4) set -- "$args0" "$args1" "$args2" "$args3" ;;
|
||||||
|
5) set -- "$args0" "$args1" "$args2" "$args3" "$args4" ;;
|
||||||
|
6) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" ;;
|
||||||
|
7) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" ;;
|
||||||
|
8) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" "$args7" ;;
|
||||||
|
9) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" "$args7" "$args8" ;;
|
||||||
|
esac
|
||||||
|
fi
|
||||||
|
|
||||||
|
# Escape application args
|
||||||
|
save () {
|
||||||
|
for i do printf %s\\n "$i" | sed "s/'/'\\\\''/g;1s/^/'/;\$s/\$/' \\\\/" ; done
|
||||||
|
echo " "
|
||||||
|
}
|
||||||
|
APP_ARGS=`save "$@"`
|
||||||
|
|
||||||
|
# Collect all arguments for the java command, following the shell quoting and substitution rules
|
||||||
|
eval set -- $DEFAULT_JVM_OPTS $JAVA_OPTS $GRADLE_OPTS "\"-Dorg.gradle.appname=$APP_BASE_NAME\"" -classpath "\"$CLASSPATH\"" org.gradle.wrapper.GradleWrapperMain "$APP_ARGS"
|
||||||
|
|
||||||
|
exec "$JAVACMD" "$@"
|
||||||
89
gradlew.bat
vendored
Normal file
89
gradlew.bat
vendored
Normal file
@@ -0,0 +1,89 @@
|
|||||||
|
@rem
|
||||||
|
@rem Copyright 2015 the original author or authors.
|
||||||
|
@rem
|
||||||
|
@rem Licensed under the Apache License, Version 2.0 (the "License");
|
||||||
|
@rem you may not use this file except in compliance with the License.
|
||||||
|
@rem You may obtain a copy of the License at
|
||||||
|
@rem
|
||||||
|
@rem https://www.apache.org/licenses/LICENSE-2.0
|
||||||
|
@rem
|
||||||
|
@rem Unless required by applicable law or agreed to in writing, software
|
||||||
|
@rem distributed under the License is distributed on an "AS IS" BASIS,
|
||||||
|
@rem WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||||
|
@rem See the License for the specific language governing permissions and
|
||||||
|
@rem limitations under the License.
|
||||||
|
@rem
|
||||||
|
|
||||||
|
@if "%DEBUG%" == "" @echo off
|
||||||
|
@rem ##########################################################################
|
||||||
|
@rem
|
||||||
|
@rem Gradle startup script for Windows
|
||||||
|
@rem
|
||||||
|
@rem ##########################################################################
|
||||||
|
|
||||||
|
@rem Set local scope for the variables with windows NT shell
|
||||||
|
if "%OS%"=="Windows_NT" setlocal
|
||||||
|
|
||||||
|
set DIRNAME=%~dp0
|
||||||
|
if "%DIRNAME%" == "" set DIRNAME=.
|
||||||
|
set APP_BASE_NAME=%~n0
|
||||||
|
set APP_HOME=%DIRNAME%
|
||||||
|
|
||||||
|
@rem Resolve any "." and ".." in APP_HOME to make it shorter.
|
||||||
|
for %%i in ("%APP_HOME%") do set APP_HOME=%%~fi
|
||||||
|
|
||||||
|
@rem Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script.
|
||||||
|
set DEFAULT_JVM_OPTS="-Xmx64m" "-Xms64m"
|
||||||
|
|
||||||
|
@rem Find java.exe
|
||||||
|
if defined JAVA_HOME goto findJavaFromJavaHome
|
||||||
|
|
||||||
|
set JAVA_EXE=java.exe
|
||||||
|
%JAVA_EXE% -version >NUL 2>&1
|
||||||
|
if "%ERRORLEVEL%" == "0" goto execute
|
||||||
|
|
||||||
|
echo.
|
||||||
|
echo ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH.
|
||||||
|
echo.
|
||||||
|
echo Please set the JAVA_HOME variable in your environment to match the
|
||||||
|
echo location of your Java installation.
|
||||||
|
|
||||||
|
goto fail
|
||||||
|
|
||||||
|
:findJavaFromJavaHome
|
||||||
|
set JAVA_HOME=%JAVA_HOME:"=%
|
||||||
|
set JAVA_EXE=%JAVA_HOME%/bin/java.exe
|
||||||
|
|
||||||
|
if exist "%JAVA_EXE%" goto execute
|
||||||
|
|
||||||
|
echo.
|
||||||
|
echo ERROR: JAVA_HOME is set to an invalid directory: %JAVA_HOME%
|
||||||
|
echo.
|
||||||
|
echo Please set the JAVA_HOME variable in your environment to match the
|
||||||
|
echo location of your Java installation.
|
||||||
|
|
||||||
|
goto fail
|
||||||
|
|
||||||
|
:execute
|
||||||
|
@rem Setup the command line
|
||||||
|
|
||||||
|
set CLASSPATH=%APP_HOME%\gradle\wrapper\gradle-wrapper.jar
|
||||||
|
|
||||||
|
|
||||||
|
@rem Execute Gradle
|
||||||
|
"%JAVA_EXE%" %DEFAULT_JVM_OPTS% %JAVA_OPTS% %GRADLE_OPTS% "-Dorg.gradle.appname=%APP_BASE_NAME%" -classpath "%CLASSPATH%" org.gradle.wrapper.GradleWrapperMain %*
|
||||||
|
|
||||||
|
:end
|
||||||
|
@rem End local scope for the variables with windows NT shell
|
||||||
|
if "%ERRORLEVEL%"=="0" goto mainEnd
|
||||||
|
|
||||||
|
:fail
|
||||||
|
rem Set variable GRADLE_EXIT_CONSOLE if you need the _script_ return code instead of
|
||||||
|
rem the _cmd.exe /c_ return code!
|
||||||
|
if not "" == "%GRADLE_EXIT_CONSOLE%" exit 1
|
||||||
|
exit /b 1
|
||||||
|
|
||||||
|
:mainEnd
|
||||||
|
if "%OS%"=="Windows_NT" endlocal
|
||||||
|
|
||||||
|
:omega
|
||||||
5
settings.gradle
Normal file
5
settings.gradle
Normal file
@@ -0,0 +1,5 @@
|
|||||||
|
rootProject.name = 'mc-metrics'
|
||||||
|
|
||||||
|
include('core')
|
||||||
|
include('bukkit')
|
||||||
|
include('sponge')
|
||||||
49
sponge/build.gradle
Normal file
49
sponge/build.gradle
Normal file
@@ -0,0 +1,49 @@
|
|||||||
|
//file:noinspection GrUnresolvedAccess
|
||||||
|
plugins {
|
||||||
|
id 'java'
|
||||||
|
id 'com.github.johnrengelman.shadow' version '7.0.0'
|
||||||
|
}
|
||||||
|
|
||||||
|
group 'ru.dmitriymx'
|
||||||
|
version '1.0-SNAPSHOT'
|
||||||
|
jar.archiveBaseName.set(project.name)
|
||||||
|
|
||||||
|
compileJava {
|
||||||
|
sourceCompatibility = targetCompatibility = JavaVersion.VERSION_1_8
|
||||||
|
options.encoding = 'UTF-8'
|
||||||
|
}
|
||||||
|
|
||||||
|
repositories {
|
||||||
|
mavenCentral()
|
||||||
|
maven { url 'https://repo.spongepowered.org/maven' }
|
||||||
|
}
|
||||||
|
|
||||||
|
dependencies {
|
||||||
|
implementation(project(':core'))
|
||||||
|
|
||||||
|
annotationProcessor 'org.projectlombok:lombok:1.18.20'
|
||||||
|
compileOnly 'org.projectlombok:lombok:1.18.20'
|
||||||
|
|
||||||
|
compileOnly('org.spongepowered:spongeapi:7.3.0') {
|
||||||
|
exclude(module: 'asm')
|
||||||
|
exclude(module: 'caffeine')
|
||||||
|
exclude(module: 'commons-lang3')
|
||||||
|
exclude(module: 'configurate-gson')
|
||||||
|
exclude(module: 'configurate-hocon')
|
||||||
|
exclude(module: 'configurate-yaml')
|
||||||
|
exclude(module: 'error_prone_annotations')
|
||||||
|
exclude(module: 'flow-math')
|
||||||
|
exclude(module: 'flow-noise')
|
||||||
|
exclude(module: 'gson')
|
||||||
|
exclude(module: 'guava')
|
||||||
|
exclude(module: 'jsr305')
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
shadowJar {
|
||||||
|
archiveBaseName.set('mc-mectics-sponge')
|
||||||
|
archiveVersion.set(project.version.toString())
|
||||||
|
archiveClassifier.set('')
|
||||||
|
|
||||||
|
relocate('assets', 'assets.minecraft_metrics')
|
||||||
|
}
|
||||||
@@ -0,0 +1,70 @@
|
|||||||
|
package ru.dmitriymx.minecraft.metrics.sponge;
|
||||||
|
|
||||||
|
import com.google.inject.Inject;
|
||||||
|
import com.typesafe.config.Config;
|
||||||
|
import lombok.SneakyThrows;
|
||||||
|
import org.spongepowered.api.config.ConfigDir;
|
||||||
|
import org.spongepowered.api.event.Listener;
|
||||||
|
import org.spongepowered.api.event.game.state.GameStartedServerEvent;
|
||||||
|
import org.spongepowered.api.event.game.state.GameStoppingServerEvent;
|
||||||
|
import org.spongepowered.api.plugin.Plugin;
|
||||||
|
import org.spongepowered.api.plugin.PluginContainer;
|
||||||
|
import ru.dmitriymx.minecraft.config.ConfigProvider;
|
||||||
|
import ru.dmitriymx.minecraft.metrics.MetricsServer;
|
||||||
|
|
||||||
|
import java.nio.file.Files;
|
||||||
|
import java.nio.file.Path;
|
||||||
|
|
||||||
|
@Plugin(id = "minecraft_metrics",
|
||||||
|
name = "MinecraftMetrics",
|
||||||
|
description = "Minecraft Prometheus Exporter",
|
||||||
|
version = "1.0",
|
||||||
|
authors = { "DmitriyMX" })
|
||||||
|
@SuppressWarnings("unused")
|
||||||
|
public class MetricsPlugin {
|
||||||
|
|
||||||
|
@Inject
|
||||||
|
private org.slf4j.Logger slf4jLogger;
|
||||||
|
|
||||||
|
@Inject
|
||||||
|
@ConfigDir(sharedRoot = false)
|
||||||
|
private Path pluginDataDirPath;
|
||||||
|
|
||||||
|
@Inject
|
||||||
|
private PluginContainer pluginContainer;
|
||||||
|
|
||||||
|
private MetricsServer metricsServer;
|
||||||
|
|
||||||
|
@Listener
|
||||||
|
@SneakyThrows
|
||||||
|
public void onGameStartedServerEvent(GameStartedServerEvent event) {
|
||||||
|
if (Files.notExists(pluginDataDirPath)) {
|
||||||
|
slf4jLogger.info("Create data dir: {}", pluginDataDirPath);
|
||||||
|
Files.createDirectories(pluginDataDirPath);
|
||||||
|
}
|
||||||
|
|
||||||
|
ConfigProvider configProvider = new SpongeConfigProvider(pluginContainer, pluginDataDirPath);
|
||||||
|
Config config = configProvider.get();
|
||||||
|
|
||||||
|
SpongeLogger logger = new SpongeLogger(slf4jLogger);
|
||||||
|
metricsServer = new MetricsServer(
|
||||||
|
config.getString("server.host"),
|
||||||
|
config.getInt("server.port"),
|
||||||
|
config.getString("server.endpoint"),
|
||||||
|
logger,
|
||||||
|
new SpongeInfoProvider()
|
||||||
|
);
|
||||||
|
|
||||||
|
//noinspection HttpUrlsUsage
|
||||||
|
logger.info("Start metrics server: http://{}:{}{}",
|
||||||
|
config.getString("server.host"),
|
||||||
|
config.getInt("server.port"),
|
||||||
|
config.getString("server.endpoint"));
|
||||||
|
metricsServer.start();
|
||||||
|
}
|
||||||
|
|
||||||
|
@Listener
|
||||||
|
public void onGameStoppingServerEvent(GameStoppingServerEvent event) {
|
||||||
|
metricsServer.stop();
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,41 @@
|
|||||||
|
package ru.dmitriymx.minecraft.metrics.sponge;
|
||||||
|
|
||||||
|
import com.typesafe.config.Config;
|
||||||
|
import com.typesafe.config.ConfigFactory;
|
||||||
|
import lombok.RequiredArgsConstructor;
|
||||||
|
import lombok.SneakyThrows;
|
||||||
|
import org.spongepowered.api.asset.Asset;
|
||||||
|
import org.spongepowered.api.plugin.PluginContainer;
|
||||||
|
import ru.dmitriymx.minecraft.config.ConfigProvider;
|
||||||
|
|
||||||
|
import java.nio.file.Files;
|
||||||
|
import java.nio.file.Path;
|
||||||
|
import java.util.Optional;
|
||||||
|
|
||||||
|
@RequiredArgsConstructor
|
||||||
|
public class SpongeConfigProvider implements ConfigProvider {
|
||||||
|
|
||||||
|
private final PluginContainer plugin;
|
||||||
|
private final Path pluginDataDirPath;
|
||||||
|
|
||||||
|
@Override
|
||||||
|
@SneakyThrows
|
||||||
|
public Config get() {
|
||||||
|
Optional<Asset> optionalAsset = plugin.getAsset("config-default.conf");
|
||||||
|
if (!optionalAsset.isPresent()) {
|
||||||
|
throw new RuntimeException("Where is 'config-default.conf'!!??");
|
||||||
|
}
|
||||||
|
|
||||||
|
Config defaultConfig = ConfigFactory.parseString(optionalAsset.get().readString());
|
||||||
|
Config config;
|
||||||
|
Path userConfigPath = pluginDataDirPath.resolve("config.conf");
|
||||||
|
if (Files.exists(userConfigPath)) {
|
||||||
|
Config userConfig = ConfigFactory.parseFile(userConfigPath.toFile());
|
||||||
|
config = userConfig.withFallback(defaultConfig).resolve();
|
||||||
|
} else {
|
||||||
|
config = defaultConfig.resolve();
|
||||||
|
}
|
||||||
|
|
||||||
|
return config;
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,17 @@
|
|||||||
|
package ru.dmitriymx.minecraft.metrics.sponge;
|
||||||
|
|
||||||
|
import org.spongepowered.api.Sponge;
|
||||||
|
import ru.dmitriymx.minecraft.metrics.MinecraftInfoProvider;
|
||||||
|
|
||||||
|
public class SpongeInfoProvider implements MinecraftInfoProvider {
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public int playersOnline() {
|
||||||
|
return Sponge.getServer().getOnlinePlayers().size();
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public double tps() {
|
||||||
|
return Sponge.getServer().getTicksPerSecond();
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,40 @@
|
|||||||
|
package ru.dmitriymx.minecraft.metrics.sponge;
|
||||||
|
|
||||||
|
import lombok.RequiredArgsConstructor;
|
||||||
|
import ru.dmitriymx.minecraft.logger.LoggerAdapter;
|
||||||
|
|
||||||
|
@RequiredArgsConstructor
|
||||||
|
public class SpongeLogger extends LoggerAdapter {
|
||||||
|
|
||||||
|
private final org.slf4j.Logger logger;
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void debug(String message) {
|
||||||
|
logger.debug(message);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void debug(String message, Throwable throwable) {
|
||||||
|
logger.debug(message, throwable);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void info(String message) {
|
||||||
|
logger.info(message);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void warn(String message) {
|
||||||
|
logger.warn(message);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void error(String message) {
|
||||||
|
logger.error(message);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void error(String message, Throwable throwable) {
|
||||||
|
logger.error(message, throwable);
|
||||||
|
}
|
||||||
|
}
|
||||||
17
sponge/src/main/resources/mcmod.info
Normal file
17
sponge/src/main/resources/mcmod.info
Normal file
@@ -0,0 +1,17 @@
|
|||||||
|
[
|
||||||
|
{
|
||||||
|
"modid": "minecraft_metrics",
|
||||||
|
"name": "MinecraftMetrics",
|
||||||
|
"version": "1.0",
|
||||||
|
"description": "Minecraft Prometheus Exporter",
|
||||||
|
"authorList": [
|
||||||
|
"DmitriyMX"
|
||||||
|
],
|
||||||
|
"dependencies": [
|
||||||
|
"spongeapi"
|
||||||
|
],
|
||||||
|
"requiredMods": [
|
||||||
|
"spongeapi"
|
||||||
|
]
|
||||||
|
}
|
||||||
|
]
|
||||||
Reference in New Issue
Block a user