Sending log entries to hosted logging services (from a linux)
Below are some instructions on forwarding log entries from a server to a centralized logging service.
There are many advantages to such an arrangement, including
access to logs from multiple related services in a single location.
access control where different groups can see different views.
search apis - query log records by indexed fields, time ranges, etc.
built-in reporting, filtering, etc on web pages and queries via API.
in virtualized and cloud environments where servers may come and go on-demand the log entries are captured even if the server and it’s volumes have gone away.
Centralized logging services, or Logging as a Service (LaaS), accept streams of log entries over the network, index them, and make them available to you.
We are going to utilize two LaaS providers in these examples: Loggly and Logsene.
Run the following commands and then verify you’ve got some new test log entries on loggly’s web site.
Replace the parameters values with those for your loggly account.
12
curl -O https://www.loggly.com/install/configure-linux.sh
sudo bash configure-linux.sh -a <loggly domain name> -t <loggly token> -u <loggly user id>
apache httpd server
Now, we set up apache httpd server log file watches in rsyslog to forward web access and error logs to loggly.
Loggly has a script and manual instructions at https://okcra.loggly.com/sources/setup/apache
We’re going to use manual instructions since our log files have names other than the defaults.
The instance I’m using is amazon linux which already has /var/spool/rsyslog. We’ll change ownership of the directory from root:root to syslog:adm as the instructions suggest. As you will see in the configuration we instruct the rsyslog daemon to use /var/spool/rsyslog as it’s work directory and drop to privileges of group adm so we need to give that group write permissions to the work directory.
The install script create we ran earlier created /etc/rsyslog.d/22-loggly.conf which configures rsyslog to forward all messages to logs-01.loggly.com via TCP port 514 using the defined format LogglyFormat.
12345678910111213
# -------------------------------------------------------
# Syslog Logging Directives for Loggly (okcra.loggly.com)
# -------------------------------------------------------
# Define the template used for sending logs to Loggly. Do not change this format.
$template LogglyFormat,"<%pri%>%protocol-version% %timestamp:::date-rfc3339% %HOSTNAME% %app-name% %procid% %msgid% [92aff88c-05a2-480e-b2f4-4d2e4fb395e7@41058] %msg%"
# Send messages to Loggly over TCP using the template.
*.* @@logs-01.loggly.com:514;LogglyFormat
# -------------------------------------------------------
# End of Syslog Logging Directives for Loggly
# -------------------------------------------------------
We’ll create a new file named /etc/rsyslog.d/23-apache-loggly.conf for our apache-related loggly config.
create config file(s)
e.g. /etc/logstash/conf.d/logstash.conf
/etc/logstash/conf.d/logstash.conf
123456789101112131415161718192021222324252627
input{file{path=>"/var/log/httpd/api_okcra_ssl_access_log"type=>"apache-access"# a type to identify those logs (will need this later)start_position=>"beginning"}}filter{if[type]=="apache-access"{# this is where we use the type from the input sectiongrok{# ensure the matching logs use LogFormat combined for this parsermatch=>["message","%{COMBINEDAPACHELOG}"]}}}output{# this is for logsene elasticsearch formatelasticsearch{host=>"logsene-receiver.sematext.com"port=>80index=>"YOUR_LOGSENE_APP_TOKEN_GOES_HERE"protocol=>"http"manage_template=>false}}
Troubleshooting
Debugging rsyslog
Stop your syslog daemon and run it in the foreground with debugging turned on, printing to stdout.
These commands worked on my particular system - ymmv.
12
sudo system rsyslog stop
RSYSLOG_DEBUG="Debug" sudo /sbin/rsyslogd -d -n -c5
Verify network connections to logger endpoints
123
sudo netstat -taupn | grep syslog
tcp 0 0 10.65.29.118:56116 54.235.102.246:514 ESTABLISHED 5197/rsyslogd
tcp 0 0 10.65.29.118:35319 54.236.68.122:514 ESTABLISHED 5197/rsyslogd