Skip to content

Instantly share code, notes, and snippets.

@SebastienGautier
Created July 18, 2018 16:06
Show Gist options
  • Save SebastienGautier/5e00d1466294f99d59832dea94265687 to your computer and use it in GitHub Desktop.
Save SebastienGautier/5e00d1466294f99d59832dea94265687 to your computer and use it in GitHub Desktop.
Get daily computer usage based on explorer.exe running time with Elasticsearch and Metricbeats
<?php
$client = ClientBuilder::create()->setHosts(['127.0.0.1'])->build();
$params = [
'index' => 'metricbeat-*',
'body' => [
'query' => [
'bool' => [
'must' => [
[
'term' => [
'system.process.name' => 'explorer.exe'
]
],
/*[
'range' => [
'@timestamp' => [
'gte' => 'now-24h',
'lte' => 'now'
]
],
]*/
],
]
],
'aggs' => [
'group_by_username' => [
'terms' => [
'field' => 'system.process.username'
],
'aggs' => [
'per_day' => [
'date_histogram' => [
'field' => '@timestamp',
'interval' => 'day',
//'time_zone' => '+02:00'
],
]
],
],
]
]
];
$response = $client->search($params);
foreach ($response['aggregations']['group_by_username']['buckets'] as $groupByUsername) {
$username = $groupByUsername['key'];
echo "<br>\n$username: <br>\n";
foreach ($groupByUsername['per_day']['buckets'] as $groupByDay) {
$date = $groupByDay['key_as_string'];
// We multiply per 10 because I'm sending the processes every 10 seconds, change this according to your update interval
$hours = $groupByDay['doc_count'] * 10 / 60 / 60;
echo "$date: $hours<br>\n";
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment