BIG-IP Blogging
Problem this snippet solves:
Summary: A CGI Script to return an RSS feed of the BIG-IP's status.
Your BIG-IP is now blogging. This is a CGI Script to return an RSS feed of the BIG-IP's status.
How to use this snippet:
So, load it up on your favorite perl-enabled web server, point your aggregator at it, and fire away...
Code :
#!/usr/bin/perl use CGI qw/:standard/; use SOAP::Lite; BEGIN { push (@INC, "."); } use iControlTypeCast; $q = new CGI; #-------------------------------------------------------------------- # Constants #-------------------------------------------------------------------- $CACHE_DIR = "/var/tmp/nscache"; $TITLE = "Live Network Status"; $LINK = $q->url(-path_info=>1,-query=>1); $DESCRIPTION = "iControl Live Network Status RSS Feed"; $MANAGINGEDITOR = "F5 DevCentral"; $LANGUAGE = "en-US"; $GENERATOR = "F5's one and only iControl!"; # Defaults $PROTOCOL = "http"; # # TODO: # # Either hard-code your connection settings here # or pass them through as query string arguments # $BIGIP = "192.168.11.1"; $PORT = "443"; $USER = "admin"; $PASS = "admin"; # extract QUERY_STRING arguments $URL = $q->url(); $URL_ABS = $q->url(-path=>1); $URL_BASE = $q->url(-base=>1); print "URL: $URL_BASE\n"; $type = $q->param('type'); $sHost = $q->param('host'); $sPort = $q->param('port'); $sUser = $q->param('user'); $sPass = $q->param('pass'); $AvailabilityStatusMap = { "AVAILABILITY_STATUS_NONE" => "Error", "AVAILABILITY_STATUS_GREEN" => "Available", "AVAILABILITY_STATUS_YELLOW" => "Offline", "AVAILABILITY_STATUS_RED" => "Offline", "AVAILABILITY_STATUS_BLUE" => "Unknown", }; $EnabledStatusMap = { "ENABLED_STATUS_NONE" => "Error", "ENABLED_STATUS_ENABLED" => "Enabled", "ENABLED_STATUS_DISABLED" => "Disabled", "ENABLED_STATUS_DISABLED_BY_PARENT" => "Disabled by Parent", }; $StatusImageMap = { "Available (Enabled)" => "$URL_BASE/images/status_circle_green.gif", "Available (Disabled)" => "$URL_BASE/images/status_circle_black.gif", "Available (Disabled by Parent)" => "$URL_BASE/images/status_circle_gray.gif", "Offline (Enabled)" => "$URL_BASE/images/status_diamond_red.gif", "Offline (Disabled)" => "$URL_BASE/images/status_diamond_black.gif", "Offline (Disabled by Parent)" => "$URL_BASE/images/status_diamond_gray.gif", "Unknown (Enabled)" => "$URL_BASE/images/status_square_blue.gif", "Unknown (Disabled)" => "$URL_BASE/images/status_square_black.gif", "Unknown (Disabled by Parent)" => "$URL_BASE/images/status_square_gray.gif", }; $TypeMap = { "vs" => "(Virtual Server)", "pool" => "(Pool)", "node" => "(Node Address)", "all" => "", }; # # Possible values: vs, pool, node # if ( "" eq $type ) { $type = "vs"; } if ( ! ( ("vs" eq $type) or ("pool" eq $type) or ("node" eq $type) or ("all" eq $type) ) ) { $type = "vs"; } $TITLE = $TITLE . " - $BIGIP $TypeMap->{$type}"; if ( "" ne $sHost ) { $BIGIP = $sHost; } if ( "" ne $sPort ) { $PORT = $sPort; } if ( "" ne $sUser ) { $USER = $sUser; } if ( "" ne $sPass ) { $PASS = $sPass; } if ( "443" eq $PORT ) { $PROTOCOL = "https"; } #-------------------------------------------------------------------- # Transport information #-------------------------------------------------------------------- sub SOAP::Transport::HTTP::Client::get_basic_credentials--> { return "$USER" => "$PASS"; } $VirtualServer = SOAP::Lite -> uri('urn -> proxy("$PROTOCOL://$BIGIP:$PORT/iControl/iControlPortal.cgi"); eval { $VirtualServer->transport->http_request->header ( 'Authorization' => 'Basic ' . MIME::Base64::encode("$USER:$PASS", '')--> ); }; $NodeAddress = SOAP::Lite -> uri('urn -> proxy("$PROTOCOL://$BIGIP:$PORT/iControl/iControlPortal.cgi"); eval { $NodeAddress->transport->http_request->header ( 'Authorization' => 'Basic ' . MIME::Base64::encode("$USER:$PASS", '')--> ); }; $Pool = SOAP::Lite -> uri('urn -> proxy("$PROTOCOL://$BIGIP:$PORT/iControl/iControlPortal.cgi"); eval { $Pool->transport->http_request->header ( 'Authorization' => 'Basic ' . MIME::Base64::encode("$USER:$PASS", '')--> ); }; #-------------------------------------------------------------------- # Application Code #-------------------------------------------------------------------- &setupCache(); &processRSS($type); #-------------------------------------------------------------------- # setupCache #-------------------------------------------------------------------- sub setupCache() { # make sure cache directory exists and can be written to mkdir $CACHE_DIR; chmod 0777, $CACHE_DIR; } #-------------------------------------------------------------------- # processRSS #-------------------------------------------------------------------- sub processRSS() { ($type) = (@_); &printHeader(); &beginRSS(); &beginChannel(); if ( "vs" eq $type ) { &processVS(); } elsif ( "pool" eq $type ) { &processPool(); } elsif ( "node" eq $type ) { &processNode(); } elsif ( "all" eq $type ) { &processVS(); &processPool(); &processNode(); } &endChannel(); &endRSS(); } #-------------------------------------------------------------------- # printHeader #-------------------------------------------------------------------- sub printHeader() { print $q->header( -type=>'text/xml', -expires=>'now', -"cache-control"=>'no-store, no-cache, must-revalidate'); print "\n"; } #-------------------------------------------------------------------- # beginRSS #-------------------------------------------------------------------- sub beginRSS() { print "\n"; } #-------------------------------------------------------------------- # endRSS #-------------------------------------------------------------------- sub endRSS () { print " \n"; } #-------------------------------------------------------------------- # beginChannel #-------------------------------------------------------------------- sub beginChannel() { print indent(1)."\n"; print indent(2)." \n"; } #-------------------------------------------------------------------- # printItem #-------------------------------------------------------------------- sub printItem() { ($item_name, $item_type, $availability_status, $enabled_status, $desc, $ts) = (@_); $creator = "iControl Live Status"; $title = "$item_type: $item_name"; $link = "$PROTOCOL://$BIGIP:$PORT/tmui/Control/jspmap/tmui/locallb/$item_type/properties.jsp?name=$item_name"; $pubdate = &getTime($ts); $guid = ""; $comment = ""; $comments = ""; $num_comments = ""; $commentRSS = ""; $trackback_ping = ""; $ae_status = "$AvailabilityStatusMap->{$availability_status} ($EnabledStatusMap->{$enabled_status})"; $description = "$TITLE \n"; print indent(2)."$LINK\n"; print indent(2)."$DESCRIPTION \n"; print indent(2)."$MANAGINGEDITOR \n"; print indent(2)."$LANGUAGE \n"; print indent(2)."$GENERATOR \n"; } #-------------------------------------------------------------------- # endChannel #-------------------------------------------------------------------- sub endChannel () { print indent(1)."$ae_status - '$desc'"; print indent(3)."
- \n"; print indent(4)."
\n"; } #-------------------------------------------------------------------- # sub getTime #-------------------------------------------------------------------- sub getTime() { ($t) = (@_); if ( "" eq $t ) { $t = time; } ($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst) = gmtime($t); $year += 1900; @mon_abbr = qw( Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec ); @day_abbr = qw( Sun Mon Tue Wed Thu Fri Sat ); # "Thu, 03 Nov 2005 11 $time_stamp = sprintf("%s, %.02d %s %d %.02d:%.02d:%.02d GMT", @day_abbr[$wday], $mday, @mon_abbr[$mon], $year, $hour, $min, $sec); return $time_stamp; } #-------------------------------------------------------------------- # indent #-------------------------------------------------------------------- sub indent() { ($i) = (@_); $s = ""; for($j=0; $j<$i; $j++) { $s = $s." "; } return $s; } #-------------------------------------------------------------------- # processVS #-------------------------------------------------------------------- sub processVS() { @cache_in = &loadCache("vs"); $soapResponse = $VirtualServer->get_list(); if ( $soapResponse->fault ) { return; } @obj_list = @{$soapResponse->result}; $soapResponse = $VirtualServer->get_object_status ( SOAP::Data->name(virtual_servers => [@obj_list]) ); if ( $soapResponse->fault ) { return; } @ObjectStatusList = @{$soapResponse->result}; my @cache_out; $size = scalar(@ObjectStatusList); for $i (0 .. $size-1) { $ObjectName = $obj_list[$i]; $ObjectStatus = $ObjectStatusList[$i]; $availability_status = $ObjectStatus->{"availability_status"}; $enabled_status = $ObjectStatus->{"enabled_status"}; $status_description = $ObjectStatus->{"status_description"}; $t0 = time; # Look for the value in the cache. foreach $cache_entry (@cache_in) { ($ts, $obj, $val) = split(/,/, $cache_entry); if ( ($obj eq $ObjectName) && ($val eq $availability_status) ) { $t0 = $ts; } } &printItem($ObjectName, "virtual_server", $availability_status, $enabled_status, $status_description, $t0); push @cache_out, "$t0,$ObjectName,$availability_status"; } &saveCache("vs", \@cache_out); } #-------------------------------------------------------------------- # processPool #-------------------------------------------------------------------- sub processPool() { @cache_in = &loadCache("pool"); $soapResponse = $Pool->get_list(); if ( $soapResponse->fault ) { return; } @obj_list = @{$soapResponse->result}; $soapResponse = $Pool->get_object_status ( SOAP::Data->name(pool_names => [@obj_list]) ); if ( $soapResponse->fault ) { return; } @ObjectStatusList = @{$soapResponse->result}; my @cache_out; $size = scalar(@ObjectStatusList); for $i (0 .. $size-1) { $ObjectName = $obj_list[$i]; $ObjectStatus = $ObjectStatusList[$i]; $availability_status = $ObjectStatus->{"availability_status"}; $enabled_status = $ObjectStatus->{"enabled_status"}; $status_description = $ObjectStatus->{"status_description"}; $t0 = time; # Look for the value in the cache. foreach $cache_entry (@cache_in) { ($ts, $obj, $val) = split(/,/, $cache_entry); if ( ($obj eq $ObjectName) && ($val eq $availability_status) ) { $t0 = $ts; } } &printItem($ObjectName, "pool", $availability_status, $enabled_status, $status_description, $t0); push @cache_out, "$t0,$ObjectName,$availability_status"; } &saveCache("pool", \@cache_out); } #-------------------------------------------------------------------- # processNode #-------------------------------------------------------------------- sub processNode() { @cache_in = &loadCache("node"); $soapResponse = $NodeAddress->get_list(); if ( $soapResponse->fault ) { return; } @obj_list = @{$soapResponse->result}; $soapResponse = $NodeAddress->get_object_status ( SOAP::Data->name(node_addresses => [@obj_list]) ); if ( $soapResponse->fault ) { return; } @ObjectStatusList = @{$soapResponse->result}; my @cache_out; $size = scalar(@ObjectStatusList); for $i (0 .. $size-1) { $ObjectName = $obj_list[$i]; $ObjectStatus = $ObjectStatusList[$i]; $availability_status = $ObjectStatus->{"availability_status"}; $enabled_status = $ObjectStatus->{"enabled_status"}; $status_description = $ObjectStatus->{"status_description"}; $t0 = time; # Look for the value in the cache. foreach $cache_entry (@cache_in) { ($ts, $obj, $val) = split(/,/, $cache_entry); if ( ($obj eq $ObjectName) && ($val eq $availability_status) ) { $t0 = $ts; } } &printItem($ObjectName, "node", $availability_status, $enabled_status, $status_description, $t0); push @cache_out, "$t0,$ObjectName,$availability_status"; } &saveCache("node", \@cache_out); } #-------------------------------------------------------------------- # sub loadCache #-------------------------------------------------------------------- sub loadCache() { ($type) = (@_); $file = "$CACHE_DIR/cache_$type.txt"; my @cache_in; if ( open(CACHE_FILE, "<$file") ) { my @rows; while ($creator \n"; print indent(4)."$title \n"; print indent(4)."$link\n"; print indent(4)."$pubdate \n"; print indent(4)."$guid \n"; print indent(4)."$comment \n"; print indent(4)."$comments \n"; print indent(4)."$num_comments \n"; print indent(4)."$commentRSS \n"; print indent(4)."$trackback_ping \n"; print indent(4)."$description \n"; print indent(3).") { chomp; push @cache_in, $_; } close(CACHE_FLE); } return @cache_in; } #-------------------------------------------------------------------- # sub saveCache #-------------------------------------------------------------------- sub saveCache() { ($type, $cache_records) = (@_); $file = "$CACHE_DIR/cache_$type.txt"; if ( open(CACHE_FILE, ">$file") ) { foreach $cache_record (@{$cache_records}) { #print "record : $cache_record\n"; printf CACHE_FILE "$cache_record\n"; } close(CACHE_FILE); chmod 0666, $file; } else { print "Couldn't save cache to $file\n"; } }
Published Mar 07, 2015
Version 1.0