From 9abe55939fcfd710269e0c2567fe3c65b882d5b6 Mon Sep 17 00:00:00 2001 From: Spencer Gardner Date: Sun, 10 Jan 2021 11:41:02 -0700 Subject: [PATCH] Added traffic_type tag --- src/Entity/IpAccount.php | 4 +++- src/Service/AccountingService.php | 7 ++++--- src/Service/InfluxDBService.php | 2 +- tests/Entity/IpAccountingTest.php | 2 +- 4 files changed, 9 insertions(+), 6 deletions(-) diff --git a/src/Entity/IpAccount.php b/src/Entity/IpAccount.php index 852bc80..12483f0 100644 --- a/src/Entity/IpAccount.php +++ b/src/Entity/IpAccount.php @@ -5,6 +5,7 @@ class IpAccount { public $ip; + public $local = false; public $down_byte = 0; public $down_packet = 0; public $up_byte = 0; @@ -14,9 +15,10 @@ class IpAccount * IpAccount constructor. * @param string $ip */ - public function __construct(string $ip) + public function __construct(string $ip, bool $local) { $this->ip = $ip; + $this->local = $local; } /** diff --git a/src/Service/AccountingService.php b/src/Service/AccountingService.php index 65f0ae4..241fce3 100644 --- a/src/Service/AccountingService.php +++ b/src/Service/AccountingService.php @@ -105,22 +105,23 @@ public function parse() $dest = new IP($line[1]); $bytes = $line[2];//byte $packets = $line[3];//packet + $local = $this->network_range->contains($source) && $this->network_range->contains($dest); if ($this->network_range->contains($source)) { $ip = $source->__toString(); if (!isset($this->data[$ip])) { - $this->data[$ip] = new IpAccount($ip); + $this->data[$ip] = new IpAccount($ip, $local); } - $this->data[$ip]->add_upload($bytes, $packets); + $this->data[$ip]->add_upload($bytes, $packets, $local); } if ($this->network_range->contains($dest)) { $ip = $dest->__toString(); if (!isset($this->data[$ip])) { - $this->data[$ip] = new IpAccount($ip); + $this->data[$ip] = new IpAccount($ip, $local); } $this->data[$ip]->add_download($bytes, $packets); diff --git a/src/Service/InfluxDBService.php b/src/Service/InfluxDBService.php index e1bcdb8..0a972ce 100644 --- a/src/Service/InfluxDBService.php +++ b/src/Service/InfluxDBService.php @@ -48,7 +48,7 @@ public function push($data) $points[] = new Point( 'net_traffic', null, - ["ip" => $d->ip, "host" => getenv('MIKROTIK_IP')], + ["ip" => $d->ip, "host" => getenv('MIKROTIK_IP'), "traffic_type" => $d->local ? "local" : "external"], $counters ); diff --git a/tests/Entity/IpAccountingTest.php b/tests/Entity/IpAccountingTest.php index 03c7da9..15888aa 100644 --- a/tests/Entity/IpAccountingTest.php +++ b/tests/Entity/IpAccountingTest.php @@ -35,7 +35,7 @@ public function testIpAddUpload(): void { $ipAccount = new \App\Entity\IpAccount(self::TEST_IP); - $ipAccount->add_upload(10,10); + $ipAccount->add_upload(10,10, false); $this->assertEquals(0, $ipAccount->down_byte); $this->assertEquals(0, $ipAccount->down_packet);