2011年11月10日木曜日

実験くん(jsPlumbでAWSの構成を可視化)

jsPlumbという、HTMLのエレメント同士をコネクタで接続できるライブラリがあります。

このライブラリはjQueryのプラグインとしてもリリースしているので、これを使用してEC2とセキュリティグループの関係を接続図としてHTMLレンダリングするように実験してみました。 


EC2とセキュリティグループの情報をjsonで出力するPHPを以下のように用意します。


jsPlumbはGoogleCodeでホストされているので、以下からダウンロードします。

jsPlumb
http://code.google.com/p/jsplumb/downloads/list

次に、htmlページを用意し、phpからjsonを取得し、jsplumbに適用させます。
<html>
<head>
<title>awsplug</title>
<meta http-equiv="content-type" content="text/html;charset=utf-8">
<style type="text/css">
#stage{
padding: 20px;
}
.component {
opacity:0.8;
filter:alpha(opacity=80);
background-color:white;
cursor:pointer;
position:absolute;
border:2px solid #346789;
width:12em;
height:10em;
z-index:20;
font-size:0.9em;
font-weight:bold;
font-family:helvetica;
padding:0.5em;
/*margin:1em;*/
}
.ec2 {background-color: #ffe0e0;}
.sg {background-color: #e0e0ff;}
</style>
</head>
<body>
<div id="stage" style="position:absolute">
</div>
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.6.0/jquery.min.js"></script>
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jqueryui/1.8.13/jquery-ui.min.js"></script>
<script type="text/javascript" src="jquery.jsPlumb-1.3.3-all-min.js "></script>
<script type="text/javascript">
;(function(){
window.awsplug = {
init : function(json){
jsPlumb.DefaultDragOptions = { cursor: "pointer", zIndex:2000 };
jsPlumb.setMouseEventsEnabled(true);
var instance = [];
for(var i=0; i<json.sg.item.length;i++){
var groupId = json.sg.item[i].groupId;
$("#stage").append("<div id=\"" + groupId + "\"></div>");
$("#" + groupId ).attr("class", "sg component")
.css("top", "30em")
.css("left", 200*i + 20)
.append(json.sg.item[i].groupName);
}
for(var i=0; i<json.ec2.item.length;i++){
instance[i] = json.ec2.item[i].instancesSet.item;
var instanceId = instance[i].instanceId;
$("#stage").append("<div id=\"" + instanceId + "\"></div>");
$("#" + instanceId).attr("class", "ec2 component")
.append(instanceId)
.css("top", "10em")
.css("left", 200*i + 20);
var groups = instance[i].groupSet.item;
groups = groups instanceof Array ? groups : new Array(groups);
for(var j=0; j<groups.length;j++){
jsPlumb.connect({source:instanceId,
target:groups[j].groupId,
connector:"Flowchart",
anchors:["Center", "Center"] });
}
}
}
};
})();
jsPlumb.ready(function() {
document.onselectstart = function () { return false; };
$.getJSON("/awsplug/awsplug.php", function(json){
awsplug.init(json);
});
});
</script>
</body>
</html>
view raw index.html hosted with ❤ by GitHub

これだけで終わりです。

さっそく実行してみます。

おおー!
うまく表示されました!

はぐれセキュリティグループが一目でわかりますね。


jsplumbはjQueryUI.Draggableをサポートしているので、コネクタで接続されたエンドポイント(ノード)はドラッグで移動することも出来ます。



ELBの接続状況など、いろいろなことが可視化できそうです。

以上です。