Skip to content

Instantly share code, notes, and snippets.

@jonathan-s jonathan-s/composer.json

Last active Aug 29, 2015
Embed
What would you like to do?
A script that creates a new user and a new device, both have some tags associated. Then the agent gets installed on the machine the script is run on.
{
"require": {
"serverdensity/sd-api-wrapper": ">=0.7.4"
}
}
<?php
namespace serverdensity;
require __DIR__.'/vendor/autoload.php';
use serverdensity\Client;
$TOKEN = 'your authentication token here';
$device_id = 'a device id here';
$client = new Client();
$client->authenticate($TOKEN);
$available = $client->api('metrics')->available($device_id, time()-7200, time());
//print_r($available);
$filter = array(
'networkTraffic' => [
'eth0' => ['rxMByteS', 'txMByteS'],
'eth1' => ['rxMByteS', 'txMByteS']
]
);
$metrics = $client->api('metrics')->metrics($device_id, $filter, time()-7200, time());
//print_r($metrics);
$data = $client->api('metrics')->formatMetrics($metrics);
//print_r($data);
echo $data[0]['source']; // Network traffic > eth0 > Received MB\\/s
echo $data[1]['source']; // Network traffic > eth0 > Transmitted MB\\/s
echo $data[2]['source']; // Network traffic > eth1 > Received MB\\/s
echo $data[3]['source']; // Network traffic > eth1 > Transmitted MB\\/s
$data[0]['data']; // contains the actual data in the following format
// [ ['x' => 123123, 'y' => 0.341] ... ]
// If we want to have the x and y points separate we can do that too.
$separateData = $client->api('metrics')->separateXYdata($data);
$separateData[0]['xPoints'];
$separateData[0]['yPoints'];
$points = $data[0];
$graphData = array();
foreach($points['data'] as $i => $array){
$graphData[] = array(
'mydate' => date("Y-m-d H:i", $array['x']),
'eth1' => $array['y']
);
}
$jsonGraph = json_encode($graphData);
$html = <<<EOT
<html>
<head></head>
<body>
<script type="text/javascript" src="http://www.amcharts.com/lib/3/amcharts.js"></script>
<script type="text/javascript" src="http://www.amcharts.com/lib/3/serial.js"></script>
<script type="text/javascript" src="http://www.amcharts.com/lib/3/themes/none.js"></script>
<script type="text/javascript">
var chart = AmCharts.makeChart("chartdiv", {
"type": "serial",
"theme": "none",
"pathToImages": "http://www.amcharts.com/lib/3/images/",
"dataProvider": $jsonGraph,
"valueAxes": [{
"axisAlpha": 0,
"inside": true,
"position": "left",
"ignoreAxisWidth": true
}],
"graphs": [{
"balloonText": "[[category]]<br><b><span style='font-size:14px;'>[[value]]</span></b>",
"bullet": "round",
"bulletSize": 6,
"title": "eth1 > Received data",
"lineColor": "#d1655d",
"lineThickness": 2,
"type": "smoothedLine",
"valueField": "eth1"
}],
"chartScrollbar": {},
"chartCursor": {
"categoryBalloonDateFormat": "HH:NN",
"cursorAlpha": 0,
"cursorPosition": "mouse"
},
"titles": [
{
"id": "Title-1",
"size": 15,
"text": "Network Traffic"
}
],
"legend": {
"useGraphSettings": true,
"valueAlign": "left",
"valueWidth": "15"
},
"categoryField": "mydate",
"dataDateFormat": "YYYY-MM-DD HH:NN",
"categoryAxis": {
"minPeriod": "mm",
"equalspacing": true,
"parseDates": true,
"minorGridAlpha": 0.1,
"minorGridEnabled": true
}
});</script>
<div id="chartdiv" style="width:100%; height:500px;"></div>
</html>
EOT;
file_put_contents('test.html', $html);
<?php
use serverdensity\Client;
require __DIR__.'/vendor/autoload.php';
$TOKEN = 'write down your token here';
$ACCOUNT_URL = 'write down your account url here';
// https://[this part in brackets is what we mean].serverdensity.io
$client = new Client();
$client->authenticate($TOKEN);
$user = array(
'firstName' => 'Chardee',
'lastName' => 'MacDennis',
'login' => 'iasip',
'password' => 'randomPassword',
'emailAddresses' => ['ChardeeMacDennis@iasip.com'],
'admin' => false,
'permissions' => array(
'limited' => true
)
);
$client->api('users')->create($user, ['My tag']);
$device = array(
'name' => 'R2D2'
);
$device = $client->api('devices')->create($device, ['My tag']);
shell_exec("curl -Lk https://www.serverdensity.com/downloads/agent-install.sh > install.sh");
shell_exec("chmod +x install.sh");
shell_exec('./install.sh -a https://'.$ACCOUNT_URL'.serverdensity.io -k '.$device['agentKey']);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.