Feed Monitoring

We’ve got a few feeds from flakey hardware, occasionally they need a kick to restart - so I wrote a script that monitors each feed and checks to see if they are stale. Super useful if you don’t like missing data.

I’ve got two versions, this API style version returns JSON of the stale feeds

Result

{"count":2,"feeds":["Floor Temperature (Hall)","Temperature (Hall)"]}

PHP

<?php
header("Content-Type: application/json; charset=utf-8");
require "vendor/autoload.php";

$client = new GuzzleHttp\Client();
$res = $client->request(
    "GET",
    "http://192.168.1.113/emoncms/feed/list.json&apikey=xxxxxxx&meta=1"
);

if ($res->getStatusCode() != 200) {
    return;
}

$data = json_decode($res->getBody());

if (!$data) {
    return;
}

$stale_feeds = [];
foreach ($data as $feed_item) {
    if ($feed_item->interval == 1) {
        $interval = 3600;
    } else {
        $interval = $feed_item->interval * 5 * 5;
    }

    $feed_time_plus_interval = $feed_item->time + $interval;

    if ($feed_time_plus_interval < mktime()) {
        $stale_feeds[] = $feed_item->name . " (" . $feed_item->tag . ")";
    }
}

echo json_encode([
    "count" => sizeof($stale_feeds),
    "feeds" => $stale_feeds,
]);

(Currently it requires Guzzle, I could probably do it without)

We can then do a lot of different things - for example, call it via Node-RED and do something. In this example we’re just logging the number of stale feeds back in EmonCMS.

You could use NodeRed to email you, or alert you in some other way when a feed is stale.

Hope it helps someone.

1 Like