EXAMPLE #08
PHP
Bash
URL monitor (cron-ready)
Probe a URL, report response time as a number — drop into cron.
08-monitor-url.php
#!/usr/bin/env php
<?php
/**
* Example: Monitor a URL and report response time as Events
*
* A practical use case: periodically check a website and send the HTTP
* status code and response time to Space Place. Run this from a cron job
* to build a response-time chart in Connect.
*
* Usage:
* php 08-monitor-url.php
*
* Cron (every 5 minutes):
* */5 * * * * /usr/bin/php /path/to/08-monitor-url.php >> /var/log/space-place-monitor.log 2>&1
*/
$baseUrl = 'https://www.spaceplace.app';
$dataStreamId = 'YOUR-DATASTREAM-UUID-HERE';
$tokenValue = 'YOUR-TOKEN-VALUE-HERE';
$urlToMonitor = 'https://example.com';
// ── Check the target URL ─────────────────────────────────────────────────────
$ch = curl_init($urlToMonitor);
curl_setopt_array($ch, [
CURLOPT_RETURNTRANSFER => true,
CURLOPT_TIMEOUT => 30,
CURLOPT_FOLLOWLOCATION => true,
CURLOPT_NOBODY => true, // HEAD request for speed
]);
$start = microtime(true);
curl_exec($ch);
$elapsed = round((microtime(true) - $start) * 1000); // milliseconds
$httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
$curlError = curl_error($ch);
curl_close($ch);
$status = $curlError ? 'error' : ($httpCode >= 200 && $httpCode < 400 ? 'up' : 'down');
echo date('Y-m-d H:i:s') . " | {$urlToMonitor} | HTTP {$httpCode} | {$elapsed}ms | {$status}\n";
// ── Report to Space Place ────────────────────────────────────────────────────
$event = [
'number' => $elapsed, // response time in ms
'source' => $urlToMonitor,
'key' => 'http-status',
'value' => $curlError ?: (string) $httpCode,
];
$ch = curl_init("{$baseUrl}/api/events");
curl_setopt_array($ch, [
CURLOPT_POST => true,
CURLOPT_RETURNTRANSFER => true,
CURLOPT_TIMEOUT => 10,
CURLOPT_HTTPHEADER => [
'Content-Type: application/json',
'Accept: application/json',
"X-DataStream-ID: {$dataStreamId}",
"X-Token: {$tokenValue}",
],
CURLOPT_POSTFIELDS => json_encode($event),
]);
$response = curl_exec($ch);
$apiCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
curl_close($ch);
if ($apiCode === 201) {
$body = json_decode($response, true);
echo " -> Reported as Event {$body['event_id']}\n";
} else {
echo " -> Failed to report (HTTP {$apiCode})\n";
}
08-monitor-url.sh
#!/usr/bin/env bash
#
# Example: Monitor a URL and report response time as Events
#
# Checks a website and sends the HTTP status code and response time to
# Space Place. Run from a cron job to build a response-time chart.
#
# Usage:
# bash 08-monitor-url.sh
#
# Cron (every 5 minutes):
# */5 * * * * /bin/bash /path/to/08-monitor-url.sh >> /var/log/sp-monitor.log 2>&1
BASE_URL="https://www.spaceplace.app"
DATASTREAM_ID="YOUR-DATASTREAM-UUID-HERE"
TOKEN_VALUE="YOUR-TOKEN-VALUE-HERE"
URL_TO_MONITOR="https://example.com"
# ── Check the target URL ─────────────────────────────────────────────────────
timing=$(curl -s -o /dev/null -w "%{http_code} %{time_total}" \
--max-time 30 -L "$URL_TO_MONITOR" 2>&1)
http_status=$(echo "$timing" | awk '{print $1}')
elapsed_sec=$(echo "$timing" | awk '{print $2}')
elapsed_ms=$(awk "BEGIN { printf \"%d\", ${elapsed_sec} * 1000 }")
if [ "$http_status" -ge 200 ] && [ "$http_status" -lt 400 ] 2>/dev/null; then
status="up"
elif [ "$http_status" = "000" ]; then
status="error"
else
status="down"
fi
echo "$(date '+%Y-%m-%d %H:%M:%S') | ${URL_TO_MONITOR} | HTTP ${http_status} | ${elapsed_ms}ms | ${status}"
# ── Report to Space Place ────────────────────────────────────────────────────
response=$(curl -s -w "\n%{http_code}" \
-X POST "${BASE_URL}/api/events" \
-H "Content-Type: application/json" \
-H "Accept: application/json" \
-H "X-DataStream-ID: ${DATASTREAM_ID}" \
-H "X-Token: ${TOKEN_VALUE}" \
-d "{
\"number\": ${elapsed_ms},
\"source\": \"${URL_TO_MONITOR}\",
\"key\": \"http-status\",
\"value\": \"${http_status}\"
}")
api_code=$(echo "$response" | tail -n1)
body=$(echo "$response" | sed '$d')
if [ "$api_code" = "201" ]; then
echo " -> Reported as Event $(echo "$body" | jq -r '.event_id')"
else
echo " -> Failed to report (HTTP ${api_code})"
fi