-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathexample.php
More file actions
44 lines (44 loc) · 1.51 KB
/
example.php
File metadata and controls
44 lines (44 loc) · 1.51 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
<?php
/**
* Update Datadog metrics for a Minecraft server using JSONAPI.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
*
* $this->mcAPI should be a JSONAPI object.
* https://github.com/alecgorge/jsonapi/blob/master/sdk/php/JSONAPI.php
*
* $this->datadog should be a datadog object from this repository.
* The constructor accepts an API key as its only argument.
*/
function task_datadog_update()
{
$apiData = $this->mcAPI->callMultiple( array( 'getOfflinePlayerNames', 'getPlayerCount' ),
array( array(), array() ) );
$metrics = array();
$metrics[0] = array();
$metrics[0]['name'] = 'minecraft.survival.players.total';
$metrics[0]['value'] = 0;
foreach( $apiData['success'] as $callFull )
{
if( $callFull['result'] != 'success' )
{
echo( 'Call to ' . $callFull['source'] . " failed.\n" );
}
else
{
$call = $callFull['success'];
switch( $callFull['source'] )
{
case 'getOfflinePlayerNames':
$metrics[0]['value'] = $metrics[0]['value'] + count( $call );
break;
case 'getPlayerCount':
$metrics[0]['value'] = $metrics[0]['value'] + (int) $call;
break;
}
}
}
$this->datadog->addMetrics( $metrics );
}