retire da-backup checks
[mirror/dsa-nagios.git] / dsa-nagios-checks / checks / dsa-check-samhain
1 #!/usr/bin/perl -w
2
3 # check_samhain.pl - check to see how many policy violations are reported
4 #   by the samhain file integrity checker.
5 #
6 # Copyright Rainer Wichmann (2004)
7 # Copyright Martin Zobel-Helas (2008)
8 #
9 # License Information:
10 # This program is free software; you can redistribute it and/or modify
11 # it under the terms of the GNU General Public License as published by
12 # the Free Software Foundation; either version 2 of the License, or
13 # (at your option) any later version.
14 #
15 # This program is distributed in the hope that it will be useful,
16 # but WITHOUT ANY WARRANTY; without even the implied warranty of
17 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18 # GNU General Public License for more details.
19 #
20 # You should have received a copy of the GNU General Public License
21 # along with this program; if not, write to the Free Software
22 # Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
23 #
24 ############################################################################
25
26 # -------------------------------------------------------------------[ Uses ]--
27
28 use strict;
29 use Getopt::Long;
30 use vars qw($PROGNAME $SAMHAIN $opt_V $opt_h $opt_v $verbose $opt_w $opt_c $opt_t $status $msg $state $retval);
31 use lib "/usr/lib/nagios/plugins";
32 use utils qw(%ERRORS &print_revision);
33
34 #my $TIMEOUT = 15;
35 #my %ERRORS=('OK'=>0,'WARNING'=>1,'CRITICAL'=>2,'UNKNOWN'=>3,'DEPENDENT'=>4);
36 #sub print_revision ($$);
37
38 # ----------------------------------------------------[ Function Prototypes ]--
39
40 sub print_help ();
41 sub print_usage ();
42 sub process_arguments ();
43
44 # ------------------------------------------------------------[ Environment ]--
45
46 $ENV{'PATH'}='';
47 $ENV{'BASH_ENV'}=''; 
48 $ENV{'ENV'}='';
49
50 # -----------------------------------------------------------------[ Global ]--
51
52 $PROGNAME = "dsa-check-samhain";
53 $SAMHAIN = "/usr/sbin/samhain"; 
54
55 # ----------------------------------------------------------------[ options ]--
56
57 Getopt::Long::Configure('bundling');
58 $status = process_arguments();
59 if ($status){
60         print "ERROR: processing arguments\n";
61         exit $ERRORS{"UNKNOWN"};
62 }
63
64 # ----------------------------------------------------------------[ timeout ]--
65
66 $SIG{'ALRM'} = sub {
67         print ("ERROR: timed out waiting for $SAMHAIN\n");
68         exit $ERRORS{"WARNING"};
69 };
70 alarm($opt_t);
71
72 # ----------------------------------------------------------[ start samhain ]--
73
74 if ( defined $SAMHAIN && -x $SAMHAIN ) {
75     if (! open (SHPIPE, "/usr/bin/sudo $SAMHAIN -t check --foreground -p err -s none -l none -m none 2>&1 | " ) ) {
76         print "ERROR: could not popen $SAMHAIN \n";
77         exit $ERRORS{'UNKNOWN'};
78     }
79 }else{
80     print "ERROR: Could not find samhain executable!\n";
81     exit $ERRORS{'UNKNOWN'};
82 }
83
84 # ---------------------------------------------------------[ read from pipe ]--
85
86 $status = 0;
87
88 while (<SHPIPE>) {
89     if (/POLICY/) {
90         ++$status;
91         print $_ if $verbose;
92     }
93 }    
94
95 if ($status < $opt_w) {
96     $msg = "OK: $status policy violations (threshold $opt_w/$opt_c)";
97     $state = $ERRORS{'OK'};
98 } elsif ($status >= $opt_w  && $status < $opt_c) {
99     $msg = "WARNING: $status policy violations (threshold w=$opt_w)";
100     $state = $ERRORS{'WARNING'};
101 } else {
102     $msg = "CRITICAL: $status policy violations (threshold w=$opt_w)";
103     $state = $ERRORS{'CRITICAL'};
104 }
105
106 # -------------------------------------------------------------[ close pipe ]--
107
108 close (SHPIPE);
109  
110 # declare an error if we also get a non-zero return code from samhain
111
112 if ( $? ) {
113     $retval = $? / 256;
114     if ( $! ) {
115       print "Error closing $SAMHAIN: $!\n" if $verbose;
116     } else {
117       print "$SAMHAIN returned exit status $retval\n" if $verbose;
118     }
119     if ($state == $ERRORS{"CRITICAL"}) { 
120         $state = $ERRORS{"CRITICAL"}; 
121     } else {
122         print "ERROR: $SAMHAIN exit status $retval\n";
123         exit $ERRORS{'UNKNOWN'};
124     }
125 }
126
127 # -------------------------------------------------------------------[ exit ]--
128
129 print "$msg | 'policy violations'=$status;$opt_w;$opt_c\n";
130 exit $state;
131
132
133 # ------------------------------------------------------------[ Subroutines ]--
134
135 sub process_arguments(){
136     GetOptions
137         ("V"   => \$opt_V, "version"    => \$opt_V,
138          "h"   => \$opt_h, "help"       => \$opt_h,
139          "v"   => \$opt_v, "verbose"    => \$opt_v,
140          "w=i" => \$opt_w, "warning=i"  => \$opt_w,   
141          "c=i" => \$opt_c, "critical=i" => \$opt_c,     
142          "t=i" => \$opt_t, "timeout=i"  => \$opt_t 
143          );
144     
145     if ($opt_V) {
146         print_revision($PROGNAME,'$Revision: 1.0 $ ');
147         exit $ERRORS{'OK'};
148     }
149     
150     if ($opt_h) {
151         print_help();
152         exit $ERRORS{'OK'};
153     }
154     
155     if (defined $opt_v ){
156         $verbose = $opt_v;
157     }
158
159     unless (defined $opt_t) {
160         $opt_t = $utils::TIMEOUT ;      # default timeout
161         # $opt_t = $TIMEOUT ;
162     }
163     
164     unless (defined $opt_w) {
165         $opt_w = 1;
166     }
167     
168     unless (defined $opt_c) {
169         $opt_c = 1;
170     }
171     
172     if ( $opt_w > $opt_c) {
173         print "Warning cannot be greater than Critical!\n";
174         exit $ERRORS{'UNKNOWN'};
175     }
176     
177     return $ERRORS{'OK'};
178 }
179
180 sub print_usage () {
181     print "Usage: $PROGNAME [-w <warn>] [-c <crit>] [-t <timeout>]\n";
182 }
183
184 sub print_help () {
185     print_revision($PROGNAME, '$Revision: 1.0 $');
186     print "Copyright (c) 2004 Rainer Wichmann
187
188 This plugin checks the number of policy violations reported by the
189 samhain file intgrity checker
190
191 ";
192     print_usage();
193     print "
194 -w, --warning=INTEGER
195    Minimum number of policy violations for which a WARNING status will result
196 -c, --critical=INTEGER
197    Minimum number of policy violations for which a CRITICAL status will result
198 -t, --timeout=SECONDS
199    The number of seconds after which a the plugin will timeout
200 -v, --verbose
201    Verbose output
202 -h, --help
203    Show this help message
204 -V, --version
205    Show the version of the plugin
206
207 ";
208 }
209
210 #sub print_revision ($$) {
211 #        my $commandName = shift;
212 #        my $pluginRevision = shift;
213 #        $pluginRevision =~ s/^\$Revision: //;
214 #        $pluginRevision =~ s/ \$\s*$//;
215 #        print "$commandName (samhain 2.2.3) $pluginRevision\n";
216 #}