-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmonitoring.pl
executable file
·143 lines (108 loc) · 2.95 KB
/
monitoring.pl
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
#!/usr/bin/env perl
use strict;
use warnings;
use FindBin;
use lib "$FindBin::RealBin/lib";
use JSON::XS;
use Getopt::Long;
use Monitoring::Plugins;
use Data::Dumper;
my $EXE = $FindBin::Bin . "/monitoring.pl";
my $USERAGENTCONF = '/etc/zabbix/zabbix_agentd.d/useragents.conf';
my $ZABBIXAGENTINIT = '/etc/init.d/zabbix-agent';
#Process Command line opts
my $module;
my $action = 'null';
my @args;
my $help;
GetOptions (
"module|m=s" => \$module,
"action|a=s" => \$action,
"args:s{,}" => \@args,
"help|h" => \$help
) or print "Invalid options\n".help()."\n";
$help and help();
#Set up the modules object
my $modules = Monitoring::Plugins->new();
sub register {
my @registrations = map {
s/,monitoring\.pl/,$EXE/;
unless (/^UserParameter/) {
"UserParameter=$_";
}
else {
$_;
}
} $modules->register();
my $registrations = join("\n", sort { $a cmp $b } @registrations);
$registrations .= "\n";
unless ( -d '/etc/zabbix/' ) {
mkdir '/etc/zabbix/';
}
unless ( -d '/etc/zabbix/zabbix_agentd.conf.d' ) {
mkdir '/etc/zabbix/zabbix_agentd.conf.d';
}
if ( open( my $rfh, '<', $USERAGENTCONF) ) {
local $/;
my $current = <$rfh>;
close $rfh;
if ($registrations eq $current) {
return 1;
}
}
open (my $wfh, '>', $USERAGENTCONF)
or die "Could not open $USERAGENTCONF for writing: $!\n";
print $wfh $registrations;
if (-f $ZABBIXAGENTINIT) {
system( "$ZABBIXAGENTINIT restart");
}
return 1;
}
=head2 Discover
Arguments: Name of module whos discover method should be invoked (SCALAR)
=cut
sub discover {
my $module = shift or return;
return if ref $module;
#Call the discovery method.
my $result = $modules->discover($module);
#print the JSON
my $encoder = JSON::XS->new->ascii->pretty;
print $encoder->encode($result)."\n";
}
=head2 Test
Arguments: Name of module whos test method should be invoked (SCALAR)
Arbitrary number of arguments that will be passed to the method.
=cut
sub test {
my $module = shift or return;
my @args = @_;
#Call the modules test function passing any suplied arguments.
my $result = $modules->test($module, @args);
$result ||= 'UNKNOWN';
print "$result\n";
}
sub help {
my $help = <<'HELP';
Usage:
monitoring.pl -h
monitoring.pl -m <module> -a (discover|test)
monitoring.pl -a discover
monitoring.pl -a register
Options:
--help | -h : Display this text.
--module | -m : Specifiy the module to load (required -a)
--action | -a : Specify the 'discovery', 'test' or 'register method.
HELP
print $help;
exit 0;
}
my %actions = (
'discover' => \&discover,
'test' => \&test,
'register' => \®ister,
);
if ( $actions{$action} ) {
$actions{$action}->($module, @args);
}
exit 0;