Skip to content

Instantly share code, notes, and snippets.

@braoru
Created February 7, 2014 14:26
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save braoru/8863516 to your computer and use it in GitHub Desktop.
Save braoru/8863516 to your computer and use it in GitHub Desktop.
logging.tcl
timing on
when RULE_INIT {
set static::field_sanatize_list [list \
"\"" "\\\""
]
}
when CLIENT_ACCEPTED {
set hsl [HSL::open -proto UDP -pool p_log_collector]
set hsl_tcp [HSL::open -proto TCP -pool p_p_elasticsearch_log_shipping]
}
when CLIENT_CLOSED {
unset hsl
unset hsl_tcp
}
when LB_SELECTED {
#logging header for debug
#set irule_a_logging_http "[IP::client_addr]:[TCP::client_port]"
#log local0. "$irule_a_logging_http: Begin to log"
# le request time adding to le log line
set time "\"@timestamp\": \"[clock format [clock seconds] -format "%Y-%m-%dT%T.000%z"]\","
set version "\"@version\": \"1\","
#le virtual server information
set virtual_server_name "\"virtual_server_name\": \"[virtual]\","
set source "\"source\": \"vs:/[virtual]\","
#bigip hostname
set bigip_hostname "\"source_host\": \"$static::tcl_platform(machine)\","
#le host client information
set source_ip "\"source_ip\": \"[IP::remote_addr]\","
set source_port "\"source_port\": \"[TCP::remote_port]\","
set request_informations "\"request_informations\":\{"
append request_informations "\"request_method\": \"[HTTP::method]\","
append request_informations "\"request_path\": \"[HTTP::path]\","
append request_informations "\"request_query\": \"[HTTP::query]\","
append request_informations "\"request_protocol\": \"HTTP/[HTTP::version]\""
append request_informations "\},"
# le magic message
set message "\"message\": \"[HTTP::method] [HTTP::uri] [HTTP::query] HTTP/[HTTP::version]\","
#
# to the le log line adding all the http headers
#
set request_headers "\"request_headers\": \{"
set request_header_list { }
foreach aHeader [HTTP::header names] {
if { not ($aHeader contains "Cookie") }{
#log local0. "$irule_a_logging_http: the header is not a cookie"
if { [lsearch $request_header_list $aHeader] == -1 } {
#log local0. "$irule_a_logging_http: adding header $aHeader"
if { [llength $request_header_list] != 0 } {
append request_headers ","
}
lappend request_header_list $aHeader
append request_headers "\"$aHeader\": \"[string map $static::field_sanatize_list [HTTP::header value $aHeader]]\""
}
}
}
append request_headers "\},"
unset request_header_list
#log local0. "$irule_a_logging_http: all the header : $header"
#the log line, le cookie fields adding to
set request_cookies "\"cookies\": \{"
set request_cookie_list {}
foreach aCookie [HTTP::cookie names] {
if { [lsearch $request_cookie_list $aCookie] == -1 } {
if { [llength $request_cookie_list] != 0 } {
append request_cookies ","
}
lappend request_cookie_list $aCookie
append request_cookies "\"$aCookie\": \"[string map $static::field_sanatize_list [HTTP::cookie value $aCookie]]\""
}
}
append request_cookies "\},"
unset request_cookie_list
#variable to complete the log line we save
#the request time I save
set http_request_time [clock clicks -milliseconds]
}
when HTTP_RESPONSE {
#log local0. "$irule_a_logging_http: we are in the response !"
# le request response code
set response_status_code "\"response_status_code\": \"[HTTP::status]\","
#
# to the le log line adding all the http headers
#
set response_headers "\"response_headers\": \{"
set response_cookies_set "\"response_cookies_set\":\["
set response_header_list { }
set response_cookie_set_list { }
set nb_cookies_set 0
foreach rHeader [HTTP::header names] {
# is it a cookie set ?
#log local0. "$irule_a_logging_http: working with $rHeader"
if { $rHeader contains "Set-Cookie" } {
#log local0. "$irule_a_logging_http: boulacookie $nb_cookies_set"
if { $nb_cookies_set != 0 } {
#log local0. "$irule_a_logging_http: COMA!"
append response_cookies_set ","
}
append response_cookies_set "\"[string map $static::field_sanatize_list [HTTP::header value $rHeader]]\""
incr nb_cookies_set
} else {
if { [lsearch $response_header_list $rHeader] == -1} {
#log local0. "$irule_a_logging_http: deal with a responce header !"
if { [llength $response_header_list] != 0 } {
append response_headers ","
}
lappend response_header_list $rHeader
append response_headers "\"$rHeader\": \"[string map $static::field_sanatize_list [HTTP::header value $rHeader]]\""
}
}
}
append response_headers "\},"
append response_cookies_set "\],"
unset response_header_list
unset response_cookie_set_list
unset nb_cookies_set
#time to answer the request needed the server as
set resp_time [expr [clock clicks -milliseconds] - $http_request_time]
set request_processing_time_ms "\"request_processing_time_ms\": \"$resp_time\","
#about the server side information we set
set destination_ip "\"destination_ip\": \"[LB::server addr]\","
set destination_port "\"destination_port\": \"[LB::server port]\","
set destination_pool "\"destination_pool\": \"[LB::server pool]\","
#no comma because end of message
set destination_name "\"destination_name\": \"[LB::server name]\""
#the log line create I do
set log_msg "\{"
#json_event object
append log_msg "$time"
append log_msg "$version"
append log_msg "$message"
append log_msg "$source"
append log_msg "$bigip_hostname"
#json_event fields object
#append log_msg "\"@fields\":\{"
append log_msg "$virtual_server_name"
append log_msg "$source_ip"
append log_msg "$source_port"
append log_msg "$destination_ip"
append log_msg "$destination_port"
append log_msg "$request_informations"
append log_msg "$request_headers"
append log_msg "$request_cookies"
append log_msg "$response_status_code"
append log_msg "$response_headers"
append log_msg "$response_cookies_set"
append log_msg "$request_processing_time_ms"
append log_msg "$destination_pool"
append log_msg "$destination_name"
#append log_msg "\},"
append log_msg "\"tags\": \[\"bigip\"\]"
append log_msg "\}\n"
#HSL::send $hsl "<190> bigip_http: $log_msg"
#log 192.168.210.219 local0. $log_msg
HSL::send $hsl "<190> [string trimleft [virtual] /Common/]: $log_msg"
HSL::send $hsl_tcp $log_msg
#clean all variable
unset time
unset virtual_server_name
unset source_ip
unset source_port
unset request_cookies
unset request_processing_time_ms
unset destination_ip
unset destination_port
unset destination_pool
unset destination_name
unset resp_time
unset response_status_code
unset response_headers
unset message
unset source
unset bigip_hostname
unset response_cookies_set
unset version
unset log_msg
#unset irule_a_logging_http
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment