Skip to content
Fetching contributors…
Cannot retrieve contributors at this time
executable file 171 lines (151 sloc) 3.33 KB
#!/usr/bin/env perl
## Copyright © 2011 by Daniel Friesel <derf@finalrewind.org>
## License: WTFPL:
## 0. You just DO WHAT THE FUCK YOU WANT TO.
use strict;
use warnings;
use 5.010;
use autodie;
use IkiWiki;
use IkiWiki::Setup;
use Nagios::Plugin;
my $np = Nagios::Plugin->new(
usage => 'Usage: %s [-v|--verbose] -c|--config <ikiwiki config file>',
);
$np->add_arg(
spec => 'config|c=s',
help => 'ikiwiki config file to load',
required => 1,
);
$np->add_arg(
spec => 'group|g=s',
help => 'group the ikiwiki files should be owned by',
);
$np->add_arg(
spec => 'user|u=s',
help => 'user the ikiwiki files should be owned by',
);
$np->getopts();
IkiWiki::Setup::load($np->opts()->config());
my ($uid, $gid);
if ($np->opts()->group()) {
$gid = getgrnam($np->opts()->group());
}
if ($np->opts()->user()) {
$uid = getpwnam($np->opts()->user());
}
my (@passed, @failed, $test);
my $tests = [
{
text => 'source directory exists',
code => sub { -e $config{'srcdir'} },
},
{
text => 'destination directory exists',
code => sub { -e $config{'destdir'} },
},
{
text => 'cgi_wrapper defined if cgiurl defined',
code => sub { length($config{'cgi_wrapper'}) },
depends => [ $config{'cgiurl'} ],
},
{
# All our wikis are git-based, so git_wrapper _must_ be defined
text => 'git_wrapper defined',
code => sub {
defined($config{'git_wrapper'})
and length($config{'git_wrapper'})
},
},
{
text => 'cgi wrapper has correct mode',
code => sub { check_wrapper(
$config{'cgi_wrapper'},
$config{'cgi_wrappermode'},
) },
depends => [ $config{'cgi_wrapper'} ],
},
{
text => 'git wrapper has correct mode',
code => sub { check_wrapper(
$config{'git_wrapper'},
$config{'git_wrappermode'},
) },
depends => [ $config{'git_wrapper'} ],
},
{
text => 'omega plugin cgi exists',
code => sub { -e $config{'omega_cgi'} },
depends => [ $config{'omega_cgi'} ],
},
{
text => 'cgi wrapper has correct owner',
code => sub {
my ($s_uid, $s_gid) = (stat($config{'cgi_wrapper'}))[4, 5];
return ($s_uid == $uid and $s_gid == $gid);
},
depends => [ $config{'cgi_wrapper'}, $uid, $gid],
},
{
text => 'git wrapper has correct owner',
code => sub {
my ($s_uid, $s_gid) = (stat($config{'git_wrapper'}))[4, 5];
return ($s_uid == $uid and $s_gid == $gid);
},
depends => [ $config{'git_wrapper'}, $uid, $gid],
},
];
sub check_wrapper {
my ($file, $mode) = @_;
$mode = ($mode ? oct($mode) : 06755);
if (not -e $file) {
return 0;
}
my $mode_on_fs = (stat($file))[2] & 07777;
return ($mode_on_fs == $mode);
}
sub verbose {
my ($str) = @_;
if ($np->opts()->verbose()) {
say $str;
}
}
CHECK: for my $check (@{ $tests }) {
verbose('check ' . $check->{'text'});
if ($check->{'depends'}) {
for my $value (@{ $check->{'depends'} }) {
if (not (defined $value and length($value))) {
verbose(' skipped (depends not satisfied)');
next CHECK;
}
}
}
if (&{ $check->{'code'} }) {
verbose(' passed');
push(@passed, $check->{'text'});
}
else {
verbose(' failed');
push(@failed, $check->{'text'});
}
}
if (@failed == 0) {
$np->nagios_exit(
OK,
sprintf(
'All %d subtests passed',
scalar @passed,
),
);
}
else {
$np->nagios_exit(
CRITICAL,
sprintf(
'%d/%d subtests failed: %s',
scalar @failed,
(@passed + @failed),
join(q{; }, @failed),
),
);
}
Something went wrong with that request. Please try again.