Compare commits
5 Commits
78a94f24d2
...
main
| Author | SHA1 | Date | |
|---|---|---|---|
| 8036a37c13 | |||
| 5e8c4ea199 | |||
| 63c505dce6 | |||
| acee1bcbdc | |||
| 8fa02c2e2d |
57
.github/workflows/ci.yml
vendored
Normal file
57
.github/workflows/ci.yml
vendored
Normal file
@@ -0,0 +1,57 @@
|
||||
name: Release
|
||||
|
||||
on:
|
||||
push:
|
||||
tags:
|
||||
- 'v*.*.*'
|
||||
|
||||
jobs:
|
||||
build:
|
||||
runs-on: ubuntu-latest
|
||||
|
||||
steps:
|
||||
- name: Checkout code
|
||||
uses: actions/checkout@v4
|
||||
|
||||
- name: Set up Rust
|
||||
uses: dtolnay/rust-toolchain@stable
|
||||
with:
|
||||
toolchain: stable
|
||||
|
||||
- name: Build release
|
||||
run: cargo build --release
|
||||
|
||||
- name: Upload release asset
|
||||
uses: actions/upload-artifact@v4
|
||||
with:
|
||||
name: satisfactory-exporter
|
||||
path: target/release/satisfactory-exporter
|
||||
|
||||
release:
|
||||
needs: build
|
||||
runs-on: ubuntu-latest
|
||||
|
||||
steps:
|
||||
- name: Checkout code
|
||||
uses: actions/checkout@v4
|
||||
|
||||
- name: Create GitHub Release
|
||||
id: create_release
|
||||
uses: actions/create-release@v1
|
||||
env:
|
||||
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
|
||||
with:
|
||||
tag_name: ${{ github.ref }}
|
||||
release_name: Release ${{ github.ref }}
|
||||
draft: false
|
||||
prerelease: false
|
||||
|
||||
- name: Upload release asset
|
||||
uses: actions/upload-release-asset@v1
|
||||
env:
|
||||
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
|
||||
with:
|
||||
upload_url: ${{ steps.create_release.outputs.upload_url }}
|
||||
asset_path: target/release/satisfactory-exporter
|
||||
asset_name: satisfactory-exporter
|
||||
asset_content_type: application/octet-stream
|
||||
@@ -9,4 +9,4 @@ axum = "0.6"
|
||||
serde = { version = "1.0", features = ["derive"] }
|
||||
prometheus = "0.13"
|
||||
reqwest = { version = "0.11", features = ["json"] }
|
||||
clap = { version = "4.0", features = ["derive"] }
|
||||
clap = { version = "4.0", features = ["derive", "env"] }
|
||||
|
||||
21
LICENSE
Normal file
21
LICENSE
Normal file
@@ -0,0 +1,21 @@
|
||||
MIT License
|
||||
|
||||
Copyright (c) 2024 Drayton Munster
|
||||
|
||||
Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
of this software and associated documentation files (the "Software"), to deal
|
||||
in the Software without restriction, including without limitation the rights
|
||||
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
copies of the Software, and to permit persons to whom the Software is
|
||||
furnished to do so, subject to the following conditions:
|
||||
|
||||
The above copyright notice and this permission notice shall be included in all
|
||||
copies or substantial portions of the Software.
|
||||
|
||||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
||||
SOFTWARE.
|
||||
68
README.md
Normal file
68
README.md
Normal file
@@ -0,0 +1,68 @@
|
||||
# Satisfactory Exporter
|
||||
|
||||
This project is a prometheus exporter for the game [Satisfactory](https://www.satisfactorygame.com/).
|
||||
It periodically queries a dedicated server via the HTTPS API for metrics and exposes them via an HTTP endpoint.
|
||||
|
||||
## Usage
|
||||
|
||||
### CLI
|
||||
|
||||
```
|
||||
Usage: satisfactory-exporter [OPTIONS] --endpoint <ENDPOINT>
|
||||
|
||||
Options:
|
||||
--update-interval <UPDATE_INTERVAL>
|
||||
Interval in seconds between each query to the server [env: SE_UPDATE_INTERVAL=] [default: 5]
|
||||
--endpoint <ENDPOINT>
|
||||
Hostname and port of the server to query [env: SE_ENDPOINT=]
|
||||
--token-file <TOKEN_FILE>
|
||||
File containing the bearer token to use for authentication. Mutually exclusive with --token [env: SE_TOKEN_FILE=]
|
||||
--token <TOKEN>
|
||||
Bearer token to use for authentication. Mutually exclusive with --token-file [env: SE_TOKEN=]
|
||||
--allow-insecure
|
||||
Allow insecure connections (e.g., to a server with a self-signed certificate) [env: SE_ALLOW_INSECURE=]
|
||||
--listen <LISTEN>
|
||||
Address:Port to which the server will listen [env: SE_LISTEN=] [default: 127.0.0.1:3030]
|
||||
-h, --help
|
||||
Print help
|
||||
-V, --version
|
||||
Print version
|
||||
|
||||
```
|
||||
|
||||
### Generating an API token
|
||||
|
||||
You can create an API token by executing the following command in the Satisfactory server console:
|
||||
|
||||
```
|
||||
server.GenerateAPIToken
|
||||
```
|
||||
|
||||
It will create a long string that looks like the following:
|
||||
|
||||
```text
|
||||
ewoJInBsIjogIkFQSVRva2VuIgp9.<Long string of characters>
|
||||
```
|
||||
|
||||
This token can then be used with the `--token` option, or saved to a file and used with the `--token-file` option.
|
||||
|
||||
### Connecting via Insecure HTTPS
|
||||
|
||||
If the server uses a self-signed certificate (the default), you can use the `--allow-insecure` option to allow the
|
||||
exporter to connect to it.
|
||||
|
||||
### Example
|
||||
|
||||
```sh
|
||||
./satisfactory-explorer --endpoint game.example.com:7777 --token-file /path/to/token.txt
|
||||
```
|
||||
|
||||
Or, for a server with a self-signed certificate:
|
||||
|
||||
```sh
|
||||
./satisfactory-explorer --endpoint game.example.com:7777 --token-file /path/to/token.txt --allow-insecure
|
||||
```
|
||||
|
||||
## License
|
||||
|
||||
This project is licensed under the MIT License. See the `LICENSE` file for details.
|
||||
104
src/main.rs
104
src/main.rs
@@ -1,9 +1,4 @@
|
||||
use axum::{
|
||||
extract::State,
|
||||
response::IntoResponse,
|
||||
routing::get,
|
||||
Router,
|
||||
};
|
||||
use axum::{extract::State, response::IntoResponse, routing::get, Router};
|
||||
use clap::Parser;
|
||||
use prometheus::{Encoder, Gauge, Registry, TextEncoder};
|
||||
use reqwest::Client;
|
||||
@@ -19,23 +14,55 @@ use tokio::time::interval;
|
||||
#[command(author, version, about, long_about = None)]
|
||||
struct Args {
|
||||
/// Interval in seconds between each query to the server
|
||||
#[arg(short, long, default_value = "5", help="Interval in seconds between each query to the server")]
|
||||
#[arg(
|
||||
long,
|
||||
default_value = "5",
|
||||
help = "Interval in seconds between each query to the server",
|
||||
env = "SE_UPDATE_INTERVAL"
|
||||
)]
|
||||
update_interval: u64,
|
||||
|
||||
/// Hostname and port of the server to query
|
||||
#[arg(short, long, help="Hostname and port of the server to query")]
|
||||
#[arg(
|
||||
long,
|
||||
help = "Hostname and port of the server to query",
|
||||
env = "SE_ENDPOINT"
|
||||
)]
|
||||
endpoint: String,
|
||||
|
||||
/// File containing the bearer token to use for authentication
|
||||
#[arg(short, long, help="File containing the bearer token to use for authentication")]
|
||||
#[arg(
|
||||
long,
|
||||
help = "File containing the bearer token to use for authentication. Mutually exclusive with --token",
|
||||
group = "token_source",
|
||||
env = "SE_TOKEN_FILE"
|
||||
)]
|
||||
token_file: Option<String>,
|
||||
|
||||
/// Bearer token to use for authentication
|
||||
#[arg(
|
||||
long,
|
||||
help = "Bearer token to use for authentication. Mutually exclusive with --token-file",
|
||||
group = "token_source",
|
||||
env = "SE_TOKEN"
|
||||
)]
|
||||
token: Option<String>,
|
||||
|
||||
/// Allow insecure connections (e.g., to a server with a self-signed certificate)
|
||||
#[arg(short, long, help="Allow insecure connections (e.g., to a server with a self-signed certificate)")]
|
||||
#[arg(
|
||||
long,
|
||||
help = "Allow insecure connections (e.g., to a server with a self-signed certificate)",
|
||||
env = "SE_ALLOW_INSECURE"
|
||||
)]
|
||||
allow_insecure: bool,
|
||||
|
||||
/// Address:Port to which the server will listen
|
||||
#[arg(short, long, help="Address:Port to which the server will listen", default_value = "127.0.0.1:3030")]
|
||||
#[arg(
|
||||
long,
|
||||
help = "Address:Port to which the server will listen",
|
||||
default_value = "127.0.0.1:3030",
|
||||
env = "SE_LISTEN"
|
||||
)]
|
||||
listen: String,
|
||||
}
|
||||
|
||||
@@ -81,18 +108,29 @@ impl Metrics {
|
||||
/// Creates a new instance of `Metrics`
|
||||
fn new() -> Self {
|
||||
Metrics {
|
||||
num_connected_players: Gauge::new("num_connected_players", "Number of connected players").unwrap(),
|
||||
tech_tier: Gauge::new("tech_tier", "Current tech tier").unwrap(),
|
||||
total_game_duration: Gauge::new("total_game_duration", "Total game duration").unwrap(),
|
||||
average_tick_rate: Gauge::new("average_tick_rate", "Average tick rate").unwrap(),
|
||||
num_connected_players: Gauge::new(
|
||||
"satisfactory_num_connected_players",
|
||||
"Number of connected players",
|
||||
)
|
||||
.unwrap(),
|
||||
tech_tier: Gauge::new("satisfactory_tech_tier", "Current tech tier").unwrap(),
|
||||
total_game_duration: Gauge::new(
|
||||
"satisfactory_total_game_duration",
|
||||
"Total game duration",
|
||||
)
|
||||
.unwrap(),
|
||||
average_tick_rate: Gauge::new("satisfactory_average_tick_rate", "Average tick rate")
|
||||
.unwrap(),
|
||||
}
|
||||
}
|
||||
|
||||
/// Updates the metrics with the provided game state
|
||||
fn update(&self, game_state: &ServerGameState) {
|
||||
self.num_connected_players.set(game_state.num_connected_players as f64);
|
||||
self.num_connected_players
|
||||
.set(game_state.num_connected_players as f64);
|
||||
self.tech_tier.set(game_state.tech_tier as f64);
|
||||
self.total_game_duration.set(game_state.total_game_duration as f64);
|
||||
self.total_game_duration
|
||||
.set(game_state.total_game_duration as f64);
|
||||
self.average_tick_rate.set(game_state.average_tick_rate);
|
||||
}
|
||||
}
|
||||
@@ -119,10 +157,18 @@ async fn main() -> Result<(), Box<dyn std::error::Error>> {
|
||||
let metrics = Arc::new(Metrics::new());
|
||||
|
||||
// Register metrics with the registry
|
||||
registry.register(Box::new(metrics.num_connected_players.clone())).unwrap();
|
||||
registry.register(Box::new(metrics.tech_tier.clone())).unwrap();
|
||||
registry.register(Box::new(metrics.total_game_duration.clone())).unwrap();
|
||||
registry.register(Box::new(metrics.average_tick_rate.clone())).unwrap();
|
||||
registry
|
||||
.register(Box::new(metrics.num_connected_players.clone()))
|
||||
.unwrap();
|
||||
registry
|
||||
.register(Box::new(metrics.tech_tier.clone()))
|
||||
.unwrap();
|
||||
registry
|
||||
.register(Box::new(metrics.total_game_duration.clone()))
|
||||
.unwrap();
|
||||
registry
|
||||
.register(Box::new(metrics.average_tick_rate.clone()))
|
||||
.unwrap();
|
||||
|
||||
// Create shared state
|
||||
let shared_state: SharedState = Arc::new(((*metrics).clone(), registry));
|
||||
@@ -139,7 +185,9 @@ async fn main() -> Result<(), Box<dyn std::error::Error>> {
|
||||
let client = client_builder.build()?;
|
||||
|
||||
// Read the bearer token if provided
|
||||
let bearer_token = args.token_file.map(|file| fs::read_to_string(file).expect("Failed to read token file"));
|
||||
let bearer_token = args.token.or(args
|
||||
.token_file
|
||||
.map(|file| fs::read_to_string(file).expect("Failed to read token file")));
|
||||
|
||||
let query_endpoint = format!("https://{}/api/v1", args.endpoint);
|
||||
|
||||
@@ -159,14 +207,12 @@ async fn main() -> Result<(), Box<dyn std::error::Error>> {
|
||||
}
|
||||
|
||||
match request.send().await {
|
||||
Ok(response) => {
|
||||
match response.json::<ServerResponse>().await {
|
||||
Ok(server_response) => {
|
||||
metrics_clone.update(&server_response.data.server_game_state);
|
||||
}
|
||||
Err(e) => eprintln!("Failed to parse service metrics: {}", e),
|
||||
Ok(response) => match response.json::<ServerResponse>().await {
|
||||
Ok(server_response) => {
|
||||
metrics_clone.update(&server_response.data.server_game_state);
|
||||
}
|
||||
}
|
||||
Err(e) => eprintln!("Failed to parse service metrics: {}", e),
|
||||
},
|
||||
Err(e) => eprintln!("Failed to fetch metrics: {}", e),
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user