diff --git a/bin/pt-agent b/bin/pt-agent index 778e08f5..937d4703 100755 --- a/bin/pt-agent +++ b/bin/pt-agent @@ -1214,24 +1214,24 @@ has 'name' => ( required => 1, ); -has 'alias' => ( - is => 'ro', - isa => 'Str', - required => 1, -); - -has 'schedule' => ( - is => 'ro', - isa => 'Str', - required => 1, -); - has 'runs' => ( is => 'ro', isa => 'ArrayRef[Percona::WebAPI::Resource::Run]', required => 1, ); +has 'run_schedule' => ( + is => 'ro', + isa => 'Str', + required => 1, +); + +has 'spool_schedule' => ( + is => 'ro', + isa => 'Str', + required => 0, +); + sub BUILDARGS { my ($class, %args) = @_; if ( ref $args{runs} eq 'ARRAY' ) { @@ -1283,6 +1283,13 @@ has 'options' => ( required => 0, ); +has 'queries' => ( + is => 'ro', + isa => 'Maybe[ArrayRef]', + required => 0, + default => undef, +); + has 'output' => ( is => 'ro', isa => 'Str', @@ -4425,9 +4432,9 @@ sub main { } my $agent_id = $o->get('agent-id'); - if ( ($o->get('check-spool') || $o->get('run-service')) && !$agent_id ) { - _err("No agent ID was found or specified. --check-spool and " - . "--run-service require an agent ID. Run pt-agent without these " + if ( ($o->get('run-service') || $o->get('send-data')) && !$agent_id ) { + _err("No agent ID was found or specified. --run-service and " + . "--send-data require an agent ID. Run pt-agent without these " . "options to create and configure the agent, then try again."); } @@ -4454,14 +4461,15 @@ sub main { ); # ######################################################################## - # --check-spool + # --send-data # ######################################################################## - if ( $o->get('check-spool') ) { + if ( my $service = $o->get('send-data') ) { # TODO: rewrite Daemon to have args passed in so we can do # a PID file check for spool procs. Or implement file locking. - check_spool( + send_data( client => $client, agent => $agent, + service => $service, spool_dir => $o->get('spool'), ); _info("Done checking spool, exit $exit_status"); @@ -4543,7 +4551,7 @@ sub main { # Percona Web API subs for agent and spool processes # # ################################################## # -# Wrapper for code common to main agent and --check-spool process: +# Wrapper for code common to main agent and --send-data processes: # connect to the Percona web API by getting a client and an Agent. sub connect_to_percona { my (%args) = @_; @@ -4752,8 +4760,6 @@ sub run_agent { _warn($EVAL_ERROR); } - # TODO: need to schedule a pt-agent --check-spool process. - # Get services only if there's a current, running config. # Without one, we won't know how to implement services. if ( $config ) { @@ -4782,8 +4788,7 @@ sub run_agent { $services = $new_services; _info('Services updated successfully: ' - . join(', ', map { $_->alias . ' (' . $_->name . ')' } - @$services)); + . join(', ', map { $_->name } @$services)); } else { _info('Services have not changed'); @@ -4893,8 +4898,8 @@ sub write_services { return; } -# Write Service->schedule lines to crontab, along with any other -# non-pt-agent lines, and load. +# Write Service->run_schedule and (optionally) Service->spool_schedule +# lines to crontab, along with any other non-pt-agent lines, and load. sub schedule_services { my (%args) = @_; @@ -4928,8 +4933,9 @@ sub schedule_services { return; } -# Combine Service->schedule lines with non-pt-agent lines, -# i.e. don't clobber the user's other crontab lines. +# Combine Service->run_schedule and (optionally) Service->spool_schedule +# lines with non-pt-agent lines, i.e. don't clobber the user's other +# crontab lines. sub make_new_crontab { my (%args) = @_; @@ -4949,7 +4955,15 @@ sub make_new_crontab { my @pt_agent_lines; foreach my $service ( @$services ) { push @pt_agent_lines, - $service->schedule . " pt-agent --run-service " . $service->name; + $service->run_schedule + . " pt-agent --run-service " + . $service->name; + if ( $service->spool_schedule ) { + push @pt_agent_lines, + $service->spool_schedule + . " pt-agent --send-data " + . $service->name; + } } my $new_crontab = join("\n", @other_lines, @pt_agent_lines) . "\n"; @@ -5105,16 +5119,18 @@ sub replace_special_vars { # Send every file or directory in each service's directory in --spool/. # E.g. --spool/query-monitor should contain files with pt-query-digest # output. The per-service dirs are created in run_service(). -sub check_spool { +sub send_data { my (%args) = @_; have_required_args(\%args, qw( client agent + service spool_dir )) or die; my $client = $args{client}; my $agent = $args{agent}; + my $service = $args{service}; my $spool_dir = $args{spool_dir}; # Iterate through the service dirs in --spool/. @@ -5374,7 +5390,7 @@ TODO =head1 OPTIONS -L<"--check-spool"> and L<"--run-service"> are mutually exclusive. +L<"--run-service"> and L<"--send-data"> are mutually exclusive. =over @@ -5398,14 +5414,10 @@ Prompt for a password when connecting to MySQL. =item --check-interval -type: time; default: 5m +type: time; default: 10m Sleep time between checks. -=item --check-spool - -Check for new L<"--spool"> data to send. - =item --config type: Array @@ -5478,7 +5490,13 @@ Port number to use for connection. type: string -Run a Percona Web Service. +Run the given Percona Web Service. + +=item --send-data + +type: string + +Send data for the given Percona Web Service. =item --set-vars diff --git a/lib/Percona/WebAPI/Resource/Run.pm b/lib/Percona/WebAPI/Resource/Run.pm index 5488a446..81ec57d0 100644 --- a/lib/Percona/WebAPI/Resource/Run.pm +++ b/lib/Percona/WebAPI/Resource/Run.pm @@ -40,6 +40,13 @@ has 'options' => ( required => 0, ); +has 'queries' => ( + is => 'ro', + isa => 'Maybe[ArrayRef]', + required => 0, + default => undef, +); + has 'output' => ( is => 'ro', isa => 'Str', diff --git a/lib/Percona/WebAPI/Resource/Service.pm b/lib/Percona/WebAPI/Resource/Service.pm index 7d57d617..af39a003 100644 --- a/lib/Percona/WebAPI/Resource/Service.pm +++ b/lib/Percona/WebAPI/Resource/Service.pm @@ -28,24 +28,24 @@ has 'name' => ( required => 1, ); -has 'alias' => ( - is => 'ro', - isa => 'Str', - required => 1, -); - -has 'schedule' => ( - is => 'ro', - isa => 'Str', - required => 1, -); - has 'runs' => ( is => 'ro', isa => 'ArrayRef[Percona::WebAPI::Resource::Run]', required => 1, ); +has 'run_schedule' => ( + is => 'ro', + isa => 'Str', + required => 1, +); + +has 'spool_schedule' => ( + is => 'ro', + isa => 'Str', + required => 0, +); + sub BUILDARGS { my ($class, %args) = @_; if ( ref $args{runs} eq 'ARRAY' ) { diff --git a/t/pt-agent/make_new_crontab.t b/t/pt-agent/make_new_crontab.t index 1c031a17..113fdf2d 100644 --- a/t/pt-agent/make_new_crontab.t +++ b/t/pt-agent/make_new_crontab.t @@ -54,10 +54,10 @@ my $run0 = Percona::WebAPI::Resource::Run->new( ); my $svc0 = Percona::WebAPI::Resource::Service->new( - name => 'query-monitor', - alias => 'Query Monitor', - schedule => '* 8 * * 1,2,3,4,5', - runs => [ $run0 ], + name => 'query-monitor', + run_schedule => '* 8 * * 1,2,3,4,5', + spool_schedule => '* 9 * * 1,2,3,4,5', + runs => [ $run0 ], ); # Empty crontab, add the service. @@ -116,6 +116,7 @@ SKIP: { $new_crontab, "* 0 * * * date > /dev/null * 8 * * 1,2,3,4,5 pt-agent --run-service query-monitor +* 9 * * 1,2,3,4,5 pt-agent --send-data query-monitor ", "Runs crontab -l by default" ); diff --git a/t/pt-agent/samples/crontab001.out b/t/pt-agent/samples/crontab001.out index b6747f2c..1cb21262 100644 --- a/t/pt-agent/samples/crontab001.out +++ b/t/pt-agent/samples/crontab001.out @@ -1 +1,2 @@ * 8 * * 1,2,3,4,5 pt-agent --run-service query-monitor +* 9 * * 1,2,3,4,5 pt-agent --send-data query-monitor diff --git a/t/pt-agent/samples/crontab002.out b/t/pt-agent/samples/crontab002.out index 71ff08a9..3da1db8a 100644 --- a/t/pt-agent/samples/crontab002.out +++ b/t/pt-agent/samples/crontab002.out @@ -1,2 +1,3 @@ 17 3 * * 1 cmd * 8 * * 1,2,3,4,5 pt-agent --run-service query-monitor +* 9 * * 1,2,3,4,5 pt-agent --send-data query-monitor diff --git a/t/pt-agent/samples/crontab003.out b/t/pt-agent/samples/crontab003.out index 71ff08a9..3da1db8a 100644 --- a/t/pt-agent/samples/crontab003.out +++ b/t/pt-agent/samples/crontab003.out @@ -1,2 +1,3 @@ 17 3 * * 1 cmd * 8 * * 1,2,3,4,5 pt-agent --run-service query-monitor +* 9 * * 1,2,3,4,5 pt-agent --send-data query-monitor diff --git a/t/pt-agent/schedule_services.t b/t/pt-agent/schedule_services.t index b55a4a52..23cf2505 100644 --- a/t/pt-agent/schedule_services.t +++ b/t/pt-agent/schedule_services.t @@ -38,10 +38,10 @@ my $run0 = Percona::WebAPI::Resource::Run->new( ); my $svc0 = Percona::WebAPI::Resource::Service->new( - name => 'query-monitor', - alias => 'Query Monitor', - schedule => '* 8 * * 1,2,3,4,5', - runs => [ $run0 ], + name => 'query-monitor', + run_schedule => '* 8 * * 1,2,3,4,5', + spool_schedule => '* 9 * * 1,2,3,4,5', + runs => [ $run0 ], ); # First add a fake line so we can know that the real, existing @@ -84,6 +84,7 @@ is( $crontab, "* 0 * * * date > /dev/null * 8 * * 1,2,3,4,5 pt-agent --run-service query-monitor +* 9 * * 1,2,3,4,5 pt-agent --send-data query-monitor ", "schedule_services()" ); @@ -118,10 +119,10 @@ is( # ############################################################################# $svc0 = Percona::WebAPI::Resource::Service->new( - name => 'query-monitor', - alias => 'Query Monitor', - schedule => '* * * * Foo', # "foo":0: bad day-of-week - runs => [ $run0 ], + name => 'query-monitor', + run_schedule => '* * * * Foo', # "foo":0: bad day-of-week + spool_schedule => '* 8 * * Mon', + runs => [ $run0 ], ); eval {