Skip to content

Instantly share code, notes, and snippets.

@ssaunier
Created December 4, 2011 16:34
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ssaunier/1430616 to your computer and use it in GitHub Desktop.
Save ssaunier/1430616 to your computer and use it in GitHub Desktop.
Cronjob for ssaunier/livestats and a Ducksboard dashboard
<?php
/*
* A usage example for the livestats project pushing data
* to a Ducksboard dashboard.
*
* https://github.com/ssaunier/livestats
* https://ducksboard.jira.com/wiki/display/API/API+Reference
*
* Copyright 2011, Sébastien Saunier <sebastien.saunier@gmail.com>
* Dual licensed under the MIT or GPL Version 2 licenses.
*
* Date: 12/05/2011
*
*/
// Parameters given by Ducksboard when creating a custom 4-numbers widget.
$apikey = 'YOUR_API_KEY';
define(total_id, YOUR_WIDGET_TOTAL_ID);
define(read_id, YOUR_WIDGET_READ_ID);
define(write_id, YOUR_WIDGET_WRITE_ID);
define(idle_id, YOUR_WIDGET_IDLE_ID);
function curl_post_ducksboard($id, $value) {
global $apikey;
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, 'https://push.ducksboard.com/values/' . $id .'/');
curl_setopt($ch, CURLOPT_USERPWD, "$apikey:ignored");
curl_setopt($ch, CURLOPT_POSTFIELDS, '{ "value" : ' . $value . ' }');
curl_exec($ch);
curl_close($ch);
}
// Require 3 files from https://github.com/ssaunier/livestats/tree/master/backend/php
require_once('/path/to/livestats/backend/php/State.php');
require_once('/path/to/livestats/backend/php/config.inc.php');
require_once('/path/to/livestats/backend/php/DBConnector.php');
function job($previous) {
global $livestats_db_config;
$db = new DBConnector($livestats_db_config);
$state = State::countStates($db);
unset($db);
if ($previous['total'] != $state['total'])
curl_post_ducksboard(total_id, $state['total']);
if ($previous['reading'] != $state['reading'])
curl_post_ducksboard(read_id, $state['reading']);
if ($previous['writing'] != $state['writing'])
curl_post_ducksboard(write_id, $state['writing']);
if ($previous['idle'] != $state['idle'])
curl_post_ducksboard(idle_id, $state['idle']);
return $state;
}
// Run the job every 15 seconds for 10 minutes.
$stopDate = strtotime('+ 9 minutes 45 seconds');
$previousState = array('total' => 0, 'reading' => 0, 'writing' => 0, 'idle' => 0);
while (time() < $stopDate) {
$previousState = job($previousState);
sleep(15);
}
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment