やさぐれSEの技術メモ

皆が辛い思いをしないようにハマったポイントなどを書きます

ffmpegによる動画ファイルからの音声抽出

ffmpegとは

ffmpegは動画及び音声を記録・変換・再生するためのOSSです。
https://ja.wikipedia.org/wiki/FFmpeg

LinuxCLI上でも実行でき、非常に便利です。

今回の環境

Google Compute Engine上で検証を実施しました。

Debian Linuxでの手順となります。
Linux instance-1 4.9.0-8-amd64 #1 SMP Debian 4.9.110-3+deb9u6 (2018-10-08) x86_64 GNU/Linux

インストール

パッケージのインストールのみとなります。
非常に簡単です。

$ sudo apt-get install ffmpeg
~省略~
Do you want to continue? [Y/n] Y ←Yを入力します。

操作方法

詳細については公式HPをご確認頂ければと思います。
https://www.ffmpeg.org/

動画情報の確認
下記コマンドで動画ファイルの詳細を確認できます。
$ ffmpeg -i 動画ファイルのパス

$ sudo ffmpeg -i test.mov
ffmpeg version 3.2.12-1~deb9u1 Copyright (c) 2000-2018 the FFmpeg developers
built with gcc 6.3.0 (Debian 6.3.0-18+deb9u1) 20170516
configuration: --prefix=/usr --extra-version='1~deb9u1' --toolchain=hardened --libdir=/usr/lib/x86_64-linux-gnu --incdir=/usr/include/x86_64-linux-gnu --enable-gpl --disable-stripping --enable-avresample --enable-avisynth --enable-gnutls --enable-ladspa --enable-libass --enable-libbluray --enable-libbs2b --enable-libcaca --enable-libcdio --enable-libebur128 --enable-libflite --enable-libfontconfig --enable-libfreetype --enable-libfribidi --enable-libgme --enable-libgsm --enable-libmp3lame --enable-libopenjpeg --enable-libopenmpt --enable-libopus --enable-libpulse --enable-librubberband --enable-libshine --enable-libsnappy --enable-libsoxr --enable-libspeex --enable-libssh --enable-libtheora --enable-libtwolame --enable-libvorbis --enable-libvpx --enable-libwavpack --enable-libwebp --enable-libx265 --enable-libxvid --enable-libzmq --enable-libzvbi --enable-omx --enable-openal --enable-opengl --enable-sdl2 --enable-libdc1394 --enable-libiec61883 --enable-chromaprint --enable-frei0r --enable-libopencv --enable-libx264 --enable-shared
libavutil 55. 34.101 / 55. 34.101
libavcodec 57. 64.101 / 57. 64.101
libavformat 57. 56.101 / 57. 56.101
libavdevice 57. 1.100 / 57. 1.100
libavfilter 6. 65.100 / 6. 65.100
libavresample 3. 1. 0 / 3. 1. 0
libswscale 4. 2.100 / 4. 2.100
libswresample 2. 3.100 / 2. 3.100
libpostproc 54. 1.100 / 54. 1.100
Input #0, mov,mp4,m4a,3gp,3g2,mj2, from 'test.mov':
Metadata:
major_brand : qt 
minor_version : 512
compatible_brands: qt 
encoder : Lavf58.12.100
Duration: 00:02:57.59, start: 0.000000, bitrate: 3666 kb/s
Stream #0:0(eng): Video: h264 (Constrained Baseline) (avc1 / 0x31637661), yuv420p, 1920x1080 [SAR 1:1 DAR 16:9], 3532 kb/s, 29.97 fps, 29.97 tbr, 30k tbn, 59.94 tbc (default)
Metadata:
handler_name : DataHandler
encoder : Lavc58.18.100 libx264
Stream #0:1(eng): Audio: aac (LC) (mp4a / 0x6134706D), 44100 Hz, stereo, fltp, 128 kb/s (default)
Metadata:
handler_name : DataHandler
At least one output file must be specified

動画から音声ファイルを抽出する

下記のコマンドにて動画から音声ファイルを抽出します。
細かなオプションは色々あるのですが、変換先の拡張子を指定すると勝手に変換をしてくれるようです。
かなり便利です。

$ ffmpeg -i 動画ファイル 変換後の音声ファイル

例 movファイルからflacファイルを抽出

# 動画から音声を抽出
$ ffmpeg -i test.mov test_stereo.flac

# 音声ファイルの確認
$ sudo ffmpeg -i test_stereo.flac
ffmpeg version 3.2.12-1~deb9u1 Copyright (c) 2000-2018 the FFmpeg developers
built with gcc 6.3.0 (Debian 6.3.0-18+deb9u1) 20170516
configuration: --prefix=/usr --extra-version='1~deb9u1' --toolchain=hardened --libdir=/usr/lib/x86_64-linux-gnu --
incdir=/usr/include/x86_64-linux-gnu --enable-gpl --disable-stripping --enable-avresample --enable-avisynth --enable-gnutls --
enable-ladspa --enable-libass --enable-libbluray --enable-libbs2b --enable-libcaca --enable-libcdio --enable-libebur128 --enable-
libflite --enable-libfontconfig --enable-libfreetype --enable-libfribidi --enable-libgme --enable-libgsm --enable-libmp3lame --
enable-libopenjpeg --enable-libopenmpt --enable-libopus --enable-libpulse --enable-librubberband --enable-libshine --enable-
libsnappy --enable-libsoxr --enable-libspeex --enable-libssh --enable-libtheora --enable-libtwolame --enable-libvorbis --enable-
libvpx --enable-libwavpack --enable-libwebp --enable-libx265 --enable-libxvid --enable-libzmq --enable-libzvbi --enable-omx --
enable-openal --enable-opengl --enable-sdl2 --enable-libdc1394 --enable-libiec61883 --enable-chromaprint --enable-frei0r --
enable-libopencv --enable-libx264 --enable-shared
libavutil 55. 34.101 / 55. 34.101
libavcodec 57. 64.101 / 57. 64.101
libavformat 57. 56.101 / 57. 56.101
libavdevice 57. 1.100 / 57. 1.100
libavfilter 6. 65.100 / 6. 65.100
libavresample 3. 1. 0 / 3. 1. 0
libswscale 4. 2.100 / 4. 2.100
libswresample 2. 3.100 / 2. 3.100
libpostproc 54. 1.100 / 54. 1.100
Input #0, flac, from 'test_stereo.flac':
Metadata:
MAJOR_BRAND : qt 
MINOR_VERSION : 512
COMPATIBLE_BRANDS: qt 
ENCODER : Lavf57.56.101
Duration: 00:02:57.56, start: 0.000000, bitrate: 1291 kb/s
Stream #0:0: Audio: flac, 44100 Hz, stereo, s32 (24 bit)
At least one output file must be specified

動画ファイルから音声を抽出とステレオ→モノラルに変更する

音声抽出に加えてモノラル音声に変更する例となります。
他にもビットレートやサンプリング周波数も変えることが出来ます。
このあたりは公式ドキュメント等々をご確認ください。

$ ffmpeg -i 動画ファイル -ac 1 変換後の音声ファイル

# 変換コマンド
$ ffmpeg -i test.mov -ac 1 test_mono.flac

# 音声の確認
$ sudo ffmpeg -i test_mono.flac
ffmpeg version 3.2.12-1~deb9u1 Copyright (c) 2000-2018 the FFmpeg developers
built with gcc 6.3.0 (Debian 6.3.0-18+deb9u1) 20170516
configuration: --prefix=/usr --extra-version='1~deb9u1' --toolchain=hardened --libdir=/usr/lib/x86_64-linux-gnu --incdir=/usr/include/x86_64-linux-gnu --enable-gpl --disable-stripping --enable-avresample --enable-avisynth --enable-gnutls --enable-ladspa --enable-libass --enable-libbluray --enable-libbs2b --enable-libcaca --enable-libcdio --enable-libebur128 --enable-libflite --enable-libfontconfig --enable-libfreetype --enable-libfribidi --enable-libgme --enable-libgsm --enable-libmp3lame --enable-libopenjpeg --enable-libopenmpt --enable-libopus --enable-libpulse --enable-librubberband --enable-libshine --enable-libsnappy --enable-libsoxr --enable-libspeex --enable-libssh --enable-libtheora --enable-libtwolame --enable-libvorbis --enable-libvpx --enable-libwavpack --enable-libwebp --enable-libx265 --enable-libxvid --enable-libzmq --enable-libzvbi --enable-omx --enable-openal --enable-opengl --enable-sdl2 --enable-libdc1394 --enable-libiec61883 --enable-chromaprint --enable-frei0r --enable-libopencv --enable-libx264 --enable-shared
libavutil 55. 34.101 / 55. 34.101
libavcodec 57. 64.101 / 57. 64.101
libavformat 57. 56.101 / 57. 56.101
libavdevice 57. 1.100 / 57. 1.100
libavfilter 6. 65.100 / 6. 65.100
libavresample 3. 1. 0 / 3. 1. 0
libswscale 4. 2.100 / 4. 2.100
libswresample 2. 3.100 / 2. 3.100
libpostproc 54. 1.100 / 54. 1.100
Input #0, flac, from 'test_mono.flac':
Metadata:
MAJOR_BRAND : qt 
MINOR_VERSION : 512
COMPATIBLE_BRANDS: qt 
ENCODER : Lavf57.56.101
Duration: 00:02:57.56, start: 0.000000, bitrate: 693 kb/s
Stream #0:0: Audio: flac, 44100 Hz, mono, s32 (24 bit) ←ここがモノラルになっています
At least one output file must be specified

GCEのロケールを日本語にする

Google Cloud Platformの仮想サーバであるCompute Engine(GCE)のロケールを日本語にする方法です。

条件

  • Google Cloud Platform のCompute Engine
  • Ubuntsの最新OS

記述したきっかけ

Teratailというサイトで質問を見かけて、検証して回答をしたのでブログにも載せようと思いました。

下記のエラーが出る場合の対処となります。

E: Unable to locate package language-pack-ja

海外の記事を読んでいたところ、Docker等々でも起こるようなのでご参考になればと思います。

手順

ロケールのインストールを行ないます。
# sudo apt-get install locales locales-all

ロケールを日本語に変更します。
# sudo update-locale LANG=ja_JP.UTF-8

ロケール環境変数が変更されたことを確認します。
# env | grep LANG

一度コンソールから抜け、再度ログインを実行します。
# exit

参考サイト

下記サイトに対応方法が記述されています。
https://stackoverflow.com/questions/43708896/unable-to-locate-package-language-pack-en

Embulkのインストール及びElasticsearchとの連携設定

Elasticsearchに対してデータを投入するEmbulkについて記述しようと思います。 (すっかり忘れていました)

前提条件

RHEL7.4
Embulk v0.9.4

インストール

Embulkの最新バージョンを取得します。全ユーザが利用可能なようにしています。
# curl --create-dirs -o /usr/local/bin/embulk -L "https://dl.embulk.org/embulk-latest.jar"

実行権限を付与します。
# chmod +x /usr/local/bin/embulk

実行できることの確認 兼 バージョン確認をします。
# embulk -versionembulk 0.9.4

プラグインのインストール

Embulkには各種プラグインがありますので、利用することをオススメします。

インストールされているプラグインの確認

# embulk gem list
2018-02-13 13:16:19.226 +0900: Embulk v0.9.4

********************************** INFORMATION **********************************
Join us! Embulk-announce mailing list is up for IMPORTANT announcement such as
compatibility-breaking changes and key feature updates.
https://groups.google.com/forum/#!forum/embulk-announce
*********************************************************************************

Gem plugin path is: /root/.embulk/lib/gems

*** LOCAL GEMS ***

bundler (1.16.0)
did_you_mean (default: 1.0.1)
embulk (0.9.2 java)
jar-dependencies (default: 0.3.10)
jruby-openssl (0.9.21 java)
jruby-readline (1.2.0 java)
json (1.8.3 java)
liquid (4.0.0)
minitest (default: 5.4.1)
msgpack (1.1.0 java)
net-telnet (default: 0.1.1)
power_assert (default: 0.2.3)
psych (2.2.4 java)
rake (default: 10.4.2)
rdoc (default: 4.2.0)
test-unit (default: 3.1.1)

メモ:プラグインのインストールは下記コマンドで実施できます。

# embulk gem install プラグイン名

Elasticsearchプラグインのインストールを実施します。

# embulk gem install embulk-output-elasticsearch
2018-02-13 13:17:47.128 +0900: Embulk v0.9.2
********************************** INFORMATION **********************************
Join us! Embulk-announce mailing list is up for IMPORTANT announcement such as
compatibility-breaking changes and key feature updates.
https://groups.google.com/forum/#!forum/embulk-announce
*********************************************************************************

Gem plugin path is: /root/.embulk/lib/gems

Fetching: embulk-output-elasticsearch-0.4.5.gem (100%)
Successfully installed embulk-output-elasticsearch-0.4.5
1 gem installed

コンフィグの作成

EmbulkのElasticsearch関連部分のみ記述します。
・接続先 接続先のIPアドレスはKibanaからの接続と同様にクライアントノードを指定します。
冗長化構成を取れないので、指定のElasticsearchがダウンしている場合は  手動でIPアドレスを書き換える必要があります。

・user及びpassword x-packのセキュリティ設定を実施している場合に必要です。
※embulk用にユーザを追加した方が良いと思います。

in: 
~省略~
  type: elasticsearch
  index: embulk
  index_type: embulk 
  nodes:  
    - {host: 192.168.0.3, port: 9200}  #接続先は1行しか記述出来ない。冗長設定なし 
  cluster_name: クラスタ名を記述  
  auth_method: basic #x-packのセキュリティ有効時は認証が必要   
  user: "elasticsearchに登録されているユーザ名" #x-packのセキュリティ有効時は認証が必要   
  password: "パスワード" #x-packのセキュリティ有効時は認証が必要

あとはEmbulkを実行すればデータがElasticsearchに投入できます!

Elasticsearch6.3のインストールと考察

Elasticsearch6.3がリリースされましたので、インストールと検証を行いました。

インストールの流れ

検証した結果、下記の流れが良いと思います。
6.3からX-Packも含まれていますので、そのインストールが減ります。
基本的には6.2と変わらないです。

今回は(1)~(8)について書きます

==== ここから ====
(1)Elasticsearch:インストール及び最低限の設定
(2)Elasticsearch:日本語パッケージのインストール
(3)Elasticsearch:運用支援ツール(Curator)のインストール
(4)Kibana:インストール及び最低限の設定
(5)Kibana:ライセンスの変更(BASIC→Trial or GOLD等)
(6)Elasticsearch:TLS設定
(7)Elasticsearch:Config(yml)の修正及びElasticsearchの起動
(8)Elasticsearch:ビルトインアカウントのパスワード変更
==== ここまで====

(9)Elasticsearch:起動コマンドの整備
(10)Elasticsearch:スナップショットの取得
(11)Elasticsearch:SSL設定(任意)

インストール

(1)Elasticsearch:インストール及び最低限の設定

公式HPに従って簡単にインストールができます。
6.2とほとんど変わりませんので、手順は割愛させていただきます。

www.elastic.co

Configについては最低限の定義で良いです。
下記のようにバインドするIPアドレスの定義のみでOKです。

network.host: 各インスタンスのIPアドレス(19.168.0.1~5)

(2)Elasticsearch:日本語パッケージのインストール

大きく変わらないので割愛します。

(3)Elasticsearch:運用支援ツール(Curator)のインストール

大きく変わらないので割愛します。

(4)Kibana:インストール及び最低限の設定

Elasticsearchをインストールすると同時にX-Packもインストールされるわけですが、その際に「BASIC」ライセンスも適用されます。(6.2では「Trial」)

この状態ですと、ビルトインアカウントのパスワード変更や、セキュリティ設定の有効化が出来ません(厳密には有効になるがパスワード有りでも無しでも動作する)。

ライセンスの変更は以前と同様にCurlで出来るのですが、「Trial」ライセンスへの変更はKibanaから実施しますので、ここでKibanaを設定します。

インストール方法は大きく変わらないので割愛します。

www.elastic.co

Configは管理コンソールとElasticsearchに接続可能な最低限の定義で良いです。

server.host: "192.168.0.0"
elasticsearch.url: "http://192.168.0.3:9200"

(5)Kibana:ライセンスの変更(BASIC→Trial or GOLD等)

Kibanaにアクセスします f:id:gitya107:20180623164946p:plain

[Management]→[License Management]と選択します。 f:id:gitya107:20180623165050p:plain

今回は評価版で実施するので「Start Trial」を選択します。 f:id:gitya107:20180623165142p:plain

「Start my trial」を選択します。 f:id:gitya107:20180623165239p:plain

これで評価版ライセンスになります。評価版は全ての機能が利用できます。 f:id:gitya107:20180623165527p:plain

一応Elasticsearch側からも確認します。

# curl -XGET -u 'http://192.168.0.3:9200/_xpack/license'
{
  "license" : {
    "status" : "active",
    "uid" : "cb332d0c-7475-48a4-89fa-f129ed3a5ac5",
    "type" : "trial",
    "issue_date" : "2018-06-18T05:14:26.334Z",
    "issue_date_in_millis" : 1529298866334,
    "expiry_date" : "2018-07-18T05:14:26.334Z",
    "expiry_date_in_millis" : 1531890866334,
    "max_nodes" : 1000,
    "issued_to" : "elasticsearch",
    "issuer" : "elasticsearch",
    "start_date_in_millis" : -1
  }
}

(6)Elasticsearch:TLS設定

一応公式のセキュリティ設定部分を頑張って読みました。
結果として6.2と変わらないのかなと思います。
公式サイトのドキュメントがより一層解りにくくなりました。

www.elastic.co

使っていたコマンドが地味に変更となります。
6.2(certutil)→6.3(elasticsearch-certutil)

認証局の構築及びCA証明書・プライベートキーの作成

# /usr/share/elasticsearch/bin/elasticsearch-certutil ca
This tool assists you in the generation of X.509 certificates and certificate
signing requests for use with SSL/TLS in the Elastic stack.

The 'ca' mode generates a new 'certificate authority'
This will create a new X.509 certificate and private key that can be used
to sign certificate when running in 'cert' mode.

Use the 'ca-dn' option if you wish to configure the 'distinguished name'
of the certificate authority

By default the 'ca' mode produces a single PKCS#12 output file which holds:
    * The CA certificate
    * The CA's private key

If you elect to generate PEM format certificates (the -pem option), then the output will
be a zip file containing individual files for the CA certificate and private key

Please enter the desired output file [elastic-stack-ca.p12]:Enter
Enter password for elastic-stack-ca.p12 :パスワードを入力

インスタンス向け証明書及びプライベートキーの作成

# /usr/share/elasticsearch/bin/elasticsearch-certutil cert elastic-stack-ca.p12
This tool assists you in the generation of X.509 certificates and certificate
signing requests for use with SSL/TLS in the Elastic stack.

The 'cert' mode generates X.509 certificate and private keys.
    * By default, this generates a single certificate and key for use
       on a single instance.
    * The '-multiple' option will prompt you to enter details for multiple
       instances and will generate a certificate and key for each one
    * The '-in' option allows for the certificate generation to be automated by describing
       the details of each instance in a YAML file

    * An instance is any piece of the Elastic Stack that requires a SSL certificate.
      Depending on your configuration, Elasticsearch, Logstash, Kibana, and Beats
      may all require a certificate and private key.
    * The minimum required value for each instance is a name. This can simply be the
      hostname, which will be used as the Common Name of the certificate. A full
      distinguished name may also be used.
    * A filename value may be required for each instance. This is necessary when the
      name would result in an invalid file or directory name. The name provided here
      is used as the directory name (within the zip) and the prefix for the key and
      certificate files. The filename is required if you are prompted and the name
      is not displayed in the prompt.
    * IP addresses and DNS names are optional. Multiple values can be specified as a
      comma separated string. If no IP addresses or DNS names are provided, you may
      disable hostname verification in your SSL configuration.

    * All certificates generated by this tool will be signed by a certificate authority (CA).
    * The tool can automatically generate a new CA for you, or you can provide your own with the
         -ca or -ca-cert command line options.

By default the 'cert' mode produces a single PKCS#12 output file which holds:
    * The instance certificate
    * The private key for the instance certificate
    * The CA certificate

If you specify any of the following options:
    * -pem (PEM formatted output)
    * -keep-ca-key (retain generated CA key)
    * -multiple (generate multiple certificates)
    * -in (generate certificates from an input file)
then the output will be be a zip file containing individual certificate/key files

Please enter the desired output file [elastic-certificates.p12]:Enter
Enter password for elastic-certificates.p12 :パスワードを入力

Certificates written to /root/elastic-certificates.p12

This file should be properly secured as it contains the private key for
your instance.

This file is a self contained file and can be copied and used 'as is'
For each Elastic product that you wish to configure, you should copy
this '.p12' file to the relevant configuration directory
and then follow the SSL configuration instructions in the product guide.

・証明書を配置するディレクトリの作成
# mkdir /etc/elasticsearch/certificate

ディレクトリの権限等を修正します。

# chgrp elasticsearch /etc/elasticsearch/certificate
# chmod o-rx /etc/elasticsearch/certificate

・SCP等で全インスタンスに証明書及びプライベートキー(elastic-stack-ca.p12 , elastic-certificates.p12 )を配布します。
 また、作成したディレクトリにファイルを移動します。

・ファイル権限等を修正します。

chgrp elasticsearch /etc/elasticsearch/elastic-stack-ca.p12 /etc/elasticsearch/elastic-certificates.p12
chmod 640 /etc/elasticsearch/elastic-stack-ca.p12 /etc/elasticsearch/elastic-certificates.p12

・キーストアの設定を実施します。
[CA]

# /usr/share/elasticsearch/bin/elasticsearch-keystore add xpack.security.transport.ssl.keystore.secure_password
Enter value for xpack.security.transport.ssl.keystore.secure_password:   elastic-stack-ca.p12作成時のパスワード

[インスタンス ]

# /usr/share/elasticsearch/bin/elasticsearch-keystore add xpack.security.transport.ssl.truststore.secure_password
Enter value for xpack.security.transport.ssl.truststore.secure_password:  elastic-certificates.p12 作成時のパスワード

(7)Elasticsearch:Config(yml)の修正及びElasticsearchの起動

6.2のコンフィグを参考に設定をしてください。
X-Packの部分だけ下記に記述します。

xpack.security.enabled: true

# 6.3 から入れておいたほうが良いなと思う定義
# トークンを使わないので要らないような・・・・
xpack.security.authc.token.enabled: false

xpack.security.transport.ssl.enabled: true
xpack.security.transport.ssl.verification_mode: certificate
xpack.security.transport.ssl.keystore.path: /etc/elasticsearch/certificates/elastic-certificates.p12
xpack.security.transport.ssl.truststore.path: /etc/elasticsearch/certificates/elastic-certificates.p12

6.2と同様に起動します。

(8)Elasticsearch:ビルトインアカウントのパスワード変更

6.2と手順は変わりません。
6.2ではデフォルトパスワードが設定されなかったのに、6.3では「changeme」が設定されているようです。(5.x系に回帰???)

# /usr/share/elasticsearch/bin/elasticsearch-setup-passwords interactive
Initiating the setup of passwords for reserved users elastic,kibana,logstash_system,beats_system.
You will be prompted to enter passwords as the process progresses.
Please confirm that you would like to continue [y/N]y


Enter password for [elastic]:パスワードを入力
Reenter password for [elastic]:パスワードを入力
Enter password for [kibana]:パスワードを入力
Reenter password for [kibana]:パスワードを入力
Enter password for [logstash_system]:パスワードを入力
Reenter password for [logstash_system]:パスワードを入力
Enter password for [beats_system]:パスワードを入力
Reenter password for [beats_system]:パスワードを入力
Changed password for user [kibana]
Changed password for user [logstash_system]
Changed password for user [beats_system]
Changed password for user [elastic]

さいごに

残りの手順は6.2と変わりません。 過去の記事を参考に実施していただければと思います。

Kibanaのインストール及び設定

前回まででElasticsearchの設定について一通り記述しましたので、今回はKibanaのインストールと設定方法を記述します。

Kibanaのインストール

(1)Kibanaをダウンロードします。
# wget https://artifacts.elastic.co/downloads/kibana/kibana-6.2.2-x86_64.rpm

(2)Kibanaのインストールを実施します。
# rpm --install kibana-6.2.2-x86_64.rpm

インストールが正常に完了したことを確認します。
# echo $?

(3)サービスの自動登録をします。(※まだ起動しません)

# systemctl daemon-reload
# systemctl enable kibana.service

X-Packのインストール

公式HP

www.elastic.co

(1)X-Packのダウンロードをします。
# wget https://artifacts.elastic.co/downloads/packs/x-pack/x-pack-6.2.2.zip

(2)X-Packのインストールを実施します。

最初普通にインストールして上手く行かず、海外サイトを調べていたところkibanaユーザで実施する必要があると発見しました。Elasticsearch側は普通にインストールできるのに、Kibanaは何故なんでしょう。

# sudo -u kibana /usr/share/kibana/bin/kibana-plugin install file:///root/x-pack-6.2.2.zip
Attempting to transfer from file:///root/x-pack-6.2.1.zip
Transferring 314111900 bytes....................
Transfer complete
Retrieving metadata from plugin archive
Extracting plugin archive
Extraction complete
Optimizing and caching browser bundles...
Plugin installation complete

Kibanaの設定

(1)コンフィグの修正を実施します。

# cp -p /etc/kibana/kibana.yml /etc/kibana/kibana.yml.org
# diff /etc/kibana/kibana.yml /etc/kibana/kibana.yml.org
# vi /etc/kibana/kibana.yml

設定箇所に ★ をつけます。

# Kibana is served by a back end server. This setting specifies the port to use.
#server.port: 5601

# Specifies the address to which the Kibana server will bind. IP addresses and host names are both valid values.
# The default is 'localhost', which usually means remote machines will not be able to connect.
# To allow connections from remote users, set this parameter to a non-loopback address.
#server.host: "localhost"

# ★アクセス許可IPアドレスの設定(0.0.0.0でどのIPでも許可します)
server.host: "192.168.0.0"

# Enables you to specify a path to mount Kibana at if you are running behind a proxy. This only affects
# the URLs generated by Kibana, your proxy is expected to remove the basePath value before forwarding requests
# to Kibana. This setting cannot end in a slash.
#server.basePath: ""

# The maximum payload size in bytes for incoming server requests.
#server.maxPayloadBytes: 1048576

# The Kibana server's name.  This is used for display purposes.
#server.name: "your-hostname"

# ★kibanaのサーバ名設定。elasticsearchのように${HOSTNAME}は使えないようです。
server.name: kibana

# The URL of the Elasticsearch instance to use for all your queries.
#elasticsearch.url: "http://localhost:9200"

# ★kibanaが接続するelasticsearchのノード設定。clientノードを指定します。
elasticsearch.url: "http://192.168.0.3:9200"

# When this setting's value is true Kibana uses the hostname specified in the server.host
# setting. When the value of this setting is false, Kibana uses the hostname of the host
# that connects to this Kibana instance.
#elasticsearch.preserveHost: true

# Kibana uses an index in Elasticsearch to store saved searches, visualizations and
# dashboards. Kibana creates a new index if the index doesn't already exist.
#kibana.index: ".kibana"

# The default application to load.
#kibana.defaultAppId: "home"

# If your Elasticsearch is protected with basic authentication, these settings provide
# the username and password that the Kibana server uses to perform maintenance on the Kibana
# index at startup. Your Kibana users still need to authenticate with Elasticsearch, which
# is proxied through the Kibana server.
#elasticsearch.username: "user"
#elasticsearch.password: "pass"

# ★x-packを使用してセキュリティ有効化している場合は認証設定が必要です。
# 接続ユーザは「kibana」を使用します。
elasticsearch.username: "kibana"
elasticsearch.password: "ビルトインアカウントのパスワード変更で指定したパスワード"

# Enables SSL and paths to the PEM-format SSL certificate and SSL key files, respectively.
# These settings enable SSL for outgoing requests from the Kibana server to the browser.
#server.ssl.enabled: false
#server.ssl.certificate: /path/to/your/server.crt
#server.ssl.key: /path/to/your/server.key

# Optional settings that provide the paths to the PEM-format SSL certificate and key files.
# These files validate that your Elasticsearch backend uses the same key files.
#elasticsearch.ssl.certificate: /path/to/your/client.crt
#elasticsearch.ssl.key: /path/to/your/client.key

# Optional setting that enables you to specify a path to the PEM file for the certificate
# authority for your Elasticsearch instance.
#elasticsearch.ssl.certificateAuthorities: [ "/path/to/your/CA.pem" ]

# To disregard the validity of SSL certificates, change this setting's value to 'none'.
#elasticsearch.ssl.verificationMode: full

# Time in milliseconds to wait for Elasticsearch to respond to pings. Defaults to the value of
# the elasticsearch.requestTimeout setting.
#elasticsearch.pingTimeout: 1500

# Time in milliseconds to wait for responses from the back end or Elasticsearch. This value
# must be a positive integer.
#elasticsearch.requestTimeout: 30000

# List of Kibana client-side headers to send to Elasticsearch. To send *no* client-side
# headers, set this value to [] (an empty list).
#elasticsearch.requestHeadersWhitelist: [ authorization ]

# Header names and values that are sent to Elasticsearch. Any custom headers cannot be overwritten
# by client-side headers, regardless of the elasticsearch.requestHeadersWhitelist configuration.
#elasticsearch.customHeaders: {}

# Time in milliseconds for Elasticsearch to wait for responses from shards. Set to 0 to disable.
#elasticsearch.shardTimeout: 0

# Time in milliseconds to wait for Elasticsearch at Kibana startup before retrying.
#elasticsearch.startupTimeout: 5000

# Specifies the path where Kibana creates the process ID file.
#pid.file: /var/run/kibana.pid

# Enables you specify a file where Kibana stores log output.
# デフォルトではログファイルが作成されません。何かあった場合にここにログファイルのパスを指定してデバッグを行います。
# ファイルの作成はしてくれないので、自分で作成します。
# コマンド例)touch /var/log/kibana.log
# コマンド例)chown kibana:kibana /var/log/kibana.log
#logging.dest: stdout ←/var/log/kibana.logを指定します

# Set the value of this setting to true to suppress all logging output.
#logging.silent: false

# Set the value of this setting to true to suppress all logging output other than error messages.
#logging.quiet: false

# Set the value of this setting to true to log all events, including system usage information
# and all requests.
#logging.verbose: false

# Set the interval in milliseconds to sample system and process performance
# metrics. Minimum is 100ms. Defaults to 5000.
#ops.interval: 5000

# The default locale. This locale can be used in certain circumstances to substitute any missing
# translations.
#i18n.defaultLocale: "en"

# ★x-packに関する定義を行う
## X-Pack
#Graph Settings
xpack.graph.enabled: false

#★Monitoring Settings
# General Monitoring Settings
xpack.monitoring.enabled: true

#R★eporting Settings
# General Reporting Settings
xpack.reporting.enabled: true

#★Security Settings
# General Security Settings : Defalut true
xpack.security.enabled: true

(2)Kibanaを起動します。
# systemctl start kibana.service

※エラーが出ないことを確認してください。

Kibanaに接続する

(1)ブラウザでKibanaに接続します。
http://Kibanaのホスト名又はIP:5601

(2)Kibanaのログイン画面が表示されたら、ユーザ名に「elastic」、パスワードを入力してログインをしてください。

※画像が無くすみません!

以上で環境が整い、使えるはずです!

Elasticsearchのインストール及び設定 その10 SSL設定(任意)

Elasticsearchに対する外部からの接続(API)部分をSSL化します。
こちらは任意の設定となります。

公式HPだけだと正直解りませんでした。

www.elastic.co

こちらの記事を基に手順の確立をしました。

www.idata.co.il

上記記事に書かれている「certgen」というコマンドは古いようで、「certutil」というコマンドを利用して設定を実施します。

正直他の手段でSSL化をした方が良いような気がします・・・・

構築手順

(1)下記のコマンドを実行します。いずれかのサーバで実施します。

# /usr/share/elasticsearch/bin/x-pack/certutil cert -multiple -pem

(2)対話形式で設定をしていきます。

ちょっと見難いので表示を分割して記述します。

This tool assists you in the generation of X.509 certificates and certificate
signing requests for use with SSL/TLS in the Elastic stack.

The 'cert' mode generates X.509 certificate and private keys.
    * By default, this generates a single certificate and key for use
       on a single instance.
    * The '-multiple' option will prompt you to enter details for multiple
       instances and will generate a certificate and key for each one
    * The '-in' option allows for the certificate generation to be automated by describing
       the details of each instance in a YAML file

    * An instance is any piece of the Elastic Stack that requires a SSL certificate.
      Depending on your configuration, Elasticsearch, Logstash, Kibana, and Beats
      may all require a certificate and private key.
    * The minimum required value for each instance is a name. This can simply be the
      hostname, which will be used as the Common Name of the certificate. A full
      distinguished name may also be used.
    * A filename value may be required for each instance. This is necessary when the
      name would result in an invalid file or directory name. The name provided here
      is used as the directory name (within the zip) and the prefix for the key and
      certificate files. The filename is required if you are prompted and the name
      is not displayed in the prompt.
    * IP addresses and DNS names are optional. Multiple values can be specified as a
      comma separated string. If no IP addresses or DNS names are provided, you may
      disable hostname verification in your SSL configuration.

    * All certificates generated by this tool will be signed by a certificate authority (CA).
    * The tool can automatically generate a new CA for you, or you can provide your own with the
         -ca or -ca-cert command line options.

By default the 'cert' mode produces a single PKCS#12 output file which holds:
    * The instance certificate
    * The private key for the instance certificate
    * The CA certificate

If you elect to generate PEM format certificates (the -pem option), then the output will
be a zip file containing individual files for the instance certificate, the key and the CA certificate

If you elect to generate multiple instances certificates, the output will be a zip file
containing all the generated certificates

これより下が実際に入力する部分となります。
全Elasticsearchのホスト名とIP、またkibanaについても入力をします。
入力箇所には ★ をつけます。

Enter instance name: rhel1 ★ホスト名を入力
Enter name for directories and files of rhel1 [rhel1]:
Enter IP Addresses for instance (comma-separated if more than one) []: 192.168.0.1 ★IPアドレスを入力
Enter DNS names for instance (comma-separated if more than one) []: rhel1 ★ホスト名を入力
Would you like to specify another instance? Press 'y' to continue entering instance information: y ★連続入力は「y」
Enter instance name: rhel2 ★
Enter name for directories and files of rhel2 [rhel2]:
Enter IP Addresses for instance (comma-separated if more than one) []: 192.168.0.2 ★
Enter DNS names for instance (comma-separated if more than one) []: rhel2 ★
Would you like to specify another instance? Press 'y' to continue entering instance information: y ★
Enter instance name: rhel3 ★
Enter name for directories and files of rhel3 [rhel3]:
Enter IP Addresses for instance (comma-separated if more than one) []: 192.168.0.3 ★
Enter DNS names for instance (comma-separated if more than one) []: rhel3 ★
Would you like to specify another instance? Press 'y' to continue entering instance information: y ★
Enter instance name: rhel4 ★
Enter name for directories and files of rhel4 [rhel4]:
Enter IP Addresses for instance (comma-separated if more than one) []: 192.168.0.4 ★
Enter DNS names for instance (comma-separated if more than one) []: rhel4 ★
Would you like to specify another instance? Press 'y' to continue entering instance information: y ★
Enter instance name: rhel5 ★
Enter name for directories and files of elastic5 [elastic5]:
Enter IP Addresses for instance (comma-separated if more than one) []: 192.168.0.5 ★
Enter DNS names for instance (comma-separated if more than one) []: rhel5 ★
Would you like to specify another instance? Press 'y' to continue entering instance information: y ★
Enter instance name: kibana ★
Enter name for directories and files of kibana [kibana]:
Enter IP Addresses for instance (comma-separated if more than one) []: 192.168.0.6 ★
Enter DNS names for instance (comma-separated if more than one) []: kibana ★
Would you like to specify another instance? Press 'y' to continue entering instance information: ←Kibanaで終了なのでEnter
Please enter the desired output file [certificate-bundle.zip]: ssl2.zip

Certificates written to /root/ssl2.zip

This file should be properly secured as it contains the private keys for
all instances.

After unzipping the file, there will be a directory for each instance.
Each instance has a certificate and private key.
For each Elastic product that you wish to configure, you should copy
the certificate, key, and CA certificate to the relevant configuration directory
and then follow the SSL configuration instructions in the product guide.

For client applications, you may only need to copy the CA certificate and
configure the client to trust this certificate.

(3)zipファイルが作成されますので(上記だと /root/ssl2.zip)、全てのElasticsearchサーバの証明書置き場(TLS設定で作成した/etc/elasticsearch/certificate)にコピーし、解凍しておいてください。

手元に環境が無く、確認する術がないのですが、以下のようなファイルができます。

  • ホスト名.crt
  • ホスト名.key
  • ca.crt

(4)全てのElasticsearchサーバのkeystoreにSSLパスワードの設定をします。

#/usr/share/elasticsearch/bin/elasticsearch-keystore add xpack.security.http.ssl.secure_key_passphrase
Enter value for xpack.security.http.ssl.secure_key_passphrase: パスワードを入力

(5)全てのElasticsearchサーバのymlに下記の定義を追加します。(最下行に挿入で大丈夫です)

# vi /etc/elasticsearch/elasticsearch.yml

以下、追加する定義です。
各Elasticsearchでホスト名に該当する証明書及び鍵の指定をします。
また、更改鍵についても指定します。

※zip展開時にパスが異なるようでしたら、実態に合わせて記述の変更をお願いします。

xpack.security.http.ssl.enabled: true
xpack.security.http.ssl.key: /etc/elasticsearch/certificate/ホスト名.key
xpack.security.http.ssl.certificate: /etc/elasticsearch/certificate/ホスト名.crt
xpack.ssl.certificate_authorities: [ "/etc/elasticsearch/certificate/ca.crt" ]

(6)全てのノードを再起動します。

アクセス方法

Curlでのアクセス

公開鍵を指定してcurlを実施します。
※このあたりは我流で実施したので、正解かは怪しいです。

例) # curl --cacert /etc/elasticsearch/certificate/ca/ca.crt -u elastic https://192.168.0.3:9200

kibanaサーバからのアクセス

kibanaのyml(/etc/kibana/kibana.yml)に以下の設定が必要となります。

※ Kibanaの設定は別途記述しますが、SSL通信では使用しない想定なのでここに定義を記述します。

elasticsearch.url: "https://192.168.0.3:9200"    #httpsに変更
elasticsearch.username: "kibana"                    #elasticsearchに接続するアカウント
elasticsearch.password: "パスワード"           #elasticsearchに接続するパスワード
server.ssl.enabled: true                                    #kibanaに対するSSL通信を設定
server.ssl.certificate: /etc/kibana/kibana.crt  #kibana用の証明書ファイルの指定
server.ssl.key: /etc/kibana/kibana.key          #kibana用の鍵ファイルの指定
elasticsearch.ssl.certificateAuthorities: [ "/etc/kibana/ca.crt" ]  #elasticsearchの公開鍵の指定

上記編集後にkibanaの再起動を実施し、httpsでkibanaに接続してください。