Skip to content

Commit

Permalink
webのリファクタ
Browse files Browse the repository at this point in the history
  • Loading branch information
kazeburo committed May 20, 2010
1 parent 82e9542 commit 7f4a2c4
Show file tree
Hide file tree
Showing 5 changed files with 364 additions and 245 deletions.
44 changes: 37 additions & 7 deletions README
Expand Up @@ -2,15 +2,45 @@ CloudForecast - server resource monitoring framework

WARNING: Alpha quality code

# schedule daemon
CF_DEBUG=1 ./cloudforecast_radar -c cloudforecast.yaml -l server_list.yaml
サーバ等のリソース監視をするためのツールです。
RRDToolの薄いラッパー、情報取得のためのフレームワークとして設計されています。
CloudForecastは、4つのプロセスによって動作します。

# fetcher worker
CF_DEBUG=1 ./cf_fetcher_worker -c cloudforecast.yaml
- 巡回デーモン
- グラフ閲覧 HTTPD
- 情報取得Gearmanワーカー
- RRDファイル更新Gearmanワーカー

小規模な監視では、Gearmanがなくても動作可能です。
動作イメージはdocsディレクトリ以下の cloudforecast.png になります

# 巡回デーモン
$ ./cloudforecast_radar -r -c cloudforecast.yaml -l server_list.yaml
- 起動すると5分ごとに巡回を行います
- -r 再起動オプション。ライブラリや設定ファイルを更新すると自動で再起動します
- -c 設定ファイル
- -c サーバ一覧

# rrd update worker
CF_DEBUG=1 ./cf_updater_worker -c cloudforecast.yaml

# web server
CF_DEBUG=1 ./cloudforecast_web -p 5000 -c cloudforecast.yaml -l server_list.yaml
$ ./cloudforecast_web -r- p 5000 -c cloudforecast.yaml -l server_list.yaml
- グラフ閲覧 HTTPD
- -p ポート httpdのport


# 情報取得Gearmanワーカー
$ ./cf_fetcher_worker -r -c cloudforecast.yaml \
-max-workers 2 -max-request-per-child 100 -max-exection-time 60
- geamarnでのリソース情報取得ワーカー
- -max-worker preforkするワーカー数
- -max-request-per-child 1ワーカープロセス処理回数。この回数を超えるとプロセスが新しく作り直される
- -max-exection-time ワーカーの1回の取得作業でこれ以上の時間かかっている場合、そのワーカーを停止します

# RRDファイル更新Gearmanワーカー
$ ./cf_updater_worker -r -c cloudforecast.yaml \
-max-workers 2 -max-request-per-child 100 -max-exection-time 60
 - gearmanでのリソース情報をrrdファイルに書き込むワーカー

#環境変数
CF_DEBUG=1 をするとdebugログが出力されます

176 changes: 17 additions & 159 deletions cloudforecast_web
Expand Up @@ -3,171 +3,29 @@
use FindBin;
use lib "$FindBin::Bin/lib";
use lib "$FindBin::Bin/site-lib";
use CloudForecast::Web -base;
use CloudForecast::ConfigLoader;
use CloudForecast::Host;
use CloudForecast::Web::Server;
use Getopt::Long;

my $root_dir = $FindBin::Bin;
my $config_yaml = $root_dir . '/cloudforecast.yaml';
my $server_list_yaml = $root_dir . '/server_list.yaml';
my $config = $root_dir . '/cloudforecast.yaml';
my $server_list = $root_dir .'/server_list.yaml';
my $restarter = 0;
my $port = 5000;

my @argv = @ARGV;
Getopt::Long::Configure("no_ignore_case", "pass_through");
GetOptions(
'c|config=s' => \$config_yaml,
'l|server-list=s' => \$server_list_yaml,
'port=s' => \$port,
'r|restarter' => \$restarter,
'c|config=s' => \$config,
'l|server-list=s' => \$server_list,
);

die 'config not found' unless $config_yaml;
die 'server_list not found' unless $server_list_yaml;
die 'config not found' unless $config;
die 'server_list not found' unless $server_list;

my $configloader = CloudForecast::ConfigLoader->new({
CloudForecast::Web::Server->new({
port => $port,
restarter => $restarter,
root_dir => $root_dir,
global_config => $config_yaml,
server_list => $server_list_yaml,
});
$configloader->load_all();

my $global_config = $configloader->global_config;
my $server_list = $configloader->server_list;
my $all_hosts = $configloader->all_hosts;

my $page_title = $server_list_yaml;
$page_title =~ s!^(.+)/!!;
$page_title =~ s!\.[^.]+$!!;

sub get_host {
my $host = shift;
my $host_instance = CloudForecast::Host->new({
address => $host->{address},
hostname => $host->{hostname},
details => $host->{details},
resources => $host->{resources},
component_config => $host->{component_config},
global_config => $global_config,
});
$host_instance;
}

get '/' => sub {
my $req = shift;
my $p = shift;
return render('index.mt');
};

get '/server' => sub {
my $req = shift;

my $address = $req->param('address');
return [ 404, [], ['Address Not Found'] ] unless $address;

my $host = $all_hosts->{$address};
return [ 404, [], ['Host Not Found'] ] unless $host;

my $host_instance = get_host($host);
my @graph_list = $host_instance->list_graph;

return render('server.mt');
};

get '/graph' => sub {
my $req = shift;

my $address = $req->param('address');
return [ 404, [], ['Address Not Found'] ] unless $address;
my $resource = $req->param('resource');
return [ 404, [], ['Resource Not Found'] ] unless $resource;
my $key = $req->param('key');
return [ 404, [], ['Graph type key Not Found'] ] unless $key;

my $span = $req->param('span') || 'd';
my $host = $all_hosts->{$address};
return [ 404, [], ['Host Not Found'] ] unless $host;

my $host_instance = get_host($host);
my ($img,$err) = $host_instance->draw_graph($resource,$key, $span);

return [ 500, [], ['Internal Server Error', $err] ] unless $img;
return [ 200, ['Content-Type','image/png'], [$img] ];
};

get '/default.css' => sub {
my $req = shift;
return [ 200, ['Content-Type','text/css'], [render('css.mt')] ];
};


run_server(@argv);

__DATA__
@@ index.mt
<html>
<head>
<title>CloudForecast Server List</title>
<link rel="stylesheet" type="text/css" href="/default.css" />
</head>
<body>
<h1 class="title"><?= $page_title ?> </h1>
<ul>
<? my $i=0 ?>
<? for my $server ( @$server_list ) { ?>
<li><a href="#group-<?= $i ?>"><?= $server->{title} ?></a></li>
<? $i++ } ?>
</ul>
<hr>
<ul>
<? my $k=0 ?>
<? for my $server ( @$server_list ) { ?>
<li id="group-<?= $k ?>"><?= $server->{title} ?></li>
<ul>
<? for my $host ( @{$server->{hosts}} ) { ?>
<li><a href="/server?address=<?= $host->{address} ?>"><?= $host->{address} ?></a> <strong><?= $host->{hostname} ?></strong> <span class="details"><?= $host->{details} ?></a></li>
<? } ?>
</ul>
<? $k++ } ?>
</ul>
</body>
</html>
@@ server.mt
<html>
<head>
<title>CloudForecast Server List</title>
<link rel="stylesheet" type="text/css" href="/default.css" />
</head>
<body>
<h1 class="title"><?= $page_title ?> </h1>
<h2><span class="address"><?= $host->{address} ?></span> <strong><?= $host->{hostname} ?></strong> <span class="details"><?= $host->{details} ?></a></h2>
<? for my $resource ( @graph_list ) { ?>
<h4><?= $resource->{resource_class} ?></h4>
<? for my $graph ( @{$resource->{graphs}} ) { ?>
<nobr />
<? for my $term ( qw/d w m y/ ) { ?>
<img src="/graph?span=<?= $term ?>&amp;address=<?= $host->{address} ?>&amp;resource=<?= $resource->{resource} ?>&amp;key=<?= $graph ?>" />
<? } ?>
<br />
<? } ?>
<? } ?>
</body>
</html>
@@ css.mt
a { color: #5555cc;}
a:link { color: #5555cc;}
a:visited { color: #555599;}
a:active { color: #999999; }
a:hover { color: #999999; }
ol, ul{
list-style-position:inside;
}
global_config => $config,
server_list => $server_list,
})->run;
Binary file added docs/cloudforecast.png
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.

0 comments on commit 7f4a2c4

Please sign in to comment.