Blame local/ipf-mod.pl

Packit Service b38f0b
#!/usr/bin/perl -s
Packit Service b38f0b
##
Packit Service b38f0b
## IP Filter UCD-SNMP pass module
Packit Service b38f0b
##
Packit Service b38f0b
## Allows read IP Filter's tables (In, Out, AccIn, AccOut),
Packit Service b38f0b
## fetching rules, hits and bytes (for accounting tables only).
Packit Service b38f0b
##
Packit Service b38f0b
## Author: Yaroslav Terletsky <ts@polynet.lviv.ua>
Packit Service b38f0b
## Date: $ Tue Dec  1 10:24:08 EET 1998 $
Packit Service b38f0b
## Version: 1.1a
Packit Service b38f0b
Packit Service b38f0b
# Put this file in /usr/local/bin/ipf-mod.pl and then add the following 
Packit Service b38f0b
# line to your snmpd.conf file (without the # at the front):
Packit Service b38f0b
#
Packit Service b38f0b
#   pass .1.3.6.1.4.1.2021.13.2 /usr/local/bin/ipf-mod.pl
Packit Service b38f0b
Packit Service b38f0b
# enterprises.ucdavis.ucdExperimental.ipFilter	= .1.3.6.1.4.1.2021.13.2
Packit Service b38f0b
# ipfInTable.ipfInEntry.ipfInIndex		integer	= 1.1.1
Packit Service b38f0b
# ipfInTable.ipfInEntry.ipfInRule		string	= 1.1.2
Packit Service b38f0b
# ipfInTable.ipfInEntry.ipfInHits		counter	= 1.1.3
Packit Service b38f0b
# ipfOutTable.ipfOutEntry.ipfOutIndex		integer	= 1.2.1
Packit Service b38f0b
# ipfOutTable.ipfOutEntry.ipfOutRule		string	= 1.2.2
Packit Service b38f0b
# ipfOutTable.ipfOutEntry.ipfOutHits		counter	= 1.2.3
Packit Service b38f0b
# ipfAccInTable.ipfAccInEntry.ipfAccInIndex	integer	= 1.3.1
Packit Service b38f0b
# ipfAccInTable.ipfAccInEntry.ipfAccInRule	string	= 1.3.2
Packit Service b38f0b
# ipfAccInTable.ipfAccInEntry.ipfAccInHits	counter	= 1.3.3
Packit Service b38f0b
# ipfAccInTable.ipfAccInEntry.ipfAccInBytes	counter	= 1.3.4
Packit Service b38f0b
# ipfAccOutTable.ipfAccOutEntry.ipfAccOutIndex	integer	= 1.4.1
Packit Service b38f0b
# ipfAccOutTable.ipfAccOutEntry.ipfAccOutRule	string	= 1.4.2
Packit Service b38f0b
# ipfAccOutTable.ipfAccOutEntry.ipfAccOutHits	counter	= 1.4.3
Packit Service b38f0b
# ipfAccOutTable.ipfAccOutEntry.ipfAccOutBytes	counter	= 1.4.4
Packit Service b38f0b
Packit Service b38f0b
# variables types
Packit Service b38f0b
%type = ('1.1.1', 'integer', '1.1.2', 'string', '1.1.3', 'counter',
Packit Service b38f0b
	 '2.1.1', 'integer', '2.1.2', 'string', '2.1.3', 'counter',
Packit Service b38f0b
	 '3.1.1', 'integer', '3.1.2', 'string', '3.1.3', 'counter',
Packit Service b38f0b
	 '3.1.4', 'counter',
Packit Service b38f0b
	 '4.1.1', 'integer', '4.1.2', 'string', '4.1.3', 'counter',
Packit Service b38f0b
	 '4.1.4', 'counter');
Packit Service b38f0b
Packit Service b38f0b
# getnext sequence
Packit Service b38f0b
%next = ('1.1.1', '1.1.2', '1.1.2', '1.1.3', '1.1.3', '2.1.1',
Packit Service b38f0b
	 '2.1.1', '2.1.2', '2.1.2', '2.1.3', '2.1.3', '3.1.1',
Packit Service b38f0b
	 '3.1.1', '3.1.2', '3.1.2', '3.1.3', '3.1.3', '3.1.4',
Packit Service b38f0b
	 '3.1.4', '4.1.1',
Packit Service b38f0b
	 '4.1.1', '4.1.2', '4.1.2', '4.1.3', '4.1.3', '4.1.4');
Packit Service b38f0b
Packit Service b38f0b
# ipfilter's commands to fetch needed information
Packit Service b38f0b
$ipfstat_comm="/sbin/ipfstat";
Packit Service b38f0b
$ipf_in="$ipfstat_comm -ih 2>/dev/null";
Packit Service b38f0b
$ipf_out="$ipfstat_comm -oh 2>/dev/null";
Packit Service b38f0b
$ipf_acc_in="$ipfstat_comm -aih 2>/dev/null";
Packit Service b38f0b
$ipf_acc_out="$ipfstat_comm -aoh 2>/dev/null";
Packit Service b38f0b
Packit Service b38f0b
$OID=$ARGV[0];
Packit Service b38f0b
$IPF_OID='.1.3.6.1.4.1.2021.13.2';
Packit Service b38f0b
$IPF_OID_NO_DOTS='\.1\.3\.6\.1\.4\.1\.2021\.13\.2';
Packit Service b38f0b
Packit Service b38f0b
# exit if OID is not one of IPF-MIB's
Packit Service b38f0b
exit if $OID !~ /^$IPF_OID_NO_DOTS(\D|$)/;
Packit Service b38f0b
Packit Service b38f0b
# get table, entry, column and row numbers
Packit Service b38f0b
$tecr = $OID;
Packit Service b38f0b
$tecr =~ s/^$IPF_OID_NO_DOTS(\D|$)//;
Packit Service b38f0b
($table, $entry, $col, $row, $rest) = split(/\./, $tecr);
Packit Service b38f0b
Packit Service b38f0b
# parse 'get' request
Packit Service b38f0b
if($g) {
Packit Service b38f0b
	# exit if OID is wrong specified
Packit Service b38f0b
	if(!defined $table or !defined $entry or !defined $col or !defined $row or defined $rest) {
Packit Service b38f0b
		print "[1] NO-SUCH NAME\n" if $d;
Packit Service b38f0b
		exit;
Packit Service b38f0b
	}
Packit Service b38f0b
Packit Service b38f0b
	# get the OID's value
Packit Service b38f0b
	$value = &get_value($table, $entry, $col, $row);
Packit Service b38f0b
	print "value=$value\n" if $d;
Packit Service b38f0b
Packit Service b38f0b
	# exit if OID does not exist
Packit Service b38f0b
	print "[2] NO-SUCH NAME\n" if $d and !defined $value;
Packit Service b38f0b
	exit if !defined $value;
Packit Service b38f0b
Packit Service b38f0b
	# set ObjectID and reply with response
Packit Service b38f0b
	$tec = "$table.$entry.$col";
Packit Service b38f0b
	$ObjectID = "${IPF_OID}.${tec}.${row}";
Packit Service b38f0b
	&response;
Packit Service b38f0b
}
Packit Service b38f0b
Packit Service b38f0b
# parse 'get-next' request
Packit Service b38f0b
if($n) {
Packit Service b38f0b
	# set values if 0 or unspecified
Packit Service b38f0b
	$table = 1, $a = 1 if !$table or !defined $table;
Packit Service b38f0b
	$entry = 1, $a = 1 if !$entry or !defined $entry;
Packit Service b38f0b
	$col = 1, $a = 1 if !$col or !defined $col;
Packit Service b38f0b
	$row = 1, $a = 1 if !$row or !defined $row;
Packit Service b38f0b
Packit Service b38f0b
	if($a) {
Packit Service b38f0b
		# get the OID's value
Packit Service b38f0b
		$value = &get_value($table, $entry, $col, $row);
Packit Service b38f0b
		print "value=$value\n" if $d;
Packit Service b38f0b
Packit Service b38f0b
		# set ObjectID and reply with response
Packit Service b38f0b
		$tec = "$table.$entry.$col";
Packit Service b38f0b
		$ObjectID = "${IPF_OID}.${tec}.${row}";
Packit Service b38f0b
		&response;
Packit Service b38f0b
	}
Packit Service b38f0b
Packit Service b38f0b
	# get next OID's value
Packit Service b38f0b
	$row++;
Packit Service b38f0b
	$value = &get_value($table, $entry, $col, $row);
Packit Service b38f0b
Packit Service b38f0b
	# choose new table/column if rows exceeded
Packit Service b38f0b
	if(!defined $value) {
Packit Service b38f0b
		$tec = "$table.$entry.$col";
Packit Service b38f0b
		$tec = $next{$tec} if !$a;
Packit Service b38f0b
		$table = $tec;
Packit Service b38f0b
		$entry = $tec;
Packit Service b38f0b
		$col = $tec;
Packit Service b38f0b
		$table =~ s/\.\d\.\d$//;
Packit Service b38f0b
		$entry =~ s/^\d\.(\d)\.\d$/$1/;
Packit Service b38f0b
		$col =~ s/^\d\.\d\.//;
Packit Service b38f0b
		$row = 1;
Packit Service b38f0b
Packit Service b38f0b
		# get the OID's value
Packit Service b38f0b
		$value = &get_value($table, $entry, $col, $row);
Packit Service b38f0b
		print "value=$value\n" if $d;
Packit Service b38f0b
	}
Packit Service b38f0b
Packit Service b38f0b
	# set ObjectID and reply with response
Packit Service b38f0b
	$tec = "$table.$entry.$col";
Packit Service b38f0b
	$ObjectID = "${IPF_OID}.${tec}.${row}";
Packit Service b38f0b
	&response;
Packit Service b38f0b
}
Packit Service b38f0b
Packit Service b38f0b
##############################################################################
Packit Service b38f0b
Packit Service b38f0b
# fetch values from 'ipfInTable' and 'ipfOutTable' tables
Packit Service b38f0b
sub fetch_hits_n_rules {
Packit Service b38f0b
	local($row, $col, $ipf_output) = @_;
Packit Service b38f0b
	local($asdf, $i, @ipf_lines, $length);
Packit Service b38f0b
Packit Service b38f0b
	# create an entry if no rule exists
Packit Service b38f0b
	$ipf_output = "0 empty list for ipfilter" if !$ipf_output;
Packit Service b38f0b
Packit Service b38f0b
	@ipf_lines = split("\n", $ipf_output);
Packit Service b38f0b
	$length = $#ipf_lines + 1;
Packit Service b38f0b
Packit Service b38f0b
	for($i = 1; $i < $length + 1; $i++) {
Packit Service b38f0b
		$hits{$i} = $ipf_lines[$i-1];
Packit Service b38f0b
		$hits{$i} =~ s/^(\d+).*$/$1/;
Packit Service b38f0b
		$rule{$i} = $ipf_lines[$i-1];
Packit Service b38f0b
		$rule{$i} =~ s/^\d+ //;
Packit Service b38f0b
		if($i == $row) {
Packit Service b38f0b
			return $i if $col == 1;
Packit Service b38f0b
			return $rule{$i} if $col == 2;
Packit Service b38f0b
			return $hits{$i} if $col == 3;
Packit Service b38f0b
		}
Packit Service b38f0b
	}
Packit Service b38f0b
	# return undefined value
Packit Service b38f0b
	undef $asdf;
Packit Service b38f0b
	return $asdf;
Packit Service b38f0b
}
Packit Service b38f0b
Packit Service b38f0b
# fetch values from 'ipfAccInTable' and 'ipfAccOutTable' tables
Packit Service b38f0b
sub fetch_hits_bytes_n_rules {
Packit Service b38f0b
	local($row, $col, $ipf_output) = @_;
Packit Service b38f0b
	local($asdf, $i, @ipf_lines, $length);
Packit Service b38f0b
Packit Service b38f0b
	# create an entry if no rule exists
Packit Service b38f0b
	$ipf_output = "0 0 empty list for ipacct" if !$ipf_output;
Packit Service b38f0b
Packit Service b38f0b
	@ipf_lines = split("\n", $ipf_output);
Packit Service b38f0b
	$length = $#ipf_lines + 1;
Packit Service b38f0b
Packit Service b38f0b
	for($i = 1; $i < $length + 1; $i++) {
Packit Service b38f0b
		$hits{$i} = $ipf_lines[$i-1];
Packit Service b38f0b
		$hits{$i} =~ s/^(\d+) .*$/$1/;
Packit Service b38f0b
		$bytes{$i} = $ipf_lines[$i-1];
Packit Service b38f0b
		$bytes{$i} =~ s/^\d+ (\d+) .*/$1/;
Packit Service b38f0b
		$rule{$i} = $ipf_lines[$i-1];
Packit Service b38f0b
		$rule{$i} =~ s/^\d+ \d+ //;
Packit Service b38f0b
		if($i == $row) {
Packit Service b38f0b
			return $i if $col == 1;
Packit Service b38f0b
			return $rule{$i} if $col == 2;
Packit Service b38f0b
			return $hits{$i} if $col == 3;
Packit Service b38f0b
			return $bytes{$i} if $col == 4;
Packit Service b38f0b
		}
Packit Service b38f0b
	}
Packit Service b38f0b
	# return undefined value
Packit Service b38f0b
	undef $asdf;
Packit Service b38f0b
	return $asdf;
Packit Service b38f0b
}
Packit Service b38f0b
Packit Service b38f0b
# get the values from ipfilter's tables
Packit Service b38f0b
sub get_value {
Packit Service b38f0b
	local($table, $entry, $col, $row) = @_;
Packit Service b38f0b
Packit Service b38f0b
	if($table == 1) {
Packit Service b38f0b
		# fetch ipfInTable data
Packit Service b38f0b
		$ipf_output = `$ipf_in`;
Packit Service b38f0b
		$value = &fetch_hits_n_rules($row, $col, $ipf_output);
Packit Service b38f0b
	} elsif($table == 2) {
Packit Service b38f0b
		# fetch ipfOutTable data
Packit Service b38f0b
		$ipf_output = `$ipf_out`;
Packit Service b38f0b
		$value = &fetch_hits_n_rules($row, $col, $ipf_output);
Packit Service b38f0b
	} elsif($table == 3) {
Packit Service b38f0b
		# fetch ipfAccInTable data
Packit Service b38f0b
		$ipf_output = `$ipf_acc_in`;
Packit Service b38f0b
		$value = &fetch_hits_bytes_n_rules($row, $col, $ipf_output);
Packit Service b38f0b
	} elsif($table == 4) {
Packit Service b38f0b
		# fetch ipfAccOutTable data
Packit Service b38f0b
		$ipf_output = `$ipf_acc_out`;
Packit Service b38f0b
		$value = &fetch_hits_bytes_n_rules($row, $col, $ipf_output);
Packit Service b38f0b
	}
Packit Service b38f0b
	return $value;
Packit Service b38f0b
}
Packit Service b38f0b
Packit Service b38f0b
# generate response to 'get' or 'get-next' request
Packit Service b38f0b
sub response {
Packit Service b38f0b
	# print ObjectID, its type and the value
Packit Service b38f0b
	if(defined $ObjectID and defined $type{$tec} and defined $value) {
Packit Service b38f0b
		print "$ObjectID\n";
Packit Service b38f0b
		print "$type{$tec}\n";
Packit Service b38f0b
		print "$value\n";
Packit Service b38f0b
	}
Packit Service b38f0b
	exit;
Packit Service b38f0b
}