Skip to content

Commit

Permalink
Merge pull request #172 from glensc/dm-detect
Browse files Browse the repository at this point in the history
dm: detect if raid devices present
  • Loading branch information
glensc authored Sep 1, 2017
2 parents e33601d + 9552372 commit b147d08
Show file tree
Hide file tree
Showing 5 changed files with 43 additions and 12 deletions.
10 changes: 5 additions & 5 deletions bin/check_raid.pl
Original file line number Diff line number Diff line change
Expand Up @@ -150,16 +150,16 @@
print "Please include output of **ALL** commands in bugreport\n\n";
}

if ($mp->opts->sudoers) {
my $res = sudoers($mp->opts->debug, $mc->active_plugins(1));
$mp->plugin_exit(OK, $res ? "sudoers updated" : "sudoers not updated");
}

my @plugins = $mc->active_plugins;
if (!@plugins) {
$mp->plugin_exit($plugin_options{options}{noraid_status}, "No active plugins (No RAID found)");
}

if ($mp->opts->sudoers) {
sudoers($mp->opts->debug, @plugins);
$mp->plugin_exit(OK, "sudoers updated");
}

# print active plugins
if ($mp->opts->list_plugins) {
foreach my $p (@plugins) {
Expand Down
4 changes: 3 additions & 1 deletion lib/App/Monitoring/Plugin/CheckRaid.pm
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,8 @@ sub plugin {
# Returns the plugin objects
sub active_plugins {
my $this = shift;
# whether the query is for sudo rules
my $sudo = shift || 0;

my @plugins = ();

Expand All @@ -75,7 +77,7 @@ sub active_plugins {
next unless $plugin->can('check');

# skip inactive plugins (disabled or no tools available)
next unless $plugin->active;
next unless $plugin->active($sudo);

push(@plugins, $plugin);
}
Expand Down
25 changes: 24 additions & 1 deletion lib/App/Monitoring/Plugin/CheckRaid/Plugins/dm.pm
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,18 @@ sub program_names {
qw(dmsetup);
}

sub active {
my ($this, $sudo) = @_;

# return if parent said NO
my $res = $this->SUPER::active(@_);
return $res unless $res;

# check if there really are any devices
my $c = $this->parse;
return !!@$c;
}

sub sudo {
my ($this, $deep) = @_;
# quick check when running check
Expand Down Expand Up @@ -54,7 +66,7 @@ sub parse_raid {
sync_ratio
sync_action
mismatch_cnt
);
);

my %h;
@h{@cols} = split;
Expand Down Expand Up @@ -129,6 +141,17 @@ sub get_fh {
sub parse {
my $this = shift;

# cache for single run
if (!defined($this->{parsed})) {
$this->{parsed} = $this->_parse;
}

return $this->{parsed};
}

sub _parse {
my $this = shift;

my @devices;
my $fh = $this->get_fh();
while (<$fh>) {
Expand Down
13 changes: 8 additions & 5 deletions lib/App/Monitoring/Plugin/CheckRaid/Sudoers.pm
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ our @EXPORT_OK = @EXPORT;
#
# if sudoers config has "#includedir" directive, add file to that dir
# otherwise update main sudoers file
# @returns true if file was updated
sub sudoers {
my $dry_run = shift;
my @plugins = @_;
Expand All @@ -29,7 +30,7 @@ sub sudoers {

unless (@sudo) {
warn "Your configuration does not need to use sudo, sudoers not updated\n";
return;
return 0;
}

my @rules = join "\n", (
Expand All @@ -50,7 +51,7 @@ sub sudoers {
warn "--- sudoers ---\n";
print @rules;
warn "--- sudoers ---\n";
return;
return 0;
}

my $sudoers = find_file('/usr/local/etc/sudoers', '/etc/sudoers');
Expand Down Expand Up @@ -100,10 +101,12 @@ sub sudoers {
# use the new file
rename($new, $sudoers) or die $!;
warn "$sudoers file updated.\n";
} else {
warn "$sudoers file not changed.\n";
unlink($new);
return 1;
}

warn "$sudoers file not changed.\n";
unlink($new);
return 0;
}

# return first "#includedir" directive from $sudoers file
Expand Down
3 changes: 3 additions & 0 deletions lib/App/Monitoring/Plugin/CheckRaid/Utils.pm
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,9 @@ sub find_sudo {
local $/ = undef;
local $_ = <$fh>;
close($fh) or die $!;
# prefer -n to skip password prompt
push(@sudo, '-n') if /-n/;
# ..if not supported, add -A as well
push(@sudo, '-A') if /-A/;

return \@sudo;
Expand Down

0 comments on commit b147d08

Please sign in to comment.