|
| 1 | +#!/usr/bin/perl |
| 2 | + |
| 3 | +use FindBin; |
| 4 | +use lib "$FindBin::Bin/lib"; |
| 5 | +use lib "$FindBin::Bin/site-lib"; |
| 6 | +use CloudForecast::Web -base; |
| 7 | +use CloudForecast::ConfigLoader; |
| 8 | +use CloudForecast::Host; |
| 9 | +use Getopt::Long; |
| 10 | + |
| 11 | +my $root_dir = $FindBin::Bin; |
| 12 | +my $config_yaml = $root_dir . '/cloudforecast.yaml'; |
| 13 | +my $server_list_yaml = $root_dir . '/server_list.yaml'; |
| 14 | + |
| 15 | +my @argv = @ARGV; |
| 16 | +Getopt::Long::Configure("no_ignore_case", "pass_through"); |
| 17 | +GetOptions( |
| 18 | + 'c|config=s' => \$config_yaml, |
| 19 | + 'l|server-list=s' => \$server_list_yaml, |
| 20 | +); |
| 21 | + |
| 22 | +die 'config not found' unless $config_yaml; |
| 23 | +die 'server_list not found' unless $server_list_yaml; |
| 24 | + |
| 25 | +my $configloader = CloudForecast::ConfigLoader->new({ |
| 26 | + root_dir => $root_dir, |
| 27 | + global_config => $config_yaml, |
| 28 | + server_list => $server_list_yaml, |
| 29 | +}); |
| 30 | +$configloader->load_all(); |
| 31 | + |
| 32 | +my $global_config = $configloader->global_config; |
| 33 | +my $server_list = $configloader->server_list; |
| 34 | +my $all_hosts = $configloader->all_hosts; |
| 35 | + |
| 36 | +my $page_title = $server_list_yaml; |
| 37 | +$page_title =~ s!^(.+)/!!; |
| 38 | +$page_title =~ s!\.[^.]+$!!; |
| 39 | + |
| 40 | +sub get_host { |
| 41 | + my $host = shift; |
| 42 | + my $host_instance = CloudForecast::Host->new({ |
| 43 | + address => $host->{address}, |
| 44 | + hostname => $host->{hostname}, |
| 45 | + details => $host->{details}, |
| 46 | + resources => $host->{resources}, |
| 47 | + component_config => $host->{component_config}, |
| 48 | + global_config => $global_config, |
| 49 | + }); |
| 50 | + $host_instance; |
| 51 | +} |
| 52 | + |
| 53 | +get '/' => sub { |
| 54 | + my $req = shift; |
| 55 | + my $p = shift; |
| 56 | + return render('index.mt'); |
| 57 | +}; |
| 58 | + |
| 59 | +get '/server' => sub { |
| 60 | + my $req = shift; |
| 61 | + |
| 62 | + my $address = $req->param('address'); |
| 63 | + return [ 404, [], ['Address Not Found'] ] unless $address; |
| 64 | + |
| 65 | + my $host = $all_hosts->{$address}; |
| 66 | + return [ 404, [], ['Host Not Found'] ] unless $host; |
| 67 | + |
| 68 | + my $host_instance = get_host($host); |
| 69 | + my @graph_list = $host_instance->list_graph; |
| 70 | + |
| 71 | + return render('server.mt'); |
| 72 | +}; |
| 73 | + |
| 74 | +get '/graph' => sub { |
| 75 | + my $req = shift; |
| 76 | + |
| 77 | + my $address = $req->param('address'); |
| 78 | + return [ 404, [], ['Address Not Found'] ] unless $address; |
| 79 | + my $resource = $req->param('resource'); |
| 80 | + return [ 404, [], ['Resource Not Found'] ] unless $resource; |
| 81 | + my $key = $req->param('key'); |
| 82 | + return [ 404, [], ['Graph type key Not Found'] ] unless $key; |
| 83 | + |
| 84 | + my $span = $req->param('span') || 'd'; |
| 85 | + my $host = $all_hosts->{$address}; |
| 86 | + return [ 404, [], ['Host Not Found'] ] unless $host; |
| 87 | + |
| 88 | + my $host_instance = get_host($host); |
| 89 | + my ($img,$err) = $host_instance->draw_graph($resource,$key, $span); |
| 90 | + |
| 91 | + return [ 500, [], ['Internal Server Error', $err] ] unless $img; |
| 92 | + return [ 200, ['Content-Type','image/png'], [$img] ]; |
| 93 | +}; |
| 94 | + |
| 95 | +get '/default.css' => sub { |
| 96 | + my $req = shift; |
| 97 | + return [ 200, ['Content-Type','text/css'], [render('css.mt')] ]; |
| 98 | +}; |
| 99 | + |
| 100 | + |
| 101 | +run_server(@argv); |
| 102 | + |
| 103 | +__DATA__ |
| 104 | +@@ index.mt |
| 105 | +<html> |
| 106 | +<head> |
| 107 | +<title>CloudForecast Server List</title> |
| 108 | +<link rel="stylesheet" type="text/css" href="/default.css" /> |
| 109 | +</head> |
| 110 | +<body> |
| 111 | +<h1 class="title"><?= $page_title ?> </h1> |
| 112 | +
|
| 113 | +<ul> |
| 114 | +<? my $i=0 ?> |
| 115 | +<? for my $server ( @$server_list ) { ?> |
| 116 | +<li><a href="#group-<?= $i ?>"><?= $server->{title} ?></a></li> |
| 117 | +<? $i++ } ?> |
| 118 | +</ul> |
| 119 | +
|
| 120 | +<hr> |
| 121 | +
|
| 122 | +<ul> |
| 123 | +<? my $k=0 ?> |
| 124 | +<? for my $server ( @$server_list ) { ?> |
| 125 | +<li id="group-<?= $k ?>"><?= $server->{title} ?></li> |
| 126 | +<ul> |
| 127 | + <? for my $host ( @{$server->{hosts}} ) { ?> |
| 128 | + <li><a href="/server?address=<?= $host->{address} ?>"><?= $host->{address} ?></a> <strong><?= $host->{hostname} ?></strong> <span class="details"><?= $host->{details} ?></a></li> |
| 129 | + <? } ?> |
| 130 | +</ul> |
| 131 | +<? $k++ } ?> |
| 132 | +</ul> |
| 133 | +
|
| 134 | +</body> |
| 135 | +</html> |
| 136 | +
|
| 137 | +@@ server.mt |
| 138 | +<html> |
| 139 | +<head> |
| 140 | +<title>CloudForecast Server List</title> |
| 141 | +<link rel="stylesheet" type="text/css" href="/default.css" /> |
| 142 | +</head> |
| 143 | +<body> |
| 144 | +<h1 class="title"><?= $page_title ?> </h1> |
| 145 | +<h2><span class="address"><?= $host->{address} ?></span> <strong><?= $host->{hostname} ?></strong> <span class="details"><?= $host->{details} ?></a></h2> |
| 146 | +
|
| 147 | +<? for my $resource ( @graph_list ) { ?> |
| 148 | +<h4><?= $resource->{resource_class} ?></h4> |
| 149 | +<? for my $graph ( @{$resource->{graphs}} ) { ?> |
| 150 | +<nobr /> |
| 151 | +<? for my $term ( qw/d w m y/ ) { ?> |
| 152 | +<img src="/graph?span=<?= $term ?>&address=<?= $host->{address} ?>&resource=<?= $resource->{resource} ?>&key=<?= $graph ?>" /> |
| 153 | +<? } ?> |
| 154 | +<br /> |
| 155 | +<? } ?> |
| 156 | +<? } ?> |
| 157 | +
|
| 158 | +</body> |
| 159 | +</html> |
| 160 | +
|
| 161 | +
|
| 162 | +@@ css.mt |
| 163 | +
|
| 164 | +a { color: #5555cc;} |
| 165 | +a:link { color: #5555cc;} |
| 166 | +a:visited { color: #555599;} |
| 167 | +a:active { color: #999999; } |
| 168 | +a:hover { color: #999999; } |
| 169 | +
|
| 170 | +ol, ul{ |
| 171 | + list-style-position:inside; |
| 172 | +} |
| 173 | +
|
0 commit comments