Cisco-Accounting

 view release on metacpan or  search on metacpan

lib/Cisco/Accounting.pm  view on Meta::CPAN

	if ($self->{'data'})  {
		return	$self->{'data'}->get_history();
	}
	else  {
		return 0;
	}
}

##
## clears the output buffer
##
sub clear_output()  {
	my ($self) = shift;
	
	$self->{'data'} = '';
}


##
## clears ip accounting information on the remote device
## this procedure should be used with eval {}
##
sub clear_accounting()  {
	my ($self) = shift;
	
	my $disconnect = 0;
	
	# if the connection is not yet active then we assume that it has to be closed again 
	if (!$self->{'session'})  {
		$disconnect = 1;
		eval {
			$self->_connect();
		};
		if ($@)  {
			croak $@;
		}
	}
	
	eval {
		if ($self->{'acct_type'} =~ /cisco/i)  {
			$self->{'session'}->cmd('clear ip accounting');
		}
		elsif ($self->{'acct_type'} =~ /ipcad/i)  {
			my @output = $self->{'session'}->cmd("rsh localhost clear ip accounting");
			if (  grep { $_ =~ /permission denied/i } @output)  {
				croak "cannot clear ip accounting : permission denied";
			}
		}
	};
	if ($@) {
		croak $@;
	}
	
	if ( ($disconnect > 0) && ($self->{'persistent'} <= 0) )  {
		$self->_disconnect();
	}
}


##
## Send a keepalive (new line character), do not do any error checking here
## Useful if 'persistent' is enabled, but still it's up to you to call the keepalive in time before session times out
##
sub keepalive()  {
	my ($self) = shift;
	
	if ($self->{'session'})  {
		eval  {
			$self->{'session'}->cmd(" ");
		};
	}
}




### TODO: do not go to config mode unless really needed

##
## Enable (1) or Disable (0) ip accounting depending on $status
##
sub _modify_accounting_settings()  {
	my ($self) = shift;
	my ($status) = shift;
	my (@int_id) = @_;
	
	## IPCAD interfaces are always enabled
	if ($self->{'acct_type'} =~ /ipcad/i)  {
		## nothing to do
		return;
	}
	
	## first check if we need to do something
	next unless ((scalar @int_id) > 0);
	next unless ((scalar @{$self->{'interfaces'}}) > 0);

	my $disconnect = 0;
	my ($id, $int);
	
	# if the connection is not yet active then we assume that it has to be closed again 
	if (!$self->{'session'})  {
		$disconnect = 1;
		eval {
			$self->_connect();
		};
		if ($@)  {
			croak $@;
		}
	}
	
	eval {
		if ($self->{'acct_type'} =~ /cisco/i)  {
			## go to config mode
			$self->{'session'}->config();	
			foreach $int (@{$self->{'interfaces'}})  {
				## only enable/disable ip accounting if needed
				if ((grep { $int->get_id() =~ /^$_$/}  @int_id) && ($int->get_accounting_status() != $status))  {
					$self->{'session'}->cmd('interface '.$int->get_interface());
					my $set_no = "";
					$set_no = "no" unless ($status > 0);
					$self->{'session'}->cmd("$set_no ip accounting output-packets");
					$self->{'session'}->cmd('exit');
					# change the interface status
					$int->set_accounting_status($status);

lib/Cisco/Accounting.pm  view on Meta::CPAN

This contains only data if the Cisco::Accounting was created with the 'lastpoll_data => 1' option.

This output can be useful if you're polling many times and you are interested in the summarized data of all polls but you also
want to know the details of each separate poll.

	$output = $acct->get_lastpoll_output();

Return value :

$output		Reference to hash with all the aggregated information. The hash contains for each unique source-destination 
			host pair the following values :

	'source'			=> source ip address
	'destination'		=> destination ip address
	'lastPollBytes'		=> number of bytes for this pair seen during last do_accounting()
	'lastPollPackets'	=> number of packets for this pair seen during last do_accounting()
	'totalBytes'		=> total number of bytes seen for this pair every time do_accounting() was called
	'totalPakcets'		=> total number of packets seen for this pair every time do_accounting() was called
	'polls'				=> number of times this pair was seen every time do_accounting() was called
						   ex. if we've 'polled' twice and only once this pair was seen then the value = 1

=item get_output()

Returns a reference to the hash containing all aggregated information. 

	$output = $acct->get_output();

Return value :

$output		Reference to hash with all the aggregated information. The hash contains for each unique source-destination 
			host pair the following values :

	'source'			=> source ip address
	'destination'		=> destination ip address
	'lastPollBytes'		=> number of bytes for this pair seen during last do_accounting()
	'lastPollPackets'	=> number of packets for this pair seen during last do_accounting()
	'totalBytes'		=> total number of bytes seen for this pair every time do_accounting() was called
	'totalPakcets'		=> total number of packets seen for this pair every time do_accounting() was called
	'polls'				=> number of times this pair was seen every time do_accounting() was called
						   ex. if we've 'polled' twice and only once this pair was seen then the value = 1

=item get_statistics()

Returns a reference to a hash of some general statistics.

	$stats = $acct->get_statistics()

Return value :

$stats		Reference to hash containing some general statistics :

	starttime			timestamp of the first time do_accounting() was called
	lastpolltime		timestamp of the last time do_accounting() was called
	totalpolls			number of times do_accounting() was called
	totalbytes			total number of bytes seen for every time do_accounting() was called
	totalpackets		total number of packets seen for every time do_accounting() was called
	totalpolledlines	total number of lines that was parsed and aggregated
	totalskippedlines	total number of lines that were skipped (headers etc.)
	uniquehostpairs		total number of unique host pairs that were seen

=item keepalive()

If you have a persistent connection but you're only calling do_accounting() every 5 minutes for example then you might receive
a connection timeout.
This can be solved by sending a keepalive every 30 seconds for example.
The keepalive just sends a newline character to the remote host avoiding a connection timeout.

	$acct->keepalive()

=back

=head1 SUPPORTED DEVICES

At the moment only telnet connections are supported, not SSH.

All Cisco routers and switches that support 'IP Accounting'. Make sure that you have enable permissions and in configure permissions in
case you're enabling or disabling IP Accounting on interfaces.

All hosts (usually Unix hosts) that are running the IPCAD daemon with default settings. Make sure that the username has sufficient permissions on the host.

=head1 EXAMPLES

Some working examples can be found in the test folder of the source code.

Also you can check out 'CIPAT' on SourceForge. This is a front-end to Cisco::Accounting and allows you to easily enter the required
parameters using a small wizard and to generate reports in different formats.


CONNECT TO CISCO ROUTER AND PARSE IP ACCOUNTING 5 TIMES

	use Cisco::Accounting;
	use Data::Dumper;

	my %data = (
			'host'		=>	"foo",
			'user'		=>	"user",
			'pwd'		=>	"pass",
			'enable_user'	=>	"user",
			'enable_pwd'	=>	"enable_pass",		
		);

	my $acct;	
	my @interfaces;
	my $output;
	my $stats;
	my $historical;
	my $count = 5;
	my $i = 0;
	my $interval = 10;

	## initialize : make a new object, get the interfaces and enable ip accounting on 2 interfaces
	eval {  
		$acct = Cisco::Accounting->new(%data);	
		@interfaces = $acct->get_interfaces();
		$acct->enable_accounting(2,1);  
	};
	die ($@) if ($@);

	## start polling, 5 times with interval of 60 seconds
	while ($i < $count)  {

		## parse IP accounnting info and clear the accounting after each 'poll'
		eval {
			print "getting accounting information";
			$acct->do_accounting();
			print " ... OK\n";
			$acct->clear_accounting();
		};



( run in 1.017 second using v1.01-cache-2.11-cpan-e93a5daba3e )