Skip to content

Commit

Permalink
agents: split checks into separate files
Browse files Browse the repository at this point in the history
  • Loading branch information
sni committed Dec 12, 2023
1 parent 3245c86 commit 45815aa
Show file tree
Hide file tree
Showing 14 changed files with 679 additions and 221 deletions.
11 changes: 11 additions & 0 deletions MANIFEST
Original file line number Diff line number Diff line change
Expand Up @@ -177,6 +177,17 @@ META.yml
plugins/plugins-available/agents/description.txt
plugins/plugins-available/agents/lib/Thruk/Agents/SNClient.pm
plugins/plugins-available/agents/lib/Thruk/Agents/SNClient/Checks/Base.pm
plugins/plugins-available/agents/lib/Thruk/Agents/SNClient/Checks/CPU.pm
plugins/plugins-available/agents/lib/Thruk/Agents/SNClient/Checks/Drivesize.pm
plugins/plugins-available/agents/lib/Thruk/Agents/SNClient/Checks/Kernelstats.pm
plugins/plugins-available/agents/lib/Thruk/Agents/SNClient/Checks/Memory.pm
plugins/plugins-available/agents/lib/Thruk/Agents/SNClient/Checks/Mount.pm
plugins/plugins-available/agents/lib/Thruk/Agents/SNClient/Checks/Network.pm
plugins/plugins-available/agents/lib/Thruk/Agents/SNClient/Checks/OMD.pm
plugins/plugins-available/agents/lib/Thruk/Agents/SNClient/Checks/Pagefile.pm
plugins/plugins-available/agents/lib/Thruk/Agents/SNClient/Checks/Process.pm
plugins/plugins-available/agents/lib/Thruk/Agents/SNClient/Checks/Service.pm
plugins/plugins-available/agents/lib/Thruk/Agents/SNClient/Checks/Temperature.pm
plugins/plugins-available/agents/lib/Thruk/Controller/agents.pm
plugins/plugins-available/agents/lib/Thruk/Utils/Agents.pm
plugins/plugins-available/agents/lib/Thruk/Utils/CLI/Agents.pm
Expand Down
21 changes: 20 additions & 1 deletion plugins/plugins-available/agents/lib/Thruk/Agents/SNClient.pm
Original file line number Diff line number Diff line change
Expand Up @@ -352,7 +352,8 @@ sub _extract_checks {
$mod =~ s/\//::/gmx;
$mod =~ s/\.pm$//gmx;
$mod->import;
push @{$checks}, @{$mod->get_checks($c, $inventory, $hostname, $password, $section)};
my $add = $mod->get_checks($c, $inventory, $hostname, $password, $section);
push @{$checks}, @{$add} if $add;
}

# compute host configuration
Expand Down Expand Up @@ -472,6 +473,24 @@ sub check_host_match {
return(Thruk::Utils::Agents::check_wildcard_match($Thruk::Globals::HOSTNAME, $hosts));
}

##########################################################

=head2 get_disabled_config
get_disabled_config($c, $key, $default)
returns disabled config for this key with a fallback
=cut
sub get_disabled_config {
my($c, $key, $fallback) = @_;

my $dis = $c->config->{'Thruk::Agents'}->{'snclient'}->{'disable'}->{$key}
? $c->config->{'Thruk::Agents'}->{'snclient'}->{'disable'}
: { $key => $fallback };
return($dis);
}

##########################################################
sub _check_nsc_web_extra_options {
my($c) = @_;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,6 @@ package Thruk::Agents::SNClient::Checks::Base;
use warnings;
use strict;

use Thruk::Agents::SNClient ();
use Thruk::Base ();
use Thruk::Utils::Agents ();
use Thruk::Utils::Log qw/:all/;

Expand All @@ -30,55 +28,18 @@ sub get_checks {
my $checks = [];

# agent check itself
push @{$checks}, { 'id' => 'inventory', 'name' => 'agent inventory', check => 'inventory', parent => 'agent version'};
push @{$checks}, { 'id' => 'version', 'name' => 'agent version', check => 'check_snclient_version', 'noperf' => 1};

if($inventory->{'cpu'}) {
push @{$checks}, { 'id' => 'cpu', 'name' => 'cpu', check => 'check_cpu', parent => 'agent version' };
push @{$checks}, { 'id' => 'cpuutilization', 'name' => 'cpu utilization', check => 'check_cpu_utilization', parent => 'agent version' };
}

if($inventory->{'memory'}) {
for my $mem (@{$inventory->{'memory'}}) {
if($mem->{'type'} eq 'physical') {
push @{$checks}, {
'id' => 'mem',
'name' => 'memory',
'check' => 'check_memory',
'args' => { "type" => "physical" },
'parent' => 'agent version',
'info' => Thruk::Agents::SNClient::make_info($mem),
};
}
if($mem->{'type'} eq 'committed') {
push @{$checks}, {
'id' => 'mem.swap',
'name' => 'memory swap',
'check' => 'check_memory',
'parent' => 'agent version',
'args' => { "type" => "committed" },
'info' => Thruk::Agents::SNClient::make_info($mem),
};
}
}
}

if($inventory->{'pagefile'}) {
my $disabled_config = $c->config->{'Thruk::Agents'}->{'snclient'}->{'disable'}->{'pagefile'}
? $c->config->{'Thruk::Agents'}->{'snclient'}->{'disable'}
: { 'pagefile' => { 'name' => '!= total'}};
for my $page (@{$inventory->{'pagefile'}}) {
push @{$checks}, {
'id' => 'pagefile.'.Thruk::Utils::Agents::to_id($page->{'name'}),
'name' => $page->{'name'} eq 'total' ? 'pagefile' : 'pagefile '.$page->{'name'},
'check' => 'check_pagefile',
'args' => { "filter" => "name='".$page->{'name'}."'" },
'parent' => 'agent version',
'info' => Thruk::Agents::SNClient::make_info($page),
'disabled' => Thruk::Utils::Agents::check_disable($page, $disabled_config, 'pagefile'),
};
}
}
push @{$checks}, {
'id' => 'inventory',
'name' => 'agent inventory',
'check' => 'inventory',
'parent' => 'agent version',
};
push @{$checks}, {
'id' => 'version',
'name' => 'agent version',
'check' => 'check_snclient_version',
'noperf' => 1,
};

if($inventory->{'uptime'}) {
push @{$checks}, {
Expand All @@ -99,158 +60,6 @@ sub get_checks {
};
}

if($inventory->{'network'}) {
for my $net (@{$inventory->{'network'}}) {
push @{$checks}, {
'id' => 'net.'.Thruk::Utils::Agents::to_id($net->{'name'}),
'name' => 'net '.$net->{'name'},
'check' => 'check_network',
'args' => { "device" => $net->{'name'} },
'parent' => 'agent version',
'info' => Thruk::Agents::SNClient::make_info($net),
'disabled' => Thruk::Utils::Agents::check_disable($net, $c->config->{'Thruk::Agents'}->{'snclient'}->{'disable'}, 'network'),
};
}
}

if($inventory->{'drivesize'}) {
for my $drive (@{$inventory->{'drivesize'}}) {
my $prefix = "disk";
$drive->{'fstype'} = lc($drive->{'fstype'} // '');
$prefix = "nfs" if $drive->{'fstype'} eq 'nfs';
$prefix = "nfs" if $drive->{'fstype'} eq 'nfs4';
$prefix = "cifs" if $drive->{'fstype'} eq 'cifs';
$prefix = "fuse" if $drive->{'fstype'} eq 'fuseblk';
$prefix = "fuse" if $drive->{'fstype'} eq 'fuse';
if($drive->{'type'} && $drive->{'type'} eq 'cdrom') {
# add check if cdrom is empty
push @{$checks}, {
'id' => 'cdrom.'.Thruk::Utils::Agents::to_id($drive->{'drive_or_id'}),
'name' => 'cdrom empty '.$drive->{'drive_or_id'},
'check' => 'check_drivesize',
'args' => { "drive" => $drive->{'drive_or_id'}, 'warn' => 'mounted = 1' },
'parent' => 'agent version',
'info' => Thruk::Agents::SNClient::make_info($drive),
'disabled' => Thruk::Utils::Agents::check_disable($drive, $c->config->{'Thruk::Agents'}->{'snclient'}->{'disable'}, ['cdrom']),
};
} else {
push @{$checks}, {
'id' => 'df.'.Thruk::Utils::Agents::to_id($drive->{'drive_or_id'}),
'name' => $prefix.' '.$drive->{'drive_or_id'},
'check' => 'check_drivesize',
'args' => { "drive" => $drive->{'drive_or_id'} },
'parent' => 'agent version',
'info' => Thruk::Agents::SNClient::make_info($drive),
'disabled' => !$drive->{'drive'} ? 'drive has no name' : Thruk::Utils::Agents::check_disable($drive, $c->config->{'Thruk::Agents'}->{'snclient'}->{'disable'}, ['drivesize', $prefix]),
};
}
}
}

if($inventory->{'mount'}) {
my $disabled_config = $c->config->{'Thruk::Agents'}->{'snclient'}->{'disable'}->{'mount'}
? $c->config->{'Thruk::Agents'}->{'snclient'}->{'disable'}
: { 'mount' => { 'fstype' => '= cdfs', 'mount' => '~ ^(/var/lib/docker|/Volumes/com.apple.TimeMachine.localsnapshots|/private/tmp/)' }};
for my $mount (@{$inventory->{'mount'}}) {
$mount->{'fstype'} = lc($mount->{'fstype'} // '');
push @{$checks}, {
'id' => 'mount.'.Thruk::Utils::Agents::to_id($mount->{'mount'}),
'name' => 'mount '.$mount->{'mount'},
'check' => 'check_mount',
'args' => { "mount" => $mount->{'mount'}, "options" => $mount->{'options'}, "fstype" => $mount->{'fstype'} },
'parent' => 'agent version',
'info' => Thruk::Agents::SNClient::make_info($mount),
'disabled' => Thruk::Utils::Agents::check_disable($mount, $c->config->{'Thruk::Agents'}->{'snclient'}->{'disable'}, 'mount'),
'noperf' => 1,
};
}
}

if($inventory->{'service'}) {
my $services = Thruk::Base::list($inventory->{'service'});
# generic services check
if(scalar @{$services} > 0) {
push @{$checks}, {
'id' => 'svc',
'name' => 'services',
'check' => 'check_service',
'parent' => 'agent version',
};
}

# specifically configured service checks
my $wanted = {};
my $configs = Thruk::Base::list($c->config->{'Thruk::Agents'}->{'snclient'}->{'service'});
for my $cfg (@{$configs}) {
next unless Thruk::Agents::SNClient::check_host_match($cfg->{'host'});
next unless Thruk::Utils::Agents::check_wildcard_match($section, ($cfg->{'section'} // 'ANY'));
next unless $cfg->{'service'};
for my $n (@{Thruk::Base::list($cfg->{'service'})}) {
$wanted->{$n} = $cfg;
}
}
for my $svc (@{$services}) {
next unless $wanted->{$svc->{'name'}};
my $cfg = $wanted->{$svc->{'name'}};
push @{$checks}, {
'id' => 'svc.'.Thruk::Utils::Agents::to_id($svc->{'name'}),
'name' => Thruk::Agents::SNClient::make_name($cfg->{'name'} // 'service %s', { '%s' => $svc->{'name'} }),
'check' => 'check_service',
'args' => { "service" => $svc->{'name'} },
'parent' => 'agent version',
'info' => Thruk::Agents::SNClient::make_info($svc),
};
}
}

if($inventory->{'process'}) {
my $already_checked = {};
my $wanted = {};
my $configs = Thruk::Base::list($c->config->{'Thruk::Agents'}->{'snclient'}->{'proc'});
for my $cfg (@{$configs}) {
next unless Thruk::Agents::SNClient::check_host_match($cfg->{'host'});
next unless Thruk::Utils::Agents::check_wildcard_match($section, ($cfg->{'section'} // 'ANY'));
next unless $cfg->{'match'};
for my $n (@{Thruk::Base::list($cfg->{'match'})}) {
push @{$wanted->{$n}}, $cfg;
}
}
my $procs = Thruk::Base::list($inventory->{'process'});
for my $p (@{$procs}) {
my($cfg, $match, $user);
for my $m (sort keys %{$wanted}) {
$match = $m;
## no critic
next unless $p->{'command_line'} =~ m|$m|i;
## use critic

for my $cf (@{$wanted->{$m}}) {
$user = Thruk::Utils::Agents::check_wildcard_match($p->{'username'}, $cf->{'user'});
next unless $user;
$cfg = $cf;
last;
}
}
next unless $cfg;
my $username = $user ne 'ANY' ? $p->{'username'} : "";
my $id = 'proc.'.Thruk::Utils::Agents::to_id($match.'_'.($username || 'ANY'));
next if $already_checked->{$id};
$already_checked->{$id} = 1;
my $filter = [ "filter='command_line ~~ /".$match."/'" ];
if($user ne 'ANY') {
push @{$filter}, "filter='username ~~ /".$user."/'";
}
push @{$checks}, {
'id' => $id,
'name' => Thruk::Agents::SNClient::make_name($cfg->{'name'} // 'proc %e %u', { '%e' => $p->{'exe'}, '%u' => $username }),
'check' => 'check_process',
'args' => $filter,
'parent' => 'agent version',
'info' => Thruk::Agents::SNClient::make_info($p),
};
}
}

# external scripts
if($inventory->{'scripts'}) {
for my $script (@{$inventory->{'scripts'}}) {
Expand All @@ -263,23 +72,6 @@ sub get_checks {
}
}

if($inventory->{'omd'}) {
my $disabled_config = $c->config->{'Thruk::Agents'}->{'snclient'}->{'disable'}->{'omd'}
? $c->config->{'Thruk::Agents'}->{'snclient'}->{'disable'}
: { 'omd' => { 'autostart' => '!= 1'}};
for my $omd (@{$inventory->{'omd'}}) {
push @{$checks}, {
'id' => 'omd.'.Thruk::Utils::Agents::to_id($omd->{'site'}),
'name' => 'omd site '.$omd->{'site'},
'check' => 'check_omd',
'args' => { "site" => $omd->{'site'} },
'parent' => 'agent version',
'info' => Thruk::Agents::SNClient::make_info($omd),
'disabled' => Thruk::Utils::Agents::check_disable($omd, $disabled_config, 'omd'),
};
}
}

if($inventory->{'eventlog'}) {
push @{$checks}, {
'id' => 'eventlog',
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
package Thruk::Agents::SNClient::Checks::CPU;

use warnings;
use strict;

=head1 NAME
Thruk::Agents::SNClient::Checks::CPU - returns cpu checks for snclient
=head1 METHODS
=cut

##########################################################

=head2 get_checks
get_checks()
returns snclient checks
=cut
sub get_checks {
my($self, $c, $inventory, $hostname, $password, $section) = @_;
my $checks = [];

if($inventory->{'cpu'}) {
push @{$checks}, {
'id' => 'cpu',
'name' => 'cpu',
'check' => 'check_cpu',
'parent' => 'agent version',
};
push @{$checks}, {
'id' => 'cpuutilization',
'name' => 'cpu utilization',
'check' => 'check_cpu_utilization',
'parent' => 'agent version',
};
}

return $checks;
}

##########################################################

1;
Loading

0 comments on commit 45815aa

Please sign in to comment.