#!/usr/bin/perl -w
# $Revision: 1-BIGIP 10.1 $
# $Date: February 25, 2010 $
# $Source: http://devcentral.f5.com/ $ 

use strict;
use warnings;
use Carp;
use Data:umper;

use LWP::UserAgent;
use Time::gmtime;
use XML::Element;
use XML::LibXML;
use Sys:yslog qw{EFAULT setlogsock};
use XML::TreeBuilder;

my $bigipSystem;
my $username;
my $password;
my $dashboardUA;
my $loginSessionURL;
my $loginCookieURL;
my $mcpqURL;
my $rstatsURL;
my $result;
my $xmlHostInfoTree;
my $xmlWOMInfoTree;
my $xmlConfigContentTree;
my $dashboardVersionContent;
my $dashboardWOMContent;
my $dashboardConfigContent;
my $logoutURL;
my $header;
my $message;
my $timestamp;

my $EMPTY = q{};
my $COMMA = q{,};
my $DOT = q{.};
my $EQUALS = q{=};
my $SPACE = q{ };
my $QUOTE = q{'};

my $sock_type = q{tcp};

## put your syslog sink in $stream_location - Perl will send messages to this host.
my $stream_location = '';

my $timeout = 2;
my $facility = q{local0};
my $options = q{perrror};
my $priority = q{info};

#setup the syslog information - in this case we're logging to QRadar
setlogsock($sock_type, $stream_location, $timeout);

##Section to get the data from the BIG-IP
#setup the information we need - BIG-IP URL and access credentials
$bigipSystem = shift or croak "usage: $0 BIGIPSystemName";
openlog($bigipSystem, $options, $facility);
$bigipSystem = 'https://' . $bigipSystem;

# enter the username and password for the user you'd like this script to be.
# this BIG-IP user must be able to access the acceleration dashboard in the GUI.
$username = '';
$password = '';

#Setup the URLs we'll use on the BIG-IP
$loginSessionURL = $bigipSystem . '/tmui/logon.jsp';
$loginCookieURL = $bigipSystem . '/tmui/logmein.html';
$mcpqURL = $bigipSystem . '/mcpq/mcpq';
$rstatsURL = $bigipSystem . '/rtstats/rtstats';
$logoutURL = $bigipSystem . '/tmui/logout.html';

#setup the useragent - we need a cookie jar and should only ever talk to the BIG-IP using HTTPS
#make sure you have Crypt:SLeay installed
$dashboardUA = LWP::UserAgent->new();
$dashboardUA->cookie_jar({});
$dashboardUA->protocols_allowed(undef);
$dashboardUA->protocols_allowed(['https']);

#get the JSESSIONID cookie for logging into BIG-IP and then login, we don't actually care about the page contents.
$dashboardUA->get($loginSessionURL);
$dashboardUA->post($loginCookieURL,
                [       'username'      => $username,
                        'passwd'        => $password
                ]);

# This is the data that goes into the log message header - add or remove things here that you
# want to see at the beginning of each log message
$result = $dashboardUA->post($mcpqURL,
                [       'func'  => 'dbvar',
                        'obj1'  => 'hostname',
                        'obj2'  => 'version.version',
#                       'obj3'  => 'version.build',
#                       'obj4'  => 'failover.state'
                ]);
$dashboardVersionContent = $result->content;
$dashboardVersionContent =~ s/\>\s+\</\>\</g; #take out any white space beteen XML nodes

# Get the WOM stats now
$result = $dashboardUA->post($rstatsURL,
                [       'table' => 'endpoint_isession_stat',
#                       'table' => 'plugin_cifs_stats',
#                       'table' => 'log_stat',
                        'table' => 'virtaul_server_stat',
                ]);
$dashboardWOMContent = $result->content;
$dashboardWOMContent =~ s/\>\s+\</\>\</g; #take out any white space beteen XML nodes

#Get configuration and license information
$result = $dashboardUA->post($mcpqURL,
                [       'func'  => 'mcp',
#                       'obj1'  => 'virtual_server_profile',
#                       'obj2'  => 'ltcfg_instance',
                        'obj3'  => 'woc_peer',
#                       'obj4'  => 'module_allocation',
#                       'obj5'  => 'license_blob',
                ]);
$dashboardConfigContent = $result->content;
$dashboardConfigContent =~ s/\>\s+\</\>\</g; #take out any white space beteen XML nodes

$result = undef;

##Section to process the data from the BIG-IP
$xmlHostInfoTree = XML::TreeBuilder->new( );
$xmlWOMInfoTree = XML::TreeBuilder->new( );
$xmlConfigContentTree = XML::TreeBuilder->new( );

#take the xml content and turn it into a tree
$xmlHostInfoTree->parse($dashboardVersionContent);
$xmlWOMInfoTree->parse($dashboardWOMContent);
$xmlConfigContentTree->parse($dashboardConfigContent);

$header = $EMPTY;
$timestamp = $xmlWOMInfoTree->find_by_tag_name( 'TimestampBefore' );
$timestamp = gmctime($timestamp->attr('seconds')) . ' GMT';

$header = join $header, 'device_timestamp', $EQUALS, $QUOTE, $timestamp , $QUOTE, $SPACE;

foreach my $db_variable ($xmlHostInfoTree->find_by_tag_name( 'db_variable' )) {
    foreach my $display_name ( $db_variable->find_by_tag_name ( 'display_name' )) {
        $header.= $display_name->as_text . $EQUALS;
    }
    foreach my $value ( $db_variable->find_by_tag_name ( 'value' )) {
        $header .= $QUOTE . $value->as_text . $QUOTE . $SPACE;
    }
}
# add a timestamp here - the one from the BIG-IP
$xmlHostInfoTree = $xmlHostInfoTree->delete;

$message = $EMPTY;
#print the endpoint isession stats now

foreach my $endpointiSessionStat ($xmlWOMInfoTree->find_by_tag_name( 'endpoint_isession_stat' )){
    $message .= $header . "message_type='endpoint_isession_stat'" . $SPACE;
    foreach my $statEntity ($endpointiSessionStat->content_list( )){
        $message .= $statEntity->tag() . $EQUALS . $QUOTE . $statEntity->as_text() . $QUOTE . $SPACE;
    }
    chop($message);
#    print($message . "\n");
    syslog($priority, $message);
    $message = $EMPTY;
}
$xmlWOMInfoTree = $xmlWOMInfoTree->delete;

#print the WANOpt Controller Peer inforamtion now
foreach my $wocPeer ($xmlConfigContentTree->find_by_tag_name( 'woc_peer' )){
    $message .= $header . "message_type='woc_peer'" . $SPACE;
    foreach my $configEntity ($wocPeer->content_list( )){
        $message .= $configEntity->tag() . $EQUALS . $QUOTE . $configEntity->as_text() . $QUOTE . $SPACE;
    }
    chop($message);
    syslog($priority, $message);
#    print($message . "\n");
    $message = $EMPTY;
}
$xmlConfigContentTree = $xmlConfigContentTree->delete;

#Logout of the BIG-IP
$dashboardUA->get($logoutURL);
exit 0;
