最近一直在看dancer,发现真心好用,之前在看mojo,搞了好久没有搞懂.只能怪自己太挫了.
作为练手就写了一套简易的监控系统,主要是利用net::ssh2模块监控AIX服务器的CPU,IO负载,还有磁盘空间.当然如果你是linux/unix作为监控服务器简易使用net::ssh:perl 或者使用SNMP来获取服务器信息.
在lib下添加
login.pm
package login; use Dancer ':syntax'; use Net::SSH2; use Data::Dumper; $| = 1; prefix '/monitor'; my $envMon = { NST => { '192.168.1.100' => [ 'user', 'passwd' ], '192.168.1.101' => [ 'user', 'passwd' ], '192.168.1.102' => [ 'user', 'passwd' ], }, UAT => { '192.168.1.103' => [ 'user', 'passwd' ], '192.168.1.104' => [ 'user', 'passwd' ], } }; sub mon { my ( $host, $user, $passwd, $env ) = @_; my @monArr; my $monOut; my $ssh2 = Net::SSH2->new(); my $row; $ssh2->connect("$host") or die "$!"; if ( $ssh2->auth_password( "$user", "$passwd" ) ) { my $chan = $ssh2->channel(); # $chan->blocking(1); $chan->shell(); #monitor memory useage 这款是监控内存,因为linux/unix内存机制都是有多少用多少,所以就去掉了 # print $chan "svmon -G\n"; # # while(<$chan>){ # if(/^memory\s+([\d.]+)\s+([\d.]+)\s+([\d.]+)/){ # push @{$monOut->{'memory'}},sprintf"%0.2f",$2/$1*100; # } #} #monitor disk data print $chan "df -g\n"; while (<$chan>) { if (/^\S+\s+([\d.]+)\s+([\d.]+)\s+(\d+)%\s+[^\/]+(\S+)$/) { # push @monArr, $3, $1, $2; if ( $3 > 80 ) { push @{ $monOut->{'disk'} }, $4, $1, $2, ''; ++$row; } #以下是根据自己需要,如果想监控所有硬盘就去掉注释,我这块只监控大于80%已用空间 #else{ # push @{$monOut->{'disk'}},$4,$1,$2, # '' . $3 . '%'; #} } } unless ( defined @{ $monOut->{'disk'} } ) { push @{ $monOut->{'disk'} }, 'normal', 'normal', 'normal', 'normal'; $row = 1; } # monitor cpu data print $chan "iostat\n"; while (<$chan>) { if (/^\s+\S+\s+\S+\s+(\S+)\s+(\S+)\s+(\S+)\s+([\d.]+)/) { push @{ $monOut->{'cpu'} }, $1, $2, $3, $4; } } $monOut->{'ip'} = $host; $monOut->{'env'} = $env; $monOut->{'row'} = $row; } $monOut; } get '/login' => sub { template 'login' => { table => 'login' }; }; #登陆页面,这里直接用明文了,根据自己需要可以加密为md5,当然也可以保存在数据库中 post '/login' => sub { if ( params->{username} eq 'test' && params->{password} eq 'test' ) { session user => params->{username}; redirect params->{path}; } else { redirect '/monitor/login'; } }; get '/logout' => sub { session->destroy; redirect '/monitor/login'; }; get '/:env' => sub { if ( session('user') ) { my $monitorOut; my $env = params->{env}; $env = uc($env); if ( defined $envMon->{$env} ) { for ( keys %{ $envMon->{$env} } ) { push @$monitorOut, mon( $_, $envMon->{$env}->{$_}[0], $envMon->{$env}->{$_}[1], $env ); } template 'monitor' => { table => $monitorOut }; #template 'monitor' => {table => '2'}; } } else { template 'monitor' => { table => 'Please Login!' }; } }; get '/', sub { if ( session('user') ) { template 'monitor' => {}; } else { template 'monitor' => { table => 'Please Login!' }; } }; 1;'.$3.'%
在public/css/style.css最后添加
.jindu_g{position: relative;width:200px;border: 1px solid #B1D632;padding: 1px;} .jindu_g div{display: block;position: relative; background: green; color: #333333; height: 20px;line-height: 20px; } .jindu_g div span{position: absolute;width: 200px; text-align: center;font-weight: bold; } .jindu_r{position: relative;width:200px;border: 1px solid #B1D632;padding: 1px;} .jindu_r div{display: block;position: relative; background: red; color: #333333; height: 20px;line-height: 20px; } .jindu_r div span{position: absolute;width: 200px; text-align: center;font-weight: bold; }
呈现的效果为磁盘使用的进度条.
views/monitor.tt
<% IF session.user%><%ELSE%>
环境 IP CPU监控 磁盘空间 <% FOREACH k IN table %> user sys idle iowait 挂载目录 totle Free %Used <%END%> <%END%> <%END%> "> <%k.env%> " > <%k.ip%> <% FOREACH memory IN k.memory%>"> <%memory%>% <%END%> <% FOREACH cpu IN k.cpu%>"> <%cpu%> <%END%> <% FOREACH fdisk IN k.disk%><%fdisk%> <% IF loop.count % 4 == 0 %><%table%> <%END%>
输出页面
源码在附件中,中间去除了敏感信息,可能运行会有问题,只给大家提供一个思路,有什么问题可以MAIL或Q讨论