From 62585ae8fb60dbb630b51967de6916e87746454b Mon Sep 17 00:00:00 2001 From: Laurens van der Drift Date: Mon, 27 May 2024 21:01:48 +0200 Subject: [PATCH] Added back end --- Web/back-end.php | 63 ++++++++++++++++++++++++++++++++++++++++++++++++ Web/setup.md | 41 +++++++++++++++++++++++++++++++ 2 files changed, 104 insertions(+) create mode 100644 Web/back-end.php create mode 100644 Web/setup.md diff --git a/Web/back-end.php b/Web/back-end.php new file mode 100644 index 0000000..bf701b7 --- /dev/null +++ b/Web/back-end.php @@ -0,0 +1,63 @@ +/metrics'; //your URL +$username = ''; //leave empty +// Retrieve an API key from the UptimeKuma dashboard here /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"; +} +?> \ No newline at end of file diff --git a/Web/setup.md b/Web/setup.md new file mode 100644 index 0000000..5f34ba0 --- /dev/null +++ b/Web/setup.md @@ -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 `/metrics`). + +2. **Configure the PHP Script** + - Copy the PHP script provided below. + - Replace `/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.