| 1 | package MYDan::Monitor::Make; | ||
| 2 | use strict; | ||
| 3 | use warnings; | ||
| 4 | use Carp; | ||
| 5 | use YAML::XS; | ||
| 6 | |||
| 7 | use File::Basename; | ||
| 8 | use POSIX qw( :sys_wait_h ); | ||
| 9 | use Data::Dumper; | ||
| 10 | use MYDan::Node; | ||
| 11 | use MYDan::Subscribe; | ||
| 12 | |||
| 13 | our %c = ( code => 'stat', timeout => 60, interval => 60 ); | ||
| 14 | |||
| 15 | sub new | ||
| 16 | { | ||
| 17 | my ( $class, %this ) = @_; | ||
| 18 | map{ | ||
| 19 | die "$_ undef." unless $this{$_}; | ||
| 20 | system( "mkdir -p '$this{$_}'" ) unless -e $this{$_}; | ||
| 21 | }qw( make conf ); | ||
| 22 | |||
| 23 | $this{subscribe} = MYDan::Subscribe->new(); | ||
| 24 | |||
| 25 | bless \%this, ref $class || $class; | ||
| 26 | } | ||
| 27 | |||
| 28 | sub make | ||
| 29 | { | ||
| 30 | my ( $this, @node, %skip, %node, %collect ) = @_; | ||
| 31 | my ( $make, $conf, $subscribe, $option ) = @$this{qw( make conf subscribe option )}; | ||
| 32 | |||
| 33 | |||
| 34 | my %n = map{ $_ => 1 }@node; | ||
| 35 | for my $file ( glob "$conf/collect/*" ) | ||
| 36 | { | ||
| 37 | my $name = basename $file; | ||
| 38 | |||
| 39 | $collect{$name} = eval{ YAML::XS::LoadFile $file }; | ||
| 40 | |||
| 41 | if( $@ ) | ||
| 42 | { | ||
| 43 | my $project = ( $name =~ /:(.+)$/ ) ? $1 : $name; | ||
| 44 | $skip{$project} = 1; | ||
| 45 | $subscribe->input( $project, 'error', "laod $name error" ); | ||
| 46 | } | ||
| 47 | } | ||
| 48 | |||
| 49 | |||
| 50 | for ( MYDan::Node->new( $option->dump( 'range' ) )->db()->select( 'name,attr,node,info' ) ) | ||
| 51 | { | ||
| 52 | my ( $name, $attr, $node, $info ) = @$_; | ||
| 53 | $node{$node}{$name}{$attr} = 1 | ||
| 54 | } | ||
| 55 | |||
| 56 | for my $node ( keys %node ) | ||
| 57 | { | ||
| 58 | next unless $n{$node}; | ||
| 59 | my @name = sort keys %{$node{$node}}; | ||
| 60 | next if grep{ $skip{$_} }@name; | ||
| 61 | |||
| 62 | my %config; | ||
| 63 | for my $name ( @name ) | ||
| 64 | { | ||
| 65 | %config = ( %config, %{$collect{$name}} ) if $collect{$name}; | ||
| 66 | map{ %config = ( %config, %{$collect{"$name:$_"}} ) if $collect{"$name:$_"} } | ||
| 67 | sort keys %{$node{$node}{$name}} | ||
| 68 | } | ||
| 69 | eval{ YAML::XS::DumpFile "$make/$node", | ||
| 70 | +{ | ||
| 71 | conf => +{ | ||
| 72 | stat => +{ | ||
| 73 | code => 'stat', | ||
| 74 | timeout => 60, | ||
| 75 | interval=> 60, | ||
| 76 | param =>+{ test => \%config } | ||
| 77 | } | ||
| 78 | }, | ||
| 79 | target => $node | ||
| 80 | } | ||
| 81 | }; | ||
| 82 | if( $@ ) | ||
| 83 | { | ||
| 84 | map{ $subscribe->input( $_, 'error', "dump $node error" ); }@name; | ||
| 85 | } | ||
| 86 | } | ||
| 87 | |||
| 88 | } | ||
| 89 | |||
| 90 | 1; |