| 1 | #!/usr/bin/perl | ||||||||||
| 2 | |||||||||||
| 3 | 8 | ▶ | use strict; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 4 | 8 | ▶ | use warnings; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 5 | 8 | ▶ | use FindBin; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 6 | 8 | ▶ | use lib "$FindBin::Bin/../lib"; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 7 | 8 | ▶ | use App::derived; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 8 | 8 | ▶ | use Getopt::Long qw/:config posix_default no_ignore_case bundling/; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 9 | 8 | ▶ | use Pod::Usage qw/pod2usage/; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 10 | 8 | ▶ | use Module::Load; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 11 | |||||||||||
| 12 | 8 | ▶ | GetOptions( | ||||||||
|
Branch: 1 branch
| |||||||||||
| 13 | 'h|help' => \my $help, | ||||||||||
| 14 | 'i=i' => \my $interval, | ||||||||||
| 15 | 'M=s@' => \my @plugins, | ||||||||||
| 16 | 'port=i' => \my $port, | ||||||||||
| 17 | 'host=i' => \my $host, | ||||||||||
| 18 | ) or pod2usage(1); | ||||||||||
| 19 | |||||||||||
| 20 | 8 | ▶ | pod2usage(-verbose=>2,-exitval=>0) if $help; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 21 | 8 | my ($derivedfile) = @ARGV; | |||||||||
| 22 | 8 | ▶ | pod2usage(-verbose=>1,-exitval=>1) unless $derivedfile; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 23 | |||||||||||
| 24 | 8 | ▶ | $interval = 10 unless $interval; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 25 | 8 | ▶ | $port = 12306 unless $port; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 26 | 8 | ▶ | $host = '0' unless defined $host; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 27 | |||||||||||
| 28 | 8 | ▶ | push @plugins, 'Memcached' if @plugins == 0; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 29 | |||||||||||
| 30 | 8 | my $derived = App::derived->new( | |||||||||
| 31 | interval => $interval, | ||||||||||
| 32 | ); | ||||||||||
| 33 | 8 | my $services = load_derviedfile($derivedfile); | |||||||||
| 34 | |||||||||||
| 35 | 8 | for my $service ( keys %$services ) { | |||||||||
| 36 | 16 | $derived->add_service($service, $services->{$service}); | |||||||||
| 37 | } | ||||||||||
| 38 | |||||||||||
| 39 | 8 | foreach my $plugin ( @plugins ) { | |||||||||
| 40 | 8 | my ($plugin, @args) = split /,/, $plugin; | |||||||||
| 41 | 8 | my %args = map { split /=/, $_ } @args; | |||||||||
| 42 | |||||||||||
| 43 | 8 | ▶ | $plugin = "App::derived::Plugin::" . $plugin if $plugin !~ m!^\+!; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 44 | 8 | load($plugin); | |||||||||
| 45 | 8 | $derived->add_plugin( $plugin, { | |||||||||
| 46 | host => $host, | ||||||||||
| 47 | port => $port, | ||||||||||
| 48 | %args | ||||||||||
| 49 | }); | ||||||||||
| 50 | } | ||||||||||
| 51 | |||||||||||
| 52 | 8 | $derived->run; | |||||||||
| 53 | |||||||||||
| 54 | 8 | ▶ | sub load_derviedfile { | ||||||||
|
Subroutine
load_derviedfile: called CC=4 SCAR=14.0
| |||||||||||
| 55 | 8 | my $file = shift; | |||||||||
| 56 | 8 | my %services; | |||||||||
| 57 | 8 | ▶ | open(my $fh, '<:utf8', $file) or die "cannot load file $file: $!"; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 58 | 8 | ▶ | while (my $line = <$fh>) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 59 | 16 | ▶ | if (my ($name, $command) = ($line =~ /^([^:]+)\s*:\s*(.+)/)) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 60 | 16 | $services{$name} = $command; | |||||||||
| 61 | } | ||||||||||
| 62 | } | ||||||||||
| 63 | 8 | return \%services; | |||||||||
| 64 | } | ||||||||||
| 65 | |||||||||||
| 66 | __END__ | ||||||||||