おれさまラボ

実際に手を動かして理解を深めるブログ。

Squid のアクセスログをリモート先の td-agent で受信する

■ 環境
OSは、CentOS7.6 を利用しています。
[root@fluentd1 ~]# cat /etc/*release
CentOS Linux release 7.6.1810 (Core)
NAME="CentOS Linux"
VERSION="7 (Core)"
ID="centos"
ID_LIKE="rhel fedora"
VERSION_ID="7"
PRETTY_NAME="CentOS Linux 7 (Core)"
ANSI_COLOR="0;31"
CPE_NAME="cpe:/o:centos:centos:7"
BUG_REPORT_URL="https://bugs.centos.org/"
 
 
CENTOS_MANTISBT_PROJECT="CentOS-7"
CENTOS_MANTISBT_PROJECT_VERSION="7"
REDHAT_SUPPORT_PRODUCT="centos"
REDHAT_SUPPORT_PRODUCT_VERSION="7"
 
 
CentOS Linux release 7.6.1810 (Core)
CentOS Linux release 7.6.1810 (Core)
 
td-agent は、0.12.43 を使用しています。
[root@fluentd1 ~]# td-agent --version
td-agent 0.12.43
 
■ 手順
 
fluentdサーバ上の /etc/td-agent/td-agent.conf に以下を追記します。
<source>
  @type syslog
  port 514
  bind 0.0.0.0
  tag local0
</source>
 
 
<match local0.**>
  @type file
  path /var/log/td-agent/squid-td
</match>
 
td-agent を再起動します。
[root@fluentd1 ~]# systemctl restart td-agent
 
ログを確認し、514ポートがバインドされたことを確認します。併せて、その他のエラーがでていないことも確認します。ここでうまくいかない場合は、rsyslogが起動していないか確認してください。起動していた場合は、停止/disable してください。
[root@fluentd1 ~]# tail -f /var/log/td-agent/td-agent.log
  </source>
  <match local0.**>
    @type file
    path /var/log/td-agent/squid-td
    buffer_path /var/log/td-agent/squid-td.*
  </match>
</ROOT>
2019-03-09 11:34:57 +0900 [info]: listening fluent socket on 0.0.0.0:24224
2019-03-09 11:34:57 +0900 [info]: listening dRuby uri="druby://127.0.0.1:24230" object="Engine"
2019-03-09 11:34:57 +0900 [info]: listening syslog socket on 0.0.0.0:514 with udp
 
Squidサーバで、rsyslogの設定を編集します。
# Save squid access log
local0.*                                                /var/log/squid/access.log
 
 
# ### begin forwarding rule ###
# The statement between the begin ... end define a SINGLE forwarding
# rule. They belong together, do NOT split them. If you create multiple
# forwarding rules, duplicate the whole block!
# Remote Logging (we use TCP for reliable delivery)
#
# An on-disk queue is created for this action. If the remote host is
# down, messages are spooled to disk and sent when it is up again.
#$ActionQueueFileName fwdRule1 # unique name prefix for spool files
#$ActionQueueMaxDiskSpace 1g   # 1gb space limit (use as much as possible)
#$ActionQueueSaveOnShutdown on # save messages to disk on shutdown
#$ActionQueueType LinkedList   # run asynchronously
#$ActionResumeRetryCount -1    # infinite retries if host is down
# remote host is: name/ip:port, e.g. 192.168.0.1:514, port optional
local0.* @192.168.1.114:514
# ### end of the forwarding rule ###
 
何らかの方法で、Squid 経由でのWEBアクセスを発生させます。今回は、curl を使ってローカルに立てているWebサーバーへ アクセスさせました。
 
td-agentサーバーで、tcpdump を使ってsyslogが飛んできていることを確認します。
[root@fluentd1 ~]# tcpdump -s0 -i any dst port 514 -n
tcpdump: verbose output suppressed, use -v or -vv for full protocol decode
listening on any, link-type LINUX_SLL (Linux cooked), capture size 262144 bytes
11:48:00.918113 IP 192.168.1.124.46610 > 192.168.1.114.syslog: SYSLOG local0.info, length: 167
11:48:03.167586 IP 192.168.1.124.46610 > 192.168.1.114.syslog: SYSLOG local0.info, length: 186
 
td-agentサーバーで、JSON形式のログが記録されていることを確認します。
2019-03-09T11:48:00+09:00       local0.local0.info      {"host":"proxy1","ident":"(squid-1)","message":"127.0.0.1 - - [09/Mar/2019:11:48:00 +0900] \"GET cache_object://localhost:8080/info HTTP/1.0\" 200 2640 \"-\" \"-\" TCP_MISS:HIER_NONE"}
2019-03-09T11:48:03+09:00       local0.local0.info      {"host":"proxy1","ident":"(squid-1)","message":"192.168.1.123 - - [09/Mar/2019:11:48:03 +0900] \"GET http://192.168.1.121/dummy.iso HTTP/1.1\" 200 10737418680 \"-\" \"curl/7.29.0\" TCP_MISS:HIER_DIRECT"}
 
以上です。

Squid のアクセスログを rsyslog サーバーへ転送する

■ 環境
OS は CentOS 7.4 を利用します。
[root@proxy1 ~]# cat /etc/*release
CentOS Linux release 7.4.1708 (Core)
NAME="CentOS Linux"
VERSION="7 (Core)"
ID="centos"
ID_LIKE="rhel fedora"
VERSION_ID="7"
PRETTY_NAME="CentOS Linux 7 (Core)"
ANSI_COLOR="0;31"
CPE_NAME="cpe:/o:centos:centos:7"
BUG_REPORT_URL="https://bugs.centos.org/"
 
 
CENTOS_MANTISBT_PROJECT="CentOS-7"
CENTOS_MANTISBT_PROJECT_VERSION="7"
REDHAT_SUPPORT_PRODUCT="centos"
REDHAT_SUPPORT_PRODUCT_VERSION="7"
 
 
CentOS Linux release 7.4.1708 (Core)
CentOS Linux release 7.4.1708 (Core)
 
Squid は 3.5.20 を利用します。
[root@proxy1 ~]# squid -v
Squid Cache: Version 3.5.20
Service Name: squid
configure options:  '--build=x86_64-redhat-linux-gnu' '--host=x86_64-redhat-linux-gnu' '--program-prefix=' '--prefix=/usr' '--exec-prefix=/usr' '--bindir=/usr/bin' '--sbindir=/usr/sbin' '--sysconfdir=/etc' '--datadir=/usr/share' '--includedir=/usr/include' '--libdir=/usr/lib64' '--libexecdir=/usr/libexec' '--sharedstatedir=/var/lib' '--mandir=/usr/share/man' '--infodir=/usr/share/info' '--disable-strict-error-checking' '--exec_prefix=/usr' '--libexecdir=/usr/lib64/squid' '--localstatedir=/var' '--datadir=/usr/share/squid' '--sysconfdir=/etc/squid' '--with-logdir=$(localstatedir)/log/squid' '--with-pidfile=$(localstatedir)/run/squid.pid' '--disable-dependency-tracking' '--enable-eui' '--enable-follow-x-forwarded-for' '--enable-auth' '--enable-auth-basic=DB,LDAP,MSNT-multi-domain,NCSA,NIS,PAM,POP3,RADIUS,SASL,SMB,SMB_LM,getpwnam' '--enable-auth-ntlm=smb_lm,fake' '--enable-auth-digest=file,LDAP,eDirectory' '--enable-auth-negotiate=kerberos' '--enable-external-acl-helpers=file_userip,LDAP_group,time_quota,session,unix_group,wbinfo_group,kerberos_ldap_group' '--enable-cache-digests' '--enable-cachemgr-hostname=localhost' '--enable-delay-pools' '--enable-epoll' '--enable-ident-lookups' '--enable-linux-netfilter' '--enable-removal-policies=heap,lru' '--enable-snmp' '--enable-ssl-crtd' '--enable-storeio=aufs,diskd,rock,ufs' '--enable-wccpv2' '--enable-esi' '--enable-ecap' '--with-aio' '--with-default-user=squid' '--with-dl' '--with-openssl' '--with-pthreads' '--disable-arch-native' 'build_alias=x86_64-redhat-linux-gnu' 'host_alias=x86_64-redhat-linux-gnu' 'CFLAGS=-O2 -g -pipe -Wall -Wp,-D_FORTIFY_SOURCE=2 -fexceptions -fstack-protector-strong --param=ssp-buffer-size=4 -grecord-gcc-switches   -m64 -mtune=generic -fpie' 'LDFLAGS=-Wl,-z,relro  -pie -Wl,-z,relro -Wl,-z,now' 'CXXFLAGS=-O2 -g -pipe -Wall -Wp,-D_FORTIFY_SOURCE=2 -fexceptions -fstack-protector-strong --param=ssp-buffer-size=4 -grecord-gcc-switches   -m64 -mtune=generic -fpie' 'PKG_CONFIG_PATH=:/usr/lib64/pkgconfig:/usr/share/pkgconfig'
 
squid.conf の編集
$ cp -ip /etc/squid/squid.conf /etc/squid/squid.conf.20190222.bef
$ vi /etc/squid/squid.conf
$ diff /etc/squid/squid.conf /etc/squid/squid.conf.20190222.bef
34d33
< access_log syslog:local0.info combined
 
squid の再起動
$ systemctl status squid
$ systemctl restart squid
$ systemctl status squid
 
■ rsyslog.conf の編集(送信側:squid サーバー)
$ cp -ip  /etc/rsyslog.conf /etc/rsyslog.conf.20190222.bef
$ vi /etc/rsyslog.conf
$ diff /etc/rsyslog.conf /etc/rsyslog.conf.20190222.bef
75,76d74
< # Save squid access log
< local0.*                                              /var/log/squid/access.log
92c90
< local0.* @192.168.1.116:514
---
> *.* @192.168.1.116:514
 
■ rsyslog の再起動(送信側:squid サーバー)
$ systemctl status rsyslog
$ systemctl restart rsyslog
$ systemctl status rsyslog
 
■ ログ保存フォルダの作成(受信側:rsyslog サーバー)
$ mkdir /var/log/192.168.1.124
 
■ rsyslog.conf の編集(受信側:rsyslog サーバー)
$ cp -ip  /etc/rsyslog.conf /etc/rsyslog.conf.20190222.bef
$ vi /etc/rsyslog.conf
 
# 以下の内容を追記する。
---
# proxy1
$template proxy,"/var/log/192.168.1.124/accesslog_%$year%%$month%%$day%".log
:fromhost-ip, isequal, "192.168.1.124" ?proxy
&stop
 
■ rsyslog の再起動(受信側:rsyslog サーバー)
$ systemctl status rsyslog
$ systemctl restart rsyslog
$ systemctl status rsyslog
 
■ 動作確認
プロキシサーバー経由でWEBアクセスを行うと、受信側:rsyslog サーバーの所定のフォルダへアクセスログが記録されます。
 
以上です。

fluentd(td-agent)をインストールする

■ インストールスクリプトのダウンロード & 実行
 
■ サービス起動
[root@fluentd1 ~]# systemctl status td-agent
● td-agent.service - LSB: data collector for Treasure Data
   Loaded: loaded (/etc/rc.d/init.d/td-agent; bad; vendor preset: disabled)
   Active: inactive (dead)
     Docs: man:systemd-sysv-generator(8)
 
[root@fluentd1 ~]# systemctl start td-agent
 
[root@fluentd1 ~]# systemctl status td-agent
● td-agent.service - LSB: data collector for Treasure Data
   Loaded: loaded (/etc/rc.d/init.d/td-agent; bad; vendor preset: disabled)
   Active: active (running) since 日 2019-03-03 14:59:46 JST; 7s ago
     Docs: man:systemd-sysv-generator(8)
  Process: 1492 ExecStart=/etc/rc.d/init.d/td-agent start (code=exited, status=0/SUCCESS)
Main PID: 1516 (ruby)
   CGroup: /system.slice/td-agent.service
           tq1516 /opt/td-agent/embedded/bin/ruby /usr/sbin/td-agent --log /var/log/td-agent/td-a...
           mq1519 /opt/td-agent/embedded/bin/ruby /usr/sbin/td-agent --log /var/log/td-agent/td-a...
 
 
3月 03 14:59:45 fluentd1.oresamalabo.local systemd[1]: Starting LSB: data collector for Treas.....
3月 03 14:59:45 fluentd1.oresamalabo.local runuser[1509]: pam_unix(runuser:session): session o...)
3月 03 14:59:46 fluentd1.oresamalabo.local td-agent[1492]: [44B blob data]
3月 03 14:59:46 fluentd1.oresamalabo.local systemd[1]: Started LSB: data collector for Treasu...a.
Hint: Some lines were ellipsized, use -l to show in full.
 
■ 動作確認
[root@fluentd1 ~]# curl -X POST -d 'json={"json":"message"}' http://localhost:8888/debug.test
[root@fluentd1 ~]# tail -3 /var/log/td-agent/td-agent.log
2019-03-03 14:59:46 +0900 [info]: listening fluent socket on 0.0.0.0:24224
2019-03-03 14:59:46 +0900 [info]: listening dRuby uri="druby://127.0.0.1:24230" object="Engine"
2019-03-03 15:00:35 +0900 debug.test: {"json":"message"}
 
設定ファイルはここ。
* 設定ファイル -> /etc/td-agent/td-agent.conf
* ログファイル -> /var/log/td-agent/td-agent.log
 
参考
CentOS7でFluentdを使用してログ収集をしてみよう

IdP 導入の検討は難しい

2/18日のメモから。

昨今、SaaS利用について情シスが頭を悩ます中、それを解決するソリューションとして IdPによる認証認可が登場してきている。よくまとまってると思える記事があったのでここで備忘も兼ねて紹介。

現在、仕事でもIdPの導入検討がなされているが、運用がかなり難しそうだと感じる。対象となる企業が、情報漏えいをどこまで重要と考えるかも含め、導入には慎重になったほうが良いと思った。

続きを読む

id コマンドの使い方

はじめに

とある Vagrantfile を読んでいると、idコマンドが使われていました。 用途としては、Provisioning が rootユーザで実行されているかをチェックしていました。 気になったので、id コマンドの使い方を改めて勉強しました。

使い方

rootユーザで試してみます。

$ whoami
root

-uオプションでは、ログインしているユーザの、ユーザIDを表示します。

$ id -u
0

-gオプションでは、ログインしているユーザの、グループIDを表示します。

$ id -g
0

-Gオプションでは、所属しているグループを表示します。

$ id -G
0

-nオプションでは、IDの代わりにユーザ名を表示します。

$ id -u -n
root

-unのように指定することもできます。

$ id -un
root

-rオプションでは、実際のユーザID、およびグループIDを表示できます。

$ id -u -r
0

usernameをつけると、指定したユーザのユーザID、およびグループIDを表示できます。

$ id -u root
0

複数の情報を同時に表示することはできないようです。

$ id -u -g
id: cannot print "only" of more than one choice

情報をすべて表示したければ、idコマンドをNoオプションで実行すればよいです。

$ id
uid=0(root) gid=0(root) groups=0(root)

if文を使って、こんな風に使うことができます。

if [ `id -u` -ne 0 ]; then
    echo "You are not 'root' user."
fi

以上です。