togatttiのエンジニアメモ

過度な期待はしないでください.

fishで、Anacondaに環境パスを通す

Anacondaへの環境パスがfishだと通ってないので、設定する。

Anaconda3のインストール先を変更しないことを前提に、以下のコマンドを実行する。

Ubuntu, MacOS

set -U fish_user_paths $HOME/anaconda3/bin $fish_user_paths

どうでもいいけど、anacondaユーザは.bashrcとか.bash_profileに環境パスが追記されるけど、bashの利用者が多いのだろうか。

日時範囲を指定して、Elasticsearchのデータを取得する。

curlで日時範囲を指定して、Elasticsearchのデータを取得する方法を示す。

取得対象のデータには、

(snip)
 "@timestamp" : "2017-08-16T16:51:06+09:00"
(snip)

のような感じで、タイムスタンプが設定されていることを前提とする。

curlで、タイムスタンプを日時の範囲を指定して、データを取得したい場合は、

$ curl -XGET 'localhost:9200/elsdata-2017.08.16/elsdata/_search' -d '{
    "query": {
        "range" : {
            "@timestamp" : {
                "gte" : "2017-08-16T16:18:00+09:00", "lte": "now", "format" : "date_time_no_millis"
            }
        }
    }
}'

という感じで、JSONを渡してあげると取得できる。

gteとlteを指定することで、2017-08-16T16:18:00+09:00から現在までのデータを取得することできる。

タイムスタンプの形式によっては、formatを設定する必要があるので、ここから条件を満たすものを選び設定する。

また、10000件以上のデータを一度に取得したい場合は、scrollとsizeを指定して、リクエストすると取得できる。

以下の例は、30000万件のデータを取得する場合

$ curl -XGET 'localhost:9200/elsdata-2017.08.16/elsdata/_search?scroll=1m&size=30000&pretty' -d '{
    "query": {
        "range" : {
            "@timestamp" : {
                "gte" : "2017-08-16T16:18:00+09:00", "lte": "now", "format" : "date_time_no_millis"
            }
        }
    }
}'

embulk-parser-regexのエラー対応

Embulkプラグインのembulk-parser-regexで、regexのキーに、_(アンダースコア)を含めるとエラーになる。

regexを使う時は、キーに_を使わないようにしよう。

エラーメッセージ

Error: java.util.regex.PatternSyntaxException: named capturing group is missing trailing '>' near index 125
^(?<from>[^%]+)%(?<uid>[^%]+)%(?<did>[^%]+)%(?<mid>[^%]+)%(?<type>[^%]+)%(?<to>[^%]+)%(?<status>[^%]+)%(?<ts>[^%]+)%(?<status_desc>[^%]+)%(?<host>[^%]+)%(?<uuid>[^%]+)$
                                                                                                                             ^

confg.ymlのサンプル

parser:
    type: regex
    # エラーが出る。
    # regex: ^(?<from>[^%]+)%(?<uid>[^%]+)%(?<did>[^%]+)%(?<mid>[^%]+)%(?<type>[^%]+)%(?<to>[^%]+)%(?<status>[^%]+)%(?<ts>[^%]+)%(?<status_desc>[^%]+)%(?<host>[^%]+)%(?<uuid>[^%]+)$
    # アンダースコアを消すとうまくいく。
    regex: ^(?<from>[^%]+)%(?<uid>[^%]+)%(?<did>[^%]+)%(?<mid>[^%]+)%(?<type>[^%]+)%(?<to>[^%]+)%(?<status>[^%]+)%(?<ts>[^%]+)%(?<statusdesc>[^%]+)%(?<host>[^%]+)%(?<uuid>[^%]+)$

    columns:
(snip)
    # regex_nameもアンダースコアを修正
    # - {name: status_desc, type: string, regex_name: status_desc}
    - {name: status_desc, type: string, regex_name: statusdesc}
(snip)    

parallel-slurpで、ファイルを並列で取得する

複数のホストから、ファイルをダウンロードする必要があったので、方法を調べた。

parallel-slurp(pslurp)という便利なものがあったので、使うことにした。

インストール

OSは、Ubuntuなので、APTでインストールする。

# apt install pssh

manを読むと、やりたことが書かれてる。

        PARALLEL-SLURP(1)                                                                                            PARALLEL-SLURP(1)

NAME
       parallel-slurp - copy files from listed hosts

SYNOPSIS
       parallel-slurp [OPTIONS] -h hosts.txt -L destdir remote local

DESCRIPTION
       pssh provides a number of commands for executing against a group of computers, using SSH. It´s most useful for
       operating on clusters of homogenously-configured hosts.

       parallel-slurp gathers specified files from hosts you listed.
(snip)

使い方

対象ホストのリストを用意する。

$ cd /home/admin/
$ cat file_list
a.example.com
b.example.com
c.example.com

ダウンロードしたファイル用のディレクトリを作成する。

$ mkdir downloads

parallel-slurpを実行する。

$ parallel-slurp -ladmin -h file_list -p 3 -L /home/admin/downloads /var/log/http_access.log http_access.log

引数の説明を簡単にする。

  • -lは、作業ユーザ
  • -hは、対象ホストのリスト
  • -pは、並列処理数
  • -Lは、ダウンロードしたファイルを置くディレクト

そのあとの引数は

  • ダウンロードするファイルの絶対パス
  • ダウンロードした際のファイルの名前

実行するとディレクトリは次のようになる。

$ tree downloads/
downloads/
├── a.example.com
│   └── http_access.log
├── b.example.com
│   └── http_access.log
└── c.example.com
    └── http_access.log

3 directories, 3 files