Skip to content

Instantly share code, notes, and snippets.

@kawanet
kawanet / fluentd.conf
Last active August 29, 2015 14:21
fluentd recipes
# http://www.fluentd.org/guides/recipes/apache-add-hostname
# Generating event tags based on the hostname
<source>
type tail
path /var/log/nginx/access.log
tag "access.#{Socket.gethostname}"
format nginx
time_format %d/%b/%Y:%H:%M:%S %z
pos_file /tmp/fluentd-tail-access.pos
@kawanet
kawanet / fluent-cat-msgpack.sh
Last active December 30, 2016 03:04
標準入力の内容を fluentd に送り続けるワンライナー
# in_forward を使う場合(TCP 24224 番ポート)MessagePack 形式
# http://docs.fluentd.org/v0.12/categories/in_forward
# fluentd -c /dev/null -i '<source>\ntype forward\n</source>\n<match **>\ntype stdout\n</match>'
ruby -rsocket -rmsgpack -ne '(s=s||(s=TCPSocket.new "127.0.0.1",24224)).print ["debug",Time.now.to_i,{message:$_.chomp}].to_msgpack unless /^$/'
@kawanet
kawanet / fluentd-tcp-udp.sh
Last active August 29, 2015 14:21
fluentd で標準入力から in_tcp や in_udp 経由でログを入れるテスト
fluentd -c ./fluentd.conf -v
date | nc 127.0.0.1 5170
date | nc -u 127.0.0.1 5160
date | socat tcp:127.0.0.1:5170 stdin
date | socat udp:127.0.0.1:5160 stdin
@kawanet
kawanet / fluent-cat-syslog.sh
Last active August 29, 2015 14:21
fluentd で標準入力から syslog 経由でログを送る
# use mlogger https://github.com/nbrownus/mlogger on mac
logger=$(which mlogger 2> /dev/null || which logger)
$logger -n 127.0.0.1 -P 5140 --udp -i -p info -t debug
# -t => "ident"
# -p => tag suffix
# -i => "pid"
#
# sample result:
@kawanet
kawanet / nginx-for-fluentd.conf
Last active August 29, 2015 14:21
nginx のアクセスログをファイルに落とさずに fluentd に直送する
server {
listen 8000;
access_log syslog:server=127.0.0.1:8514 combined;
error_log syslog:server=127.0.0.1:5140;
:
}
@kawanet
kawanet / nginx-ltsv-for-fluentd.conf
Created May 25, 2015 17:31
nginx のアクセスログを LTSV 形式で、ログファイルに落とさずに fluentd に直送する
log_format ltsv
'time:$time_local\t'
'remote:$remote_addr\t'
'method:$request_method\t'
'user:$remote_user\t'
'path:$request_uri\t'
'code:$status\t'
'size:$body_bytes_sent\t'
'referer:$http_referer\t'
'agent:$http_user_agent\t'
@kawanet
kawanet / fluentd-udp-json.js
Last active August 29, 2015 14:21
JavaScript から fluentd にログを送る
#!/usr/bin/env node
/**
* @see http://docs.fluentd.org/articles/in_udp
* @see https://nodejs.org/api/dgram.html#dgram_socket_send_buf_offset_length_port_address_callback
*/
var dgram = require("dgram");
var data = { foo: "bar" };
data.time = Math.floor(new Date() / 1000);
@kawanet
kawanet / fullpath.sh
Last active August 29, 2015 14:22
sh で相対パスで起動されたシェルスクリプトを絶対パスで起動し直す
#!/usr/bin/env bash
# re-launch itself with full path specified
[ "${0:0:1}" = "/" ] || exec -- "$0" "$@"
# relative path available then
./path/to/file
@kawanet
kawanet / su-when-root.sh
Created May 27, 2015 16:53
root 権限で実行されていたら特定ユーザに su してシェルスクリプトを起動し直す
#!/bin/sh
# re-launch itself with a specified user when launched with root user
[ "$(id -u)" -eq 0 ] && exec su - "someuser" -c "$0 $*"
# do something
whoami
@kawanet
kawanet / in_http_application_json.sh
Created May 29, 2015 22:23
fluentd のドキュメントに記載されていないけど、in_http は Content-Type: application/json も受け付ける
#!/bin/sh
# @see http://docs.fluentd.org/articles/in_http
curl -i -X POST -d 'json={"action":"login","user":2}' http://localhost:8888/test.tag.here
# below posts as same as above
curl -i -X POST -H 'Content-Type: application/json' -d '{"action":"login","user":2}' http://localhost:8888/test.tag.here