Twitterで見かけたのですが、JVMのモニタリング・・・というかJMX + 便利コマンド各種をSSHとかTelnet経由で使えるようにlistenしてくれる面白いツールがありました。 - CRaSH the shell for the Java Platform -- http://www.crashub.org/ ソースコードはGitHubで、リリースファイルのダウンロードはGoogleCode使ってるようです。 - https://github.com/crashub - https://code.google.com/p/crsh/ 公式の"Demo"からWeb上で試せるんですが、スレッドの一覧とか、スレッドにinterrupt送れたりと、スレッドが固まってしまった場合にインタラクティブに制御出来ます。実運用環境で利用できる代物かどうかは未知数ですが、アイデアとしては非常に面白いです。VisualVMのPluginとしても利用できるようです。Groovyでカスタムコマンドを作成出来るというのも面白そうですね。 今日は crash-1.2.0-war.tar.gz をDLして、Win上のTomcat6にデプロイしてみました。 tarボールを解凍すると crash.war が出てきますので、Tomcat6の /webapps/ 以下に配置してTomcatを起動します。 こんな感じで配備ログが出て来ました。 #pre||> 情報: Webアプリケーションディレクトリ crash を配備します 3 30, 2013 10:55:15 午後 org.crsh.plugin.PluginLifeCycle configureProperty 情報: Configuring property vfs.refresh_period=1 from properties ... 3 30, 2013 10:55:15 午後 org.crsh.plugin.PluginLifeCycle configureProperty 情報: Configuring property ssh.port=2000 from properties ... 3 30, 2013 10:55:15 午後 org.crsh.ssh.SSHPlugin init 情報: Booting SSHD .... 3 30, 2013 10:55:15 午後 org.crsh.ssh.term.SSHLifeCycle doInit 情報: About to start CRaSSHD 3 30, 2013 10:55:16 午後 org.crsh.ssh.term.SSHLifeCycle doInit 情報: CRaSSHD started on port 2000 ... ||< どうやらポート2000でSSHで接続できるようです。TomcatのJVMにSSHで接続できるなんて、新鮮ですね。 ログインするときのユーザ名とパスワードですが、warが展開されたあとの "/WEB-INF/crash/crash.properties" ファイルで指定します。1.2.0-warでのデフォルトは以下のようになっていて、"admin"ユーザ、パスワード"admin"でログインできます。 crash/WEB-INF/crash/crash.properties: #pre||> # SSH configuration crash.ssh.port=2000 #crash.ssh.keypath=/path/to/the/key/file # Telnet configuration crash.telnet.port=5000 # Authentication configuration crash.auth=simple crash.auth.simple.username=admin crash.auth.simple.password=admin ||< 実際にSSHクライアントを起動し、接続してみます。 #pre||> [user@LB-L400B-120908 ~]$ ssh -p 2000 admin@localhost admin@localhost's password: ______ .~ ~. |`````````, .'. ..'''' | | | |'''|''''' .''```. .'' |_________| | | `. .' `. ..' | | `.______.' | `. .' `. ....'' | | 1.2.0 Follow and support the project on http://www.crashub.org Welcome to LB-L400B-120908 + ! It is Sat Mar 30 22:55:23 JST 2013 now % ||< "help"と入力するとコマンドのヘルプが見れます。 #pre||> % help Try one of these commands with the -h or --help switch: NAME DESCRIPTION dashboard env display the term env filter A filter for a stream of map help provides basic help java various java language commands jdbc JDBC connection jmx Java Management Extensions jndi Java Naming and Directory Interface jpa Java persistance API jvm JVM informations log logging commands man format and display the on-line manual pages shell shell related command sleep sleep for some time sort Sort a map system vm system properties commands thread JVM thread commands ||< 各コマンドでも"-h"や"help"でコマンドごとのヘルプを表示できます。 #pre||> % thread help usage: thread[-h | --help] COMMAND [ARGS] The most commonly used thread commands are: interrupt interrupt vm threads stop stop vm threads top thread top ls list the vm threads dump dump vm threads produce produces threads for pipe ||< "thread top"とかすると、TOPで3秒間隔くらいで更新されました。Ctrl-Cで戻れるとか、これがJavaで動いているのかとびっくりします。"thread dump"の使い方がちょっと分からない・・・。 Groovy読める人は、"/WEB-INF/crash/commands/base" の下に各コマンドのGroovyソースコードが置いてあるので、それ読むと良いかもしれません。 あと便利なことに、CRaSHのコマンドラインってTAB補完が効くんですよね。上下カーソルでコマンド履歴もアクセスできます。 まるでGNU readlineのようなのですが、依存ライブラリをチラ見したら "jline" とかいうjarが入ってました。 - JLine - JLine -- http://jline.sourceforge.net/ - jline/jline2 · GitHub -- https://github.com/jline/jline2 やっぱりreadlineみたいな機能をJavaで実現するためのライブラリのようですが、これもまた面白そうですね。 以上、CRaSHの紹介でした。