Fluentd is an open-source data collector that unifies data collection and consumption.
It has different types of plugins that retrieve logs from external sources, parse them, and send them to log management tools like Site24x7 AppLogs. tail, forward, udp, tcp, http, syslog, exec, and windows_eventlog are common input plugins.
The way log management tools read logs varies with the type of log and how it’s sent. In this blog, we'll cover different use cases with their syntax.
<source>
@type syslog
port 5140
bind 0.0.0.0
tag system
</source>
<source>
@type windows_eventlog
@id windows_eventlog
channels application,system,security
tag winevt.raw
<storage>
@type local
persistent true
path C:\opt\td-agent\winevt.pos
</storage>
</source>
<source>
@type http
port 9880
bind 0.0.0.0
body_size_limit 32m
keepalive_timeout 10s
</source>
<source>
@type exec
tag system.loadavg
command cat /proc/loadavg | cut -d ' ' -f 1,2,3
run_interval 1m
<parse>
@type tsv
keys avg1,avg5,avg15
delimiter " "
</parse>
</source>
<source>
@type tail
path /var/log/sample-log.csv
pos_file /var/log/td-agent/csv.log.pos
tag dummy.access
<parse>
@type csv
keys time,host,req_id,user
time_key time
</parse>
</source>
<match dummy.*>
@type stdout
</match>
<source>
@type tail
path /var/log/httpd-access.log
exclude_path ["/path/to/*.gz", "/path/to/*.zip"]
pos_file /var/log/td-agent/httpd-access.log.pos
tag dummy.apache
<parse>
@type apache2
</parse>
</source>
<parse>
@type multiline
format_firstline /\d{4}-\d{1,2}-\d{1,2}/
format1 /^(?<time>\d{4}-\d{1,2}-\d{1,2} \d{1,2}:\d{1,2}:\d{1,2}) \[(?<thread>.*)\] (?<level>[^\s]+)(?<message>.*)/
</parse>
<filter foo.bar>
@type grep
<exclude>
key log_level
pattern /INFO/
</exclude>
</filter>
<filter foo.bar>
@type grep
<regexp>
key hostname
pattern /^web\d+\.example\.com$/
</regexp>
</filter>
Input = 172.21.163.159 - - [07/Jun/2017:19:53:11 +0530] \"GET /tets?private_token=0101032030301&user=as7&cid=12e09qweqweqq HTTP/1.1\" 200 12 \"-\" \"Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/52.0.2743.116 Safari/537.36\"
<source>
@type tail
path /var/log/httpd-access.log
pos_file /var/log/td-agent/httpd-access.log.pos
tag apache.access
<parse>
@type apache2
</parse>
</source>
<filter apache.access>
@type record_transformer
enable_ruby
<record>
path ${record["path"].gsub(/(private_token=)(\d+)/,'\1****'}
</record>
</filter>
<filter foo.bar>
@type parser
key_name log
reserve_data true
remove_key_name_field true
<parse>
@type json
</parse>
</filter>
<filter foo.bar>
@type record_transformer
remove_keys hostname,$.kubernetes.pod_id
</filter>
<filter foo.bar>
@type record_transformer
<record>
hostname "#{Socket.gethostname}"
</record>
</filter>
Input = {"total":100, "count":10}
Output = {"total":100, "count":10, "avg":"10"}
<filter foo.bar>
@type record_transformer
enable_ruby
auto_typecast true
<record>
avg ${record["total"] / record["count"]}
</record>
</filter>
Fluentd provides different options for customizing the way you parse logs. Logstash is a similar log shipper that is equally popular as Fluentd. It follows similar steps for using appropriate syntax and plugins to define how your logs should be parsed. Understanding the basics can help you parse logs based on your use cases.
Site24x7 supports log exports through both Fluentd and Logstash.