Linux – Logstash: UNIX Epoch time not getting converted to readable format

epoch-timefilterlinuxlogstashsquid

I have setup an ELK stack and I am trying to parse squid log entries.

And I am having a problem trying to convert the following UNIX/Epoc time as

1442469455.757

to a human readable format.

While trouble shooting I get the following error:

Received an event that has a different character encoding than you
configured.

and this comes with a "_dateparsefailure" tag which means it failed.

I have used the following logstash filter

filter {
if [type] == "squid" {
        grok {
        patterns_dir   => [ "/etc/logstash/patterns" ]
        match => { message => "%{SQUID_LOG}" }
        }
        date {
          match => [ "timestamp", "UNIX" ]
        }
   }
}

The regex pattern defined to match the timestamp in the main pattern "%{SQUID_LOG}" is: (%{DATA:timestamp})

Please let me know if there is a permanent solution or a workaround for this.

Thanks in Advance.

UPDATE:

This seems to be caused by the extra space after the timestamp as mentioned below:

value=>"1438744871.647\\xA0\\xA0\\xA0\\xA0\\xA0", :exception=>"Invalid UNIX epoch value '1438744871.647\\xA0\\xA0\\xA0\\xA0\\xA0'", :config_parsers=>"UNIX", :config_locale=>"default=en_GB", :level=>:warn

Is there a way to get rid of those '\\xA0\\xA0\\xA0\\xA0\\xA0' after the timestamp ?

Config:

input { stdin { } }

filter {
        grok {
        match => { message => "((%{DATA:time_stamp}) (%{NUMBER:time_elapsed_ms}) (%{IPV4:client_ip}) (%{WORD:req_stat})/(%{INT:reply_code}) (%{INT:request_size}) (%{WORD:http_method}) (%{URIPROTO:request_protocol}://)?%{IPORHOST:request_hostname}(?::%{POSINT:port})?(?:%{URIPATHPARAM:uri_param}|) (%{USERNAME:user}) (%{WORD:squid_hierarchy})/(%{HOST:server}|-) (%{DATA:content_type}) (%{WORD:snaction}|-))" }
        add_tag => "NONU"
        }
        mutate {
        strip => [ "time_stamp" ]
        }
        date {
         match => [ "time_stamp", "UNIX" ]
        }
   }

output {
  stdout { codec => rubydebug }
}

Sample data:

1442469456.136      1 19.108.217.100 DENIED/407 3864 CONNECT fei.wsp.microsoft.com:443 - HIER_NONE/- text/html -

Best Answer

If the errors are really being caused by the extra whitespace in the time_stamp field, you can use the mutate filter to strip it out. Your filter would then look like this:

filter {
  if [type] == "squid" {
    grok {
      patterns_dir   => [ "/etc/logstash/patterns" ]
      match => { message => "%{SQUID_LOG}" }
    }
    mutate {
      strip => ["time_stamp"]
    }
    date {
      match => [ "time_stamp", "UNIX" ]
    }
 }
}

Update

If all the log entries have exactly 6 extra spaces after the timestamp, update your grok pattern as follows. Note the extra spaces between time_stamp and time_epapsed_ms.

((%{DATA:time_stamp})      (%{NUMBER:time_elapsed_ms}) (%{IPV4:client_ip}) (%{WORD:req_stat})/(%{INT:reply_code}) (%{INT:request_size}) (%{WORD:http_method}) (%{URIPROTO:request_protocol}://)?%{IPORHOST:request_hostname}(?::%{POSINT:port})?(?:%{URIPATHPARAM:uri_param}|) (%{USERNAME:user}) (%{WORD:squid_hierarchy})/(%{HOST:server}|-) (%{DATA:content_type}) (%{WORD:snaction}|-))

If there's a possibility that it might be more or less than 6 spaces, the following should work.

((%{DATA:time_stamp})%{SPACE}(%{NUMBER:time_elapsed_ms}) (%{IPV4:client_ip}) (%{WORD:req_stat})/(%{INT:reply_code}) (%{INT:request_size}) (%{WORD:http_method}) (%{URIPROTO:request_protocol}://)?%{IPORHOST:request_hostname}(?::%{POSINT:port})?(?:%{URIPATHPARAM:uri_param}|) (%{USERNAME:user}) (%{WORD:squid_hierarchy})/(%{HOST:server}|-) (%{DATA:content_type}) (%{WORD:snaction}|-))