13.12.2017

FastNetMon Advanced notify script in Perl

This script implements example notify script in Perl which implements JSON interface for FastNetMon Advanced.

NB! JSON based script uses different arguments from text based notify script. Please keep it in mind!

Please install JSON processing library for Perl:

sudo apt-get install -y libjson-perl

It prints all information received from FastNetMon to log file /tmp/fastnetmon_notify_script.log:

#!/usr/bin/perl

use strict;
use warnings;

use JSON;

use Data::Dumper;

# Write some debug to /tmp
open my $fl, ">>", "/tmp/fastnetmon_notify_script.log" or die "Could not open file for writing";

# This script executed from FastNetMon this way: ban 11.22.33.44

if (scalar @ARGV != 2) {
    print {$fl} "Please specify all arguments. Got only: @ARGV\n";
    die "Please specify all arguments\n";
}

my ($action, $ip_address) = @ARGV;
# action could be: ban, unban, partial_block

# Read data from stdin
my $input_attack_details = join '', <STDIN>;

# try to decode this data to json
my $attack_details = eval{  decode_json($input_attack_details); };

# report error
if ($@) {
    print {$fl} "JSON decode failed: $input_attack_details\n";
    die "JSON decode failed\n";
}

print {$fl} "Received notification about $ip_address with action $action\n";
print {$fl} Dumper($attack_details);

close $fl;

exit 0;

Please put it to file /usr/local/bin/notify_json.pl.

And set executable bit for it:

sudo chmod +x /usr/local/bin/notify_json.pl

You need to use following mode from FastNetMon to use this script properly:

sudo fcli set main notify_script_enabled enable
sudo fcli set main notify_script_format json
sudo fcli set main notify_script_path /usr/local/bin/notify_json.pl
sudo fcli commit

More documentation about JSON formats you could find here.