Skip to content

Instantly share code, notes, and snippets.

@fdcore
Last active September 17, 2018 21:43
Show Gist options
  • Save fdcore/14e2ba537c2760afe9b1a7772a2ed914 to your computer and use it in GitHub Desktop.
Save fdcore/14e2ba537c2760afe9b1a7772a2ed914 to your computer and use it in GitHub Desktop.
<?php
$data = array(
[
'measurement' => 'time',
'tags' => [
"host" => 'server02',
],
'fields' => ['value' => mt_rand(1, 100)],
'timestamp' => exec('date +%s%N')
]
);
// <measurement>[,<tag_key>=<tag_value>[,<tag_key>=<tag_value>]] <field_key>=<field_value>[,<field_key>=<field_value>] [<timestamp>]
function influxdb_send($url, $db, $data) {
$points = [];
foreach ($data as $metric) {
$tags = []; // tags array
$fields = []; // fields array
foreach ($metric['tags'] as $tag => $value) {
$tags[]="$tag=$value";
}
foreach ($metric['fields'] as $tag => $value) {
$fields[]="$tag=$value";
}
if(empty($metric['timestamp'])) {
$metric['timestamp'] = exec('date +%s%N');
}
$point = [implode(',', $tags), implode(',', $fields), $metric['timestamp']];
$points[]=$metric['measurement'] . ',' . implode(' ', $point);
}
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url . "write?db=" . $db);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, implode("\n", $points));
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: text/plain'));
$result=curl_exec($ch);
return $result;
}
// influxdb_send('http://127.0.0.1:8086/', 'test', $data);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment