-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathvantages_report.html
90 lines (76 loc) · 2.68 KB
/
vantages_report.html
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
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
<?php
require_once 'php/vendor/autoload.php';
require_once 'php/login.php';
//parse command line parameters
if (isset($_GET['duration'])) $duration=$_GET['duration'];
else $duration=1;
$timestamp = time() - ($duration * 24 * 60 * 60);
//open driver manager and geoip reader
$manager = new MongoDB\Driver\Manager(
$mongodbConnectionString,
[
'ssl' => TRUE,
'username' => $mongodbUsername,
'password' => $mongodbPassword,
'authSource' => $mongodbAuthSource
],
[
'ca_file' => $mongodbSslCaCert,
'pem_file' => $mongodbSslPem
]
);
$reader = new GeoIp2\Database\Reader('/usr/local/share/GeoLite2/GeoLite2-City.mmdb');
//query for distinct vantages
$cursor = $manager->executeCommand(
'proddle',
new MongoDB\Driver\Command(
[
'aggregate' => 'measurements',
'pipeline' => [
['$match' => ['timestamp' => ['$gte' => $timestamp]]],
[
'$group' => [
'_id' => [
'vantage_hostname' => '$vantage_hostname',
'vantage_ip_address' => '$vantage_ip_address',
'measurement_class' => '$measurement_class'
],
'count' => ['$sum' => 1]
]
],
['$sort' => ['vantage_hostname' => 1, 'vantage_ip_address' => 1, 'measurement_class' => 1]]
]
]
)
);
//geolocate vantage
$array = $cursor->toArray()[0];
$coordinates = [];
for ($i=0; $i < count($array->result); ++$i) {
$result = $array->result[$i];
$coordinate = (object) array();
if (isset($result->_id->vantage_ip_address)) {
$coordinate->vantage = (object) array();
$record = $reader->city($result->_id->vantage_ip_address);
$coordinate->vantage->latitude = $record->location->latitude;
$coordinate->vantage->longitude = $record->location->longitude;
}
$coordinates[$i] = $coordinate;
}
$array->coordinates = $coordinates;
echo MongoDB\BSON\toJSON(MongoDB\BSON\fromPHP($array));
/*$array = $cursor->toArray()[0]->result;
for ($i=0; $i < count($array); ++$i) {
$vantage = $array[$i];
echo ($i != 0 ? ',' : '') . '{';
echo '"hostname":"' . $vantage->_id->hostname . '"';
echo ',"ip_address":"' . $vantage->_id->ip_address . '"';
//geolocate ip address
$record = $reader->city($vantage->_id->ip_address);
echo ',"latitude":' . $record->location->latitude . ',"longitude":' . $record->location->longitude;
echo ',"measurement":"' . $vantage->_id->measurement . '"';
echo ',"count":' . $vantage->count;
echo '}';
}
echo ']}';*/
?>