2012-02-11 [vim][linux] ソースをうまく貼り付けられない時に。
■[vim][linux] ソースをうまく貼り付けられない時に。

ソースをvimで、張り付けようとして、
var socket = io.listen(app);
socket.on('connection', function(client) {
// connect
// client.on('message', function(message) {
// // message
// });
// client.on('disconnect', function() {
// // disconnect
// });
// });
みたいになって貼り付けられない場合。
- :a して、貼り付け、改行いれて、esc*2でうまく張り付きます!
var socket = io.listen(app);
socket.on('connection', function(client) {
// connect
client.on('message', function(message) {
// message
});
client.on('disconnect', function() {
// disconnect
});
});
つかだくんありがとう。いんふらっぷ。
2012-01-31 mongotop
■[linux][mongodb] mongotop

mongodb2.0からmongotopと言うコマンドが入って来てました。
結構便利だったので、紹介。
RESTのページや、ログからスロークエリを見つけることも多いですが、mongotopだとリアルタイムに状況が見られるのでトラブルシューティングにはかなりよいかと。
mongos経由では使用できないので、怪しいmongodに対して実行しましょう。
コマンド
$ /usr/local/mongodb-linux-x86_64-2.0.2/bin/mongotop -h 192.168.0.100:27017
表示例
通常時はこんな感じになる。local.oplog.rsはレプリケーションのためのoplogへのアクセスでこれはいつも1000〜2000ms前後になっている。
$ /usr/local/mongodb-linux-x86_64-2.0.2/bin/mongotop -h 192.168.0.100:27017
connected to: 192.168.0.100:27017
ns total read write 2012-01-31T13:38:08
local.oplog.rs 1903ms 1903ms 0ms
testdb.TestCol01 11ms 2ms 8ms
testdb.TestCol02 8ms 8ms 0ms
testdb.TestCol03 6ms 0ms 4ms
遅いサーバだとこんな感じになるので遅いコレクション(ここではtestdb.TestCol01)に対するアクセスを見直す。
そのmongod全体のクエリが重いのであれば、ディスクI/Oとか、グローバルロックを疑う。
ns total read write 2012-01-31T14:48:51
local.oplog.rs 1636ms 1636ms 0ms
testdb.TestCol01 153ms 122ms 30ms
testdb.TestCol02 36ms 3ms 30ms
testdb.TestCol03 23ms 23ms 0ms
testdb.TestCol04 18ms 18ms 0ms
- 作者: 佐々木達也
- 出版社/メーカー: 秀和システム
- 発売日: 2011/04
- メディア: 大型本
- 購入: 7人 クリック: 759回
- この商品を含むブログ (20件) を見る
2012-01-30 AWSで今から始めるmongodb(1) インストールからサーバ接続まで
■[linux][mongodb] AWSで今から始めるmongodb(1) インストールからサーバ接続まで

どもども、今日はスタバでベンティ飲んだら腹がタポタポくわのです。
そういえば、mongodbについて書いてないなぁと思いまして、自分の知識の定着の意味も含めてちょっとしばらく書いていこうかと思っています。
今日のテーマ
mongodbとは?
まず、mongodbです。
ドキュメント指向ということで、データ形式はJSONですべて表現されます。
PRIMARY> db.TestUser.findOne()
{
"_id" : "aaaaaaaaaaaaa",
"userid" : "akuwano",
"lastLoginTime" : NumberLong("1326287399283"),
"username" : "Akihiro Kuwano"
}
こんな感じ、また、スキーマレスなので途中でカラムが増えたりしたときもMySQLなどのようにALTER TABLEでの定義変更などは必要ありません。
詳しくはこの辺をどうぞ!
EC2インスタンスのlaunch方法
割愛。
quick-launch楽ちんですね。
インストール
インストールは、以下のようにmongodbのダウンロードサイトからバイナリをダウンロードしてみるのが一番簡単です。
せっかくなのでEC2インスタンスにインストールしてみましょう。
$ ssh -i akuwano.pem ec2-user@ec2-hogehoge.ap-northeast-1.compute.amazonaws.com
__| __|_ )
___|\___|___|
See /usr/share/doc/system-release/ for latest release notes.
There are 18 security update(s) out of 26 total update(s) available
$ cd /usr/local
$ sudo wget http://fastdl.mongodb.org/linux/mongodb-linux-x86_64-2.0.2.tgz
[root@ip-hogehoge mongodb-linux-x86_64-2.0.2]$ ls -R
.:
GNU-AGPL-3.0 README THIRD-PARTY-NOTICES bin
./bin:
bsondump mongo mongod mongodump mongoexport mongofiles mongoimport mongorestore mongos mongosniff mongostat mongotop
インストールされるファイルはこんな感じ。
設定〜起動
では次はインストール後の設定から起動まで。
cd /usr/local/mongodb-linux-x86_64-2.0.2
sudo mkdir -p /usr/local/mongodb-linux-x86_64-2.0.2/{logs,conf,data}
sudo su -c "cat <<'EOF' > /usr/local/mongodb-linux-x86_64-2.0.2/conf/mongod.conf
port = 27017
logpath=/usr/local/mongodb-linux-x86_64-2.0.2/logs/mongod.log
pidfilepath=/var/run/mongod1.pid
logappend=true
fork = true
dbpath=/usr/local/mongodb-linux-x86_64-2.0.2/data
directoryperdb=true
noauth = true
# Replication Options
#replSet = replSetTest001
rest = true
# Sharding Options
#shardsvr=true
# Journal Option
journal = true
EOF"
いよいよ起動!
sudo /usr/local/mongodb-linux-x86_64-2.0.2/bin/mongod -f /usr/local/mongodb-linux-x86_64-2.0.2/conf/mongod.conf
$ vmstat 1
procs -----------memory---------- ---swap-- -----io---- --system-- -----cpu-----
r b swpd free buff cache si so bi bo in cs us sy id wa st
1 3 0 7112 4236 534524 0 0 37 426 26 27 0 0 95 4 0
0 3 0 7076 4236 534684 0 0 0 29876 462 836 0 4 0 93 4
0 3 0 7072 4240 534700 0 0 0 31024 509 869 0 4 0 93 4
0 2 0 7068 4240 534484 0 0 0 27240 394 171 0 4 0 88 8
0 3 0 6596 4240 535644 0 0 0 22948 408 505 0 4 0 95 1
しばらくはデータファイルのファイル確保でDISKをガリガリ書きますが、しばらく経てば終わります。
接続
次にローカルからmongoクライアントで接続してみましょう。
$ /usr/local/mongodb-linux-x86_64-2.0.2/bin/mongo
MongoDB shell version: 2.0.2
connecting to: test
>
OK
データのインサート
ちゃんとデータが入れられるかの確認、クライアントより
testdbデータベースの、testcolコレクションに、1オブジェクト追加してみます。
データ追加。
mongo> doc = { cdname: 'cdname001',
... cdtitle : 'Example CD Name'
... }
{ "cdname" : "cdname001", "cdtitle" : "Example CD Name" }
mongo>
mongo> db.testcol.insert(doc);
findで確認。
> db.testcol.find()
{ "_id" : ObjectId("4f25e4d097a5113670430159"), "cdname" : "cdname001", "cdtitle" : "Example CD Name" }
OKですね。
"_id" : ObjectId("4f25e4d097a5113670430159")はmongodb側で自動でつけてくれます、MySQLなどのRDBでいえばPK(PrimaryKey)にあたり、sharding*1の時のキーになります。
DBへのアクセス制限
これだと、誰でもアクセスできてしまうので、ユーザ、パスワードをつけてアクセス制限してみましょう。
$ /usr/local/mongodb-linux-x86_64-2.0.2/bin/mongo
MongoDB shell version: 2.0.2
connecting to: test
> use testdb
switched to db testdb
> db.addUser('user', 'password')
{
"updatedExisting" : true,
"n" : 1,
"connectionId" : 6,
"err" : null,
"ok" : 1
}
{
"_id" : ObjectId("4f25e51097a511367043015b"),
"user" : "user",
"readOnly" : false,
"pwd" : "3bcfc22a1cd6be41bc7814c13d3ce94c"
}
> use admin
switched to db admin
> db.addUser('user', 'password')
{
"updatedExisting" : true,
"n" : 1,
"connectionId" : 6,
"err" : null,
"ok" : 1
}
{
"_id" : ObjectId("4f25e50197a511367043015a"),
"user" : "user",
"readOnly" : false,
"pwd" : "3bcfc22a1cd6be41bc7814c13d3ce94c"
}
> exit
db.addUser('user', 'password') - ユーザ、パスワードの設定のためのコマンドになります。
$ ./mongo ec2-hoge.ap-northeast-1.compute.amazonaws.com:27017/testdb -u user -p HOGEHOGE
MongoDB shell version: 2.0.2
connecting to: ec2-hoge.ap-northeast-1.compute.amazonaws.com:27017/testdb
Mon Jan 30 09:46:51 uncaught exception: login failed
exception: login failed
アクセス不可。よしよし。
$ ./mongo ec2-hoge.ap-northeast-1.compute.amazonaws.com:27017/testdb -u user -p password
MongoDB shell version: 2.0.2
connecting to: ec2-hoge.ap-northeast-1.compute.amazonaws.com:27017/testdb
>
アクセスできましたー!
これでセキュリティもOKです。
EC2のセキュリティグループの設定
ここまでOKにしたら、AWSのManagementConsoleからEC2のセキュリティグループ設定を変更しましょう。
27017ポートへのアクセスを有効にします。画像参照。
[Apply Rule Changes]をおしたら有効になります。
これで完了。
これでmongodbがどこからでも使える状態になりました。
というのをたまに防備録的にまとめていきたいと思います。よろしくお願いします。
*1:後述予定(汗)
2011-12-22 割りと便利!w ppssで手軽に並列処理をしてみる
■[linux]割りと便利!w ppssで手軽に並列処理をしてみる

分散処理を手軽にやりたい。
GNU Parallel も便利だけど、ppssならシェルスクリプトだから設置するだけで実行できて便利!
って事で試してみた。
インストール
これだけ。
$ cd ~/bin $ wget http://ppss.googlecode.com/files/ppss-2.85.tgz $ tar zxvf ppss-2.85.tgz $ rm ppss-2.85.tgz
オプション
今回使うオプション
# -d 対象ディレクトリ # -f リストファイル # -c 実行コマンド # -p 並列数
ppss --helpの結果
kuwano@kuwano03:~/bin$ ./ppss -h
|P|P|S|S| Distributed Parallel Processing Shell Script 2.85
PPSS is a Bash shell script that executes commands in parallel on a set
of items, such as files in a directory, or lines in a file. The purpose
of PPSS is to make it simple to benefit from multiple CPUs or CPU cores.
This short summary only discusses options for stand-alone mode. For a
full listing of all options, run PPSS with the options --help
Usage ./ppss [ options ]
--command | -c Command to execute. Syntax: '<command> ' including the single quotes.
Example: -c 'ls -alh '. It is also possible to specify where an item
must be inserted: 'cp "$ITEM" /somedir'.
--sourcedir | -d Directory that contains files that must be processed. Individual files
are fed as an argument to the command that has been specified with -c.
--sourcefile | -f Each single line of the supplied file will be fed as an item to the
command that has been specified with -c. Read input from stdin with
-f -
--config | -C If the mode is config, a config file with the specified name will be
generated based on all the options specified. In the other modes.
this option will result in PPSS reading the config file and start
processing items based on the settings of this file.
--disable-ht | -j Disable hyper threading. Is enabled by default.
--log | -l Sets the name of the log file. The default is ppss-log.txt.
--processes | -p Start the specified number of processes. Ignore the number of available
CPUs.
--quiet | -q Shows no output except for a progress indication using percents.
--delay | -D Adds an initial random delay to the start of all parallel jobs to spread
the load. The delay (seconds) is only used at the start of all 'threads'.
--daemon Daemon mode. Do not exit after items are professed, but keep looking
for new items and process them. Read the manual how to use this!
See --help for important additional options regarding daemon mode.
--no-recursion|-r By default, recursion of directories is enabled when the -d option is
used. If this is not prefered, this can be disabled with this option
Only files within the specified directory will be processed.
--email | -e PPSS sends an e-mail if PPSS has finished. It is also used if processing
of an item has failed (configurable, see -h).
--help Extended help, including options for distributed mode and Amazon EC2.
Example: encoding some wav files to mp3 using lame:
./ppss -d /path/to/wavfiles -c 'lame '
Extended usage: use --help
使い方例
特定ディレクトリ以下にあるファイルを2並列処理でgzipする
- 下準備
$ mkdir -p ~/test; for i in `seq 1 100` ; do echo "test $i" > ~/test/test$i ;done
- 実行
kuwano@kuwano03:~$ ~/bin/ppss -d ~/test/ -c "gzip " -p 2 12月 22 16:20:09: 12月 22 16:20:09: ========================================================= 12月 22 16:20:09: |P|P|S|S| 12月 22 16:20:09: Distributed Parallel Processing Shell Script vers. 2.85 12月 22 16:20:09: ========================================================= 12月 22 16:20:09: Hostname: kuwano03 12月 22 16:20:09: --------------------------------------------------------- 12月 22 16:20:10: CPU: Intel(R) Core(TM) i5 CPU 760 @ 2.80GHz 12月 22 16:20:10: Starting 2 parallel workers. 12月 22 16:20:10: --------------------------------------------------------- 12月 22 16:20:26: One job is remaining. 12月 22 16:20:26: Total processing time (hh:mm:ss): 00:00:17 12月 22 16:20:26: Finished. Consult ppss_dir/job_log for job output.
URLリストファイルにあるURLを2並列処理で~/testにwget->保存する
- 下準備
$ cat <<'EOF' >urllist.txt http://www.ameba.jp/ http://now.ameba.jp/ http://d.hatena.ne.jp/akuwano EOF
- 実行
kuwano@kuwano03:~$ ~/bin/ppss -f urllist.txt -c 'wget -q -P ~/test/ "$ITEM"' -p 2 12月 22 16:30:58: 12月 22 16:30:58: ========================================================= 12月 22 16:30:58: |P|P|S|S| 12月 22 16:30:58: Distributed Parallel Processing Shell Script vers. 2.85 12月 22 16:30:58: ========================================================= 12月 22 16:30:58: Hostname: kuwano03 12月 22 16:30:58: --------------------------------------------------------- 12月 22 16:30:58: CPU: Intel(R) Core(TM) i5 CPU 760 @ 2.80GHz 12月 22 16:30:58: Starting 2 parallel workers. 12月 22 16:30:58: --------------------------------------------------------- 12月 22 16:30:59: One job is remaining. 12月 22 16:31:00: Total processing time (hh:mm:ss): 00:00:02 12月 22 16:31:00: Finished. Consult ppss_dir/job_log for job output.
$ITEMには-d ディレクトリリストか、-f ファイルリストから渡された値が入ってくるので、実際に実行するコマンドに当てはめてやる。
ログ
ログのディレクトリが、デフォルトは./ppss_logにつくられて、処理結果が保存されるので確認。
-l オプション でログ出力を変更可能
なんか
他にもサーバ間で分散処理させたり、デーモンとして動かすこともできるらしいです。
まだ試してないけど便利かもねー。(無責任w)
割りと便利なのと手軽に使えるのでちょいっとした処理の時にサクッと入れるといいかも。
2011-12-12 DevLOVE HangerFlight - Snow Barrage で話してきました
■[event]DevLOVE HangerFlight - Snow Barrage で話してきました

DevLOVE HangerFlight - Snow Barrageでサービスエンジニアについて話してきました。
DevLoveさんで「泥臭い運用から、プログラマブルインフラ構築(に行きたい)」と言う題名でお話させていただきました!
DevLoveさんは、開発の人が多かったり、熱い人が多かったりするイメージでしたので正直ガクブルしていましたが、、、お話できてよかったです!
まあ、でも究極的には組織として、今足りない所を見つけてそこにどうやってコミットできるか、なのかもしれません。
自分もそこにいたるまでの道を探している途中ではありますが、何かのきっかけになれば幸いです。
そして、ぱぱんださんをはじめ他の皆さんのお話に考えさせられる事も多く、帰り道はじっくり噛み締めて帰れたと思います。
グループディスカッションや、渾身会もみなさんと交流できてとても良い時間を過ごせてよかったです。
DevLoveさんTシャツも買ってしまったりしましてすいません><
途中からスタッフになったのかと錯覚してしまいましたw
ありがとうございます!家宝に?します!w
そして
聞いていただいた方。
ついーとしていただいた方。
発表していただいた方。
そしてDevLoveスタッフの方!
本当にありがとうございました!
とげった
DevLOVE HFSB by 銀河
当日のツイートを@crea_memoさんがまとめてくださいました!ありがとうございます!
DevOpsについて
ちょっと自分のスライドに出てきたDevOpsに関しての書籍やスライドを紹介しておきます。
「ウェブオペレーション ―サイト運用管理の実践テクニック」
とても良い本だと思うので是非読んでみるとよいかとおもいます。
- 作者: John Allspaw,Jesse Robbins,角征典
- 出版社/メーカー: オライリージャパン
- 発売日: 2011/05/14
- メディア: 大型本
- 購入: 9人 クリック: 818回
- この商品を含むブログ (39件) を見る
第1回 DevOpsカンファレンス に参加&発表してきました
以前開催されたDevOpsカンファレンスの時の記事になります。
id:MIZZY(@gosukenator)さんの「10分でわかるDevOps」は本当に10分でDevOpsがわかる良い資料になっています。




終わったら :set nopaste