forked from VinokurovVE/tests
Map caching, clickhouse test service
This commit is contained in:
97
monitor/index.ts
Normal file
97
monitor/index.ts
Normal file
@ -0,0 +1,97 @@
|
||||
import { serve } from 'bun';
|
||||
import { Database } from 'bun:sqlite';
|
||||
|
||||
const db = new Database('./data/servers.db');
|
||||
|
||||
// Initialize the database table if it doesn't exist
|
||||
db.run(`
|
||||
CREATE TABLE IF NOT EXISTS servers (
|
||||
id INTEGER PRIMARY KEY AUTOINCREMENT,
|
||||
name TEXT NOT NULL,
|
||||
ip TEXT NOT NULL,
|
||||
ping_rate INTEGER NOT NULL
|
||||
)
|
||||
`);
|
||||
|
||||
// Function to check server availability
|
||||
async function checkServer(server: { name: string; ip: string; ping_rate: number; }) {
|
||||
try {
|
||||
const response = await Bun.spawn(['ping', '-c', '1', server.ip]);
|
||||
if (response.exitCode === 0) {
|
||||
console.log(`[${new Date().toISOString()}] ${server.name} (${server.ip}) is up.`);
|
||||
} else {
|
||||
console.error(`[${new Date().toISOString()}] ${server.name} (${server.ip}) is down!`);
|
||||
}
|
||||
} catch (error) {
|
||||
console.error(`[${new Date().toISOString()}] Error pinging ${server.name} (${server.ip}):`, error);
|
||||
}
|
||||
}
|
||||
|
||||
// Function to monitor servers based on their individual ping rates
|
||||
async function monitorServer(server: any) {
|
||||
while (true) {
|
||||
await checkServer(server);
|
||||
await new Promise(resolve => setTimeout(resolve, server.ping_rate));
|
||||
}
|
||||
}
|
||||
|
||||
// Start monitoring all servers
|
||||
async function startMonitoring() {
|
||||
const servers = db.query(`SELECT * FROM servers`).all();
|
||||
servers.forEach(server => monitorServer(server));
|
||||
}
|
||||
|
||||
// Start monitoring in the background
|
||||
startMonitoring();
|
||||
|
||||
// API Server to manage servers
|
||||
const server = serve({
|
||||
port: process.env.MONITOR_PORT || 1234,
|
||||
fetch: async (req) => {
|
||||
const url = new URL(req.url);
|
||||
const pathname = url.pathname;
|
||||
const method = req.method;
|
||||
|
||||
if (pathname === '/servers' && method === 'GET') {
|
||||
const servers = db.query(`SELECT * FROM servers`).all();
|
||||
return new Response(JSON.stringify(servers), {
|
||||
headers: { 'Content-Type': 'application/json' },
|
||||
});
|
||||
}
|
||||
|
||||
if (pathname === '/server' && method === 'POST') {
|
||||
const data = await req.json();
|
||||
const { name, ip, ping_rate } = data;
|
||||
|
||||
if (!name || !ip || !ping_rate) {
|
||||
return new Response('Missing fields', { status: 400 });
|
||||
}
|
||||
|
||||
db.run(
|
||||
`INSERT INTO servers (name, ip, ping_rate) VALUES (?, ?, ?)`,
|
||||
name, ip, ping_rate
|
||||
);
|
||||
return new Response('Server added', { status: 201 });
|
||||
}
|
||||
|
||||
if (pathname.startsWith('/server/') && method === 'PUT') {
|
||||
const id = pathname.split('/').pop();
|
||||
const data = await req.json();
|
||||
const { name, ip, ping_rate } = data;
|
||||
|
||||
if (!id || !name || !ip || !ping_rate) {
|
||||
return new Response('Missing fields', { status: 400 });
|
||||
}
|
||||
|
||||
db.run(
|
||||
`UPDATE servers SET name = ?, ip = ?, ping_rate = ? WHERE id = ?`,
|
||||
name, ip, ping_rate, id
|
||||
);
|
||||
return new Response('Server updated', { status: 200 });
|
||||
}
|
||||
|
||||
return new Response('Not Found', { status: 404 });
|
||||
},
|
||||
});
|
||||
|
||||
console.log(`API server and monitoring running on http://localhost:${server.port}`);
|
Reference in New Issue
Block a user