-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcheck_openmanage
executable file
·5505 lines (4940 loc) · 185 KB
/
check_openmanage
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
#!/usr/bin/perl
#
# Nagios plugin
#
# Monitor Dell server hardware status using Dell OpenManage Server
# Administrator, either locally via NRPE, or remotely via SNMP.
#
# Copyright (C) 2008-2017 Trond H. Amundsen
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful, but
# WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
# General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
#
require 5.006; # Perl v5.6.0 or newer is required
use strict;
use warnings;
use POSIX qw(isatty ceil strtol);
use Getopt::Long qw(:config no_ignore_case);
use Sys::Hostname;
# Global (package) variables used throughout the code
use vars qw( $NAME $VERSION $AUTHOR $CONTACT $E_OK $E_WARNING $E_CRITICAL
$E_UNKNOWN $FW_LOCK $USAGE $HELP $LICENSE
$snmp_session $snmp_error $omreport $globalstatus $global
$linebreak $omopt_chassis $omopt_system $blade
$exit_code $snmp
%check %opt %reverse_exitcode %status2nagios %country
%snmp_status %snmp_probestatus %probestatus2nagios %sysinfo
%blacklist %nagios_alert_count %count %snmp_enclosure %snmp_controller
@perl_warnings @controllers @enclosures @perfdata
@report_storage @report_chassis @report_other
);
#---------------------------------------------------------------------
# Initialization and global variables
#---------------------------------------------------------------------
# Collect perl warnings in an array
$SIG{__WARN__} = sub { push @perl_warnings, [@_]; };
# Version and similar info
$NAME = 'check_openmanage';
$VERSION = '3.7.13-beta';
$AUTHOR = 'Trond H. Amundsen';
$CONTACT = 't.h.amundsen@usit.uio.no';
# Exit codes
$E_OK = 0;
$E_WARNING = 1;
$E_CRITICAL = 2;
$E_UNKNOWN = 3;
# Firmware update lock file [FIXME: location on Windows?]
$FW_LOCK = '/var/lock/.spsetup'; # default on Linux
# Usage text
$USAGE = <<"END_USAGE";
Usage: $NAME [OPTION]...
END_USAGE
# Help text
$HELP = <<'END_HELP';
GENERAL OPTIONS:
-f, --config Specify configuration file
-p, --perfdata Output performance data [default=no]
-t, --timeout Plugin timeout in seconds [default=30]
-c, --critical Custom temperature critical limits
-w, --warning Custom temperature warning limits
-F, --fahrenheit Use Fahrenheit as temperature unit
-d, --debug Debug output, reports everything
-h, --help Display this help text
-V, --version Display version info
SNMP OPTIONS:
-H, --hostname Hostname or IP (required for SNMP)
-C, --community SNMP community string [default=public]
-P, --protocol SNMP protocol version [default=2c]
--port SNMP port number [default=161]
-6, --ipv6 Use IPv6 instead of IPv4 [default=no]
--tcp Use TCP instead of UDP [default=no]
OUTPUT OPTIONS:
-i, --info Prefix any alerts with the service tag
-e, --extinfo Append system info to alerts
-s, --state Prefix alerts with alert state
-S, --short-state Prefix alerts with alert state abbreviated
-o, --okinfo Verbosity when check result is OK
-B, --show-blacklist Show blacklistings in OK output
-I, --htmlinfo HTML output with clickable links
CHECK CONTROL AND BLACKLISTING:
-a, --all Check everything, even log content
-b, --blacklist Blacklist missing and/or failed components
--only Only check a certain component or alert type
--check Fine-tune which components are checked
--no-storage Don't check storage
--vdisk-critical Make any alerts on virtual disks critical
For more information and advanced options, see the manual page or URL:
https://github.com/trondham/check_openmanage
END_HELP
# Version and license text
$LICENSE = <<"END_LICENSE";
$NAME $VERSION
Copyright (C) 2008-2017 $AUTHOR
License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>
This is free software: you are free to change and redistribute it.
There is NO WARRANTY, to the extent permitted by law.
Written by $AUTHOR <$CONTACT>
END_LICENSE
# Options with default values
%opt = ( 'blacklist' => [], # blacklisting
'check' => [], # check control
'critical' => [], # temperature critical limits
'warning' => [], # temperature warning limits
'tempunit' => 'C', # temperature unit
'fahrenheit' => 0, # Use fahrenheit
'configfile' => undef, # configuration file
'timeout' => 30, # default timeout is 30 seconds
'snmp_timeout' => 5, # SNMP transport layer timeout
'debug' => 0, # debugging / verbose output
'help' => 0, # display help output
'perfdata' => undef, # output performance data
'legacy_perfdata' => 0, # legacy performance data output
'info' => 0, # display servicetag
'extinfo' => 0, # display extra info
'htmlinfo' => undef, # html tags in output
'postmsg' => undef, # post message
'state' => 0, # display alert type
'short-state' => 0, # display alert type (short)
'okinfo' => 0, # default "ok" output level
'show_blacklist' => 0, # show blacklisted components
'linebreak' => undef, # specify linebreak
'version' => 0, # plugin version info
'all' => 0, # check everything
'only' => undef, # only one component
'no_storage' => 0, # don't check storage
'omreport' => undef, # omreport path
'port' => 161, # default SNMP port
'hostname' => undef, # hostname or IP
'community' => 'public', # SMNP v1 or v2c
'protocol' => '2c', # default SNMP protocol 2c
'ipv6' => 0, # default is IPv4
'tcp' => 0, # default is UDP
'username' => undef, # SMNP v3
'authpassword' => undef, # SMNP v3
'authkey' => undef, # SMNP v3
'authprotocol' => undef, # SMNP v3
'privpassword' => undef, # SMNP v3
'privkey' => undef, # SMNP v3
'privprotocol' => undef, # SMNP v3
'use_get_table' => 0, # hack for SNMPv3 on Windows with net-snmp
'hide_servicetag' => 0, # hidden servicetag
'vdisk_critical' => 0, # make vdisk alerts critical
);
# Get options
GetOptions('b|blacklist=s' => \@{ $opt{blacklist} },
'check=s' => \@{ $opt{check} },
'c|critical=s' => \@{ $opt{critical} },
'w|warning=s' => \@{ $opt{warning} },
'tempunit=s' => \$opt{tempunit},
'F|fahrenheit' => \$opt{fahrenheit},
'f|config=s' => \$opt{configfile},
't|timeout=i' => \$opt{timeout},
'snmp-timeout=i' => \$opt{snmp_timeout},
'd|debug' => \$opt{debug},
'h|help' => \$opt{help},
'V|version' => \$opt{version},
'p|perfdata:s' => \$opt{perfdata},
'legacy-perfdata' => \$opt{legacy_perfdata},
'i|info' => \$opt{info},
'e|extinfo' => \$opt{extinfo},
'I|htmlinfo:s' => \$opt{htmlinfo},
'postmsg=s' => \$opt{postmsg},
's|state' => \$opt{state},
'S|short-state' => \$opt{shortstate},
'o|ok-info=i' => \$opt{okinfo},
'B|show-blacklist' => \$opt{show_blacklist},
'linebreak=s' => \$opt{linebreak},
'a|all' => \$opt{all},
'only=s' => \$opt{only},
'no-storage' => \$opt{no_storage},
'omreport=s' => \$opt{omreport},
'port=i' => \$opt{port},
'H|hostname=s' => \$opt{hostname},
'C|community=s' => \$opt{community},
'P|protocol=s' => \$opt{protocol},
'6|ipv6' => \$opt{ipv6},
'tcp' => \$opt{tcp},
'U|username=s' => \$opt{username},
'authpassword=s' => \$opt{authpassword},
'authkey=s' => \$opt{authkey},
'authprotocol=s' => \$opt{authprotocol},
'privpassword=s' => \$opt{privpassword},
'privkey=s' => \$opt{privkey},
'privprotocol=s' => \$opt{privprotocol},
'use-get_table' => \$opt{use_get_table},
'hide-servicetag' => \$opt{hide_servicetag},
'vdisk-critical' => \$opt{vdisk_critical},
) or do { print $USAGE; exit $E_UNKNOWN };
# If user requested help
if ($opt{help}) {
print $USAGE, $HELP;
exit $E_UNKNOWN;
}
# If user requested version info
if ($opt{version}) {
print $LICENSE;
exit $E_UNKNOWN;
}
# Initialize blacklist
%blacklist = ();
# Check flags, override available with the --check option
%check = ( 'storage' => 1, # check storage subsystem
'memory' => 1, # check memory (dimms)
'fans' => 1, # check fan status
'power' => 1, # check power supplies
'temp' => 1, # check temperature
'cpu' => 1, # check processors
'voltage' => 1, # check voltage
'batteries' => 1, # check battery probes
'amperage' => 1, # check power consumption
'intrusion' => 1, # check intrusion detection
'sdcard' => 1, # check removable flash media (SD cards)
'alertlog' => 0, # check the alert log
'esmlog' => 0, # check the ESM log (hardware log)
'esmhealth' => 1, # check the ESM log overall health
'servicetag' => 1, # check that the servicetag is sane
);
# Messages
@report_storage = (); # messages with associated nagios level (storage)
@report_chassis = (); # messages with associated nagios level (chassis)
@report_other = (); # messages with associated nagios level (other)
# Read config file
parse_configfile() if defined $opt{configfile};
# Setting timeout
$SIG{ALRM} = sub {
print "PLUGIN TIMEOUT: $NAME timed out after $opt{timeout} seconds\n";
exit $E_UNKNOWN;
};
alarm $opt{timeout};
# If we're using SNMP
$snmp = defined $opt{hostname} ? 1 : 0;
# SNMP session variables
$snmp_session = undef;
$snmp_error = undef;
# The omreport command
$omreport = undef;
# Default line break
$linebreak = isatty(*STDOUT) ? "\n" : '<br/>';
# Line break from option
if (defined $opt{linebreak}) {
if ($opt{linebreak} eq 'REG') {
$linebreak = "\n";
}
elsif ($opt{linebreak} eq 'HTML') {
$linebreak = '<br/>';
}
else {
$linebreak = $opt{linebreak};
}
}
# Exit with status=UNKNOWN if there is firmware upgrade in progress
if (!$snmp && -f $FW_LOCK) {
print "MONITORING DISABLED - Firmware update in progress ($FW_LOCK exists)\n";
exit $E_UNKNOWN;
}
# List of controllers and enclosures
@controllers = (); # controllers
@enclosures = (); # enclosures
%snmp_enclosure = (); # enclosures
# Counters for everything
%count
= (
'pdisk' => 0, # number of physical disks
'vdisk' => 0, # number of logical drives (virtual disks)
'temp' => 0, # number of temperature probes
'volt' => 0, # number of voltage probes
'amp' => 0, # number of amperage probes
'intr' => 0, # number of intrusion probes
'dimm' => 0, # number of memory modules
'mem' => 0, # total memory
'fan' => 0, # number of fan probes
'cpu' => 0, # number of CPUs
'bat' => 0, # number of batteries
'power' => 0, # number of power supplies
'sd' => 0, # number of SD cards
'esm' => {
'Critical' => 0, # critical entries in ESM log
'Non-Critical' => 0, # warning entries in ESM log
'Ok' => 0, # ok entries in ESM log
},
'alert' => {
'Critical' => 0, # critical entries in alert log
'Non-Critical' => 0, # warning entries in alert log
'Ok' => 0, # ok entries in alert log
},
);
# Performance data
@perfdata = ();
# Global health status
$global = 1; # default is to check global status
$globalstatus = $E_OK; # default global health status is "OK"
# Nagios error levels reversed
%reverse_exitcode
= (
$E_OK => 'OK',
$E_WARNING => 'WARNING',
$E_CRITICAL => 'CRITICAL',
$E_UNKNOWN => 'UNKNOWN',
);
# OpenManage (omreport) and SNMP error levels
%status2nagios
= (
'Unknown' => $E_CRITICAL,
'Critical' => $E_CRITICAL,
'Non-Critical' => $E_WARNING,
'Ok' => $E_OK,
'Non-Recoverable' => $E_CRITICAL,
'Other' => $E_CRITICAL,
);
# Status via SNMP
%snmp_status
= (
1 => 'Other',
2 => 'Unknown',
3 => 'Ok',
4 => 'Non-Critical',
5 => 'Critical',
6 => 'Non-Recoverable',
);
# Probe Status via SNMP
%snmp_probestatus
= (
1 => 'Other', # probe status is not one of the following:
2 => 'Unknown', # probe status is unknown (not known or monitored)
3 => 'Ok', # probe is reporting a value within the thresholds
4 => 'nonCriticalUpper', # probe has crossed upper noncritical threshold
5 => 'criticalUpper', # probe has crossed upper critical threshold
6 => 'nonRecoverableUpper', # probe has crossed upper non-recoverable threshold
7 => 'nonCriticalLower', # probe has crossed lower noncritical threshold
8 => 'criticalLower', # probe has crossed lower critical threshold
9 => 'nonRecoverableLower', # probe has crossed lower non-recoverable threshold
10 => 'failed', # probe is not functional
);
# Probe status translated to Nagios alarm levels
%probestatus2nagios
= (
'Other' => $E_CRITICAL,
'Unknown' => $E_CRITICAL,
'Ok' => $E_OK,
'nonCriticalUpper' => $E_WARNING,
'criticalUpper' => $E_CRITICAL,
'nonRecoverableUpper' => $E_CRITICAL,
'nonCriticalLower' => $E_WARNING,
'criticalLower' => $E_CRITICAL,
'nonRecoverableLower' => $E_CRITICAL,
'failed' => $E_CRITICAL,
);
# System information gathered
%sysinfo
= (
'hostname' => 'N/A', # hostname
'bios' => 'N/A', # BIOS version
'biosdate' => 'N/A', # BIOS release date
'serial' => 'N/A', # serial number (service tag)
'express_sc' => 'N/A', # express service code
'model' => 'N/A', # system model
'rev' => q{}, # system revision
'osname' => 'N/A', # OS name
'osver' => 'N/A', # OS version
'om' => 'N/A', # OMSA version
'bmc' => 0, # HAS baseboard management controller (BMC)
'rac' => 0, # HAS remote access controller (RAC)
'rac_name' => 'N/A', # remote access controller (RAC)
'bmc_fw' => 'N/A', # BMC firmware
'rac_fw' => 'N/A', # RAC firmware
);
# Country and language list for URL generation
%country
= (
# EMEA
at => { c => 'at', l => 'de' }, # Austria
be => { c => 'be', l => 'nl' }, # Belgium
cz => { c => 'cz', l => 'cs' }, # Czech Republic
de => { c => 'de', l => 'de' }, # Germany
dk => { c => 'dk', l => 'da' }, # Denmark
es => { c => 'es', l => 'es' }, # Spain
fi => { c => 'fi', l => 'fi' }, # Finland
fr => { c => 'fr', l => 'fr' }, # France
gr => { c => 'gr', l => 'el' }, # Greece
it => { c => 'it', l => 'it' }, # Italy
il => { c => 'il', l => 'en' }, # Israel
me => { c => 'me', l => 'en' }, # Middle East
no => { c => 'no', l => 'no' }, # Norway
nl => { c => 'nl', l => 'nl' }, # The Netherlands
pl => { c => 'pl', l => 'pl' }, # Poland
pt => { c => 'pt', l => 'pt' }, # Portugal
ru => { c => 'ru', l => 'ru' }, # Russia
se => { c => 'se', l => 'sv' }, # Sweden
uk => { c => 'uk', l => 'en' }, # United Kingdom
za => { c => 'za', l => 'en' }, # South Africa
# America
br => { c => 'br', l => 'pt' }, # Brazil
ca => { c => 'ca', l => 'en' }, # Canada
mx => { c => 'mx', l => 'es' }, # Mexico
us => { c => 'us', l => 'en' }, # USA
# Asia/Pacific
au => { c => 'au', l => 'en' }, # Australia
cn => { c => 'cn', l => 'zh' }, # China
in => { c => 'in', l => 'en' }, # India
jp => { c => 'jp', l => 'ja' }, # Japan
);
# Adjust which checks to perform
adjust_checks() if defined $opt{check};
# Blacklisted components
set_blacklist($opt{blacklist}) if defined $opt{blacklist};
# If blacklisting is in effect, don't check global health status
if (scalar keys %blacklist > 0) {
$global = 0;
}
# Take into account new hardware and blades
$omopt_chassis = 'chassis'; # default "chassis" option to omreport
$omopt_system = 'system'; # default "system" option to omreport
$blade = 0; # if this is a blade system
# Some initializations and checking before we begin
if ($snmp) {
snmp_initialize(); # initialize SNMP
snmp_check(); # check that SNMP works
snmp_detect_blade(); # detect blade via SNMP
}
else {
# Find the omreport binary
find_omreport();
# Check help output from omreport, see which options are available.
# Also detecting blade via omreport.
check_omreport_options();
}
# Temperature unit
if ($opt{fahrenheit}) {
$opt{tempunit} = 'F';
}
# Check tempunit syntax
if ($opt{tempunit} !~ m{\A C|F|K|R \z}xms) {
print "ERROR: Unknown temperature unit '$opt{tempunit}'\n";
exit $E_UNKNOWN;
}
#---------------------------------------------------------------------
# Helper functions
#---------------------------------------------------------------------
# Make a regex from a glob pattern. Shamelessly stolen from Perl
# Cookbook chapter 6.9
sub glob2regex {
my $globstr = shift;
my %patmap
= ( '*' => '.*',
'?' => '.',
'[' => '[',
']' => ']',
);
$globstr =~ s{(.)} { $patmap{$1} || "\Q$1" }ge;
return '\A' . $globstr . '\z';
}
#
# Read config file
#
sub parse_configfile {
our $tiny = undef;
# Regexp for boolean values
our $off = qr{\A (0|off|false) \s* \z}ixms;
our $on = qr{\A (1|on|true) \s* \z}ixms;
# Mapping between command line options and the corresponding
# config file options
our %opt2config
= ( 'info' => 'output_servicetag',
'extinfo' => 'output_sysinfo',
'postmsg' => 'output_post_message',
'state' => 'output_servicestate',
'shortstate' => 'output_servicestate_abbr',
'show_blacklist' => 'output_blacklist',
'hide_servicetag' => 'output_hide_servicetag',
'htmlinfo' => 'output_html',
'okinfo' => 'output_ok_verbosity',
'protocol' => 'snmp_version',
'community' => 'snmp_community',
'port' => 'snmp_port',
'ipv6' => 'snmp_use_ipv6',
'tcp' => 'snmp_use_tcp',
'warning' => 'temp_threshold_warning',
'critical' => 'temp_threshold_critical',
'all' => 'check_everything',
'perfdata' => 'performance_data',
'tempunit' => 'temperature_unit',
'timeout' => 'timeout',
'snmp_timeout' => 'snmp_timeout',
'blacklist' => 'blacklist',
'legacy_perfdata' => 'legacy_performance_data',
'vdisk_critical' => 'vdisk_critical',
);
# Load the perl module
if ( eval { require Config::Tiny; 1 } ) {
$tiny = Config::Tiny->new();
}
else {
print "ERROR: Required perl module 'Config::Tiny' not found\n";
exit $E_UNKNOWN;
}
# Read the config file
$tiny = Config::Tiny->read($opt{configfile})
or do { report('other', (sprintf q{Couldn't read configuration file: %s}, Config::Tiny->errstr()), $E_UNKNOWN);
return; };
# Syntax check
foreach my $section (keys %{ $tiny }) {
KEYWORD:
foreach my $keyword (keys %{ $tiny->{$section} }) {
next KEYWORD if $keyword eq 'check_everything';
if ($keyword =~ m{\A check_(.+)}xms) {
my $c = $1;
foreach my $cl (keys %check) {
next KEYWORD if $c eq $cl;
}
}
else {
LEGAL:
foreach my $legal (keys %opt2config) {
next KEYWORD if $keyword eq $opt2config{$legal};
}
}
if ($section eq '_') {
report('other', qq{CONFIG ERROR: In the global section: Unknown statement "$keyword"}, $E_UNKNOWN);
}
else {
report('other', qq{CONFIG ERROR: Unknown statement "$keyword" in section "$section"}, $E_UNKNOWN);
}
}
}
# Adjust checks according to statements in the configuration file
sub configfile_adjust_checks {
my $keyword = shift;
CHECK_CONFIG:
foreach my $key (keys %check) {
my $copt = join '_', 'check', $key;
next CHECK_CONFIG if !defined $tiny->{$keyword}->{$copt} or $tiny->{$keyword}->{$copt} eq q{};
if ($tiny->{$keyword}->{$copt} =~ m{$on}ixms) {
$check{$key} = 1;
}
elsif ($tiny->{$keyword}->{$copt} =~ m{$off}ixms) {
$check{$key} = 0;
}
else {
report('other', "CONFIG ERROR: Rvalue for '$copt' must be boolean (True/False)", $E_UNKNOWN);
}
}
return;
}
# Set blacklist according to statements in the configuration file
sub configfile_set_blacklist {
my $keyword = shift;
if (defined $tiny->{$keyword}->{blacklist} and $tiny->{$keyword}->{blacklist} ne q{}) {
# set_blacklist() takes an array ref
set_blacklist([$tiny->{$keyword}->{blacklist}]);
}
return;
}
# Set timeout according to statements in the configuration file
sub configfile_set_timeout {
my $keyword = shift;
if (defined $tiny->{$keyword}->{timeout} and $tiny->{$keyword}->{timeout} ne q{}) {
if ($tiny->{$keyword}->{timeout} =~ m{\A \d+ \z}xms) { # integer
$opt{timeout} = $tiny->{$keyword}->{timeout};
}
else {
report('other', "CONFIG ERROR: Rvalue for 'timeout' must be a positive integer", $E_UNKNOWN);
}
}
return;
}
# Set SNMP timeout according to statements in the configuration file
sub configfile_set_snmp_timeout {
my $keyword = shift;
if (defined $tiny->{$keyword}->{snmp_timeout} and $tiny->{$keyword}->{snmp_timeout} ne q{}) {
if ($tiny->{$keyword}->{snmp_timeout} =~ m{\A \d+ \z}xms) { # integer
$opt{snmp_timeout} = $tiny->{$keyword}->{snmp_timeout};
}
else {
report('other', "CONFIG ERROR: Rvalue for 'snmp_timeout' must be a positive integer", $E_UNKNOWN);
}
}
return;
}
# Set a boolean option
sub configfile_set_boolean {
my ($keyword, $bool) = @_;
my $cbool = $opt2config{$bool};
if (defined $tiny->{$keyword}->{$cbool} and $tiny->{$keyword}->{$cbool} ne q{}) {
if ($tiny->{$keyword}->{$cbool} =~ m{$on}ixms) {
$opt{$bool} = 1;
}
elsif ($tiny->{$keyword}->{$cbool} =~ m{$off}ixms) {
$opt{$bool} = 0;
}
else {
report('other', "CONFIG ERROR: Rvalue for '$cbool' must be boolean (True/False)", $E_UNKNOWN);
}
}
return;
}
# Set htmlinfo option from config file
sub configfile_set_htmlinfo {
my $keyword = shift;
my $conf = $opt2config{htmlinfo};
if (defined $tiny->{$keyword}->{$conf} and $tiny->{$keyword}->{$conf} ne q{}) {
if ($tiny->{$keyword}->{$conf} =~ m{$on}ixms) {
$opt{htmlinfo} = 1;
}
elsif ($tiny->{$keyword}->{$conf} =~ m{$off}ixms) {
$opt{htmlinfo} = undef;
}
else {
$opt{htmlinfo} = $tiny->{$keyword}->{$conf};
}
}
return;
}
# Set OK output verbosity
sub configfile_set_ok_verbosity {
my $keyword = shift;
my $conf = $opt2config{okinfo};
if (defined $tiny->{$keyword}->{$conf} and $tiny->{$keyword}->{$conf} ne q{}) {
if ($tiny->{$keyword}->{$conf} =~ m{\A \d+ \z}xms) {
$opt{okinfo} = $tiny->{$keyword}->{$conf};
}
else {
report('other', "CONFIG ERROR: Rvalue for '$conf' must be a positive integer", $E_UNKNOWN);
}
}
return;
}
# Set SNMP protocol version from config file
sub configfile_set_snmp_version {
my $keyword = shift;
my $conf = $opt2config{protocol};
if (defined $tiny->{$keyword}->{$conf} and $tiny->{$keyword}->{$conf} ne q{}) {
if ($tiny->{$keyword}->{$conf} =~ m{\A (1|2(?:c)?|3) \z}xms) {
$opt{protocol} = $tiny->{$keyword}->{$conf};
}
else {
report('other', "CONFIG ERROR: Rvalue for '$conf' must be '1', '2', '2c' or '3'", $E_UNKNOWN);
}
}
return;
}
# Set SNMP community name from config file
sub configfile_set_snmp_community {
my $keyword = shift;
my $conf = $opt2config{community};
if (defined $tiny->{$keyword}->{$conf} and $tiny->{$keyword}->{$conf} ne q{}) {
$opt{community} = $tiny->{$keyword}->{$conf};
}
return;
}
# Set SNMP port number from config file
sub configfile_set_snmp_port {
my $keyword = shift;
my $conf = $opt2config{port};
if (defined $tiny->{$keyword}->{$conf} and $tiny->{$keyword}->{$conf} ne q{}) {
if ($tiny->{$keyword}->{$conf} =~ m{\A \d+ \z}xms) { # integer
$opt{port} = $tiny->{$keyword}->{$conf};
}
else {
report('other', "CONFIG ERROR: Rvalue for '$conf' must be a positive integer", $E_UNKNOWN);
}
}
return;
}
# Set temperature threshold from config file
sub configfile_set_temp_threshold {
my $keyword = shift;
my $level = shift;
my $conf = $opt2config{$level};
if (defined $tiny->{$keyword}->{$conf} and $tiny->{$keyword}->{$conf} ne q{}) {
$opt{$level} = [$tiny->{$keyword}->{$conf}]; # array ref
}
return;
}
# Set perfdata from config file
sub configfile_set_perfdata {
my $keyword = shift;
my $conf = $opt2config{perfdata};
if (defined $tiny->{$keyword}->{$conf} and $tiny->{$keyword}->{$conf} ne q{}) {
if ($tiny->{$keyword}->{$conf} =~ m{$on}ixms) {
$opt{perfdata} = 1;
}
elsif ($tiny->{$keyword}->{$conf} =~ m{$off}ixms) {
$opt{perfdata} = undef;
}
elsif ($tiny->{$keyword}->{$conf} =~ m{\A minimal|multiline \z}xms) {
$opt{perfdata} = $tiny->{$keyword}->{$conf};
}
else {
report('other', "CONFIG ERROR: Rvalue for '$conf' must be either boolean, 'minimal' or 'multiline'", $E_UNKNOWN);
}
}
return;
}
# Set temp unit from config file
sub configfile_set_tempunit {
my $keyword = shift;
my $conf = $opt2config{tempunit};
if (defined $tiny->{$keyword}->{$conf} and $tiny->{$keyword}->{$conf} ne q{}) {
if ($tiny->{$keyword}->{$conf} =~ m{\A C|F|K|R \z}ixms) {
$opt{tempunit} = $tiny->{$keyword}->{$conf};
}
else {
report('other', "CONFIG ERROR: Rvalue for '$conf' must one of C/F/K/R", $E_UNKNOWN);
}
}
return;
}
# Set postmsg string from config file
sub configfile_set_postmsg {
my $keyword = shift;
my $conf = $opt2config{postmsg};
if (defined $tiny->{$keyword}->{$conf} and $tiny->{$keyword}->{$conf} ne q{}) {
$opt{postmsg} = $tiny->{$keyword}->{$conf}; # array ref
}
return;
}
# Sections in the config file to check for statements
my @sections = ();
# First: Populate the sections array with the global section
@sections = ('_');
# Last two steps only if hostname is defined
if (defined $opt{hostname}) {
# Second: Populate the sections array with host glob pattern (but
# not exact match)
PATTERN:
foreach my $glob (sort keys %{ $tiny }) {
next PATTERN if $glob eq '_'; # global section
next PATTERN if $glob eq $opt{hostname}; # exact match
my $regex = glob2regex($glob); # make regexp
if ($opt{hostname} =~ m{$regex}) {
push @sections, $glob;
}
}
# Third: Populate the sections array with exact hostname
if (defined $tiny->{$opt{hostname}}) {
push @sections, $opt{hostname};
}
}
# Loop through the sections array and get options
foreach my $sect (@sections) {
configfile_adjust_checks($sect);
configfile_set_blacklist($sect);
configfile_set_timeout($sect);
configfile_set_snmp_timeout($sect);
configfile_set_htmlinfo($sect);
configfile_set_ok_verbosity($sect);
configfile_set_boolean($sect, 'all');
configfile_set_boolean($sect, 'info');
configfile_set_boolean($sect, 'extinfo');
configfile_set_boolean($sect, 'state');
configfile_set_boolean($sect, 'shortstate');
configfile_set_boolean($sect, 'show_blacklist');
configfile_set_boolean($sect, 'ipv6');
configfile_set_boolean($sect, 'tcp');
configfile_set_boolean($sect, 'legacy_perfdata');
configfile_set_boolean($sect, 'hide_servicetag');
configfile_set_boolean($sect, 'vdisk_critical');
configfile_set_snmp_version($sect);
configfile_set_snmp_community($sect);
configfile_set_snmp_port($sect);
configfile_set_temp_threshold($sect, 'warning');
configfile_set_temp_threshold($sect, 'critical');
configfile_set_perfdata($sect);
configfile_set_tempunit($sect);
configfile_set_postmsg($sect);
}
return;
}
#
# Store a message in one of the message arrays
#
sub report {
my ($type, $msg, $exval, $id) = @_;
defined $id or $id = q{};
my %type2array
= (
'storage' => \@report_storage,
'chassis' => \@report_chassis,
'other' => \@report_other,
);
return push @{ $type2array{$type} }, [ $msg, $exval, $id ];
}
#
# Run command, put resulting output lines in an array and return a
# pointer to that array
#
sub run_command {
my $command = shift;
open my $CMD, '-|', $command
or do { report('other', "Couldn't run command '$command': $!", $E_UNKNOWN)
and return [] };
my @lines = <$CMD>;
close $CMD;
return \@lines;
}
#
# Run command, put resulting output in a string variable and return it
#
sub slurp_command {
my $command = shift;
open my $CMD, '-|', $command
or do { report('other', "Couldn't run command '$command': $!", $E_UNKNOWN) and return };
my $rawtext = do { local $/ = undef; <$CMD> }; # slurping
close $CMD;
# NOTE: We don't check the return value of close() since omreport
# does something weird sometimes.
return $rawtext;
}
#
# Initialize SNMP
#
sub snmp_initialize {
# Legal SNMP v3 protocols
my $snmp_v3_privprotocol = qr{\A des|aes|aes128|3des|3desde \z}xms;
my $snmp_v3_authprotocol = qr{\A md5|sha \z}xms;
# Parameters to Net::SNMP->session()
my %param
= (
'-port' => $opt{port},
'-hostname' => $opt{hostname},
'-version' => $opt{protocol},
'-timeout' => $opt{snmp_timeout},
);
# Setting the domain (IP version and transport protocol)
my $transport = $opt{tcp} ? 'tcp' : 'udp';
my $ipversion = $opt{ipv6} ? 'ipv6' : 'ipv4';
$param{'-domain'} = "$transport/$ipversion";
# Parameters for SNMP v3
if ($opt{protocol} eq '3') {
# Username is mandatory
if (defined $opt{username}) {
$param{'-username'} = $opt{username};
}
else {
print "SNMP ERROR: With SNMPv3 a username must be specified\n";
exit $E_UNKNOWN;
}
# Authpassword is optional
if (defined $opt{authpassword}) {
$param{'-authpassword'} = $opt{authpassword};
}
# Authkey is optional
if (defined $opt{authkey}) {
$param{'-authkey'} = $opt{authkey};
}
# Privpassword is optional
if (defined $opt{privpassword}) {
$param{'-privpassword'} = $opt{privpassword};
}
# Privkey is optional
if (defined $opt{privkey}) {
$param{'-privkey'} = $opt{privkey};
}
# Privprotocol is optional
if (defined $opt{privprotocol}) {
if ($opt{privprotocol} =~ m/$snmp_v3_privprotocol/xms) {
$param{'-privprotocol'} = $opt{privprotocol};
}
else {
print "SNMP ERROR: Unknown or invalid privprotocol [$opt{privprotocol}], "
. "must be one of [des|aes|aes128|3des|3desde]\n";
exit $E_UNKNOWN;
}
}
# Authprotocol is optional
if (defined $opt{authprotocol}) {
if ($opt{authprotocol} =~ m/$snmp_v3_authprotocol/xms) {
$param{'-authprotocol'} = $opt{authprotocol};
}
else {
print "SNMP ERROR: Unknown or invalid authprotocol [$opt{authprotocol}], "
. "must be one of [md5|sha]\n";
exit $E_UNKNOWN;
}
}
}
# Parameters for SNMP v2c or v1
elsif ($opt{protocol} =~ m{\A (1|2(?:c)?) \z}xms) {
$param{'-community'} = $opt{community};
}
else {
print "SNMP ERROR: Unknown or invalid SNMP version [$opt{protocol}]\n";
exit $E_UNKNOWN;
}
# Try to initialize the SNMP session
if ( eval { require Net::SNMP; 1 } ) {
($snmp_session, $snmp_error) = Net::SNMP->session( %param );
if (!defined $snmp_session) {
printf "SNMP: %s\n", $snmp_error;