forked from matteocorti/check_updates
-
Notifications
You must be signed in to change notification settings - Fork 0
/
check_updates
1227 lines (953 loc) · 30.9 KB
/
check_updates
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
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#!perl
# nagios: -epn
package main;
# check_updates is a Nagios plugin to check if RedHat or Fedora system
# is up-to-date
#
# See the INSTALL file for installation instructions
#
# Copyright (c) 2007-2016, Matteo Corti
#
# This module is free software; you can redistribute it and/or modify it
# under the terms of GNU general public license (gpl) version 3,
# or (at your option) any later version.
# See the COPYING file for details.
use 5.00800;
use strict;
use warnings;
no warnings 'once'; ## no critic (TestingAndDebugging::ProhibitNoWarnings)
our $VERSION = '1.6.19';
use Carp;
use English qw(-no_match_vars);
use POSIX qw(uname);
use Readonly;
my $plugin_module = load_module( 'Monitoring::Plugin', 'Nagios::Plugin' );
my $plugin_threshold_module =
load_module( 'Monitoring::Plugin::Threshold', 'Nagios::Plugin::Threshold' );
my $plugin_getopt_module =
load_module( 'Monitoring::Plugin::Getopt', 'Nagios::Plugin::Getopt' );
# Check which version of the monitoring plugins is available
sub load_module {
my @names = @_;
my $loaded_module;
for my $name (@names) {
my $file = $name;
# requires need either a bare word or a file name
$file =~ s{::}{/}gsxm;
$file .= '.pm';
eval { ## no critic (ErrorHandling::RequireCheckingReturnValueOfEval)
require $file;
$name->import();
};
if ( !$EVAL_ERROR ) {
$loaded_module = $name;
last;
}
}
if ( !$loaded_module ) {
#<<<
print 'CHECK_UPDATES: plugin not found: ' . join( ', ', @names ) . "\n"; ## no critic (RequireCheckedSyscall)
#>>>
exit 2;
}
return $loaded_module;
}
Readonly our $EXIT_UNKNOWN => 3;
Readonly our $YUM_RETURN_ERROR => 1;
Readonly our $YUM_RETURN_UPDATES_AVAILABLE => 100;
Readonly our $BITS_PER_BYTE => 8;
# IMPORTANT: Nagios plugins could be executed using embedded perl in this case
# the main routine would be executed as a subroutine and all the
# declared subroutines would therefore be inner subroutines
# This will cause all the global lexical variables not to stay shared
# in the subroutines!
#
# All variables are therefore declared as package variables...
#
## no critic (ProhibitPackageVars)
use vars qw(
$bootcheck
$exit_message
$help
$options
$plugin
$security_plugin
$status
$threshold
$wrong_kernel
$yum_executable
@status_lines
);
## use critic
##############################################################################
# Usage : verbose("some message string", $optional_verbosity_level);
# Purpose : write a message if the verbosity level is high enough
# Returns : n/a
# Arguments : message : message string
# level : options verbosity level
# Throws : n/a
# Comments : n/a
# See also : n/a
sub verbose {
# arguments
my $message = shift;
my $level = shift;
if ( !defined $message ) {
exit_with_error( $plugin_module->UNKNOWN,
q{Internal error: not enough parameters for 'verbose'} );
}
if ( !defined $level ) {
$level = 0;
}
# we check if options is defined as the it could be undefined if
# running from a unit test
if ( defined $options && $level < $options->verbose ) {
#<<<
print $message; ## no critic (RequireCheckedSyscalls)
#>>>
}
return;
}
##############################################################################
# Usage : debug("some message string");
# Purpose : write a debugging message
# Returns : n/a
# Arguments : message : message string
# Throws : n/a
# Comments : n/a
# See also : n/a
sub debug {
# arguments
my $message = shift;
if ( !defined $message ) {
exit_with_error( $plugin_module->UNKNOWN,
q{Internal error: not enough parameters for 'debug'} );
}
if ( defined $options && $options->get('debug') ) {
print "[DBG] $message"; ## no critic (RequireCheckedSyscalls)
}
return;
}
##############################################################################
# Usage : exit_with_error( $status, $message)
# Purpose : if a plugin object is available exits via ->nagios_exit
# otherwise prints to the shell and exit normally
# Returns : n/a
# Arguments : n/a
# Throws : n/a
# Comments : n/a
# See also : n/a
sub exit_with_error {
my $status = shift;
my $message = shift;
if ($plugin) {
$plugin->nagios_exit( $status, $message );
}
else {
#<<<
print "Error: $message"; ## no critic (RequireCheckedSyscalls)
#>>>
exit $status;
}
return;
}
##############################################################################
# Usage : get_path('program_name');
# Purpose : retrieves the path of an executable file using the
# 'which' utility
# Returns : the path of the program (if found)
# Arguments : the program name
# Throws : n/a
# Comments : n/a
# See also : n/a
sub get_path {
my $prog = shift;
my $path;
my $which_command = "which $prog";
my $which_output;
verbose "Executing 'which $prog'\n", 2;
#<<<
open $which_output, q{-|}, "$which_command 2>&1"
or $plugin->nagios_exit( $plugin->UNKNOWN, "Cannot execute $which_command: $OS_ERROR" );
while (<$which_output>) {
verbose $_, 2;
chomp;
if ( !/^which:/mxs ) {
$path = $_;
}
}
if ( !( close $which_output )
&& ( $OS_ERROR != 0 ) )
{
# close to a piped open return false if the command with non-zero
# status. In this case $! is set to 0
$plugin->nagios_exit( $plugin->UNKNOWN,
"Error while closing pipe to $which_command: $OS_ERROR" );
}
#>>>
return $path;
}
# the script is declared as a package so that it can be unit tested
# but it should not be used as a module
if ( !caller ) {
run();
}
##############################################################################
# Usage : whoami()
# Purpose : retrieve the user runnging the process
# Returns : username
# Arguments : n/a
# Throws : n/a
# Comments : n/a
# See also : n/a
sub whoami {
my $output;
my $pid = open $output, q{-|},
'whoami'
or exit_with_error( $plugin_module->UNKNOWN,
"Cannot determine the user: $OS_ERROR" );
while (<$output>) {
chomp;
return $_;
}
if ( !( close $output ) && ( $OS_ERROR != 0 ) ) {
# close to a piped open return false if the command with non-zero
# status. In this case $! is set to 0
exit_with_error( $plugin_module->UNKNOWN,
"Error while closing pipe to whoami: $OS_ERROR\n" );
}
exit_with_error( $plugin_module->UNKNOWN, 'Cannot determine the user' );
return;
}
##############################################################################
# Usage : versioncmp
# Purpose : Sort revision-like numbers
# Returns : -1, 0, or 1 depending on whether the left argument is less than,
# equal to, or greater than the right argument.
# Arguments : Globally defined $a and $b as the versions to compare
# Throws : n/a
# Comments : Included (and adapted) from Sort::Version to remove the
# dependency on Sort::Version which is not available per default
# on RH systems
# See also : Sort::Version on CPAN
sub versioncmp {
my @A = ( $a =~ /([-.]|\d+|[^-.\d]+)/gxms );
my @B = ( $b =~ /([-.]|\d+|[^-.\d]+)/gxms );
my ( $A, $B );
while ( @A and @B ) {
$A = shift @A;
$B = shift @B;
## no critic (ProhibitCascadingIfElse)
if ( $A eq q{-} and $B eq q{-} ) {
next;
}
elsif ( $A eq q{-} ) {
return -1; ## no critic (ProhibitMagicNumbers)
}
elsif ( $B eq q{-} ) {
return 1;
}
elsif ( $A eq q{.} and $B eq q{.} ) {
next;
}
elsif ( $A eq q{.} ) {
return -1; ## no critic (ProhibitMagicNumbers)
}
elsif ( $B eq q{.} ) {
return 1;
}
elsif ( $A =~ /^\d+$/xms and $B =~ /^\d+$/xms ) {
if ( $A =~ /^0/xms || $B =~ /^0/xms ) {
return $A cmp $B if $A cmp $B;
}
else {
return $A <=> $B if $A <=> $B;
}
}
else {
$A = uc $A;
$B = uc $B;
return $A cmp $B if $A cmp $B;
}
## use critic (ProhibitCascadingIfElse)
}
return @A <=> @B;
}
##############################################################################
# Usage : $kernel_version = clean_kernel_version( $kernel_version )
# Purpose : removes everything but the version number from the kernel
# version (e.g., PAE, xen, ...)
# Returns : kernel version
# Arguments : kernel_version : output of uname
# Throws : n/a
# Comments : n/a
# See also : n/a
sub clean_kernel_version {
my $kernel = shift;
# remove PAE
$kernel =~ s/[.][\w]*PAE//imxs;
$kernel =~ s/PAE-//imxs;
# remove core
$kernel =~ s/core-//imxs;
# remove Xen
$kernel =~ s/xen-//imxs;
# remove openvz
$kernel =~ s/o?vzkernel-//imxs;
# remove architecture
$kernel =~ s/[.](i[3-6]86|ppc|x86_64)$//mxs;
# remove smp
$kernel =~ s/smp$//mxs;
$kernel =~ s/smp-//imxs;
# remove RHEL, CentOS, Scientific Linux and Fedora flavours
$kernel =~ s/[.]el\d+.*//imxs;
$kernel =~ s/[.]fc\d+.*//imxs;
# remove UEK
$kernel =~ s/uek-//imxs;
# remove ml-aufs, lt-aufs
$kernel =~ s/ml-aufs-//imxs;
$kernel =~ s/lt-aufs-//imxs;
return $kernel;
}
##############################################################################
# Usage : check_running_kernel( );
# Purpose : checks if the loaded kernel is the latest available
# Returns : n/a
# Arguments : n/a
# Throws : n/a
# Comments : n/a
# See also : n/a
sub check_running_kernel {
# return if running in openvz client: /proc/vz is always present but
# /proc/bc only exists on node, but not inside container.
if ( -d '/proc/vz' && !( -d '/proc/bc' ) ) {
return;
}
if ( !$bootcheck ) {
return;
}
my $package = 'kernel';
################################################################################
# Running
my ( $sysname, $nodename, $release, $version, $machine ) = POSIX::uname();
$release = clean_kernel_version($release);
verbose "running a Linux kernel: $release\n";
################################################################################
# Installed
my @versions;
for my $rpm (
(
"$package", "$package-smp",
"$package-PAE", "$package-xen",
"$package-uek", 'ovzkernel',
'vzkernel', "$package-PAE-core",
"$package-ml-aufs", "$package-lt-aufs",
"$package-core",
)
)
{
my $output;
#<<<
my $pid = open $output, q{-|}, "rpm -q $rpm" ## no critic (RequireBriefOpen)
or exit_with_error( $plugin_module->UNKNOWN,
"Cannot list installed kernels: $OS_ERROR" );
#>>>
# there could be multiple versions of the same package installed
my @rpm_versions;
while (<$output>) {
chomp;
push @rpm_versions, $_;
}
if ( !( close $output ) && ( $OS_ERROR != 0 ) ) {
# close to a piped open return false if the command with non-zero
# status. In this case $! is set to 0
exit_with_error( $plugin_module->UNKNOWN,
"Error while closing pipe to rpm: $OS_ERROR\n" );
}
if ( $CHILD_ERROR == 0 ) {
# rpm exits with 0 only it the RPM exists
push @versions, @rpm_versions;
}
}
for (@versions) {
# strip package name
s/^$package-//mxs;
$_ = clean_kernel_version($_);
}
@versions = sort versioncmp @versions;
my $installed = $versions[-1];
if ( !$installed ) {
$plugin->nagios_exit( $plugin->UNKNOWN,
'Unable to detect installed kernel' );
}
verbose "kernel: running = $release, installed = $installed\n";
if ( $installed ne $release ) {
my $error =
"your machine is running kernel $release but a newer version ($installed) is installed: you should reboot";
if ($exit_message) {
$exit_message .= $error;
}
else {
$exit_message = $error;
}
$wrong_kernel = 1;
}
return;
}
##############################################################################
# Usage : run_yum();
# Purpose : runs yum check-update with the supplied command line argumens
# Returns : the list of updates
# Arguments : string with the command line arguments
# : (optional) yum command
# Throws : n/a
# Comments : n/a
# See also : n/a
sub run_yum {
my $arguments = shift;
my $yum_command = shift;
# per default check updates
if ( !defined $yum_command ) {
$yum_command = 'check-update';
}
my $blank_line;
my $OUTPUT_HANDLER;
my @updates;
my $command = "$yum_executable $yum_command $arguments";
debug qq{Running "$command"\n}, 1;
#<<<
my $pid = open $OUTPUT_HANDLER, q{-|}, $command ## no critic (RequireBriefOpen)
or exit_with_error( $plugin_module->UNKNOWN, "Cannot list updates: $OS_ERROR" );
#>>>
while (<$OUTPUT_HANDLER>) {
my $line = $_;
debug $line;
chomp $line;
if ( !$line ) {
$blank_line = 1;
next;
}
if ($blank_line) {
# some lines are wrapped and result and the second part
# is erroneously interpreted as a new update
if ( $line =~ m/^[ ]/mxs ) {
next;
}
if ( $line =~ /^Obsoleting/imxs ) {
next;
}
$line =~ s{[ ].*}{}mxs;
push @updates, $line;
}
}
if ( !( close $OUTPUT_HANDLER ) && ( $OS_ERROR != 0 ) ) {
# close to a piped open return false if the command with non-zero
# status. In this case $! is set to 0
exit_with_error( $plugin_module->UNKNOWN,
"Error while closing pipe to rpm: $OS_ERROR\n" );
}
if ( ( $CHILD_ERROR >> $BITS_PER_BYTE ) == $YUM_RETURN_UPDATES_AVAILABLE ) {
if ($security_plugin) {
verbose "Security updates available\n";
}
else {
verbose "Updates available\n";
}
}
elsif ( ( $CHILD_ERROR >> $BITS_PER_BYTE ) == $YUM_RETURN_ERROR ) {
exit_with_error( $plugin_module->UNKNOWN,
"Error while executing '$command'\n" );
}
return @updates;
}
##############################################################################
# Usage : check_security_option
# Purpose : checks if you supports --security (in older versions required
# a plugin)
# Returns : 1 if --security is supported, undef otherwise
# Arguments : n/a
# Throws : n/a
# Comments : n/a
# See also : n/a
sub check_security_option {
my $OUTPUT_HANDLER;
verbose "Checking if YUM/DNF supports the --security option\n", 2;
if ( $yum_executable =~ /dnf$/mxs ) {
verbose "Using DNF: no security plugin necessary\n", 2;
$security_plugin = 1;
}
else {
my $pid = open $OUTPUT_HANDLER, q{-|},
"$yum_executable --help | grep -q -- --security"
or exit_with_error( $plugin_module->UNKNOWN,
"Cannot check for security option: $OS_ERROR" );
if ( !( close $OUTPUT_HANDLER ) && ( $OS_ERROR != 0 ) ) {
# close to a piped open return false if the command with non-zero
# status. In this case $! is set to 0
exit_with_error( $plugin_module->UNKNOWN,
"Error while closing pipe to rpm: $OS_ERROR\n" );
}
if ( $CHILD_ERROR == 0 ) {
verbose "Using YUM: security plugin installed\n", 1;
$security_plugin = 1;
}
else {
verbose
"Using YUM and security plugin not installed: all updates will be considered security updates\n"
,;
}
}
return;
}
##############################################################################
# Usage : check_yum();
# Purpose : checks a yum based system for updates
# Returns : n/a
# Arguments : string with the command line arguments
# Throws : n/a
# Comments : n/a
# See also : n/a
## no critic (ProhibitExcessComplexity)
sub check_yum {
my $arguments = shift;
my $message;
check_security_option();
if ( $options->get('clean') ) {
verbose 'Cleaning YUM/DNF caches';
run_yum( '-q', 'clean all' );
}
my @outdated = run_yum($arguments);
my @security_updates;
$plugin->add_perfdata(
label => 'total_updates',
value => scalar @outdated,
uom => q{},
threshold => $threshold,
);
if ( @outdated > 0 ) {
if ( !$security_plugin ) {
if ( $options->get('security-only') ) {
verbose
"up2date does not distinguish security updates ignoring --security-only option\n";
}
# every update is critical since it could be a security problem
verbose
"no security plugin: every update could be a security problem\n";
$status = $plugin_module->CRITICAL;
verbose "DNF/YUM reports a non-up-to-date system\n";
$message = ( scalar @outdated ) . ' update';
if ( @outdated > 1 ) {
$message = $message . q{s};
}
$message = $message . ' available';
@status_lines = @outdated;
}
else {
if ( @outdated >= $options->get('warning') ) {
$status = $plugin_module->WARNING;
}
if ( @outdated >= $options->get('critical') ) {
$status = $plugin_module->CRITICAL;
}
if ( $options->get('security-only') ) {
# ignore any other update
$status = $plugin_module->OK;
}
@security_updates = run_yum( $arguments . ' --security' );
if ( @security_updates > 0 ) {
$status = $plugin_module->CRITICAL;
$message = ( scalar @security_updates ) . ' security update';
if ( @security_updates > 1 ) {
$message = $message . q{s};
}
# compute the array difference
my %count;
for my $element (@security_updates) {
push @status_lines, "$element (security)";
$count{$element}++;
}
for my $element (@outdated) {
$count{$element}++;
}
my @difference;
for my $element (@outdated) {
if ( $count{$element} == 1 ) {
push @difference, $element;
}
}
@outdated = @difference;
}
if ( @outdated > 0 ) {
if ($message) {
$message .= ' and ';
}
$message .= ( scalar @outdated ) . ' non-critical update';
if ( @outdated > 1 ) {
$message = $message . q{s};
}
push @status_lines, @outdated;
}
if ($message) {
$message .= ' available';
}
}
}
else {
$status = $plugin_module->OK;
$message = 'no updates available';
}
if ($security_plugin) {
$plugin->add_perfdata(
label => 'security_updates',
value => scalar @security_updates,
uom => q{},
threshold => $threshold,
);
}
if ($exit_message) {
$exit_message .= q{, } . $message;
}
else {
$exit_message = $message;
}
return;
}
## use critic (ProhibitExcessComplexity)
##############################################################################
# Usage : check_up2date();
# Purpose : checks an up2date based system for updates
# Returns : n/a
# Arguments : n/a
# Throws : n/a
# Comments : n/a
# See also : n/a
sub check_up2date {
if ( $options->get('clean') ) {
verbose 'clean not supported with up2date';
}
# parsing the output of up2date -l
if ( whoami() ne 'root' ) {
exit_with_error( $plugin_module->CRITICAL,
q{must be root to execute 'up2date -l': use sudo} );
}
my $output;
my $command =
q{/usr/sbin/up2date -lf | /bin/grep -A 64 -- '----------------------------------------------------------' | /bin/grep '[[:alpha:]]'};
my $pid = open $output, q{-|}, $command ## no critic (RequireBriefOpen)
or exit_with_error( $plugin_module->UNKNOWN,
"Cannot list updates: $OS_ERROR" );
while (<$output>) {
chomp;
my $line = $_;
$line =~ s/[ ].*//mxs;
push @status_lines, $line;
}
if ( !( close $output ) && ( $OS_ERROR != 0 ) ) {
# close to a piped open return false if the command with non-zero
# status. In this case $! is set to 0
exit_with_error( $plugin_module->UNKNOWN,
"Error while closing pipe to dnf/yum (listing updates): $OS_ERROR\n"
);
}
my $message;
if ( @status_lines > 0 ) {
$message = ( scalar @status_lines ) . ' update';
if ( @status_lines >= $options->get('warning') ) {
$status = $plugin_module->WARNING;
}
if ( @status_lines >= $options->get('critical') ) {
$status = $plugin_module->CRITICAL;
}
if ( @status_lines > 1 ) {
$message = $message . q{s};
}
$message = $message . ' available';
$plugin->add_perfdata(
label => 'updates',
value => scalar @status_lines,
uom => q{},
threshold => $threshold,
);
}
else {
$message = 'no updates available';
$plugin->add_perfdata(
label => 'updates',
value => 0,
uom => q{},
threshold => $threshold,
);
}
if ($exit_message) {
$exit_message .= " $message";
}
else {
$exit_message = $message;
}
return;
}
##############################################################################
# Usage : my $system = get_os_name_and_version();
# Purpose : returns the name of the system by parsing $VERSION_FILE
# Returns : name of the system
# Arguments : n/a
# Throws : CRITICAL if not able to read $VERSION_FILE
# Comments : n/a
# See also : n/a
sub get_os_name_and_version {
my $filename = shift;
if ( !$filename ) {
if ( -r '/etc/system-release' ) {
$filename = '/etc/system-release';
}
elsif ( -r '/etc/redhat-release' ) {
$filename = '/etc/redhat-release';
}
else {
exit_with_error( $plugin_module->UNKNOWN,
'Cannot detect Linux distribution' );
}
}
my $header;
if ( -r $filename ) {
my $TMP;
open $TMP, q{<},
$filename
or exit_with_error( $plugin_module->CRITICAL,
"Error opening $filename: $OS_ERROR" );
while (<$TMP>) {
chomp;
$header = $_;
last;
}
close $TMP
or exit_with_error( $plugin_module->CRITICAL,
"Error closing $filename: $OS_ERROR" );
}
else {
exit_with_error( $plugin_module->UNKNOWN,
"Cannot detect Linux distribution (no $filename file)" );
}
return $header;
}
##############################################################################
# Usage : my $updater = get_updated()
# Purpose : returns the name of updating system (yum or up2date)
# Returns : name of updating system or undef if not found
# Arguments : n/a
# Throws : n/a
# Comments : n/a
# See also : n/a
sub get_updater {
my $name = shift;
if ( $name =~ /Fedora/mxs
|| $name =~ /CentOS/mxs
|| $name =~ /PUIAS/mxs
|| $name =~ /CloudLinux/mxs
|| $name =~ /Scientific[ ]Linux/mxs
|| $name =~ /Red[ ]Hat.*[ ][567]/mxs
|| $name =~ /Oracle/mxs
|| $name =~ /Amazon/mxs )
{
return 'yum';
}
elsif ( $name =~ /Red[ ]Hat.*[ ]4/mxs ) {
return 'up2date';
}
return;
}
##############################################################################
# Usage : run();
# Purpose : main method
# Returns : n/a
# Arguments : n/a
# Throws : n/a
# Comments : n/a
# See also : n/a
sub run {
$status = $plugin_module->OK;
##############################################################################
# main
#
################
# initialization
$help = q{};