| 1 | package MYDan::Collector::Push; | ||
| 2 | use strict; | ||
| 3 | use warnings; | ||
| 4 | |||
| 5 | use Carp; | ||
| 6 | use YAML::XS; | ||
| 7 | |||
| 8 | use Sys::Hostname; | ||
| 9 | use LWP::UserAgent; | ||
| 10 | |||
| 11 | sub new | ||
| 12 | { | ||
| 13 | my ( $class, %this ) = @_; | ||
| 14 | |||
| 15 | map{ confess "$_ undef\n" unless $this{$_} }qw( addr form ); | ||
| 16 | my $form = $this{form}; | ||
| 17 | confess "form no HASH" unless ref $form eq 'HASH'; | ||
| 18 | |||
| 19 | map{ | ||
| 20 | my $t = $_; | ||
| 21 | map{ $this{'index'}{$_} = $t if $form->{$t} eq "\$$_" }qw( node data ); | ||
| 22 | }keys %$form; | ||
| 23 | |||
| 24 | map{ confess "$_ undef\n" unless $this{'index'}{$_} }qw( node data ); | ||
| 25 | |||
| 26 | $this{form}{$this{'index'}{node}} = $ENV{nshostname} || hostname; | ||
| 27 | |||
| 28 | $this{ua} = my $ua = LWP::UserAgent->new(); | ||
| 29 | $ua->proxy( @{$this{proxy}} ) if $this{proxy}; | ||
| 30 | $ua->agent('Mozilla/9 [en] (Centos; Linux)'); | ||
| 31 | $ua->timeout( 10 ); | ||
| 32 | $ua->default_header ( 'Cache-control' => 'no-cache', 'Pragma' => 'no-cache' ); | ||
| 33 | |||
| 34 | map{ $this{load}{$_} = 1 } @{$this{'keys'}} if $this{'keys'}; | ||
| 35 | |||
| 36 | return bless \%this, ref $class || $class; | ||
| 37 | } | ||
| 38 | |||
| 39 | sub push | ||
| 40 | { | ||
| 41 | my ( $this, $data, @data ) = splice @_, 0, 2; | ||
| 42 | |||
| 43 | my ( $ua, $addr, $form, $load, $index ) = @$this{ qw( ua addr form load index ) }; | ||
| 44 | |||
| 45 | for my $type ( keys %$data ) | ||
| 46 | { | ||
| 47 | map{ push @data, $_ if !$load || $load->{$_->[0][0]} }@{$data->{$type}}; | ||
| 48 | } | ||
| 49 | |||
| 50 | my $res = $ua->post( | ||
| 51 | $addr, | ||
| 52 | +{ %{$form}, $index->{data} => YAML::XS::Dump \@data } | ||
| 53 | ); | ||
| 54 | printf "push %s\n", $res->is_success ? 'OK' : 'FAIL'; | ||
| 55 | } | ||
| 56 | |||
| 57 | 1; |