-
Notifications
You must be signed in to change notification settings - Fork 8
/
check_apparmor
executable file
·139 lines (116 loc) · 2.8 KB
/
check_apparmor
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
#!/usr/bin/env perl
# check_apparmor - Check status of AppArmor on the machine
#
# Copyright (c) 2016 Amadeus Germany GmbH
#
# Ported from aa-status from AppArmor
#
# Copyright (c) 2005-2006 Novell/SUSE
# Copyright (c) 2011 Canonical Ltd.
#
# SPDX-License-Identifier: GPL-2.0-only
use 5.012;
use strict;
use warnings;
use autodie;
### START PLUGIN LOADER
sub load_module {
my $module;
eval "require $_" and $module = $_ and last for @_;
$module->import();
return $module;
}
my $plugin_module;
BEGIN {
$plugin_module = load_module('Monitoring::Plugin', 'Nagios::Plugin', 'Nagios::Monitoring::Plugin');
}
### END PLUGIN LOADER
my $np = $plugin_module->new(
usage => "Usage: %s [ -v|--verbose ] ",
version => '1.8.0',
plugin => 'check_apparmor',
blurb => 'Check status of AppArmor on the machine',
license => 'GPL-2.0-only',
);
$np->getopts;
if (! -d "/sys/kernel/security/apparmor") {
$np->nagios_exit(CRITICAL, "apparmor not enabled");
}
my ($unconfined, $unconfined_with_profile, $enforcing, $complaining) = (0, 0, 0, 0);
my ($profiles_enforcing, $profiles_complaining) = (0, 0);
open(my $profiles, "/sys/kernel/security/apparmor/profiles");
while (<$profiles>) {
chomp;
/^([^\(]+)\s+\((\w+)\)$/;
if ($2 eq "enforce") {
$profiles_enforcing++;
} elsif ($2 eq "complain") {
$profiles_complaining++;
} else {
$np->nagios_exit(UNKNOWN, "Invalid profile state $2 for $1");
}
}
close($profiles);
$np->add_perfdata(
label => "profiles_enforcing",
value => $profiles_enforcing,
);
$np->add_perfdata(
label => "profiles_complaining",
value => $profiles_complaining,
);
sub read_file {
my $path = shift;
no autodie qw(open);
open(my $attrs, "<", $path) or return undef;
my $attr = <$attrs>;
chomp($attr);
close($attrs);
return $attr;
}
opendir(my $dh, "/proc");
while (readdir $dh) {
if ($_ =~ /\D/) {
next;
}
my $attr = read_file("/proc/$_/attr/current") or next;
if ($attr eq "unconfined") {
$unconfined++;
} elsif ($attr =~ /^([^\(]+)\s+\((\w+)\)$/) {
my $mode = $2;
if ($mode eq "unconfined") {
$unconfined_with_profile++;
} elsif ($mode eq "enforce") {
$enforcing++;
} elsif ($mode eq "complain") {
$complaining++;
} else {
$np->nagios_exit(UNKNOWN, "Invalid profile state $mode for $_");
}
} else {
$np->nagios_exit(UNKNOWN, "Invalid process state $attr for $_");
}
}
closedir($dh);
$np->add_perfdata(
label => "unconfined",
value => $unconfined,
);
$np->add_perfdata(
label => "unconfined_with_profile",
value => $unconfined_with_profile,
);
$np->add_perfdata(
label => "enforcing",
value => $enforcing,
);
$np->add_perfdata(
label => "complaining",
value => $complaining,
);
if ($unconfined_with_profile > 0) {
$np->nagios_exit(WARNING,
sprintf("%d processes are unconfined but have a profile defined", $unconfined_with_profile)
);
}
$np->nagios_exit(OK, "");