Added back end

This commit is contained in:
Laurens van der Drift
2024-05-27 21:01:48 +02:00
parent 08f28440b9
commit 62585ae8fb
2 changed files with 104 additions and 0 deletions

63
Web/back-end.php Normal file
View File

@@ -0,0 +1,63 @@
<?php
// Define the URL and API key for the metrics endpoint
$url = '<yourURL>/metrics'; //your URL
$username = ''; //leave empty
// Retrieve an API key from the UptimeKuma dashboard here <yourURL>/settings/api-keys
$password = '***************'; //your API key
// Initialize a new cURL session
$ch = curl_init($url);
// Set cURL options
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); // Return the transfer as a string
curl_setopt($ch, CURLOPT_HTTPAUTH, CURLAUTH_BASIC); // Use basic HTTP authentication
curl_setopt($ch, CURLOPT_USERPWD, "$username:$password"); // Set username and password for the connection
// Execute the cURL session
$response = curl_exec($ch);
// Get the HTTP status code of the response
$http_status = curl_getinfo($ch, CURLINFO_HTTP_CODE);
// Close the cURL session
curl_close($ch);
// Check if the request was successful
if ($http_status == 200) {
// Parse the response and extract relevant data
preg_match_all('/monitor_status\{(.*?)\} (\d+)/', $response, $matches, PREG_SET_ORDER);
$data = [];
// Loop through each match
foreach ($matches as $match) {
$labels = [];
// Split the match into parts
$parts = explode(',', $match[1]);
// Loop through each part
foreach ($parts as $part) {
// Split the part into key and value
list($key, $value) = explode('=', $part);
// Trim and store the key-value pair
$labels[trim($key)] = trim($value, '"');
}
// Add the parsed data to the data array
$data[] = [
'monitor_name' => $labels['monitor_name'],
'monitor_type' => $labels['monitor_type'],
'monitor_url' => $labels['monitor_url'],
'monitor_hostname' => $labels['monitor_hostname'],
'monitor_port' => $labels['monitor_port'],
'status' => (int) $match[2]
];
}
// Set the content type of the response to JSON
header('Content-Type: application/json');
// Output the data in JSON format
echo json_encode($data, JSON_PRETTY_PRINT);
} else {
// Output an error message if the request was not successful
echo "Failed to fetch data. HTTP Status Code: $http_status";
}
?>

41
Web/setup.md Normal file
View File

@@ -0,0 +1,41 @@
# Uptime Kuma Metrics API
This project reads the values from the metrics published by the Uptime Kuma service and creates an open API with simple information such as monitor name, monitor type, monitor URL, monitor hostname, monitor port, and status. The status is defined by Uptime Kuma metrics as follows:
- `1 = UP`
- `0 = DOWN`
- `2 = PENDING`
- `3 = MAINTENANCE`
The following instructions will guide you through setting up and using this code to fetch and display monitor data from Uptime Kuma in JSON format.
## Prerequisites
- PHP installed on your server.
- Uptime Kuma service running and accessible.
- API key generated from the Uptime Kuma dashboard.
## Setup Instructions
1. **Retrieve Your Metrics URL and API Key**
- Navigate to your Uptime Kuma dashboard.
- Go to `Settings` -> `API Keys`.
- Generate and copy the API key.
- Identify your Uptime Kuma metrics URL (usually in the form of `<yourURL>/metrics`).
2. **Configure the PHP Script**
- Copy the PHP script provided below.
- Replace `<yourURL>/metrics` with your actual Uptime Kuma metrics URL. (line 3)
- Replace `***************` with your actual API key. (line 6)
3. **That's it!** Now please start with the Discord Bot setup.
### Code Pieces Referenced by Lines
- **Lines 3-5**: Define the URL and API key for the metrics endpoint.
- **Lines 8-11**: Initialize a new cURL session and set cURL options.
- **Lines 14-16**: Execute the cURL session and get the HTTP status code.
- **Lines 19-21**: Close the cURL session and check if the request was successful.
- **Lines 23-41**: Parse the response and extract relevant data.
- **Lines 44-46**: Set the content type of the response to JSON and output the data.
- **Lines 48-50**: Output an error message if the request was not successful.