-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapi.php
More file actions
120 lines (104 loc) · 3.28 KB
/
Copy pathapi.php
File metadata and controls
120 lines (104 loc) · 3.28 KB
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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
<?php
header('Content-Type: application/json');
header('Access-Control-Allow-Origin: *');
$db_url = getenv('DATABASE_URL');
$p = parse_url($db_url);
$host = $p['host'];
$port = $p['port'] ?? 5432;
$dbname = ltrim($p['path'], '/');
$user = $p['user'];
$pass = $p['pass'];
$endpoint = explode('.', $host)[0];
$dsn = "pgsql:host=$host;port=$port;dbname=$dbname;sslmode=require;options=endpoint=$endpoint";
try {
$pdo = new PDO($dsn, $user, $pass, [
PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION
]);
} catch (Exception $e) {
http_response_code(500);
echo json_encode(['error' => 'DB connection failed', 'detail' => $e->getMessage()]);
exit;
}
$method = $_SERVER['REQUEST_METHOD'];
// UptimeRobot HEAD request
if ($method === 'HEAD') {
http_response_code(200);
exit;
}
// UptimeRobot ping
if ($method === 'GET' && isset($_GET['ping'])) {
echo json_encode(['status' => 'ok', 'message' => 'pong']);
exit;
}
// ================
// GET - Dashboard ke liye
// ================
if ($method === 'GET') {
$stmt = $pdo->query("
SELECT
m.cpu_percent, m.ram_percent, m.disk_percent, m.uptime,
m.iowait, m.swap_percent,
m.load_1, m.load_5, m.load_15,
m.processes, m.threads, m.users,
m.recorded_at, s.hostname
FROM metrics m
JOIN servers s ON s.id = m.server_id
ORDER BY m.recorded_at DESC
LIMIT 20
");
$rows = $stmt->fetchAll(PDO::FETCH_ASSOC);
echo json_encode($rows);
exit;
}
// ================
// POST - Agent se data receive karo
// ================
if ($method === 'POST') {
$body = json_decode(file_get_contents('php://input'), true);
if (!$body) {
http_response_code(400);
echo json_encode(['error' => 'Invalid JSON']);
exit;
}
$api_key = $body['api_key'] ?? '';
$hostname = $body['hostname'] ?? '';
$stmt = $pdo->prepare("SELECT id FROM servers WHERE hostname = ? AND api_key = ?");
$stmt->execute([$hostname, $api_key]);
$server = $stmt->fetch();
if (!$server) {
http_response_code(401);
echo json_encode(['error' => 'Invalid api_key or hostname']);
exit;
}
// --- Metrics save karo ---
$stmt = $pdo->prepare("
INSERT INTO metrics (
server_id, cpu_percent, ram_percent, disk_percent, uptime,
iowait, swap_percent,
load_1, load_5, load_15,
processes, threads, users
) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)
");
$stmt->execute([
$server['id'],
$body['cpu_percent'] ?? 0,
$body['ram_percent'] ?? 0,
$body['disk_percent'] ?? 0,
$body['uptime'] ?? '',
$body['iowait'] ?? 0,
$body['swap_percent'] ?? 0,
$body['load_1'] ?? 0,
$body['load_5'] ?? 0,
$body['load_15'] ?? 0,
$body['processes'] ?? 0,
$body['threads'] ?? 0,
$body['users'] ?? 0,
]);
// 7 din se purane records delete
$pdo->exec("DELETE FROM metrics WHERE recorded_at < NOW() - INTERVAL '7 days'");
echo json_encode(['status' => 'ok', 'message' => 'Metrics saved']);
exit;
}
http_response_code(405);
echo json_encode(['error' => 'Method not allowed']);
?>