diff --git a/README.md b/README.md index 8022a24..55d2879 100644 --- a/README.md +++ b/README.md @@ -93,12 +93,12 @@ When using the `export-for-prometheus` subcommand, mc-monitor will serve a Prome The sub-command accepts the following arguments, which can also be viewed using `--help`: ``` - -edition string - The edition of Minecraft server, java or bedrock (env EXPORT_EDITION) (default "java") + -bedrock-servers host:port + one or more host:port addresses of Bedrock servers to monitor, when port is omitted 19132 is used (env EXPORT_BEDROCK_SERVERS) -port int HTTP port where Prometheus metrics are exported (env EXPORT_PORT) (default 8080) -servers host:port - one or more host:port addresses of servers to monitor, when port is omitted 19132 is used (env EXPORT_SERVERS) + one or more host:port addresses of Java servers to monitor, when port is omitted 25565 is used (env EXPORT_SERVERS) ``` The following metrics are exported diff --git a/prom_cmd.go b/prom_cmd.go index 21a328c..cc84b27 100644 --- a/prom_cmd.go +++ b/prom_cmd.go @@ -16,10 +16,10 @@ import ( const promExportPath = "/metrics" type exportPrometheusCmd struct { - Servers []string `usage:"one or more [host:port] addresses of servers to monitor, when port is omitted 19132 is used"` - Port int `usage:"HTTP port where Prometheus metrics are exported" default:"8080"` - Edition string `usage:"The edition of Minecraft server, java or bedrock" default:"java"` - logger *zap.Logger + Servers []string `usage:"one or more [host:port] addresses of Java servers to monitor, when port is omitted 25565 is used"` + BedrockServers []string `usage:"one or more [host:port] addresses of Bedrock servers to monitor, when port is omitted 19132 is used"` + Port int `usage:"HTTP port where Prometheus metrics are exported" default:"8080"` + logger *zap.Logger } func (c *exportPrometheusCmd) Name() string { @@ -47,14 +47,10 @@ func (c *exportPrometheusCmd) Execute(_ context.Context, _ *flag.FlagSet, args . printUsageError("requires at least one server") return subcommands.ExitUsageError } - if !ValidEdition(c.Edition) { - printUsageError("invalid edition") - return subcommands.ExitUsageError - } logger := args[0].(*zap.Logger) - collectors, err := newPromCollectors(c.Servers, ServerEdition(c.Edition), logger) + collectors, err := newPromCollectors(c.Servers, c.BedrockServers, logger) if err != nil { log.Fatal(err) } @@ -69,7 +65,7 @@ func (c *exportPrometheusCmd) Execute(_ context.Context, _ *flag.FlagSet, args . logger.Info("exporting metrics for prometheus", zap.String("address", exportAddress), zap.String("path", promExportPath), - zap.Strings("servers", c.Servers)) + ) http.Handle(promExportPath, promhttp.Handler()) log.Fatal(http.ListenAndServe(exportAddress, nil)) diff --git a/prom_collector.go b/prom_collector.go index c4dc36e..7b39b7c 100644 --- a/prom_collector.go +++ b/prom_collector.go @@ -50,11 +50,20 @@ func (c promCollectors) Collect(metrics chan<- prometheus.Metric) { } } -func newPromCollectors(servers []string, edition ServerEdition, logger *zap.Logger) (promCollectors, error) { - collectors, err := createPromCollectors(servers, edition, logger) +func newPromCollectors(servers []string, bedrockServers []string, logger *zap.Logger) (promCollectors, error) { + var collectors []specificPromCollector + + javaCollectors, err := createPromCollectors(servers, JavaEdition, logger) + if err != nil { + return nil, err + } + collectors = append(collectors, javaCollectors...) + + bedrockCollectors, err := createPromCollectors(bedrockServers, BedrockEdition, logger) if err != nil { return nil, err } + collectors = append(collectors, bedrockCollectors...) return collectors, nil } @@ -154,7 +163,7 @@ func (c *promBedrockCollector) Collect(metrics chan<- prometheus.Metric) { func (c *promBedrockCollector) sendMetric(metrics chan<- prometheus.Metric, desc *prometheus.Desc, value float64) { metric, err := prometheus.NewConstMetric(desc, prometheus.GaugeValue, value, - c.host, c.port, string(JavaEdition)) + c.host, c.port, string(BedrockEdition)) if err != nil { c.logger.Error("failed to build metric", zap.Error(err), zap.String("name", desc.String())) } else {