-
Notifications
You must be signed in to change notification settings - Fork 1
/
nagios-lib.pl
134 lines (106 loc) · 2.42 KB
/
nagios-lib.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
BEGIN { push(@INC, ".."); };
use WebminCore;
init_config();
sub get_nagios_config{
my $lref = &read_file_lines($config{'nagios_config'});
my @rv;
my $lnum = 0;
foreach my $line (@$lref) {
my ($n, $v) = split(/\s+/, $line, 2);
if ($n) {
push(@rv, { 'name' => $n, 'value' => $v, 'line' => $lnum });
}
$lnum++;
}
return @rv;
}
sub get_nagios_home(){
return '/usr/local/nagios';
}
sub get_nagios_version(){
my %version;
my $nagios_home = get_nagios_home();
&open_execute_command(CMD, $nagios_home.'/bin/nagios -h', 1);
while(my $line = <CMD>) {
if ($line =~ /Nagios Core ([0-9\.]+)$/i) {
$version{'number'} = $1;
last;
}
}
close(CMD);
return %version;
}
sub nagios_is_running(){
my $rv = 0;
my $nagios_bin = get_nagios_home().'/bin/nagios';
&open_execute_command(CMD, '/bin/ps -ef', 1);
while(my $line = <CMD>) {
if ($line =~ /$nagios_bin/i) {
$rv = 1;
last;
}
}
close(CMD);
return $rv;
}
sub nrpe_is_running(){
my $rv = 0;
my $nrpe_bin = get_nagios_home().'/bin/nrpe';
&open_execute_command(CMD, '/bin/ps -ef', 1);
while(my $line = <CMD>) {
if ($line =~ /$nrpe_bin/i) {
$rv = 1;
last;
}
}
close(CMD);
return $rv;
}
sub get_cfg_files{
my $dirpath = $_[0];
my @files;
opendir(DIR, $dirpath) or die $!;
my @cfg_files = grep {
/\.cfg$/ # ends in .cfg
&& -f $dirpath.'/'.$_ # its a file
} readdir(DIR);
closedir(DIR);
#make file have full path name
@cfg_files = map { $dirpath.'/'.$_} @cfg_files;
push(@files, @cfg_files);
return @files;
}
sub exec_cmd{
my $cmd = $_[0];
my $cmd_out='';
my $rv = &execute_command($cmd, undef, \$cmd_out, \$cmd_out, 0, 0);
if($cmd_out){
$cmd_out = &html_escape($cmd_out);
$cmd_out =~ s/[\r\n]/<\/br>/g;
print $cmd_out;
}
return $rv;
}
sub download_file{
my $url = $_[0];
my ($proto, $x, $host, $path) = split('/', $url, 4);
my @paths = split('/', $url);
my $filename = $paths[-1];
if($filename eq ''){
$filename = 'index.html';
}
my $sslmode = $proto eq 'https:';
my $port = 80;
if($sslmode){
$port = 443;
}
&error_setup(&text('install_err3', $url));
my $tmpfile = &transname($filename);
$progress_callback_url = $url;
&http_download($host, $port, '/'.$path, $tmpfile, \$error, \&progress_callback, $sslmode);
if($error){
print &html_escape($error);
return '';
}
return $tmpfile;
}