# # Cacti Data input source # Paramaters: hostname username secret # Returns: Number of Zap channels currently in use # use strict; use IO::Socket; my $numArgs = $#ARGV + 1; if ($numArgs != 3) { print "Usage: $0 username secret host\n"; exit(); } my ($uid, $pwd, $host) = @ARGV; my $port = 5038; my $EOL = "\r\n"; my $BLANK = $EOL x 2; my $manager = new IO::Socket::INET( PeerAddr => $host, PeerPort => $port, Proto => 'tcp', Type => SOCK_STREAM, ReuseAddr => 1, Timeout => 10 ) or die "Connection Failed: $!\n"; $manager->autoflush(1); my $input = <$manager>; $input =~ s/$EOL//g; my @login = send_command ( Action => 'Login', Username => $uid, Secret => $pwd ); # print_response(@login); if ( ($login[0]{'Response'} ne 'Success') ) { print "Authentication failed for user $uid\n"; exit(); }; my @status = send_command( Action => 'Status' ); # print_response(@status); # the zap cout output to STDOUT printf "%i\n", count_zap(@status); sub send_command { my (%command) = @_; my $cstring = h2s(%command); $manager->send("$cstring$EOL"); return read_response(); }; sub read_response { my @response; while (1) { my %group = read_group(); push @response, \%group; last if $group{'Response'}; }; return @response; }; sub read_group { my @group; while (my $line = <$manager>) { last if ($line eq $EOL); $line =~ s/$EOL//g; push(@group, split(':\s*', $line)) if $line; }; return @group; }; sub print_response { my @response = @_; foreach my $group (@response) { print_group(%$group); }; }; sub print_group { my %group = @_; foreach (keys(%group)) { printf "[%s] : [%s]\n", $_, $group{$_} }; print "\n"; }; sub count_zap { my @response = @_; my $zap = grep ( $_->{'Channel'} =~ /^Zap/i , @response); return $zap; }; sub h2s { my (%thash) = @_; my $tstring = ''; foreach my $key (keys %thash) { $tstring .= sprintf "%s: %s$EOL", $key, $thash{$key}; }; return $tstring; };