line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
package App::OverWatch; |
2
|
|
|
|
|
|
|
|
3
|
|
|
|
|
|
|
# ABSTRACT: Watch over your infrastructure |
4
|
|
|
|
|
|
|
our $VERSION = '0.1'; # VERSION |
5
|
|
|
|
|
|
|
|
6
|
6
|
|
|
6
|
|
3375
|
use strict; |
|
6
|
|
|
|
|
12
|
|
|
6
|
|
|
|
|
224
|
|
7
|
6
|
|
|
6
|
|
28
|
use warnings; |
|
6
|
|
|
|
|
9
|
|
|
6
|
|
|
|
|
163
|
|
8
|
|
|
|
|
|
|
|
9
|
6
|
|
|
6
|
|
2384
|
use App::OverWatch::DB; |
|
0
|
|
|
|
|
|
|
|
0
|
|
|
|
|
|
|
10
|
|
|
|
|
|
|
use App::OverWatch::ServiceLock; |
11
|
|
|
|
|
|
|
use App::OverWatch::EventLog; |
12
|
|
|
|
|
|
|
use App::OverWatch::Notify; |
13
|
|
|
|
|
|
|
use App::OverWatch::Config; |
14
|
|
|
|
|
|
|
|
15
|
|
|
|
|
|
|
use Config::Tiny; |
16
|
|
|
|
|
|
|
|
17
|
|
|
|
|
|
|
sub new { |
18
|
|
|
|
|
|
|
my $class = shift; |
19
|
|
|
|
|
|
|
my $rh_options = shift || {}; |
20
|
|
|
|
|
|
|
|
21
|
|
|
|
|
|
|
my $self = bless( {}, $class ); |
22
|
|
|
|
|
|
|
|
23
|
|
|
|
|
|
|
return $self; |
24
|
|
|
|
|
|
|
} |
25
|
|
|
|
|
|
|
|
26
|
|
|
|
|
|
|
|
27
|
|
|
|
|
|
|
sub servicelock { |
28
|
|
|
|
|
|
|
my $self = shift; |
29
|
|
|
|
|
|
|
|
30
|
|
|
|
|
|
|
if (!defined($self->{ServiceLock})) { |
31
|
|
|
|
|
|
|
$self->{ServiceLock} = App::OverWatch::ServiceLock->new({ |
32
|
|
|
|
|
|
|
db => $self->_db() |
33
|
|
|
|
|
|
|
}); |
34
|
|
|
|
|
|
|
} |
35
|
|
|
|
|
|
|
return $self->{ServiceLock}; |
36
|
|
|
|
|
|
|
} |
37
|
|
|
|
|
|
|
|
38
|
|
|
|
|
|
|
sub notify { |
39
|
|
|
|
|
|
|
my $self = shift; |
40
|
|
|
|
|
|
|
|
41
|
|
|
|
|
|
|
if (!defined($self->{Notify})) { |
42
|
|
|
|
|
|
|
$self->{Notify} = App::OverWatch::Notify->new({ |
43
|
|
|
|
|
|
|
db => $self->_db() |
44
|
|
|
|
|
|
|
}); |
45
|
|
|
|
|
|
|
} |
46
|
|
|
|
|
|
|
return $self->{Notify}; |
47
|
|
|
|
|
|
|
} |
48
|
|
|
|
|
|
|
|
49
|
|
|
|
|
|
|
sub eventlog { |
50
|
|
|
|
|
|
|
my $self = shift; |
51
|
|
|
|
|
|
|
|
52
|
|
|
|
|
|
|
if (!defined($self->{Eventlog})) { |
53
|
|
|
|
|
|
|
$self->{Eventlog} = App::OverWatch::Eventlog->new({ |
54
|
|
|
|
|
|
|
db => $self->_db() |
55
|
|
|
|
|
|
|
}); |
56
|
|
|
|
|
|
|
} |
57
|
|
|
|
|
|
|
return $self->{Eventlog}; |
58
|
|
|
|
|
|
|
} |
59
|
|
|
|
|
|
|
|
60
|
|
|
|
|
|
|
sub check_options { |
61
|
|
|
|
|
|
|
my $self = shift; |
62
|
|
|
|
|
|
|
my $rh_args = shift || {}; |
63
|
|
|
|
|
|
|
|
64
|
|
|
|
|
|
|
my $ra_commands = $rh_args->{valid_commands}; |
65
|
|
|
|
|
|
|
my $rh_options = $rh_args->{options}; |
66
|
|
|
|
|
|
|
my $rh_required = $rh_args->{required_options}; |
67
|
|
|
|
|
|
|
|
68
|
|
|
|
|
|
|
## Check that only one actual command was provided |
69
|
|
|
|
|
|
|
my $commands = join(', ', sort @$ra_commands); |
70
|
|
|
|
|
|
|
my @commands = map { $rh_options->{$_} ? $_ : () } @$ra_commands; |
71
|
|
|
|
|
|
|
die "Error: Please specify one and only one command ($commands)\n" |
72
|
|
|
|
|
|
|
if (scalar @commands != 1); |
73
|
|
|
|
|
|
|
|
74
|
|
|
|
|
|
|
my $command = $commands[0]; |
75
|
|
|
|
|
|
|
|
76
|
|
|
|
|
|
|
## Check all required options are defined |
77
|
|
|
|
|
|
|
for my $opt (@{ $rh_required->{$command} }) { |
78
|
|
|
|
|
|
|
die "Error: --$opt is a required option\n" |
79
|
|
|
|
|
|
|
if (!defined($rh_options->{$opt})); |
80
|
|
|
|
|
|
|
} |
81
|
|
|
|
|
|
|
|
82
|
|
|
|
|
|
|
return $command; |
83
|
|
|
|
|
|
|
} |
84
|
|
|
|
|
|
|
|
85
|
|
|
|
|
|
|
sub load_config { |
86
|
|
|
|
|
|
|
my $self = shift; |
87
|
|
|
|
|
|
|
my $path = shift; |
88
|
|
|
|
|
|
|
|
89
|
|
|
|
|
|
|
my $rh_conf; |
90
|
|
|
|
|
|
|
|
91
|
|
|
|
|
|
|
my @paths; |
92
|
|
|
|
|
|
|
if (defined($path)) { |
93
|
|
|
|
|
|
|
@paths = ( $path ); |
94
|
|
|
|
|
|
|
} else { |
95
|
|
|
|
|
|
|
@paths = ( $ENV{HOME} . "/.overwatch.conf", |
96
|
|
|
|
|
|
|
"/etc/overwatch.conf" ); |
97
|
|
|
|
|
|
|
} |
98
|
|
|
|
|
|
|
|
99
|
|
|
|
|
|
|
FILE: |
100
|
|
|
|
|
|
|
for my $file (@paths) { |
101
|
|
|
|
|
|
|
next FILE |
102
|
|
|
|
|
|
|
if (!defined($file) || ! -f $file); |
103
|
|
|
|
|
|
|
|
104
|
|
|
|
|
|
|
my $Config = Config::Tiny->read($file); |
105
|
|
|
|
|
|
|
$rh_conf = $Config->{_} |
106
|
|
|
|
|
|
|
if ($Config && defined($Config->{_})); |
107
|
|
|
|
|
|
|
} |
108
|
|
|
|
|
|
|
|
109
|
|
|
|
|
|
|
die "Error: Couldn't load configuration from " |
110
|
|
|
|
|
|
|
. join(', ', @paths) . "\n" |
111
|
|
|
|
|
|
|
if (!defined($rh_conf)); |
112
|
|
|
|
|
|
|
|
113
|
|
|
|
|
|
|
die "Error: Require 'db_type' to be set in config\n" |
114
|
|
|
|
|
|
|
if (!$rh_conf->{db_type}); |
115
|
|
|
|
|
|
|
|
116
|
|
|
|
|
|
|
$self->{Config} = App::OverWatch::Config->new($rh_conf); |
117
|
|
|
|
|
|
|
} |
118
|
|
|
|
|
|
|
|
119
|
|
|
|
|
|
|
sub load_config_string { |
120
|
|
|
|
|
|
|
my $self = shift; |
121
|
|
|
|
|
|
|
my $string = shift; |
122
|
|
|
|
|
|
|
|
123
|
|
|
|
|
|
|
my $Config = Config::Tiny->read_string($string); |
124
|
|
|
|
|
|
|
my $rh_conf = $Config->{_} |
125
|
|
|
|
|
|
|
if ($Config && defined($Config->{_})); |
126
|
|
|
|
|
|
|
|
127
|
|
|
|
|
|
|
die "Error: Couldn't load configuration from string\n" |
128
|
|
|
|
|
|
|
if (!defined($rh_conf)); |
129
|
|
|
|
|
|
|
|
130
|
|
|
|
|
|
|
die "Error: Require 'db_type' to be set in config\n" |
131
|
|
|
|
|
|
|
if (!$rh_conf->{db_type}); |
132
|
|
|
|
|
|
|
|
133
|
|
|
|
|
|
|
$self->{Config} = App::OverWatch::Config->new($rh_conf); |
134
|
|
|
|
|
|
|
} |
135
|
|
|
|
|
|
|
|
136
|
|
|
|
|
|
|
sub _db { |
137
|
|
|
|
|
|
|
my $self = shift; |
138
|
|
|
|
|
|
|
|
139
|
|
|
|
|
|
|
if (!defined($self->{DB})) { |
140
|
|
|
|
|
|
|
$self->{DB} = App::OverWatch::DB->new( $self->{Config} ); |
141
|
|
|
|
|
|
|
} |
142
|
|
|
|
|
|
|
return $self->{DB}; |
143
|
|
|
|
|
|
|
} |
144
|
|
|
|
|
|
|
|
145
|
|
|
|
|
|
|
1; |
146
|
|
|
|
|
|
|
|
147
|
|
|
|
|
|
|
__END__ |