Add a plug-in to parse the output of 'netstat -as'.

This commit is contained in:
Ramon Novoa 2018-03-01 12:17:44 +01:00
parent d2379dc57a
commit 3af4397f7f
1 changed files with 39 additions and 0 deletions

View File

@ -0,0 +1,39 @@
#!/usr/bin/env perl
# Copyright (c) 2018 Artica Soluciones Tecnologicas S.L.
use strict;
use warnings;
use Scalar::Util qw(looks_like_number);
# Call netstat.
my @out = `netstat -as 2>/dev/null`;
return if ($? != 0 || $#out < 0);
my $section = "[Unknown]";
foreach my $line (@out) {
chomp($line);
# New section.
if ($line =~ m/\s*(.*):$/) {
$section = $1;
next;
}
# Parse the data.
my ($module_name, $data) = ('', '');
if ($line =~ m/(\d+)\s+(.+)$/) {
($module_name, $data) = ($2, $1);
}
elsif ($line =~ m/\s*(.+):\s+(\d+)$/) {
($module_name, $data) = ($1, $2);
}
# No data or non-numeric data.
next unless looks_like_number($data);
print "<module>\n";
print " <name><![CDATA[[$section] $module_name]]></name>\n";
print " <type>generic_data</type>\n";
print " <module_group>Networking</module_group>\n";
print " <data>$data</data>\n";
print "</module>\n";
}