Compare commits
120 Commits
d2a2bdacd5
...
master
Author | SHA1 | Date | |
---|---|---|---|
7d3e9858bb | |||
c611dd0168 | |||
c7853d4153 | |||
1481bd1d00 | |||
1bf3c86a4c | |||
6727e1177c | |||
47f3f8b1e0 | |||
27703ff009 | |||
f788f574c0 | |||
6c19559964 | |||
f83ff0a7a6 | |||
ee69ce3c8d | |||
8f47e210d5 | |||
8c1055b468 | |||
42558f3500 | |||
0a3e8f28ba | |||
419b2b0286 | |||
59816d7410 | |||
cdb7dbf18f | |||
4696d512eb | |||
3793071c2d | |||
43e9492a47 | |||
745e4e6fe5 | |||
3274e3bc2d | |||
c463cdff0a | |||
4c7a46e493 | |||
01df7d1e13 | |||
dc06a3877d | |||
c816304a71 | |||
32a50e9de4 | |||
2bfe562269 | |||
c381d82e2a | |||
d12287a13a | |||
f193dacd14 | |||
6a9ccad31c | |||
cf49d249bd | |||
85b5b7cbce | |||
71239fa078 | |||
66f067990b | |||
9701ecdcd7 | |||
adf46b9cb8 | |||
9f40efa75a | |||
6984c35710 | |||
f395b66d77 | |||
627fe5f38f | |||
ba4807f33f | |||
3f791ad52a | |||
bfe26c2250 | |||
b1451f9e88 | |||
d8fafec1ba | |||
666036ef2e | |||
7f457497e3 | |||
7ea96c5cc3 | |||
e208b99192 | |||
58696a9566 | |||
908e9e1618 | |||
3496865c2e | |||
21c3677d0e | |||
466f96b649 | |||
4b2a428646 | |||
20acbfd7f4 | |||
d2d1597b72 | |||
d468e2b070 | |||
4a81455f96 | |||
6221a58062 | |||
98cb26e310 | |||
ab53e24978 | |||
3f399bec10 | |||
cf72d4886a | |||
3bed998640 | |||
4b7b43c036 | |||
d15f1613f0 | |||
1bd47d2b60 | |||
a8bc162d8b | |||
cf66e8c488 | |||
2b8af4050b | |||
3568e195b9 | |||
2d0f4ddca1 | |||
9ffa0c549c | |||
4543309fd1 | |||
2780f4f5f6 | |||
169d53f2cf | |||
74ccee0381 | |||
6880cd30ce | |||
d9d6d2b3bb | |||
3b33e7f9ae | |||
0f62409e88 | |||
ff11240334 | |||
358bb6272c | |||
9540a03366 | |||
0781d74067 | |||
88472c81e9 | |||
d841900cfb | |||
404fc5b267 | |||
5fd535a19c | |||
899aac977f | |||
d76c5a9131 | |||
31176e4025 | |||
7369ad4d7b | |||
0d0c3aca71 | |||
f71e423d6c | |||
103f96d5bd | |||
2cee3d9c7f | |||
b0f3d8532f | |||
4095d70722 | |||
8826a2530a | |||
53ae4dbd89 | |||
56a75560e8 | |||
9c6966f180 | |||
8383445898 | |||
29e3696be3 | |||
8f344d246f | |||
d4bba5426e | |||
dba6aa8bef | |||
2ff05f9255 | |||
fb6dbc3b5d | |||
663f997ee7 | |||
00deb93193 | |||
fd4ef15fae | |||
9453ef59d3 |
28
.gitea/workflows/ci.yml
Normal file
28
.gitea/workflows/ci.yml
Normal file
@ -0,0 +1,28 @@
|
||||
name: "Deploy CI"
|
||||
|
||||
on:
|
||||
push:
|
||||
branches:
|
||||
- master
|
||||
paths-ignore: # Files in here will not trigger a build
|
||||
- "README.md"
|
||||
- "LICENSE"
|
||||
- "grafana-dashboard.json"
|
||||
- "storage-tracker.sh"
|
||||
- "useful-stuff.md"
|
||||
- ".gitea/workflows/publish.yml"
|
||||
|
||||
jobs:
|
||||
deploy:
|
||||
runs-on: ubuntu-22.04
|
||||
steps:
|
||||
- name: Cloning repo
|
||||
uses: actions/checkout@v4
|
||||
with:
|
||||
fetch-depth: 0
|
||||
|
||||
- name: Push to dokku
|
||||
uses: dokku/github-action@master
|
||||
with:
|
||||
git_remote_url: "ssh://dokku@51.158.63.74:22/mc-tracker"
|
||||
ssh_private_key: ${{ secrets.SSH_PRIVATE_KEY }}
|
@ -4,7 +4,13 @@ on:
|
||||
push:
|
||||
branches:
|
||||
- "master"
|
||||
- "influx-only"
|
||||
paths-ignore: # Files in here will not trigger a build
|
||||
- "README.md"
|
||||
- "LICENSE"
|
||||
- "grafana-dashboard.json"
|
||||
- "storage-tracker.sh"
|
||||
- "useful-stuff.md"
|
||||
- ".gitea/workflows/ci.yml"
|
||||
|
||||
jobs:
|
||||
docker:
|
||||
@ -14,7 +20,7 @@ jobs:
|
||||
uses: actions/checkout@v4
|
||||
|
||||
- name: Restore Docker Cache
|
||||
uses: actions/cache@v3
|
||||
uses: actions/cache@v4
|
||||
id: docker-cache
|
||||
with:
|
||||
path: /usr/bin/docker
|
||||
@ -37,26 +43,11 @@ jobs:
|
||||
username: ${{ secrets.REPO_USERNAME }}
|
||||
password: ${{ secrets.REPO_TOKEN }}
|
||||
|
||||
- name: Restore Docker Build Cache
|
||||
uses: actions/cache@v3
|
||||
id: build-cache
|
||||
with:
|
||||
path: /tmp/.buildx-cache
|
||||
key: ${{ runner.os }}-buildx
|
||||
|
||||
- name: Build and Push (Latest)
|
||||
uses: docker/build-push-action@v5
|
||||
uses: docker/build-push-action@v6
|
||||
with:
|
||||
push: true
|
||||
context: .
|
||||
tags: fascinated/mc-tracker-backend:influx
|
||||
tags: fascinated/mc-tracker:latest
|
||||
cache-from: type=local,src=/tmp/.buildx-cache
|
||||
cache-to: type=local,dest=/tmp/.buildx-cache
|
||||
|
||||
- name: Save Docker Build Cache
|
||||
if: steps.build-cache.outputs.cache-hit != 'true'
|
||||
run: |
|
||||
mkdir -p /tmp/.buildx-cache
|
||||
cp -r /tmp/.buildx-cache/. /tmp/.buildx-cache-new
|
||||
rm -rf /tmp/.buildx-cache
|
||||
mv /tmp/.buildx-cache-new /tmp/.buildx-cache
|
3
.gitignore
vendored
3
.gitignore
vendored
@ -229,5 +229,4 @@ data/db.sqlite-shm
|
||||
data/db.sqlite-wal
|
||||
data/database-backups
|
||||
|
||||
data/config.json
|
||||
data/servers.json
|
||||
data/config.json
|
15
Dockerfile
15
Dockerfile
@ -1,9 +1,12 @@
|
||||
FROM node:lts-alpine
|
||||
FROM fascinated/docker-images:node-pnpm-latest AS base
|
||||
|
||||
ENV NODE_ENV=production
|
||||
WORKDIR /usr/src/app
|
||||
COPY ["package.json", "package-lock.json*", "npm-shrinkwrap.json*", "./"]
|
||||
RUN npm install --production --silent && mv node_modules ../
|
||||
|
||||
COPY ["package.json", "pnpm-lock.yaml", "./"]
|
||||
|
||||
RUN pnpm install --production --silent
|
||||
|
||||
COPY . .
|
||||
RUN chown -R node /usr/src/app
|
||||
USER node
|
||||
CMD ["node", "index.js"]
|
||||
|
||||
CMD pnpm run build && pnpm run start
|
||||
|
@ -1,2 +1,9 @@
|
||||
# backend
|
||||
# Mc Tracker
|
||||
|
||||
You can view the live instance of this project [here](https://mc-tracker.fascinated.cc).
|
||||
|
||||
## Features
|
||||
|
||||
- Track Minecraft server's player count over time
|
||||
- See player count peaks (all time)
|
||||
- Minecraft Java and Bedrock support
|
||||
|
@ -1,15 +1,13 @@
|
||||
{
|
||||
"influx": {
|
||||
"url": "http://localhost:8086",
|
||||
"token": "my-token",
|
||||
"org": "my-org",
|
||||
"bucket": "my-bucket"
|
||||
"token": "setme",
|
||||
"org": "setme",
|
||||
"bucket": "mc-tracker"
|
||||
},
|
||||
"scanner": {
|
||||
"updateCron": "*/1 * * * *",
|
||||
"pinger": {
|
||||
"pingCron": "*/1 * * * *",
|
||||
"dnsInvalidationCron": "0 */6 * * *",
|
||||
"timeout": 2000
|
||||
},
|
||||
"backup": {
|
||||
"cron": "0 0 * * *"
|
||||
}
|
||||
}
|
||||
|
127
data/servers.json
Normal file
127
data/servers.json
Normal file
@ -0,0 +1,127 @@
|
||||
[
|
||||
{
|
||||
"name": "HiveMC",
|
||||
"ip": "geo.hivebedrock.network",
|
||||
"type": "PE"
|
||||
},
|
||||
{
|
||||
"name": "Cubecraft - Bedrock",
|
||||
"ip": "play.cubecraft.net",
|
||||
"type": "PE"
|
||||
},
|
||||
{
|
||||
"name": "WildNetwork",
|
||||
"ip": "play.wildnetwork.net",
|
||||
"type": "PC"
|
||||
},
|
||||
{
|
||||
"name": "Hypixel",
|
||||
"ip": "mc.hypixel.net",
|
||||
"type": "PC"
|
||||
},
|
||||
{
|
||||
"name": "Cubecraft",
|
||||
"ip": "play.cubecraft.net",
|
||||
"type": "PC"
|
||||
},
|
||||
{
|
||||
"name": "Mineplex",
|
||||
"ip": "us.mineplex.com",
|
||||
"type": "PC"
|
||||
},
|
||||
{
|
||||
"name": "2b2t",
|
||||
"ip": "2b2t.org",
|
||||
"type": "PC"
|
||||
},
|
||||
{
|
||||
"name": "AkumaMC",
|
||||
"ip": "akumamc.net",
|
||||
"type": "PC"
|
||||
},
|
||||
{
|
||||
"name": "Wynncraft",
|
||||
"ip": "play.wynncraft.com",
|
||||
"type": "PC"
|
||||
},
|
||||
{
|
||||
"name": "Minehut",
|
||||
"ip": "minehut.com",
|
||||
"type": "PC"
|
||||
},
|
||||
{
|
||||
"name": "Grand Theft Minecart",
|
||||
"ip": "gtm.network",
|
||||
"type": "PC"
|
||||
},
|
||||
{
|
||||
"name": "Purple Prison",
|
||||
"ip": "MCSL.PURPLE.WTF",
|
||||
"type": "PC"
|
||||
},
|
||||
{
|
||||
"name": "MinecraftOnline",
|
||||
"ip": "minecraftonline.com",
|
||||
"type": "PC"
|
||||
},
|
||||
{
|
||||
"name": "Hoplite",
|
||||
"ip": "hoplite.gg",
|
||||
"type": "PC"
|
||||
},
|
||||
{
|
||||
"name": "Minemen Club",
|
||||
"ip": "minemen.club",
|
||||
"type": "PC"
|
||||
},
|
||||
{
|
||||
"name": "OPLegends",
|
||||
"ip": "play.oplegends.com",
|
||||
"type": "PC"
|
||||
},
|
||||
{
|
||||
"name": "Minecadia",
|
||||
"ip": "play.minecadia.com",
|
||||
"type": "PC"
|
||||
},
|
||||
{
|
||||
"name": "Manacube",
|
||||
"ip": "play.manacube.com",
|
||||
"type": "PC"
|
||||
},
|
||||
{
|
||||
"name": "InvadedLands",
|
||||
"ip": "invadedlands.net",
|
||||
"type": "PC"
|
||||
},
|
||||
{
|
||||
"name": "Complex Gaming",
|
||||
"ip": "hub.mc-complex.com",
|
||||
"type": "PC"
|
||||
},
|
||||
{
|
||||
"name": "Monumenta",
|
||||
"ip": "server.playmonumenta.com",
|
||||
"type": "PC"
|
||||
},
|
||||
{
|
||||
"name": "Shotbow",
|
||||
"ip": "play.shotbow.net",
|
||||
"type": "PC"
|
||||
},
|
||||
{
|
||||
"name": "Stray",
|
||||
"ip": "stray.gg",
|
||||
"type": "PC"
|
||||
},
|
||||
{
|
||||
"name": "Aetheria Anarchy",
|
||||
"ip": "aetheria.cc",
|
||||
"type": "PC"
|
||||
},
|
||||
{
|
||||
"name": "VexedMC",
|
||||
"ip": "play.vexedmc.com",
|
||||
"type": "PC"
|
||||
}
|
||||
]
|
@ -1,74 +0,0 @@
|
||||
[
|
||||
{
|
||||
"name": "WildPrison",
|
||||
"ip": "wildprison.net",
|
||||
"type": "PC",
|
||||
"id": 0
|
||||
},
|
||||
{
|
||||
"name": "Hypixel",
|
||||
"ip": "mc.hypixel.net",
|
||||
"type": "PC",
|
||||
"id": 1
|
||||
},
|
||||
{
|
||||
"name": "CubeCraft",
|
||||
"ip": "play.cubecraft.net",
|
||||
"type": "PC",
|
||||
"id": 2
|
||||
},
|
||||
{
|
||||
"name": "Mineplex",
|
||||
"ip": "mineplex.com",
|
||||
"type": "PC",
|
||||
"id": 3
|
||||
},
|
||||
{
|
||||
"name": "2b2t",
|
||||
"ip": "2b2t.org",
|
||||
"type": "PC",
|
||||
"id": 4
|
||||
},
|
||||
{
|
||||
"name": "AkumaMC",
|
||||
"ip": "akumamc.net",
|
||||
"type": "PC",
|
||||
"id": 5
|
||||
},
|
||||
{
|
||||
"name": "Wynncraft",
|
||||
"ip": "play.wynncraft.com",
|
||||
"type": "PC",
|
||||
"id": 6
|
||||
},
|
||||
{
|
||||
"name": "Minehut",
|
||||
"ip": "minehut.com",
|
||||
"type": "PC",
|
||||
"id": 7
|
||||
},
|
||||
{
|
||||
"name": "Grand Theft Minecraft",
|
||||
"ip": "gtm.network",
|
||||
"type": "PC",
|
||||
"id": 8
|
||||
},
|
||||
{
|
||||
"name": "HiveMC",
|
||||
"ip": "geo.hivebedrock.network",
|
||||
"type": "PE",
|
||||
"id": 9
|
||||
},
|
||||
{
|
||||
"name": "Purple Prison",
|
||||
"ip": "MCSL.PURPLE.WTF",
|
||||
"type": "PC",
|
||||
"id": 10
|
||||
},
|
||||
{
|
||||
"name": "MinecraftOnline",
|
||||
"ip": "minecraftonline.com",
|
||||
"type": "PC",
|
||||
"id": 11
|
||||
}
|
||||
]
|
10
data/websites.json
Normal file
10
data/websites.json
Normal file
@ -0,0 +1,10 @@
|
||||
[
|
||||
{
|
||||
"name": "Minecraft Textures",
|
||||
"url": "https://textures.minecraft.net"
|
||||
},
|
||||
{
|
||||
"name": "Minecraft Session Server",
|
||||
"url": "https://session.minecraft.net"
|
||||
}
|
||||
]
|
@ -1,7 +1,34 @@
|
||||
version: "3.4"
|
||||
version: "3"
|
||||
|
||||
services:
|
||||
backend:
|
||||
image: fascinated/mc-tracker-backend:influx
|
||||
tracker:
|
||||
restart: always
|
||||
image: fascinated/mc-tracker:latest
|
||||
volumes:
|
||||
- ./data:/usr/src/app/data
|
||||
|
||||
grafana:
|
||||
image: grafana/grafana:latest
|
||||
environment:
|
||||
NODE_ENV: production
|
||||
- GF_ANALYTICS_REPORTING_ENABLED=false
|
||||
- GF_SERVER_DOMAIN=mc-tracker.fascinated.cc
|
||||
- GF_AUTH_ANONYMOUS_ENABLED=true
|
||||
ports:
|
||||
- 3000:3000
|
||||
volumes:
|
||||
- /home/grafana/data:/var/lib/grafana
|
||||
restart: always
|
||||
|
||||
influxdb:
|
||||
image: influxdb:latest
|
||||
container_name: influxdb
|
||||
networks:
|
||||
- default
|
||||
security_opt:
|
||||
- no-new-privileges:true
|
||||
restart: always
|
||||
ports:
|
||||
- "8086:8086"
|
||||
volumes:
|
||||
- ./influx/config:/etc/influxdb2
|
||||
- ./influx/db:/var/lib/influxdb2
|
||||
|
1282
grafana-dashboard.json
Normal file
1282
grafana-dashboard.json
Normal file
File diff suppressed because it is too large
Load Diff
4084
package-lock.json
generated
4084
package-lock.json
generated
File diff suppressed because it is too large
Load Diff
@ -5,7 +5,7 @@
|
||||
"main": "index.js",
|
||||
"scripts": {
|
||||
"dev": "nodemon --exec ts-node src/index.ts",
|
||||
"build": "tsup src/index.ts --format cjs,esm --dts",
|
||||
"build": "tsup src/index.ts --format cjs",
|
||||
"start": "node dist/index.js"
|
||||
},
|
||||
"keywords": [],
|
||||
@ -14,7 +14,9 @@
|
||||
"dependencies": {
|
||||
"@influxdata/influxdb-client": "^1.33.2",
|
||||
"@types/mcping-js": "^1.5.4",
|
||||
"@types/node": "^20.10.6",
|
||||
"@types/node-cron": "^3.0.11",
|
||||
"axios": "^1.6.4",
|
||||
"dns": "^0.2.2",
|
||||
"mcpe-ping-fixed": "^0.0.3",
|
||||
"mcping-js": "^1.5.0",
|
||||
@ -23,8 +25,5 @@
|
||||
"tsup": "^8.0.1",
|
||||
"typescript": "^5.3.3",
|
||||
"winston": "^3.11.0"
|
||||
},
|
||||
"devDependencies": {
|
||||
"@types/node": "^20.10.6"
|
||||
}
|
||||
}
|
||||
|
754
pnpm-lock.yaml
generated
754
pnpm-lock.yaml
generated
File diff suppressed because it is too large
Load Diff
3
renovate.json
Normal file
3
renovate.json
Normal file
@ -0,0 +1,3 @@
|
||||
{
|
||||
"extends": ["config:recommended", ":dependencyDashboard"]
|
||||
}
|
16
src/index.ts
16
src/index.ts
@ -1,20 +1,14 @@
|
||||
import Influx from "./influx/influx";
|
||||
import Scanner from "./scanner/scanner";
|
||||
import ServerManager from "./server/serverManager";
|
||||
|
||||
/**
|
||||
* The server manager instance.
|
||||
*/
|
||||
export const serverManager = new ServerManager();
|
||||
import WebsiteManager from "./website/websiteManager";
|
||||
import {logger} from "./utils/logger";
|
||||
|
||||
/**
|
||||
* The influx database instance.
|
||||
*/
|
||||
export const influx = new Influx();
|
||||
|
||||
(async () => {
|
||||
await serverManager.init();
|
||||
new ServerManager();
|
||||
new WebsiteManager();
|
||||
|
||||
// The scanner is responsible for scanning all servers
|
||||
new Scanner();
|
||||
})();
|
||||
logger.info("Done loading!");
|
@ -14,13 +14,13 @@ export default class Influx {
|
||||
url: Config.influx.url,
|
||||
token: Config.influx.token,
|
||||
});
|
||||
logger.info("InfluxDB initialized");
|
||||
|
||||
this.writeApi = this.influx.getWriteApi(
|
||||
Config.influx.org,
|
||||
Config.influx.bucket,
|
||||
"ms"
|
||||
);
|
||||
|
||||
logger.info("InfluxDB initialized");
|
||||
}
|
||||
|
||||
/**
|
||||
@ -28,7 +28,7 @@ export default class Influx {
|
||||
*
|
||||
* @param point the point to write
|
||||
*/
|
||||
public async writePoint(point: Point): Promise<void> {
|
||||
public writePoint(point: Point) {
|
||||
this.writeApi.writePoint(point);
|
||||
}
|
||||
}
|
||||
|
@ -1,29 +0,0 @@
|
||||
import cron from "node-cron";
|
||||
|
||||
import { serverManager } from "..";
|
||||
import Config from "../../data/config.json";
|
||||
import { logger } from "../utils/logger";
|
||||
|
||||
export default class Scanner {
|
||||
constructor() {
|
||||
logger.info("Loading scanner database");
|
||||
|
||||
cron.schedule(Config.scanner.updateCron, () => {
|
||||
this.scanServers();
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* Start a server scan to ping all servers.
|
||||
*/
|
||||
private async scanServers(): Promise<void> {
|
||||
logger.info(`Scanning servers ${serverManager.getServers().length}`);
|
||||
|
||||
// ping all servers in parallel
|
||||
await Promise.all(
|
||||
serverManager.getServers().map((server) => server.pingServer())
|
||||
);
|
||||
|
||||
logger.info("Finished scanning servers");
|
||||
}
|
||||
}
|
@ -15,12 +15,7 @@ import { logger } from "../utils/logger";
|
||||
*/
|
||||
export type ServerType = "PC" | "PE";
|
||||
|
||||
export enum ServerStatus {
|
||||
OFFLINE = "Unable to reach host",
|
||||
}
|
||||
|
||||
type ServerOptions = {
|
||||
id: number;
|
||||
name: string;
|
||||
ip: string;
|
||||
port?: number;
|
||||
@ -33,35 +28,25 @@ type DnsInfo = {
|
||||
};
|
||||
|
||||
export default class Server {
|
||||
/**
|
||||
* The ID of the server.
|
||||
*/
|
||||
private id: number;
|
||||
|
||||
/**
|
||||
* The name of the server.
|
||||
*/
|
||||
private name: string;
|
||||
private readonly name: string;
|
||||
|
||||
/**
|
||||
* The IP address of the server.
|
||||
*/
|
||||
private ip: string;
|
||||
private readonly ip: string;
|
||||
|
||||
/**
|
||||
* The port of the server.
|
||||
*/
|
||||
private port: number | undefined;
|
||||
private readonly port: number | undefined;
|
||||
|
||||
/**
|
||||
* The type of server.
|
||||
*/
|
||||
private type: ServerType;
|
||||
|
||||
/**
|
||||
* The favicon of the server.
|
||||
*/
|
||||
private favicon: string | undefined;
|
||||
private readonly type: ServerType;
|
||||
|
||||
/**
|
||||
* The resolved server information from
|
||||
@ -71,8 +56,7 @@ export default class Server {
|
||||
hasResolved: false,
|
||||
};
|
||||
|
||||
constructor({ id, name, ip, port, type }: ServerOptions) {
|
||||
this.id = id;
|
||||
constructor({ name, ip, port, type }: ServerOptions) {
|
||||
this.name = name;
|
||||
this.ip = ip;
|
||||
this.port = port;
|
||||
@ -85,6 +69,7 @@ export default class Server {
|
||||
* @returns the ping response or undefined if the server is offline
|
||||
*/
|
||||
public async pingServer(): Promise<Ping | undefined> {
|
||||
const before = Date.now();
|
||||
try {
|
||||
let response;
|
||||
|
||||
@ -106,13 +91,16 @@ export default class Server {
|
||||
try {
|
||||
influx.writePoint(
|
||||
new Point("playerCount")
|
||||
.tag("id", this.getID().toString())
|
||||
.tag("ip", this.getIP().toLowerCase())
|
||||
.tag("name", this.getName())
|
||||
.intField("playerCount", response.playerCount)
|
||||
.intField("latency", Date.now() - before)
|
||||
.timestamp(response.timestamp)
|
||||
);
|
||||
} catch (err) {
|
||||
logger.warn(`Failed to write ping to influxdb`, err);
|
||||
logger.warn(
|
||||
`Failed to write point to Influx for ${this.getName()} - ${this.getIP()}`,
|
||||
err
|
||||
);
|
||||
}
|
||||
|
||||
return Promise.resolve(response);
|
||||
@ -155,15 +143,14 @@ export default class Server {
|
||||
|
||||
const serverPing = new javaPing.MinecraftServer(ip, port);
|
||||
|
||||
// todo: do something to get the latest protocol? (is this even needed??)
|
||||
return new Promise((resolve, reject) => {
|
||||
serverPing.ping(Config.scanner.timeout, 765, (err, res) => {
|
||||
serverPing.ping(Config.pinger.timeout, 765, (err, res) => {
|
||||
if (err || res == undefined) {
|
||||
return reject(err);
|
||||
}
|
||||
|
||||
this.favicon = res.favicon; // Set the favicon
|
||||
resolve({
|
||||
id: this.getID(),
|
||||
timestamp: Date.now(),
|
||||
ip: ip,
|
||||
playerCount: res.players.online,
|
||||
@ -189,7 +176,6 @@ export default class Server {
|
||||
}
|
||||
|
||||
resolve({
|
||||
id: this.getID(),
|
||||
timestamp: Date.now(),
|
||||
ip: this.getIP(),
|
||||
playerCount: res.currentPlayers,
|
||||
@ -200,12 +186,12 @@ export default class Server {
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the ID of the server.
|
||||
*
|
||||
* @returns the ID
|
||||
* Invalidates the DNS cache for the server.
|
||||
*/
|
||||
public getID(): number {
|
||||
return this.id;
|
||||
public invalidateDns() {
|
||||
this.dnsInfo = {
|
||||
hasResolved: false,
|
||||
};
|
||||
}
|
||||
|
||||
/**
|
||||
@ -243,13 +229,4 @@ export default class Server {
|
||||
public getType(): ServerType {
|
||||
return this.type;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the favicon of the server.
|
||||
*
|
||||
* @returns the favicon
|
||||
*/
|
||||
public getFavicon(): string | undefined {
|
||||
return this.favicon;
|
||||
}
|
||||
}
|
||||
|
@ -1,44 +1,46 @@
|
||||
import cron from "node-cron";
|
||||
import { logger } from "../utils/logger";
|
||||
import Server, { ServerType } from "./server";
|
||||
|
||||
import Config from "../../data/config.json";
|
||||
import Servers from "../../data/servers.json";
|
||||
import { logger } from "../utils/logger";
|
||||
|
||||
export default class ServerManager {
|
||||
private servers: Server[] = [];
|
||||
|
||||
constructor() {}
|
||||
|
||||
/**
|
||||
* Loads the servers from the config file.
|
||||
*/
|
||||
async init() {
|
||||
logger.info("Loading servers");
|
||||
constructor() {
|
||||
logger.info("Loading servers...");
|
||||
for (const configServer of Servers) {
|
||||
const server = new Server({
|
||||
id: configServer.id,
|
||||
ip: configServer.ip,
|
||||
name: configServer.name,
|
||||
type: configServer.type as ServerType,
|
||||
});
|
||||
this.servers.push(server);
|
||||
}
|
||||
// do an inital ping of all servers to load data from them
|
||||
await Promise.all(
|
||||
this.servers.map((server) => {
|
||||
try {
|
||||
server.pingServer();
|
||||
} catch (err) {}
|
||||
})
|
||||
);
|
||||
logger.info(`Loaded ${this.servers.length} servers`);
|
||||
logger.info(`Loaded ${this.servers.length} servers!`);
|
||||
|
||||
cron.schedule(Config.pinger.pingCron, async () => {
|
||||
await this.pingServers();
|
||||
});
|
||||
|
||||
cron.schedule(Config.pinger.dnsInvalidationCron, () => {
|
||||
logger.info("Invalidating DNS cache for all servers");
|
||||
for (const server of this.servers) {
|
||||
server.invalidateDns();
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the servers.
|
||||
*
|
||||
* @returns the servers
|
||||
* Ping all servers to update their status.
|
||||
*/
|
||||
public getServers(): Server[] {
|
||||
return this.servers;
|
||||
private async pingServers(): Promise<void> {
|
||||
logger.info(`Pinging servers ${this.servers.length}`);
|
||||
|
||||
// ping all servers in parallel
|
||||
await Promise.all(this.servers.map((server) => server.pingServer()));
|
||||
|
||||
logger.info("Finished pinging servers!");
|
||||
}
|
||||
}
|
||||
|
@ -1,5 +1,4 @@
|
||||
export type Ping = {
|
||||
id: number;
|
||||
timestamp: number;
|
||||
ip: string;
|
||||
playerCount: number;
|
||||
|
@ -1,41 +0,0 @@
|
||||
import fs from "fs";
|
||||
|
||||
/**
|
||||
* Creates a directory at the given path.
|
||||
*
|
||||
* @param path the path to the file
|
||||
* @param recursive whether to create the directory tree if it doesn't exist (defaults to true)
|
||||
* @returns a promise that resolves when the file is created
|
||||
*/
|
||||
export async function createDirectory(
|
||||
path: string,
|
||||
recursive?: boolean
|
||||
): Promise<void> {
|
||||
if (recursive == undefined) {
|
||||
recursive = true; // Set to true by default
|
||||
}
|
||||
|
||||
return new Promise((resolve, reject) => {
|
||||
fs.mkdir(path, { recursive: recursive }, (err) => {
|
||||
if (err) {
|
||||
reject(err);
|
||||
} else {
|
||||
resolve();
|
||||
}
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* Checks if a file exists at the given path.
|
||||
*
|
||||
* @param path the path to the file
|
||||
* @returns a promise that returns true if the file exists, false otherwise
|
||||
*/
|
||||
export async function exists(path: string): Promise<boolean> {
|
||||
return new Promise((resolve) => {
|
||||
fs.exists(path, (exists) => {
|
||||
resolve(exists);
|
||||
});
|
||||
});
|
||||
}
|
@ -9,7 +9,7 @@ interface LogInfo {
|
||||
}
|
||||
|
||||
const customFormat = format.combine(
|
||||
timestamp({ format: "YY-MM-DD HH:MM:SS" }),
|
||||
timestamp({ format: "YYYY-MM-DD HH:mm:ss" }),
|
||||
printf((info: LogInfo) => {
|
||||
return `[${info.timestamp}] ${info.level}: ${info.message}`;
|
||||
})
|
||||
|
@ -1,18 +0,0 @@
|
||||
/**
|
||||
* Gets the current date as YYYY-MM-DD.
|
||||
*
|
||||
* @returns the date
|
||||
*/
|
||||
export function getFormattedDate() {
|
||||
return new Date().toISOString().slice(0, 10);
|
||||
}
|
||||
|
||||
/**
|
||||
* Formats a timestamp as YYYY-MM-DD.
|
||||
*
|
||||
* @param timestamp the timestamp
|
||||
* @returns the formatted timestamp
|
||||
*/
|
||||
export function formatTimestamp(timestamp: number) {
|
||||
return new Date(timestamp).toISOString().slice(0, 10);
|
||||
}
|
64
src/website/website.ts
Normal file
64
src/website/website.ts
Normal file
@ -0,0 +1,64 @@
|
||||
import { Point } from "@influxdata/influxdb-client";
|
||||
import axios from "axios";
|
||||
import { influx } from "..";
|
||||
import { logger } from "../utils/logger";
|
||||
|
||||
import Config from "../../data/config.json";
|
||||
|
||||
type WebsiteOptions = {
|
||||
name: string;
|
||||
url: string;
|
||||
};
|
||||
|
||||
export default class Website {
|
||||
/**
|
||||
* The name of the website.
|
||||
*/
|
||||
private readonly name: string;
|
||||
|
||||
/**
|
||||
* The url of the website.
|
||||
*/
|
||||
private readonly url: string;
|
||||
|
||||
constructor({ name, url }: WebsiteOptions) {
|
||||
this.name = name;
|
||||
this.url = url;
|
||||
}
|
||||
|
||||
/**
|
||||
* Pings a website and gets the response.
|
||||
*
|
||||
* @returns the response
|
||||
*/
|
||||
public async pingWebsite(): Promise<void> {
|
||||
try {
|
||||
const before = Date.now();
|
||||
const response = await axios.get(this.url, {
|
||||
validateStatus: () => true, // Don't throw a error on non-200 status codes
|
||||
timeout: Config.pinger.timeout,
|
||||
});
|
||||
if (response.status === 500) {
|
||||
throw new Error("Server returned 500 status code");
|
||||
}
|
||||
|
||||
const responseTime = Date.now() - before;
|
||||
|
||||
influx.writePoint(
|
||||
new Point("websiteStatus")
|
||||
.tag("name", this.name)
|
||||
.booleanField("online", true)
|
||||
.intField("responseTime", responseTime)
|
||||
.timestamp(Date.now())
|
||||
);
|
||||
} catch (err) {
|
||||
logger.warn(`Failed to ping ${this.name}:`, err);
|
||||
influx.writePoint(
|
||||
new Point("websiteStatus")
|
||||
.tag("name", this.name)
|
||||
.booleanField("online", false)
|
||||
.timestamp(Date.now())
|
||||
);
|
||||
}
|
||||
}
|
||||
}
|
38
src/website/websiteManager.ts
Normal file
38
src/website/websiteManager.ts
Normal file
@ -0,0 +1,38 @@
|
||||
import cron from "node-cron";
|
||||
import { logger } from "../utils/logger";
|
||||
|
||||
import Config from "../../data/config.json";
|
||||
import Websites from "../../data/websites.json";
|
||||
import Website from "./website";
|
||||
|
||||
export default class WebsiteManager {
|
||||
private websites: Website[] = [];
|
||||
|
||||
constructor() {
|
||||
logger.info("Loading websites...");
|
||||
for (const configWebsite of Websites) {
|
||||
const website = new Website({
|
||||
name: configWebsite.name,
|
||||
url: configWebsite.url,
|
||||
});
|
||||
this.websites.push(website);
|
||||
}
|
||||
logger.info(`Loaded ${this.websites.length} websites!`);
|
||||
|
||||
cron.schedule(Config.pinger.pingCron, async () => {
|
||||
await this.pingWebsites();
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* Ping all websites to update their status.
|
||||
*/
|
||||
private async pingWebsites(): Promise<void> {
|
||||
logger.info(`Pinging websites ${this.websites.length}`);
|
||||
|
||||
// ping all websites in parallel
|
||||
await Promise.all(this.websites.map((website) => website.pingWebsite()));
|
||||
|
||||
logger.info("Finished pinging websites!");
|
||||
}
|
||||
}
|
12
storage-tracker.sh
Normal file
12
storage-tracker.sh
Normal file
@ -0,0 +1,12 @@
|
||||
usage_engine=$(du -s /home/tracker/influx/db/engine/data/setme/ | cut -f1)
|
||||
usage_wal=$(du -s /home/tracker/influx/db/engine/wal/setme/ | cut -f1)
|
||||
|
||||
# Calculate the sum of usage_engine and usage_wal
|
||||
total_usage=$((usage_engine + usage_wal))
|
||||
|
||||
docker exec influxdb influx write \
|
||||
--org homelab \
|
||||
--bucket influx_metrics \
|
||||
--token setme \
|
||||
--precision s \
|
||||
"storage_usage,db=mc-tracker value=$total_usage"
|
7
useful-stuff.md
Normal file
7
useful-stuff.md
Normal file
@ -0,0 +1,7 @@
|
||||
# Useful stuff
|
||||
|
||||
## Deleteing a specific server from influx
|
||||
|
||||
```bash
|
||||
influx delete --bucket mc-tracker --start 2024-01-01T00:00:00Z --stop 2024-01-05T00:00:00Z --org homelab --token nou --predicate '_measurement="playerCount" AND "name"="Grand Theft Minecraft"'
|
||||
```
|
Reference in New Issue
Block a user