File size: 1,602 Bytes
85783bc
3278e26
 
 
85783bc
3278e26
2c9ad36
 
 
3278e26
 
 
2c9ad36
3278e26
2c9ad36
3278e26
 
 
 
 
 
 
 
 
 
2c9ad36
3278e26
 
2c9ad36
3278e26
 
 
2c9ad36
3278e26
 
 
 
 
 
2c9ad36
3278e26
2c9ad36
3278e26
2c9ad36
3278e26
 
 
2c9ad36
3278e26
2c9ad36
3278e26
 
2c9ad36
3278e26
2c9ad36
3278e26
 
2c9ad36
85783bc
3278e26
85783bc
3278e26
85783bc
 
3278e26
2c9ad36
3278e26
85783bc
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
const express = require("express");
const axios = require("axios");
const fs = require("fs");
const path = require("path");

const app = express();
const PORT = process.env.PORT || 7860;
const startTime = Date.now();

// Load uptime targets
const configPath = path.join(__dirname, "up.json");
const config = JSON.parse(fs.readFileSync(configPath, "utf8"));

let stats = {};

// Init stats
config.targets.forEach(t => {
  stats[t.name] = {
    url: t.url,
    success: 0,
    fail: 0,
    lastPing: null,
    lastStatus: "pending"
  };
});

// Serve static HTML
app.use(express.static(path.join(__dirname, "public")));

// ๐Ÿ” UPTIME PINGER
async function pingTargets() {
  for (const target of config.targets) {
    try {
      await axios.get(target.url, { timeout: 8000 });
      stats[target.name].success++;
      stats[target.name].lastStatus = "online";
    } catch (err) {
      stats[target.name].fail++;
      stats[target.name].lastStatus = "offline";
    }
    stats[target.name].lastPing = new Date().toLocaleString();
  }
}

// Run pinger
setInterval(pingTargets, config.interval_seconds * 1000);
pingTargets();

// ๐Ÿง  STATUS API
app.get("/status", (req, res) => {
  const uptimeSeconds = Math.floor((Date.now() - startTime) / 1000);
  res.json({
    status: "running",
    uptimeSeconds,
    serverTime: new Date().toLocaleString(),
    targets: stats
  });
});

// Root
app.get("/", (req, res) => {
  res.sendFile(path.join(__dirname, "public", "index.html"));
});

// Start server
app.listen(PORT, () => {
  console.log(`๐Ÿš€ Uptime Monitor running on http://localhost:${PORT}`);
});