Created
December 4, 2011 16:34
-
-
Save ssaunier/1430616 to your computer and use it in GitHub Desktop.
Cronjob for ssaunier/livestats and a Ducksboard dashboard
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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 <[email protected]> | |
* 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