Skip to content
Snippets Groups Projects
Commit ff769a4b authored by Tibo's avatar Tibo
Browse files

add support for ifconfig on ubuntu 18.04. Fix issue #18

parent e291f340
No related branches found
No related tags found
No related merge requests found
Pipeline #3654 failed
......@@ -94,9 +94,11 @@ class Ifconfig extends AbstractSensor
return self::STATUS_OK;
}
const IFNAME = "/^(\\S+)\\s+Link encap:/m";
const IPV4 = '/^\\s+inet addr:(\\d{1,3}\\.\\d{1,3}\\.\\d{1,3}\\.\\d{1,3})/m';
const RXTX = '/^\\s+RX bytes:(\\d+) .*TX bytes:(\\d+)/m';
const IFNAME = '/^(?|(\S+)\s+Link encap:|(\S+): flags)/m';
const IPV4 = '/^\s+inet (?>addr:)?(\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3})/m';
const RXTX = '/^\s+RX bytes:(\d+) .*TX bytes:(\d+)/m';
const RX = '/^\s+RX packets (?>\d+) bytes (\d+)/m';
const TX = '/^\s+TX packets (?>\d+) bytes (\d+)/m';
public function parseIfconfigRecord($record)
{
......@@ -128,6 +130,7 @@ class Ifconfig extends AbstractSensor
$if = null;
foreach ($lines as $line) {
$name = $this->pregMatchOne(self::IFNAME, $line);
if ($name !== false) {
// Starting the section of a new interface
$if = new NetworkInterface();
......@@ -142,12 +145,23 @@ class Ifconfig extends AbstractSensor
continue;
}
$matches = array();
$matches = [];
if (preg_match(self::RXTX, $line, $matches) === 1) {
$if->rx = $matches[1];
$if->tx = $matches[2];
continue;
}
$rx = $this->pregMatchOne(self::RX, $line);
if ($rx !== false) {
$if->rx = $rx;
}
$tx = $this->pregMatchOne(self::TX, $line);
if ($tx !== false) {
$if->tx = $tx;
}
}
// filter out uninteresting interfaces
......@@ -161,11 +175,11 @@ class Ifconfig extends AbstractSensor
return $filtered;
}
public function pregMatchOne($pattern, $string)
public function pregMatchOne(string $pattern, string $string, int $match_group = 1)
{
$matches = array();
$matches = [];
if (preg_match($pattern, $string, $matches) === 1) {
return $matches[1];
return $matches[$match_group];
}
return false;
......
......@@ -71,10 +71,28 @@ class ExampleTest extends TestCase
$this->assertEquals(2074977132, $interfaces[1]->tx);
}
/**
* Test parsing of ifconfig string from a ubuntu 18.04 server
*
* @group ifconfig
* @group sensors
*/
public function testIfconfig1804()
{
$string = file_get_contents(__DIR__ . "/ifconfig1804");
$sensor = new Ifconfig(new \App\Server());
$interfaces = $sensor->parseIfconfig($string);
$this->assertEquals(2, count($interfaces));
$this->assertEquals("eno1", $interfaces[0]->name);
$this->assertEquals("172.20.0.8", $interfaces[1]->address);
$this->assertEquals(185252610, $interfaces[1]->rx);
$this->assertEquals(266912412, $interfaces[1]->tx);
}
/**
* @group Disks
*/
public function testDisksSensor()
{
$string = file_get_contents(__DIR__ . "/df");
......
eno1: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500
inet 193.190.205.212 netmask 255.255.255.0 broadcast 193.190.205.255
ether 10:98:36:af:d4:bf txqueuelen 1000 (Ethernet)
RX packets 4982592 bytes 448456149 (448.4 MB)
RX errors 0 dropped 43612 overruns 0 frame 0
TX packets 707408 bytes 79299197 (79.2 MB)
TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0
device interrupt 16
eno2: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500
inet 172.20.0.8 netmask 255.255.0.0 broadcast 172.20.255.255
inet6 fe80::1298:36ff:feaf:d4b0 prefixlen 64 scopeid 0x20<link>
ether 10:98:36:af:d4:b0 txqueuelen 1000 (Ethernet)
RX packets 1941945 bytes 185252610 (185.2 MB)
RX errors 0 dropped 0 overruns 0 frame 0
TX packets 1414316 bytes 266912412 (266.9 MB)
TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0
device interrupt 17
lo: flags=73<UP,LOOPBACK,RUNNING> mtu 65536
inet 127.0.0.1 netmask 255.0.0.0
inet6 ::1 prefixlen 128 scopeid 0x10<host>
loop txqueuelen 1000 (Local Loopback)
RX packets 425874 bytes 30710973 (30.7 MB)
RX errors 0 dropped 0 overruns 0 frame 0
TX packets 425874 bytes 30710973 (30.7 MB)
TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0
tun0: flags=4305<UP,POINTOPOINT,RUNNING,NOARP,MULTICAST> mtu 1500
inet 172.21.0.1 netmask 255.255.0.0 destination 172.21.0.1
inet6 fe80::8b10:9f7e:131:c4a1 prefixlen 64 scopeid 0x20<link>
unspec 00-00-00-00-00-00-00-00-00-00-00-00-00-00-00-00 txqueuelen 100 (UNSPEC)
RX packets 2870 bytes 196496 (196.4 KB)
RX errors 0 dropped 0 overruns 0 frame 0
TX packets 20201 bytes 4312778 (4.3 MB)
TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment