パラボラアンテナと星の日記

あることないこと

jqの--exit-statusオプションについて調べてまとめたので100ブクマぐらいお願いします

manすると1文が長くてつらい。neither A nor Bとか懐かしい。両方否定ね。

man jq

   o   -e / --exit-status:
       Sets  the exit status of jq to 0 if the last output values was nei-
       ther false nor null, 1 if the last output value was either false or
       null,  or 4 if no valid result was ever produced. Normally jq exits
       with 2 if there was any usage problem or system error, 3  if  there
       was a jq program compile error, or 0 if the jq program ran.

まとめ

command stdout exit status
$ echo '{}' | jq . {} 0
$ echo '{}' | jq --exit-status . {} 0
$ echo 'true' | jq . true 0
$ echo 'true' | jq --exit-status . true 0
$ echo 'false' | jq . false 0
$ echo 'false' | jq --exit-status . false 1
$ echo 'null' | jq . null 0
$ echo 'null' | jq --exit-status . null 1
$ echo 'foo' | jq . *1 4
$ echo 'foo' | jq --exit-status . *2 4

以上です

*1:エラー出力にparse error: Invalid literal at line 2, column 0

*2:エラー出力にparse error: Invalid literal at line 2, column 0

Dockerコンテナが消費するメモリの最大値を知りたいのだが

ECSで、この画面で悩むんですが、皆さんどうされてるんでしょうか。。

「Maximum Memory」にメモリの上限値を入力する。

docker runの-mオプションに相当する。

f:id:hoppie:20160628065828p:plain

memory.max_usage_in_bytesを見て調べるということでどうか。

以下はAmazon Linuxの例。

$ # 調べたいコンテナを起動(-dでデタッチ)、コンテナIDを取得する
$ CID=$(docker run -d alpine sleep 100)
$
$ # 取得したコンテナIDからmax_usage_in_bytes(使用したメモリの最大値)
$ cat /cgroup/memory/docker/$CID/memory.max_usage_in_bytes
6422528

手元のubuntuだと、「/cgroup/memory/docker」の部分は「/sys/fs/cgroup/memory/docker」でした。

ちなみに以下はcgroupで取れるmemory関連の値。抜粋。

3.7. memory

memory.max_usage_in_bytes

cgroup 内のプロセスによるメモリー最大使用量をレポートします (バイト単位)。

memory.usage_in_bytes

cgroup 内のプロセスによる現在のメモリー総使用量をレポートします (バイト単位)。

memory.limit_in_bytes

ユーザーメモリーの最大値 (ファイルキャッシュを含む) を設定します。単位が指定されていない場合、その値はバイト単位と解釈されますが、より大きな単位を示すサフィックスを使用することが可能です (キロバイトには k または K、メガバイトには m または M、ギガバイトには g または G)。 root cgroup を制限するのには、memory.limit_in_bytes は使用できません。値を適用できるのは、下位階層のグループに対してのみです。 memory.limit_in_bytes に -1 と書き込み、現行の制限値を削除します。

おまけ

Dockerコンテナ内からDockerコンテナが消費したメモリの最大値を知るにはどうすればよいかを考えてみました。

(良い方法は思いつきませんでした、、、、)

-vオプションで/cgroupをマウントする。。。という方法でどうでしょうか。

$ docker run -v /cgroup/memory/docker:/docker:ro ...

rubyコンテナを走らせ、マウントされた/dockerディレクトリを見てmemory.max_usage_in_bytesをチェックする例です。

$ docker run \
  -v /cgroup/memory/docker:/docker:ro \
  -it \
  ruby:2.3.1-alpine irb
# memory.max_usage_in_bytesを取得するヘルパ定義
irb(main):001:0> def memory_max_usage_in_bytes
irb(main):002:1>   File.read(Dir["/docker/#{ENV.fetch('HOSTNAME')}*/memory.max_usage_in_bytes"].first).to_i/1024.0/1024.0
irb(main):003:1> end
=> :memory_max_usage_in_bytes
irb(main):004:0> memory_max_usage_in_bytes
=> 11.27734375

# サイズが1万、String配列を作る
irb(main):007:0> (1..10_000).map(&:to_s); nil
=> nil
irb(main):008:0> memory_max_usage_in_bytes
=> 11.80859375

# サイズが10万、String配列を作る
irb(main):009:0> (1..100_000).map(&:to_s); nil #ai
=> nil
irb(main):010:0> memory_max_usage_in_bytes
=> 17.55859375

# サイズが100万、String配列を作る
irb(main):011:0> (1..1_000_000).map(&:to_s); nil #ai
=> nil
irb(main):012:0> memory_max_usage_in_bytes
=> 77.29296875

# サイズが300万、String配列を作る
irb(main):013:0> (1..3_000_000).map(&:to_s); nil #ai
=> nil
irb(main):014:0> memory_max_usage_in_bytes
=> 278.29296875

まわりくどいですが、これはこれで便利な気がします。

re:dashをdocker-composeで動かしたら簡単だったぜ 2016年6月

この記事では、re:dashをdockerで一通り動かすことをやってみます。

re:dashとは

こんな感じでデータをビジュアライズできるツールです。

f:id:hoppie:20160619095542g:plain

(↑公式ドキュメントより拝借)

re:dashの特徴は以下のとおりです。

  • 取り込むことのできるデータの形式が豊富!!
    • BigQuery, TD, ElasticSearch, GoogleSpreadSheet, 任意のURL, etc.
  • アラート機能(メール)もある!!

動かす

AWS, GCEで専用マシンイメージが配布されているようですが、

今回はdocker composeを使って自分のOSX内で実行してみました。

docker compose

Setupを読んで手順通りにやっていきます。

1. Make sure you have a Docker machine up and running.
2. Make sure your current working directory is the root of this GitHub repository.
3. Run docker-compose up postgres.
4. Run ./setup/docker/create_database.sh. This will access the postgres container and set up the database.
5. Run docker-compose up
6. Run docker-machine ls, take note of the ip for the Docker machine you are using, and open the web browser.
7. Visit that Docker machine IP at port 80, and you should see a Re:dash login screen.

それぞれの項目をシェルスクリプトで書いてみました。

## 1.Make sure you have a Docker machine up and running.
# docker-machineが動いているかどうかを確認します。
docker-machine ls
# devマシンとかが表示されればよいでしょう。

## 2. Make sure your current working directory is the root of this GitHub repository.
go get github.com/getredash/redash
# 今作ったところに移動
cd $GOPATH/src/!$

## 3. Run docker-compose up postgres.
# 先にdocker-compose.ymlを作る必要があります。
# 本格運用時には変数REDASH_COOKIE_SECRETは値を変更するべきであることに注意。
# その他の環境変数は変更しなくてもとりあえず動作しました。
mv docker-compose-example.yml docker-compose.yml
# データベースのセットアップのため、postgresコンテナだけ先に起動します。
docker-compose up postgres

## 4. Run docker-compose up postgres.
# 起動したら、別窓を開き、以下のコマンドを実行しセットアップを実行。
./setup/docker/create_database.sh

## 5. Run docker-compose up
# 全てのコンテナ(postgres、redis、redash本体、nginx)を起動します。
docker compose up

## 6. Run docker-machine ls, take note of the ip for the Docker machine you are using, and open the web browser.
## 7. Visit that Docker machine IP at port 80, and you should see a Re:dash login screen.

# docker-machineが動いているIPの80番ポートでre:dashが動いているはず。

re:dash動きました!!!

初回はEmail:admin Password:adminでログインできます。

f:id:hoppie:20160619095817p:plain

完!docker-composeとredashに慣れていればここまで数分だと思います!

おまけ: BigQueryのデータ投入してみる

せっかくなのでデータ投入してみます。

  • BigQueryデータソースの追加
  • クエリ・グラフ作成

BigQueryデータソースの追加

先にBigQueryが利用できるGCPのプロジェクトが必要です。

re:dashが自分のプロジェクトのBigQueryのデータにアクセスできるように、サービスアカウントを準備します。

サービスアカウントは、GCPのこちらのページから作成できます。

以下の様な感じで入力し、JSONのほうを取得。

f:id:hoppie:20160619101905p:plain

データソースの作成画面で以下の様な感じで入力、今取得したJSONをアップロードし、SAVE。

1回のクエリのスキャン上限は1000MBまでにしてみました。

f:id:hoppie:20160619101840p:plain

クエリ画面で自分のプロジェクトのBigQueryのテーブルが使えるようになりました!

f:id:hoppie:20160619102533p:plain

クエリ・グラフ作成

re:dashでは 1つのQueryが1つのTableを持ち、 1つのQueryがN個のVisualization(グラフ等)を持つ、というデータ構造のようです。

今回はBigQueryの謎サンプルデータ(nyc-tlc.green、たぶんニューヨークのタクシー会社の乗降記録)を利用してみました。

日付とレコード数を出力。こんな感じ。

f:id:hoppie:20160619103400p:plain

+New Visualization を押してポチポチしていくと折れ線グラフが作れます。

f:id:hoppie:20160619103801p:plain

このタクシーの乗降記録には、緯度経度情報もありました。せっかくなのでMapを利用してみます。

こんな風にSELECT文を書く。

f:id:hoppie:20160619110153p:plain

こうなる。どん!

f:id:hoppie:20160619110126p:plain

👏 👏 👏

参考: 今回動かした環境

  • re:dashのバージョン: 0.11。6/18時点のmaster。(8df822eee)
  • その他:
$ docker --version; docker-machine --version; docker-compose --version
Docker version 1.8.2, build 0a8c2e3
docker-machine version 0.4.1 (e2c88d6)
docker-compose version: 1.4.2

参考文献:

  1. Setting up Re:dash instance — Re:dash documentation
  2. ryotarai.hatenablog.com
  3. github.com