| 1 | package MYDan::MonitorV2::Collector; | ||
| 2 | |||
| 3 | =head1 NAME | ||
| 4 | |||
| 5 | MYDan::MonitorV2::Collector | ||
| 6 | |||
| 7 | =head1 SYNOPSIS | ||
| 8 | |||
| 9 | use MYDan::MonitorV2::Collector; | ||
| 10 | my $collector = MYDan::MonitorV2::Collector->new( %o, name => shift ); | ||
| 11 | $collector->run(); | ||
| 12 | |||
| 13 | =cut | ||
| 14 | use strict; | ||
| 15 | use warnings; | ||
| 16 | |||
| 17 | use Carp; | ||
| 18 | use YAML::XS; | ||
| 19 | use Time::HiRes qw(time); | ||
| 20 | use FindBin qw( $RealBin ); | ||
| 21 | use MYDan::Node; | ||
| 22 | use MYDan::Subscribe; | ||
| 23 | use MYDan::Util::OptConf; | ||
| 24 | |||
| 25 | sub new | ||
| 26 | { | ||
| 27 | my ( $class, %self ) = @_; | ||
| 28 | |||
| 29 | map{ die "$_ undef" unless $self{$_} }qw( conf code name run ); | ||
| 30 | $0 = "MYDan.monitorv2.collector.$self{name}"; | ||
| 31 | |||
| 32 | $self{tmp} = "$self{run}/$self{name}"; | ||
| 33 | $self{config} = eval{ YAML::XS::LoadFile "$self{conf}/$self{name}" }; | ||
| 34 | die "load fail:$@" if $@; | ||
| 35 | |||
| 36 | map{ die "$_ undef" unless $self{config}{$_} }qw( code interval target param ); | ||
| 37 | $self{code} = do "$self{code}/$self{config}{code}"; | ||
| 38 | die "load code fail" unless $self{code} && ref $self{code} eq 'CODE'; | ||
| 39 | |||
| 40 | bless \%self, ref $class || $class; | ||
| 41 | } | ||
| 42 | |||
| 43 | sub run | ||
| 44 | { | ||
| 45 | my ( $this, %ERR ) = shift; | ||
| 46 | print "start..\n"; | ||
| 47 | |||
| 48 | |||
| 49 | while( 1 ) | ||
| 50 | { | ||
| 51 | my $time = time; | ||
| 52 | my ( $config, $code, $conf, $name, $tmp ) = @$this{qw( config code conf name tmp )}; | ||
| 53 | |||
| 54 | print "check config: begin.\n"; | ||
| 55 | my $newconfig = eval{ YAML::XS::LoadFile "$conf/$name" }; | ||
| 56 | die "load config $conf/$name: $@\n" if $@; | ||
| 57 | die "config has been updated. exit.\n" | ||
| 58 | unless ( YAML::XS::Dump $newconfig ) eq ( YAML::XS::Dump $config ); | ||
| 59 | print "check config: done.\n"; | ||
| 60 | |||
| 61 | my $option = MYDan::Util::OptConf->load(); | ||
| 62 | my $range = MYDan::Node->new( $option->dump( 'range' ) ); | ||
| 63 | my $cache = MYDan::Node::DBI::Cache->new( $option->{range}{cache} ); | ||
| 64 | |||
| 65 | print "batch: begin.\n"; | ||
| 66 | my @node = $range->load( $config->{target} )->list; | ||
| 67 | print "batch: done.\n"; | ||
| 68 | |||
| 69 | |||
| 70 | print "collector: begin.\n"; | ||
| 71 | my %err = @node | ||
| 72 | ? &$code( %{$config->{param}}, node => \@node ) | ||
| 73 | : ( 'mydan.monitorv2###sys###err' => 'NoNode' ); | ||
| 74 | print "collector: done.\n"; | ||
| 75 | |||
| 76 | print "analysis: begin.\n"; | ||
| 77 | my %allkey = ( %err, %ERR ); | ||
| 78 | |||
| 79 | for my $key ( keys %allkey ) | ||
| 80 | { | ||
| 81 | $ERR{$key} = [] unless defined $ERR{$key}; | ||
| 82 | unshift @{$ERR{$key}}, $err{$key} ? 1 : 0; | ||
| 83 | } | ||
| 84 | |||
| 85 | for my $key ( keys %ERR ) | ||
| 86 | { | ||
| 87 | pop( @{$ERR{$key}} ) if scalar( @{$ERR{$key}} ) >= 11; | ||
| 88 | delete $ERR{$key} unless grep{ $_ }@{$ERR{$key}}; | ||
| 89 | } | ||
| 90 | |||
| 91 | my ( %hostc, %count ); | ||
| 92 | map{ | ||
| 93 | $hostc{$_->[2]}{$_->[0]}{$_->[1]} = 1; | ||
| 94 | $count{$_->[0]}{$_->[1]}{$_->[2]} = 1; | ||
| 95 | }$cache->select( '*' ); | ||
| 96 | |||
| 97 | my ( %currerror, %temp ); | ||
| 98 | for my $key ( keys %ERR ) | ||
| 99 | { | ||
| 100 | my ( $node, $group, $test ) = split '###', $key, 3; | ||
| 101 | |||
| 102 | if( $key =~ / (\d+)\/(\d+)$/ ) | ||
| 103 | { | ||
| 104 | my ( $a, $b ) = ( $1, $2 ); | ||
| 105 | $b = 10 if $b >= 10; | ||
| 106 | my $count = scalar grep{ $ERR{$key}[$_] } 0.. $b - 1; | ||
| 107 | $currerror{$key}++ if $count >= $a; | ||
| 108 | } | ||
| 109 | else | ||
| 110 | { | ||
| 111 | $currerror{$key}++; | ||
| 112 | } | ||
| 113 | } | ||
| 114 | |||
| 115 | for my $key ( keys %currerror ) | ||
| 116 | { | ||
| 117 | my ( $node, $group, $test ) = split '###', $key, 3; | ||
| 118 | unless( $hostc{$node} ) | ||
| 119 | { | ||
| 120 | $temp{$group}{$test}{'null'}{'null'}{$node} ++; | ||
| 121 | next; | ||
| 122 | } | ||
| 123 | for my $name ( keys %{$hostc{$node}} ) | ||
| 124 | { | ||
| 125 | map{ | ||
| 126 | $temp{$group}{$test}{$name}{$_}{$node} ++; | ||
| 127 | }keys %{$hostc{$node}{$name}} | ||
| 128 | } | ||
| 129 | } | ||
| 130 | |||
| 131 | my $subscribe = MYDan::Subscribe->new(); | ||
| 132 | for my $group ( keys %temp ) | ||
| 133 | { | ||
| 134 | for my $err ( keys %{$temp{$group}} ) | ||
| 135 | { | ||
| 136 | for my $name ( keys %{$temp{$group}{$err}} ) | ||
| 137 | { | ||
| 138 | for my $attr ( keys %{$temp{$group}{$err}{$name}} ) | ||
| 139 | { | ||
| 140 | my @errhost = keys %{$temp{$group}{$err}{$name}{$attr}}; | ||
| 141 | my @attrhost = ( $name eq 'null' && $attr eq 'null' ) | ||
| 142 | ? ( @errhost ) : ( keys %{$count{$name}{$attr}} ); | ||
| 143 | my $scale = sprintf "%d/%d", scalar( @errhost ), scalar( @attrhost ); | ||
| 144 | |||
| 145 | $subscribe->input( "name:$name attr:$attr scale:($scale) strategy: $err node:" | ||
| 146 | .join( ',', sort @errhost ), $name, $attr ); | ||
| 147 | } | ||
| 148 | } | ||
| 149 | } | ||
| 150 | } | ||
| 151 | |||
| 152 | print "dump: begin.\n"; | ||
| 153 | eval{ YAML::XS::DumpFile $tmp, \%err }; | ||
| 154 | print "Dump Fail: $@\n" if $@; | ||
| 155 | print YAML::XS::Dump \%err; | ||
| 156 | print "dump: done.\n"; | ||
| 157 | |||
| 158 | my $due = $time + $config->{interval} - time; | ||
| 159 | sleep $due if $due > 0; | ||
| 160 | } | ||
| 161 | } | ||
| 162 | |||
| 163 | 1; |