LoginSignup
13
3

More than 3 years have passed since last update.

OCI Logging Custom Log のあれこれ

Last updated at Posted at 2020-12-02

この記事はOracle Cloud Infrastructure #2 Advent Calendar 2020の12月2日分です。
(12/3 DBCS などの PaaS をはじめとする Unified Monitoring Agent の入っていない環境でのログ収集について追記しました。)

以前、 OCI Logging の Custom Log について、とりあえず試してみたのですが、今回は個人的に気になったことを諸々確認してみます。 Custom Log あれこれ、みたいな感じのテーマです。 Logging はそれなりにドキュメントにも記載があるので、あわせてご参照ください。

ログコレクタの詳細確認

とりあえず前回はログを集めてみたのですが、今回はもうちょっと確認してみます。クラウド側の構成は見たまんまといえば見たまんまなので、エージェント側です。

Custom Log の収集には Oracle が提供する fluentd ベースのエージェントが使われます。 OCI インスタンスの場合作成時に Monitoring を有効化するとこの Agent も有効になるようです。Monitoring 用に使われている oracle-cloud-agent とは別物です。以下ドキュメントです。

Agent Management Oracle Cloud Infrastructure Documentation

systemd に登録されているらしいので、 systemctl status で状態が確認できます。また、ログの吐き先なども確認できます。

[opc@testins-01 ~]$ systemctl status unified-monitoring-agent
● unified-monitoring-agent.service - unified-monitoring-agent: Fluentd based data collector for Oracle Cloud Infrastructure SIEM
   Loaded: loaded (/usr/lib/systemd/system/unified-monitoring-agent.service; enabled; vendor preset: disabled)
   Active: active (running) since Tue 2020-11-24 13:54:55 GMT; 1 day 10h ago
     Docs: https://docs.cloud.oracle.com/
  Process: 1794 ExecStart=/opt/unified-monitoring-agent/embedded/bin/fluentd --log /var/log/unified-monitoring-agent/unified-monitoring-agent.log --daemon /var/run/unified-monitoring-agent/unified-monitoring-agent.pid --log-rotate-size 1048576 --log-rotate-age 10 $EXTRA_OPTIONS (code=exited, status=0/SUCCESS)
 Main PID: 2498 (fluentd)
   Memory: 393.7M (limit: 5.0G)
   CGroup: /system.slice/unified-monitoring-agent.service
           ├─2498 /opt/unified-monitoring-agent/embedded/bin/ruby /opt/unified-monitoring-agent/embedded/bin/fluentd --log /var/log/unified-mo...
           └─2501 /opt/unified-monitoring-agent/embedded/bin/ruby -Eascii-8bit:ascii-8bit /opt/unified-monitoring-agent/embedded/bin/fluentd -...

Nov 24 13:54:16 testins-01 systemd[1]: Starting unified-monitoring-agent: Fluentd based data collector for Oracle Cloud Infrastructure SIEM...
Nov 24 13:54:29 testins-01 fluentd[1794]: /opt/unified-monitoring-agent/embedded/lib/ruby/gems/2.6.0/gems/oci-2.9.0.1125/lib/oci/ident...YPE_ENUM
Nov 24 13:54:29 testins-01 fluentd[1794]: /opt/unified-monitoring-agent/embedded/lib/ruby/gems/2.6.0/gems/oci-2.9.0.1125/lib/oci/ident...was here
Nov 24 13:54:55 testins-01 fluentd[1794]: I, [2020-11-24T13:54:54.646767 #1794]  INFO -- : Successfully fetch instance metadata for ho... overlay
Nov 24 13:54:55 testins-01 fluentd[1794]: I, [2020-11-24T13:54:54.646979 #1794]  INFO -- : using metadata {"availabilityDomain"=>"FBNg...e"=>"tes
Nov 24 13:54:55 testins-01 fluentd[1794]: I, [2020-11-24T13:54:54.647027 #1794]  INFO -- : using cert_bundle_path /etc/pki/ca-trust/ex...ndle.pem
Nov 24 13:54:55 testins-01 fluentd[1794]: I, [2020-11-24T13:54:54.647075 #1794]  INFO -- : federation_endpoint = https://auth.ap-tokyo.../v1/x509
Nov 24 13:54:55 testins-01 fluentd[1794]: I, [2020-11-24T13:54:54.647094 #1794]  INFO -- : ca-bundle /etc/pki/ca-trust/extracted/pem/t...rincipal
Nov 24 13:54:55 testins-01 systemd[1]: Started unified-monitoring-agent: Fluentd based data collector for Oracle Cloud Infrastructure SIEM.
Hint: Some lines were ellipsized, use -l to show in full.

上記の結果によると、Unified Monitoring Agent のログは以下のパスにはかれるようです。

[opc@testins-01 bin]$ more /var/log/unified-monitoring-agent/unified-monitoring-agent.log 
# Logfile created on 2020-11-20 13:56:36 +0000 by logger.rb/v1.3.0
2020-11-20 13:56:36 +0000 [info]: parsing config file is succeeded path="/etc/unified-monitoring-agent/unified-mon
itoring-agent.conf"

で、上記のログを確認すると、Unified Monitoring Agent の Conf ファイル(≒ Fluentd の Conf ファイル)は以下にあるようで、中身を見てみると、 OCI の Console で作成した Agent Config(エージェント構成) が fluentd.conf ファイルとして展開されています。また、 /etc/unified-monitoring-agent/conf.d 以下に過去のバージョンの設定は zip されてアーカイブされるようです。 Fluentd を使ったことがある方であればこの辺わかりやすいと感じました。

[opc@testins-01 bin]$ more /etc/unified-monitoring-agent/unified-monitoring-agent.conf 
@include conf.d/*/*.conf
[opc@testins-01 bin]$ cd /etc/unified-monitoring-agent/conf.d
[opc@testins-01 conf.d]$ ll
total 12
drwxr-xr-x. 2 root root  26 Nov 20 13:56 fluentd_config
-rw-r--r--. 1 root root  22 Nov 20 13:56 fluentd_config.2020-11-20 135651 UTC.zip
-rw-r--r--. 1 root root 124 Nov 20 14:32 fluentd_config.2020-11-20 143242 UTC.zip
-rw-r--r--. 1 root root 507 Nov 20 15:03 fluentd_config.2020-11-20 150341 UTC.zip
[opc@testins-01 conf.d]$ cd fluentd_config/
[opc@testins-01 fluentd_config]$ ll
total 4
-rw-r--r--. 1 root root 826 Nov 20 15:03 fluentd.conf
[opc@testins-01 fluentd_config]$ more fluentd.conf 
    <source>
        @type tail
        tag 686c61.customlog
        path  /tmp/log/*,/var/log/*
        pos_file  /etc/unifiedmonitoringagent/pos/686c61-customlog.pos
        path_key  tailed_path
        <parse>
            @type none
        </parse>
    </source>

    <match 686c61.**>
        @type oci_logging
        log_object_id  ocid1.log.oc1.ap-tokyo-1.amaa
        <buffer tag>
            @type file
            retry_timeout  3h
            path  /opt/unifiedmonitoringagent/run/buffer/686c61
            disable_chunk_backup  true
            chunk_limit_size  5MB
            flush_interval  180s
            total_limit_size  1GB
            overflow_action  throw_exception
            retry_type  exponential_backoff
        </buffer>
    </match>

以下ドキュメントによると上記の config は agent 側でダウンロードしてくるようになっているようで、以下 Agent のトラブルシューティングガイドにあるように journalctl で downloader.service のログを確認することで、状況を確認できます。

[opc@testins-01 ~]$ sudo journalctl -u unified-monitoring-agent_config_downloader.service
-- Logs begin at Tue 2020-11-24 13:54:05 GMT, end at Mon 2020-11-30 13:33:05 GMT. --
Nov 24 13:54:16 testins-01 systemd[1]: Starting unified-monitoring-agent Fluentd configuration downloader....
Nov 24 13:54:22 testins-01 ruby[1804]: /opt/unified-monitoring-agent/embedded/lib/ruby/gems/2.6.0/gems/oci-2.9.0.1125/lib/oci/identity/models/base_tag_definition_validator.rb:23: w
Nov 24 13:54:22 testins-01 ruby[1804]: /opt/unified-monitoring-agent/embedded/lib/ruby/gems/2.6.0/gems/oci-2.9.0.1125/lib/oci/identity/models/base_tag_definition_validator.rb:24: w
Nov 24 13:54:31 testins-01 ruby[1804]: I, [2020-11-24T13:54:30.662809 #1804]  INFO -- : Start parsing input argument
Nov 24 13:54:31 testins-01 ruby[1804]: I, [2020-11-24T13:54:30.663089 #1804]  INFO -- : Finish parsing argument with origin config dir /etc/unified-monitoring-agent/conf.d/fluentd_
Nov 24 13:54:31 testins-01 ruby[1804]: I, [2020-11-24T13:54:30.663109 #1804]  INFO -- : Host is in , start running updater
Nov 24 13:54:31 testins-01 ruby[1804]: I, [2020-11-24T13:54:30.663119 #1804]  INFO -- : creating signer
Nov 24 13:54:31 testins-01 ruby[1804]: I, [2020-11-24T13:54:30.663137 #1804]  INFO -- : signer type is instance
Nov 24 13:54:31 testins-01 ruby[1804]: I, [2020-11-24T13:54:30.664381 #1804]  INFO -- : Successfully fetch instance metadata for hosts in overlay
Nov 24 13:54:31 testins-01 ruby[1804]: I, [2020-11-24T13:54:30.664493 #1804]  INFO -- : using metadata {"availabilityDomain"=>"FBNg:AP-TOKYO-1-AD-1", "faultDomain"=>"FAULT-DOMAIN-2
Nov 24 13:54:31 testins-01 ruby[1804]: I, [2020-11-24T13:54:30.664537 #1804]  INFO -- : send request to federation endpoint https://auth.ap-tokyo-1.oraclecloud.com/v1/x509 to get i
Nov 24 13:54:31 testins-01 ruby[1804]: I, [2020-11-24T13:54:31.078853 #1804]  INFO -- : get cp endpoint https://logging-cp.ap-tokyo-1.oci.oraclecloud.com
Nov 24 13:54:31 testins-01 ruby[1804]: D, [2020-11-24T13:54:31.078950 #1804] DEBUG -- : Using controlplane endpoint https://logging-cp.ap-tokyo-1.oci.oraclecloud.com
Nov 24 13:54:31 testins-01 ruby[1804]: I, [2020-11-24T13:54:31.079012 #1804]  INFO -- : Load agent config from config service with fluent type FLUENTD, fluent version 1.9.3
Nov 24 13:54:31 testins-01 ruby[1804]: I, [2020-11-24T13:54:31.251615 #1804]  INFO -- : Successfully get response from config service, status code 200, request id A744064DE02E48849
Nov 24 13:54:31 testins-01 ruby[1804]: ====================
Nov 24 13:54:31 testins-01 ruby[1804]: <source>
Nov 24 13:54:31 testins-01 ruby[1804]: @type tailNov 24 13:54:31 testins-01 ruby[1804]: tag 686c61.customlog
Nov 24 13:54:31 testins-01 ruby[1804]: path  /tmp/log/*,/var/log/*
Nov 24 13:54:31 testins-01 ruby[1804]: pos_file  /etc/unifiedmonitoringagent/pos/686c61-customlog.pos
Nov 24 13:54:31 testins-01 ruby[1804]: path_key  tailed_path
Nov 24 13:54:31 testins-01 ruby[1804]: <parse>
Nov 24 13:54:31 testins-01 ruby[1804]: @type noneNov 24 13:54:31 testins-01 ruby[1804]: </parse>
Nov 24 13:54:31 testins-01 ruby[1804]: </source>
Nov 24 13:54:31 testins-01 ruby[1804]: <match 686c61.**>
Nov 24 13:54:31 testins-01 ruby[1804]: @type oci_loggingNov 24 13:54:31 testins-01 ruby[1804]: log_object_id  ocid1.log.oc1.ap-tokyo-1.amaa
Nov 24 13:54:31 testins-01 ruby[1804]: <buffer tag>
Nov 24 13:54:31 testins-01 ruby[1804]: @type file
Nov 24 13:54:31 testins-01 ruby[1804]: retry_timeout  3h
Nov 24 13:54:31 testins-01 ruby[1804]: path  /opt/unifiedmonitoringagent/run/buffer/686c61
Nov 24 13:54:31 testins-01 ruby[1804]: disable_chunk_backup  true
Nov 24 13:54:31 testins-01 ruby[1804]: chunk_limit_size  5MB
Nov 24 13:54:31 testins-01 ruby[1804]: flush_interval  180s
Nov 24 13:54:31 testins-01 ruby[1804]: total_limit_size  1GB
Nov 24 13:54:31 testins-01 ruby[1804]: overflow_action  throw_exception
Nov 24 13:54:31 testins-01 ruby[1804]: retry_type  exponential_backoff
Nov 24 13:54:31 testins-01 ruby[1804]: </buffer>
Nov 24 13:54:31 testins-01 ruby[1804]: </match>
Nov 24 13:54:31 testins-01 ruby[1804]: =====================

適当なファイルでも集めてくれるのか

OCI Logging ですが、 Service Connector Hub を通して Streaming や Functions、 Object Storage 等のサービスにログを連携してくれるので、例えばアプリから適当なファイルはいてそれを Streaming とか Functions とかに渡す、みたいなユースケースにも使えちゃうのでは?という気がしてきます。念のため試してみます。

/tmp/log/* のログを集める設定を Agent Config(エージェント構成)で作り、以下のようなファイルを書き込みます。

[opc@testins-01 pos]$ more /tmp/log/test0.log 
20201124 log log log

あたりまえっちゃああたりまえなのですが、収集されていました。

image-20201126101224856.png

DBCS とかでも集めてくれるのか

DBCS とか PaaS のログもこれでかっちょよく集められちゃうのでは?と思うのが人情というものです。が、2020/11 時点ではそもそも unified-monitoring-agent が DBCS には入ってません。


[opc@testdb ~]$ systemctl status unified-monitoring-agent
Unit unified-monitoring-agent.service could not be found.

以下のドキュメントに従ってエージェント入れることはできるので、いろいろ試してみました先述の Agent Config が落ちてこず、 fluentd.conf が更新されませんでした。 Dynamic Group を指定して、対象インスタンスを指定しているので、そのあたりの指定を工夫すればうまくいくのかもしれませんが、今後に期待というところかもしれません。一応中の人なので、問い合わせしてみているので、何かわかったら追記するかもです。
[追記] 社内でアドバイス頂いて、できました。追記します。ポイントは Dynamic Group で指定しないことでした。

まずは以下ドキュメントに則ってエージェントをインストールします。
エージェントのインストール | カスタム・ログ Oracle Cloud Infrastructure Documentation

[opc@testdg01 ~]$ wget https://objectstorage.us-phoenix-1.oraclecloud.com/n/axmjwnk4dzjv/b/unified-monitoring-agent-centos7-repo/o/unified-monitoring-agent-0.0.5.rpm
(略)
[opc@testdg01 ~]$ sudo yum install -y unified-monitoring-agent-0.0.5.rpm 
Loaded plugins: versionlock
Examining unified-monitoring-agent-0.0.5.rpm: unified-monitoring-agent-0.0.1+20200916210710-0.el7.x86_64
(略)
Agent Install Complete.

  Verifying  : unified-monitoring-agent-0.0.1+20200916210710-0.el7.x86_64                                                                                                       1/1 

Installed:
  unified-monitoring-agent.x86_64 0:0.0.1+20200916210710-0.el7                                                                                                                      

Complete!

その後、以下の手順に則って API キーを作ります。(本番で使うときはパスフレーズ付きで作成してください。)
API署名キーの生成方法

[opc@testdg01 ~]$ mkdir ~/.oci
[opc@testdg01 ~]$ openssl genrsa -out ~/.oci/oci_api_key.pem 2048                        
Generating RSA private key, 2048 bit long modulus
.................................+++
......................................................................+++
e is 65537 (0x10001)
[opc@testdg01 ~]$ chmod go-rwx ~/.oci/oci_api_key.pem               
[opc@testdg01 ~]$ openssl rsa -pubout -in ~/.oci/oci_api_key.pem -out ~/.oci/oci_api_key_public.pem             
writing RSA key

この API キーをユーザの管理画面から API キーに登録してください。
2020-12-02 224247.png
この追加後に表示されるフィンガープリントをメモして頂ければと思います。

その後以下ように、環境に合わせて OCI CLI の config ファイルを作成し、 /etc/unified-monitoring-agent/.oci ディレクトリに配置します。(ドキュメントの記載では、 .oci ディレクトリを全部コピーするイメージになっていますが、API キーの場所は config ファイルから引くと思われるので、 config ファイルのみあればいいものと理解しています。 )
また、プロファイル名([UNIFIED_MONITORING_AGENT])は決め打ちのようなので、この通り作成する必要があるのと、 [DEFAULT] のプロファイルは内容空でいいので作っておく必要があるようです。

[opc@testdg01 ~]$ vim .oci/config
[DEFAULT]

[UNIFIED_MONITORING_AGENT]
user=ocid1.user.oc1..aaaaaaaar5
fingerprint=db:58:e0:c0:9b:9d:a
key_file=/home/opc/.oci/oci_api_key.pem
tenancy=ocid1.tenancy.oc1..aaaaaaaad4sjxw322ig5o
region=ap-tokyo-1
[opc@testdg01 ~]$ sudo mkdir /etc/unified-monitoring-agent/.oci
[opc@testdg01 ~]$ sudo cp ./.oci/* /etc/unified-monitoring-agent/.oci

最後に、ここがポイントとなりますが、エージェント構成を作る際に、 Dynamic Group での指定ではなく、上記ユーザが含まれる User Group での指定とします。先ほどの config に書いたユーザが取りに来たら fluentd の config ファイルを返してくれる、という挙動になるようです。
2020-12-02 230018.png

無事、アラートログが収集できました。
2020-12-03 001035.png

(この確認の間も journalctl で agent_config_downloader.service のログとにらめっこしながらやりました。)

このやり方でやれば、特に OCI のインスタンスでなくとも、エージェントがインストールできインターネットに抜けられる環境であれば、どんな環境からでもログ収集できそうです。
以下のオフィシャルの FAQ ではわりとさらっと書いてあるのですが、確かにいわれてみればどこからでもログ集められるよ、という感じにも読めますね。
Log Management FAQs | Oracle Logging

What kind of environments does Logging support for custom logs?
You can ingest logs from Cloud Infrastructure instances, Oracle Container Engine for Kubernetes, on-premises environment, or from other cloud providers.

なお、 以下の API で投げて Custom Log として収集することもできるようです。
PutLogs | Oracle Cloud Infrastructure Documentation / API Reference and Endpoints

まとめ

OCI Logging の Custom Log について、個人的に気になったことを確認してみました。次回は Service Connector Hub か PutLogs を試してみようかなという気持ちです。

13
3
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
13
3