forked from willixix/naglio-plugins
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Naglio.pm
1807 lines (1701 loc) · 83 KB
/
Naglio.pm
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
package Naglio;
use strict;
use fields qw();
use Text::ParseWords;
use base Exporter;
our $NAME = 'naglio';
our $VERSION = '0.3_01';
our $LICENSE = 'GNU Lesser General Public License (LGPL)';
our $URL = 'http://william.leibzon.org/nagios/';
our $AUTHOR = "William Leibzon";
# ====================================== SUMMARY ================================================
#
# Name : Naglio Perl Library For Developing Nagios Plugins
# Version : 0.3
# Date : Apr ??, 2013
# Author : William Leibzon - [email protected]
# Licence : LGPL - full text at http://www.fsf.org/licenses/lgpl.txt
#
# THIS IS WORK IN PROGRESS, THE LIBRARY HAS NOT BEEN RELEASED YET AND INTERFACES MAY CHANGE
#
# ============================= LIBRARY HISTORY AND VERSIONS ====================================
#
# Note: you may safely skip this section if you're looking at documentation about this library or plugin
#
# [2006-2008] The history of this library goes back to plugins such as check_snmp_temperature.pl,
# check_mysqld,pl and others released as early as 2006 with common functions to
# support prefixes "<,>,=,!" for specifying thresholds and checking data against
# these thresholds. Several of my plugins had common architecture supporting multiple
# variables or attributes to be checked using -a/--attributes/--variables option and
# --warn and --crit options with list of thresholds for these attributes and --perfvars
# specifying variables whose data would only go as PERFOUT for graphing.
#
# [2008-2011] Threshold parsing and check code had been rewritten and support added for specifying
# range per plugin guidelines: http://nagiosplug.sourceforge.net/developer-guidelines.html
# Internal structures had been changing and becoming more complex to various cases.
# In 2010-2012 plugins started to get support for ;warn;crit output of thresholds in perf,
# as specified in the guidelines.
#
# [Early 2012] Code from check_memcached had been used as a base for check_memcached and then
# check_redis plugins with some of the latest threshold code from check_netstat
# with more updates. Starting with check_redis the code from check_options() and
# from main part of plugin that was very similar across my plugins were separated
# into their own functions. KNOWN_STATS_VARS array was introduced as well to be
# able to properly add UOM symbol ('c', '%', 's', 'ms', 'B', 'KB') to perfout.
# check_memcached and check_redis also included support for calculating rate of
# variables in a similar way to how its been done in check_snmp_netint
#
# [0.1 - July 17, 2012] In 0.6 release of check_redis.pl support had been added for long options
# with special threshold line syntax:
# --option=WARN:threshold,CRIT:threshold,ABSENT:OK|WARNING|CRITICAL|UNKNOWN,DISPLAY:YES|NO,PERF:YES|NO
# This was extension from just doing --option=WARN,CRIT to have a more universal
# and extendable way to specify and alike parameters for checking. check_redis 0.6
# also introduced support automatically adding long options with above syntax based
# on description in KNOWN_STATS_VARS. The functions for the library were all separated
# into their own section of the code. When inported to check_memcached global variables
# were added to that section and accessor functions written for some of them.
# This is considered 0.1 version of the library
#
# [0.2 - Aug 28, 2012] In August the library code in check_memcached had been re-written from
# just functions to object-oriented perl interface. All variables were hidden from
# direct access with accessor functions written. Documentation header had been added
# to each library function and the header for the library itself. This was major work
# taking over a week to do although functions and mainly sllllame as in 0.1. They are
# not stabilized and so library is only to be included within plugins. Support was
# also added for regex matching with PATTERN option spec. Also added NAME spec.
# License changed to LGPL from GPL for this code.
# [0.21 - Sep 3, 2012] Fix bug in handling absent data
# [0.22 - Mar 23, 2013] Fixed bug in parse_threshold function (reported by Charlie Langrall)
#
# ================================== LIBRARY TODO =================================================
#
# (a) Add library function to support '--extra-opts' to read plugin options from a file
# This is to be compatible with http://nagiosplugins.org/extra-opts
# (b) Support regex matching and allowing multiple data for same threshold definition.
# [DONE]
# (c) Support for expressions in places of numeric values for thresholds. The idea is to allow
# to refer to another variable or to special macro. I know at least one person has extended
# my check_mysqld to support using mysql variables (not same as status data) for thresholds.
# I also previouslyh had planned such support with experimental check_snmp_attributes plugin
# library/base. The idea was also floated around on nagios-devel list.
# (d) Support specifying variables as expressions. This is straight out of check_snmp_atributes
# and maybe part of it can be reused for this
# (e) Add common SNMP functions into library as so many of my plugins use it#
# (f) Add more functions to make this library easier to use and stabilize its interfaces.
# Port my plugins to this library.
# (f) Add support for functions in Nagios-Plugins perl library. While its interfaces are
# different, I believe, it'd be possible to add "shim" code to support them too.
# (h) Write proper Perl-style documentation as well as web documentation (much of above maybe
# moved to web documentation) and move library to separate GITHUB project. Release it.
# (i) Port this library to Python and write one or two example plugins
#
# ================================================================================================
$0 = $NAME;
our @ISA = qw(Exporter);
our @EXPORT_OK = qw(%ERRORS %STATUS_TEXT verb readable_time trim isnum);
our @EXPORT = qw()
our %ERRORS = ('OK'=>0,'WARNING'=>1,'CRITICAL'=>2,'UNKNOWN'=>3,'DEPENDENT'=>4);
our %STATUS_TEXT = (0=>'OK',1=>'WARNING',2=>'CRITICAL',3=>'UNKNOWN',4=>'DEPENDENT');
my $DEFAULT_PERF_OK_STATUS_REGEX = 'GAUGE|COUNTER|^DATA$|BOOLEAN';
############################ START OF THE LIBRARY FUNCTIONS #####################################
# @DESCRIPTION : Library object constructor
# @LAST CHANGED : 08-27-12 by WL
# @INPUT : Hash array of named config settings. All parameters are optiona. Currently supported are:
# plugin_name => string - short name of the plugin
# plugin_description => string - plugin longer description
# plugin_authors => string - list of plugin authors
# knownStatsVars => reference to hash - hash array defining known variables, what type they are, their description
# usage_text => string, - text string to display when calling default usage function
# usage_function => &ref - OR function that would display helpful text in case of error with options for this plugin
# verbose => 1 or "" or "filename" - set to 1 or "" if verbose/debug or to filename to send data to (may not be called "0" or "1")
# output_comparison_symbols => 0 or 1 - 1 means library output in case threshold is met can use "<", ">", "="
# 0 means output is something like "less than or equal", "more than", etc.
# all_variables_perf => 0 or 1 - 1 means data for all variables would go to PERF. This is what '-A *' or just -A do
# enable_long_options => 0 or 1 - 1 enables long options generated based on knownStatsVars. This is automatically enabled (from 0
# to 1) when plugin references additional_options_list() unless this is set to -1 at library init
# enable_rate_of_change => 0 or 1 - enables support for calculating rate of change based on previously saved data, default is 1
# enable_regex_match => 0 or 1 - when set to 1 each threshold-specified var name is treated as regex and can match
# to multiple collected data. this can also be enabled per-variable with PATTERN spec
# @RETURNS : Reference representing object instance of this library
# @PRIVACY & USE : PUBLIC, To be used when initializing the library
sub lib_init {
my $invocant = shift;
my $class = ref($invocant) || $invocant;
my %other_args = @_;
# These used to be global variables, now these are object local variables in self with accessor
my @allVars = (); # all variables after options processing
my @perfVars = (); # performance variables list [renamed from @o_perfVarsL in earlier code]
my %thresholds=(); # hash array of thresholds for above variables, [this replaced @o_warnL and @o_critL in earlier code]
my %dataresults= (); # This is where data is loaded. It is a hash with variable names as keys and array for value:
# $dataresults{$var}[0] - undef of value of this variable
# $dataresults{$var}[1] - 0 if variable not printed out to status line yet, 1 or more otherwise
# $dataresults{$var}[2] - 0 if variable data not yet put into PERF output, -1 if PERF output is preset, 1 after output
# $dataresults{$var}[3] - string, '' to start with, holds ready performance data output for this variable
# $dataresults{$var}[4] - only for regex matches. name of match var (which should be key in thresholds), otherwise undef
my %dataVars = (); # keys are variables from allVars and perfVars, values is array of data that matched i.e. keys in dataresults
my @ar_warnLv = (); # used during options processing
my @ar_critLv = (); # used during options processing
my @ar_varsL= (); # used during options processing
my @prev_time= (); # timestamps if more then one set of previois performance data
my $self = { # library and nagios versions
_NaglioLibraryVersion => 0.2, # this library's version
_NagiosVersion => 3, # assume nagios core 3.x unless known otherwise
# library internal data structures
_allVars => \@allVars,
_perfVars => \@perfVars,
_thresholds => \%thresholds,
_dataresults => \%dataresults,
_datavars => \%dataVars,
_ar_warnLv => \@ar_warnLv,
_ar_critLv => \@ar_critLv,
_ar_varsL => \@ar_varsL,
_prevTime => \@prev_time,
_prevPerf => {}, # array that is populated with previous performance data
_checkTime => undef, # time when data was last checked
_statuscode => "OK", # final status code
_statusinfo => "", # if there is an error, this has human info about what it is
_statusdata => "", # if there is no error but we want some data in status line, this var gets it
_perfdata => "", # this variable collects performance data line
_saveddata => "", # collects saved data (for next plugin re-run, not implimented yet)
_init_args => \%other_args,
# copy of data from plugin option variables
o_variables => undef, # List of variables for warn and critical checks
o_crit => undef, # Comma-separated list of critical thresholds for each checked variable
o_warn => undef, # Comma-separated list of warning thresholds for each checked variable
o_perf => undef, # defined or undef. perf option means all data from variables also goes as PERFDATA
o_perfvars => undef, # List of variables only for PERFDATA
o_prevperf => undef, # previously saved performance data coming from $SERVICEPERFDATA$ macro
# library special input variables (similar to options)
o_rprefix => '', # prefix used to distinguish rate variables
o_rsuffix => '_rate', # suffix used to distinguish rate variables
knownStatusVars => {}, # Special HASH ARRAY with names and description of known variables
perfOKStatusRegex => $DEFAULT_PERF_OK_STATUS_REGEX,
verbose => 0, # verbose, same as debug, same as o_verb
plugin_name => '', # this and next 3 parameters are currently not used
plugin_description => '', # but its still better if these are provided
plugin_authors => '', # in the future these maybe used for help & usage functions
usage_text => '' # usage text, this is alternative to usage_function()
# library setting variables
debug_file => "", # instead of setting file name in verbose, can also set it here
output_comparison_symbols => 1, # should plugin output >,<.=,! for threshold match
# if 0, it will say it in human form, i.e. "less"
all_variables_perf => 0, # should we all variables go to PERF (even those not listed in o_variables and o_perfvars)
# this is the option set to 1 when --perfvars '*' is used
enable_long_options => 0, # enable support for long options generated based on knownStatusVars description
enable_rate_of_change => 1, # enables support for calculating rate of change and for rate of change long options
enable_regex_match => 0, # 0 is not enabled, 1 means variables in o_variables and o_perfvars are considered regex to match actual data
# a value of 2 means its enabled, but for options with PATTERN specifier (this is not configurable value)
};
# bless to create an object
bless $self, $class;
# deal with arguments that maybe passed to library when initalizing
if (exists($other_args{'KNOWN_STATUS_VARS'})) {
$self->{'knownStatusVars'} = $other_args{'KNOWN_STATUS_VARS'};
}
$self->{'plugin_name'} = $other_args{'plugin_name'} if exists($other_args{'plugin_name'});
$self->{'plugin_description'} = $other_args{'plugin_description'} if exists($other_args{'plugin_description'});
$self->{'plugin_authors'} = $other_args{'plugin_authors'} if exists($other_args{'plugin_authors'});
$self->{'_usage_function'} = $other_args{'usage_function'} if exists($other_args{'usage_function'});
$self->{'usage_text'} = $other_args{'usage'} if exists($other_args{'usage'});
$self->configure(%other_args);
# return self object
return $self;
}
# This is just an alias for object constructor lib_init function
sub new {
return lib_init(@_);
}
# @DESCRIPTION : Allows to confiure some settings after initialization (all these can also be done as part of lib_init)
# @LAST CHANGED : 08-27-12 by WL
# @INPUT : Hash array of named config settings. All parameters are optiona. Currently supported are:
# verbose => 1 or "" or "filename" - set to 1 or "" if verbose/debug or to filename to send data to (may not be called "0" or "1")
# output_comparison_symbols => 0 or 1 - 1 means library output in case threshold is met can use "<", ">", "="
# 0 means output is something like "less than or equal", "more than", etc.
# all_variables_perf => 0 or 1 - 1 means data for all variables would go to PERF. This is what '-A *' or just -A do
# enable_long_options => 0 or 1 - 1 enables long options generated based on knownStatsVars. This is automatically enabled (from 0
# to 1) when plugin references additional_options_list() unless this is set to -1 at library init
# enable_rate_of_change => 0 or 1 - enables support for calculating rate of change based on previously saved data, default is 1
# enable_regex_match => 0 or 1 - when set to 1 each threshold-specified var name is treated as regex and can match
# to multiple collected data. this can also be enabled per-variable with PATTERN spec
# @RETURNS : nothing (future: 1 on success, 0 on error)
# @PRIVACY & USE : PUBLIC, Must be used as an object instance function.
sub configure {
my $self = shift;
my %args = @_;
if (exists($args{'verbose'}) || exists($args{'debug'})) {
$self->{'verbose'} = 1;
if (exists($args{'verbose'}) && $args{'verbose'}) {
$self->{'debug_file'} = $args{'verbose'};
}
if (exists($args{'debug_log_filename'})) {
$self->{'debug_file'} = $args{'debug_log_filename'};
}
}
$self->{'all_variables_perf'} = $args{'all_variables_perf'} if exists($args{'all_variables_perf'});
$self->{'enable_long_options'} = $args{'enable_long_options'} if exists($args{'enable_long_options'});
$self->{'enable_rate_of_change'} = $args{'enable_rate_of_change'} if exists($args{'enable_rate_of_change'});
$self->{'enable_regex_match'} = 1 if exists($args{'enable_regex_match'}) && $args{'enable_regex_match'}!=0;
$self->{'output_comparison_symbols'} = $args{'output_comparison_symbols'} if exists($args{'output_comparison_symbols'});
}
# @DESCRIPTION : Allows functions to be used both directly and as an object referenced functions
# In the 2nd case they get $self as 1st argument, in 1st they don't. So this just adds
# $self if its if its not there so their argument list is known.
# Functions that allow both should still check if $self is defined.
# @LAST CHANGED : 08-20-12 by WL
# @INPUT : arbitrary list of arguments
# @RETURNS : arbitrary list of arguments with 1st being object hash or undef
# @PRIVACY & USE : PRIVATE
sub _self_args {
return @_ if ref($_[0]) && exists($_[0]->{'_NaglioLibraryVersion'});
unshift @_,undef;
return @_;
}
# @DESCRIPTION : Sets function to be called to display help text on using plugin in case of error
# @LAST CHANGED : 08-22-12 by WL
# @INPUT : reference to usage function
# @RETURNS : nothing
# @PRIVACY & USE : PUBLIC, Must be used as an object instance function :
sub set_usage_function {
my ($self, $usage_function) = @_;
$self->{'_usage_function'} = $usage_function;
}
# @DESCRIPTION : Usage function. For right now it just calls usage function given as a parameter
# In the future if it is not available, it'll print something standard.
# @LAST CHANGED : 08-22-12 by WL
# @INPUT : none
# @RETURNS : nothing
# @PRIVACY & USE : PUBLIC, But primary for internal use. Must be used as an object instance function.
sub usage {
my $self = shift;
if (defined($self) && defined($self->{'_usage_function'})) { &{$self->{'_usage_function'}}(); }
else if (defined($self) && defined($self->{'usage_text'})) { print $self->{'usage_text'}; print "\n"; }
}
# @DESCRIPTION : This function converts time in seconds to nice & short output format
# @LAST_CHANGED : 02-10-13 by WL
# @INPUT : ARG1 - time in seconds
# @RETURNS : string of time for human consumption
# @PRIVACY & USE : PUBLIC, Maybe used directly or as an object instance function :
sub readable_time {
my ($self,$total_sec) = _self_args(@_);
my ($sec,$mins,$hrs,$days);
my $txtout="";
sub div_mod { return int( $_[0]/$_[1]) , ($_[0] % $_[1]); }
($mins,$sec) = div_mod($total_sec,60);
($hrs,$mins) = div_mod($mins,60);
($days,$hrs) = div_mod($hrs,24);
$txtout .= "$days days " if $days>0;
$txtout .= (($txtout ne '')?' ':'').$hrs." hours" if $hrs>0;
$txtout .= (($txtout ne '')?' ':'').$mins." minutes" if $mins>0 && ($days==0 || $hrs==0);
$txtout .= (($txtout ne '')?' ':'').$secs." seconds" if ($sec>0 || $mins==0) && ($hrs==0 && $days==0);
return $txtout;
}
# @DESCRIPTION : If debug / verbose option is set, function prints its input out or to debug file
# @LAST_CHANGED : 08-20-12 by WL
# @INPUT : ARG1 - string of debug text
# @RETURNS : nothing
# @PRIVACY & USE : PUBLIC, Maybe used directly or as object instance function
sub verb {
my ($self,$in) = _self_args(@_);
my $debug_file_name = "";
if (defined($o_verb) || (defined($self) && defined($self->{'verbose'}) && $self->{'verbose'} ne 0)) {
$debug_file_name = $self->{'debug_file'} if defined($self) && $self->{'debug_file'} ne "";
$debug_file_name = $self->{'verbose'} if $debug_file_name ne "" && defined($self) &&
($self->{'verbose'} ne 0 && $self->{'verbose'} ne 1 && $self->{'verbose'} ne '');
$debug_file_name = $o_verb if $debug_file_name ne "" && defined($o_verb) && $o_verb ne "";
if ($debug_file_name ne "") {
if (!open (DEBUGFILE, ">>$debug_file_name")) {
print $in, "\n";
}
else {
print DEBUGFILE $in,"\n";
close DEBUGFILE;
}
}
else {
print $in, "\n";
}
}
}
# @DESCRIPTION : Check of string is a a number supporting integers, negative, decimal floats
# @LAST CHANGED : 08-20-12 by WL
# @INPUT : ARG1 - string of text to be checked
# @RETURNS : 1 if its a number, 0 if its not a number
# @PRIVACY & USE : PUBLIC, To be used statically and not as an object instance reference
sub isnum {
my $num = shift;
if (defined($num) && $num =~ /^[-|+]?((\d+\.?\d*)|(^\.\d+))$/ ) { return 1 ;}
return 0;
}
# @DESCRIPTION : Check of string is a a number supporting integers, negative, decimal floats
# @LAST CHANGED : 08-20-12 by WL
# @INPUT : ARG1 - string of text to be checked
# @RETURNS : 1 if its a number, 0 if its not a number
# @PRIVACY & USE : PUBLIC, To be used statically and not as an object instance function
sub trim {
my $string = shift;
$string =~ s/^\s+//;
$string =~ s/\s+$//;
return $string;
}
# @DESCRIPTION : Takes as input string from PERF or SAVED data from previous plugin invocation
# which should contain space-separated list of var=data pairs. The string is
# parsed and it returns back hash array of var=>data pairs.
# - Function written in 2007 for check_snmp_netint, first release 06/01/07
# - Modified to use quotewords as suggested by Nicholas Scott, release of 05/20/12
# @LAST CHANGED : 08-27-12 by WL
# @INPUT : ARG1 - string of text passed from SERVICEPERFDATA OR SERVICESAVEDDATA MACRO
# @RETURNS : hash array (see description)
# @PRIVACY & USE : PUBLIC, Maybe used directly or as object instance function
# TODO: double-check this works when there are no single quotes as check_snmp_netint always did quotes
sub process_perf {
my ($self,$in) = _self_args(@_);
my %pdh;
my ($nm,$dt);
use Text::ParseWords;
foreach (quotewords('\s+',1,$in)) {
if (/(.*)=(.*)/) {
($nm,$dt)=($1,$2);
if (defined($self)) { $self->verb("prev_perf: $nm = $dt"); }
else { verb("prev_perf: $nm = $dt"); }
# in some of my plugins time_ is to profile execution time for part of plugin
# $pdh{$nm}=$dt if $nm !~ /^time_/;
$pdh{$nm}=$dt;
$pdh{$nm}=$1 if $dt =~ /(\d+)[csB%]/; # 'c' or 's' or B or % maybe have been added
# support for more than one set of previously cached performance data
# push @prev_time,$1 if $nm =~ /.*\.(\d+)/ && (!defined($prev_time[0]) || $prev_time[0] ne $1);
}
}
return %pdh;
}
# @DESCRIPTION : Converts variables with white-spaces with per-name enclosed with ''
# @LAST CHANGED : 08-24-12 by WL
# @INPUT : ARG1 - varible name
# @RETURNS : name for perf-out output
# @PRIVACY & USE : PUBLIC, but its use should be limited. To be used statically and not as an object instance function
sub perf_name {
my $in = shift;
my $out = $in;
$out =~ s/'\/\(\)/_/g; #' get rid of special characters in performance description name
if ($in !~ /\s/ && $in eq $out) {
return $in;
}
return "'".$out."'";
}
# @DESCRIPTION : Determines appropriate output name (for STATUS and PERF) taking into account
# rate variales prefix/suffix and 'NAME' override in long thresholds line specification
# @LAST CHANGED : 08-26-12 by WL
# @INPUT : ARG1 - variable name (variable as found in dataresults)
# @RETURNS : name for output
# @PRIVACY & USE : PUBLIC, but its use should be limited. To be as an object instance function,
sub out_name {
my ($self,$dname) = @_;
my $thresholds = $self->{'_thresholds'};
my $dataresults = $self-> {'_dataresults'};
my $vr = $self->data2varname($dname,1);
my $name_out;
if (defined($vr) && exists($thresholds->{$vr}{'NAME'})) {
if (exists($thresholds->{$vr}{'PATTERN'}) || $self->{'enable_regex_match'} == 1) {
$thresholds->{$vr}{'NAMES_INDEX'} = {} if !exists($thresholds->{$vr}{'NAMES_INDEX'});
if (!exists($thresholds->{$vr}{'NAMES_INDEX'}{$dname})) {
my $ncount = scalar(keys %{$thresholds->{$vr}{'NAMES_INDEX'}});
$ncount++;
$thresholds->{$vr}{'NAMES_INDEX'}{$dname} = $ncount;
}
$name_out = $thresholds->{$vr}{'NAME'} .'_'. $thresholds->{$vr}{'NAMES_INDEX'}{$dname};
}
else {
$name_out = $thresholds->{$vr}{'NAME'};
}
}
else {
# this is for output of rate variables which name internally start with &
if ($dname =~ /^&(.*)/) {
$name_out = $self->{'o_rprefix'}.$1.$self->{'o_rsuffix'};
}
else {
$name_out = $dname;
}
}
return $name_out;
}
# @DESCRIPTION : Builds statusline. Adds info on error conditions that would preceed status data.
# @LAST CHANGED : 08-20-12 by WL
# @INPUT : ARG1 - variable name
# ARG2 - string argument for status info
# @RETURNS : nothing (future: 1 on success, 0 on error)
# @PRIVACY & USE : PUBLIC, but its direct use is discouraged. Must be used as an object instance function
sub addto_statusinfo_output {
my ($self, $var, $sline) = @_;
$self->{'_statusinfo'} .= ", " if $self->{'_statusinfo'};
$self->{'_statusinfo'} .= trim($sline);
$self->{'_dataresults'}{$var}[1]++;
}
# @DESCRIPTION : Accessor function for statusinfo
# @LAST CHANGED : 08-22-12 by WL
# @INPUT : none
# @RETURNS : statusinfo (error conditions and messages) string
# @PRIVACY & USE : PUBLIC. Must be used as an object instance function
sub statusinfo {
my $self = shift;
if (defined($self) && defined($self->{'_statusinfo'})) {
return $self->{'_statusinfo'};
}
return undef;
}
# @DESCRIPTION : Builds Statuline. Adds variable data for status line output in non-error condition.
# @LAST CHANGED : 08-26-12 by WL
# @INPUT : ARG1 - variable name
# ARG2 - formatted for human consumption text of collected data for this variable
# @RETURNS : nothing (future: 1 on success, 0 on error)
# @PRIVACY & USE : PUBLIC, but its direct use is discouraged. Must be used as an object instance function
sub addto_statusdata_output {
my ($self,$dvar,$data) = @_;
my $thresholds = $self->{'_thresholds'};
my $dataresults = $self -> {'_dataresults'};
my $avar = $self->data2varname($dvar,1);
# $self->verb("debug: addto_statusdata_output - dvar is $dvar and avar is $avar");
if ((!exists($thresholds->{$avar}{'DISPLAY'}) || $thresholds->{$avar}{'DISPLAY'} eq 'YES') &&
(!exists($dataresults->{$dvar}[1]) || $dataresults->{$dvar}[1] == 0)) {
$self->{'_statusdata'} .= ", " if $self->{'_statusdata'};
if (defined($data)) {
$self->{'_statusdata'} .= trim($data);
}
elsif (exists($dataresults->{$dvar}[0])) {
$self->{'_statusdata'} .= $self->out_name($dvar) ." is ".$dataresults->{$dvar}[0];
}
$dataresults->{$dvar}[1]++;
}
}
# @DESCRIPTION : Accessor function for statusdata
# @LAST CHANGED : 08-22-12 by WL
# @INPUT : none
# @RETURNS : statusdata string (non-error data from some variables)
# @PRIVACY & USE : PUBLIC. Must be used as an object instance function
sub statusdata {
my $self = shift;
if (defined($self) && defined($self->{'_statusdata'})) {
return $self->{'_statusdata'};
}
return undef;
}
# @DESCRIPTION : This function sets text or data for data variable PERFORMANCE output
# (;warn;crit would be added to it later if thresholds were set for this variable)
# @LAST CHANGED : 08-26-12 by WL
# @INPUT : ARG1 - variable name
# ARG2 - either "var=data" text or just "data" (in which case var= is prepended to it)
# ARG3 - UOM symol ('c' for continous, '%' for percent, 's' for seconds) to added after data
# if undef then it is looked up in known variables and if one is present there, its used
# ARG4 - one of: "REPLACE" - if existing preset perfdata is present, it would be replaced with ARG2
# "ADD" - if existing preset perfdata is there, ARG2 string would be added to it (DEFAULT)
# "IFNOTSET - only set perfdata to ARG2 if it is empty, otherwise keep existing
# @RETURNS : nothing (future: 0 on success, -1 on error)
# @PRIVACY & USE : PUBLIC, but its use should be limited to custom variables added by plugins to data
# Must be used as an object instance function
sub set_perfdata {
my ($self,$avar,$adata,$unit,$opt) = @_;
my $dataresults = $self->{'_dataresults'};
my $thresholds = $self->{'_thresholds'};
my $known_vars = $self->{'knownStatusVars'};
my $bdata = $adata;
my $vr = undef;
# default operation is ADD
if (!defined($opt)) {
$opt = "ADD";
}
else {
$opt = uc $opt;
}
if (defined($adata)) {
# if only data wthout "var=" create proper perf line
$bdata = perf_name($self->out_name($avar)).'='.$adata if $adata !~ /=/;
if (defined($unit)) {
$bdata .= $unit;
}
else {
# appending UOM is done here
$vr = $self->data2varname($avar,1);
if (defined($vr)) {
if (exists($thresholds->{$vr}{'UOM'})) {
$bdata .= $thresholds->{$vr}{'UOM'};
}
elsif (exists($known_vars->{$vr}[2])) {
$bdata .= $known_vars->{$vr}[2];
}
}
}
# preset perfdata in dataresults array
$dataresults->{$avar}=[undef,0,0,''] if !defined($dataresults->{$avar});
$dataresults->{$avar}[2]=-1;
if ($opt eq "REPLACE" || !exists($dataresults->{$avar}[3]) || $dataresults->{$avar}[3] eq '') {
$dataresults->{$avar}[3]=$bdata;
}
elsif (exists($dataresults->{$avar}[3]) && $dataresults->{$avar}[3] ne '' && $opt eq "ADD") {
$dataresults->{$avar}[3].=$adata;
}
}
}
# @DESCRIPTION : This function is used when building performance output
# @LAST CHANGED : 08-26-12 by WL
# @INPUT : ARG1 - variable name
# ARG2 - optional data argument, if not present variable's dataresults are used
# ARG3 - one of: "REPLACE" - if existing preset perfdata is present, it would be replaced with ARG2
# "ADD" - if existing preset perfdata is there, ARG2 string would be added to it
# "IFNOTSET - only set perfdata to ARG2 if it is empty, otherwise keep existing (DEFAULT)
# @RETURNS : nothing (future: 1 on success, 0 on error)
# @PRIVACY & USE : PUBLIC, but its direct use is discouraged. Must be used as an object instance function
sub addto_perfdata_output {
my ($self,$avar,$adata, $opt) = @_;
my $thresholds = $self->{'_thresholds'};
my $dataresults = $self-> {'_dataresults'};
my $vr = undef;
if (!defined($opt)) {
$opt = "IFNOTSET";
}
else {
$opt = uc $opt;
}
$vr = $self->data2varname($avar,1);
if (defined($avar) && defined($vr) &&
(!exists($thresholds->{$vr}{'PERF'}) || $thresholds->{$vr}{'PERF'} eq 'YES') &&
(!defined($dataresults->{$avar}[2]) || $dataresults->{$avar}[2] < 1)) {
my $bdata = '';
if (defined($adata)) {
$bdata .= trim($adata);
}
# this is how most perfdata gets added
elsif (defined($dataresults->{$avar}[0])) {
$bdata .= perf_name($self->out_name($avar)) .'='. $dataresults->{$avar}[0];
}
# this would use existing preset data now if it was present due to default
# setting UOM from KNOWN_STATUS_VARS array is now in set_perfdata if 3rd arg is undef
$self->set_perfdata($avar,$bdata,undef,$opt);
# now we actually add to perfdata from [3] of dataresults
if (exists($dataresults->{$avar}[3]) && $dataresults->{$avar}[3] ne '') {
$bdata = trim($dataresults->{$avar}[3]);
$self->{'_perfdata'} .= " " if $self->{'_perfdata'};
$self->{'_perfdata'} .= $bdata;
$dataresults->{$avar}[2]=0 if $dataresults->{$avar}[2] < 0;
$dataresults->{$avar}[2]++;
}
}
}
# @DESCRIPTION : Accessor function for map from data collected to variable names specified in options and thresholds
# @LAST CHANGED : 08-22-13 by WL
# @INPUT : ARG1 - data variable name
# ARG2 - if undef or 0 return undef if no match for ARG1 found, if 1 return ARG1
# @RETURNS : string of variable name as was specified with --variables or --thresholds
# @PRIVACY & USE : PUBLIC. Must be used as an object instance function
sub data2varname {
my ($self,$dname,$ropt) = @_;
my $dataresults = $self->{'_dataresults'};
return $dataresults->{$dname}[4] if defined($self) && defined($dataresults->{$dname}[4]);
return $dname if defined($ropt) && $ropt eq 1;
return undef;
}
# @DESCRIPTION : Sets list and info on known variables and regex for acceptable data types.
# This function maybe called more than once. If called again, new vars in subsequent
# calls are added to existing ones and existing vars are replaced if they are there again.
# @LAST CHANGED : 08-22-12 by WL
# @INPUT : ARG1 - ref to hash array of known vars. Keys are variable names. Data is an array. Example is:
# 'version' => [ 'misc', 'VERSION', '' ],
# 'utilization' => [ 'misc', 'GAUGE', '%' ],
# 'cmd_get' => [ 'misc', 'COUNTER', 'c', "Total Number of Get Commands from Start" ],
# The array elements are:
# 1st - string of source for this variable. not used by the library at all, but maybe used by code getting the data
# 2nd - type of data in a variable. May be "GAUGE", "VERSION", "COUNTER", "BOOLEAN", "TEXTINFO", "TEXTDATA", "SETTING"
# 3rd - either empty or one-character UOM to be added to perforance data - 'c' for continous, '%' percent, 's' seconds
# 4th - either empty or a description of this variable. If not empty, the variable becomes long-option and this is help text
# ARG2 - regex of acceptable types of data for performance output. Anything else is ignored (i.e. no no output to perf), but
# is still available for threshold checks. if this is undef, then default of 'GAUGE|COUNTER|^DATA$|BOOLEAN' is used
# @RETURNS : nothing (future: 1 on success, 0 on error)
# @PRIVACY & USE : PUBLIC, Must be used as object instance function
sub set_knownvars {
my ($self, $known_vars_in, $vartypes_regex_in) = @_;
my $known_vars = $self->{'knownStatusVars'};
if (defined($known_vars_in)) {
foreach (keys %{$known_vars_in}) {
$known_vars->{$_} = $known_vars_in->{$_};
}
}
if (defined($vartypes_regex_in)) {
$self->{'perfOKStatusRegex'} = $vartypes_regex_in;
}
else {
$self->{'perfOKStatusRegex'} = $DEFAULT_PERF_OK_STATUS_REGEX;
}
}
# @DESCRIPTION : Adds known variables definition one at a time
# @LAST CHANGED : 08-22-12 by WL
# @INPUT : ARG1 - variable name
# ARG2 - string of source for this variable. not used by the library at all, but maybe used by code getting the data
# ARG3 - type of data in a variable. May be "GAUGE", "VERSION", "COUNTER", "BOOLEAN", "TEXTINFO", "TEXTDATA", "SETTING"
# ARG4 - either empty or one-character UOM symbol to be added to perforance data - 'c' for continous, '%' percent, 's' seconds
# ARG5 - either empty or a description of this variable. If not empty, the variable becomes long-option and this is help text
# @RETURNS : nothing (future: 1 on success, 0 on error)
# @PRIVACY & USE : PUBLIC, Must be used as object instance function
sub add_knownvar {
my ($self, $varname, $source, $type, $unit, $description) = @_;
my $temp = { $varname => [ $source, $type, $unit, $description] };
$self->set_knownvars($temp,undef);
}
# @DESCRIPTION : This function is used for checking data values against critical and warning thresholds
# @LAST CHANGED : 08-20-12 by WL
# @INPUT : ARG1 - variable name (used for text output in case it falls within threshold)
# ARG2 - data to be checked
# ARG3 - threshold to be checked, internal structure returned by parse_threshold()
# @RETURNS : Returns "" (empty string) if data is not within threshold range
# and text message for status line out about how data is within range otherwise
# @PRIVACY & USE : PUBLIC. Maybe used directly or as an object instance function
sub check_threshold {
my ($self,$attrib,$data,$th_array) = _self_args(@_);
my $mod = $th_array->[0];
my $lv1 = $th_array->[1];
my $lv2 = $th_array->[2];
my $issymb = 1;
$issymb = 0 if defined($self) && $self->{'output_comparison_symbols'} eq 0;
# verb("debug check_threshold: $mod : ".(defined($lv1)?$lv1:'')." : ".(defined($lv2)?$lv2:''));
return "" if !defined($lv1) || ($mod eq '' && $lv1 eq '');
return " " . $attrib . " is " . $data . ( ($issymb==1)?' = ':' equal to ' ). $lv1 if $mod eq '=' && $data eq $lv1;
return " " . $attrib . " is " . $data . ( ($issymb==1)?' != ':' not equal to ' ). $lv1 if $mod eq '!' && $data ne $lv1;
return " " . $attrib . " is " . $data . ( ($issymb==1)?' > ':' more than ' ) . $lv1 if $mod eq '>' && $data>$lv1;
return " " . $attrib . " is " . $data . ( ($issymb==1)?' > ':' more than ' ) . $lv2 if $mod eq ':' && $data>$lv2;
return " " . $attrib . " is " . $data . ( ($issymb==1)?' >= ':' more than or equal to ' ) . $lv1 if $mod eq '>=' && $data>=$lv1;
return " " . $attrib . " is " . $data . ( ($issymb==1)?' < ':' less than ' ). $lv1 if ($mod eq '<' || $mod eq ':') && $data<$lv1;
return " " . $attrib . " is " . $data . ( ($issymb==1)?' <= ':' less than or equal to ' ) . $lv1 if $mod eq '<=' && $data<=$lv1;
return " " . $attrib . " is " . $data . " in range $lv1..$lv2" if $mod eq '@' && $data>=$lv1 && $data<=$lv2;
return "";
}
# @DESCRIPTION : This function is called to parse threshold string
# @LAST CHANGED : 08-20-12 by WL
# (the code in this function can be traced back to late 2006. It has not much changed from 2008)
# @INPUT : ARG1 - String for one variable WARN or CRIT threshold which can be as follows:
# data - warn if data is above this value if numeric data, or equal for non-numeric
# >data - warn if data is above this value (default for numeric values)
# <data - warn if data is below this value (must be followed by number)
# =data - warn if data is equal to this value (default for non-numeric values)
# !data - warn if data is not equal to this value
# Threshold can also be specified as range in two forms:
# num1:num2 - warn if data is outside range i.e. if data<num1 or data>num2
# \@num1:num2 - warn if data is in range i.e. data>=num1 && data<=num2
# @RETURNS : Returns reference to a hash array, this library's structure for holding processed threshold spec
# @PRIVACY & USE : PUBLIC. Maybe used directly or as an object instance function
sub parse_threshold {
my ($self,$thin) = _self_args(@_);
# link to an array that holds processed threshold data
# array: 1st is type of check, 2nd is threshold value or value1 in range, 3rd is value2 in range,
# 4th is extra options such as ^, 5th is nagios spec string representation for perf out
my $th_array = [ '', undef, undef, '', '' ];
my $th = $thin;
my $at = '';
$at = $1 if $th =~ s/^(\^?[@|>|<|=|!]?~?)//; # check mostly for my own threshold format
$th_array->[3]='^' if $at =~ s/\^//; # deal with ^ option
$at =~ s/~//; # ignore ~ if it was entered
if ($th =~ /^\:([-|+]?\d+\.?\d*)/) { # :number format per nagios spec
$th_array->[1]=$1;
$th_array->[0]=($at !~ /@/)?'>':'<=';
$th_array->[5]=($at != /@/)?('~:'.$th_array->[1]):($th_array->[1].':');
}
elsif ($th =~ /([-|+]?\d+\.?\d*)\:$/) { # number: format per nagios spec
$th_array->[1]=$1;
$th_array->[0]=($at !~ /@/)?'<':'>=';
$th_array->[5]=($at != /@/)?'':'@';
$th_array->[5].=$th_array->[1].':';
}
elsif ($th =~ /([-|+]?\d+\.?\d*)\:([-|+]?\d+\.?\d*)/) { # nagios range format
$th_array->[1]=$1;
$th_array->[2]=$2;
if ($th_array->[1] > $th_array->[2]) {
print "Incorrect format in '$thin' - in range specification first number must be smaller then 2nd\n";
if (defined($self)) { $self->usage(); }
exit $ERRORS{"UNKNOWN"};
}
$th_array->[0]=($at !~ /@/)?':':'@';
$th_array->[5]=($at != /@/)?'':'@';
$th_array->[5].=$th_array->[1].':'.$th_array->[2];
}
if (!defined($th_array->[1])) { # my own format (<,>,=,!)
$th_array->[0] = ($at eq '@')?'<=':$at;
$th_array->[1] = $th;
$th_array->[5] = '~:'.$th_array->[1] if ($th_array->[0] eq '>' || $th_array->[0] eq '>=');
$th_array->[5] = $th_array->[1].':' if ($th_array->[0] eq '<' || $th_array->[0] eq '<=');
$th_array->[5] = '@'.$th_array->[1].':'.$th_array->[1] if $th_array->[0] eq '=';
$th_array->[5] = $th_array->[1].':'.$th_array->[1] if $th_array->[0] eq '!';
}
if ($th_array->[0] =~ /[>|<]/ && !isnum($th_array->[1])) {
print "Numeric value required when '>' or '<' are used !\n";
if (defined($self)) { $self->usage(); }
exit $ERRORS{"UNKNOWN"};
}
# verb("debug parse_threshold: $th_array->[0] and $th_array->[1]");
$th_array->[0] = '=' if !$th_array->[0] && !isnum($th_array->[1]) && $th_array->[1] ne '';
if (!$th_array->[0] && isnum($th_array->[1])) { # this is just the number by itself, becomes 0:number check per nagios guidelines
$th_array->[2]=$th_array->[1];
$th_array->[1]=0;
$th_array->[0]=':';
$th_array->[5]=$th_array->[2];
}
return $th_array;
}
# @DESCRIPTION : this function checks that for numeric data warn threshold is within range of critical
# @LAST CHANGED : 08-20-12 by WL
# @INPUT : ARG1 - warhing threshold structure (reference to hash array)
# ARG2 - critical threshold structure (reference to hash array)
# @RETURNS : Returns 1 if warning does not fall within critical (there is an error)
# Returns 0 if everything is ok and warning is within critical
# @PRIVACY & USE : PUBLIC, but its use is discouraged. Maybe used directly or as an object instance function.
sub threshold_specok {
my ($self, $warn_thar,$crit_thar) = _self_args(@_);
return 1 if defined($warn_thar) && defined($warn_thar->[1]) &&
defined($crit_thar) && defined($crit_thar->[1]) &&
isnum($warn_thar->[1]) && isnum($crit_thar->[1]) &&
$warn_thar->[0] eq $crit_thar->[0] &&
(!defined($warn_thar->[3]) || $warn_thar->[3] !~ /\^/) &&
(!defined($crit_thar->[3]) || $crit_thar->[3] !~ /\^/) &&
(($warn_thar->[1]>$crit_thar->[1] && ($warn_thar->[0] =~ />/ || $warn_thar->[0] eq '@')) ||
($warn_thar->[1]<$crit_thar->[1] && ($warn_thar->[0] =~ /</ || $warn_thar->[0] eq ':')) ||
($warn_thar->[0] eq ':' && $warn_thar->[2]>=$crit_thar->[2]) ||
($warn_thar->[0] eq '@' && $warn_thar->[2]<=$crit_thar->[2]));
return 0; # return with 0 means specs check out and are ok
}
# @DESCRIPTION : this compares var names from data to names given as plugin options treating them regex
# @LAST CHANGED : 08-26-12 by WL
# @INPUT : ARG1 - the name to search for
# @RETURNS : Keyname for what first one that matched from _thresholds
# Undef if nothing matched
# @PRIVACY & USE : PUBLIC, but its direct use should be rare. Must be used as an object instance function.
sub var_pattern_match {
my ($self, $name) = @_;
my $thresholds = $self->{'_thresholds'};
my $allvars = $self->{'_allVars'};
my $is_regex_match = $self->{'enable_regex_match'};
my $v;
my $pattern;
foreach $v (@{$allvars}) {
$pattern='';
if ($is_regex_match eq 1 && !defined($thresholds->{$v}{'PATTERN'})) {
$pattern=$v;
}
elsif ($is_regex_match ne 0 && defined($thresholds->{$v}{'PATTERN'})) {
$pattern = $thresholds->{$v}{'PATTERN'};
}
if ($pattern ne '' && $name =~ /$pattern/) {
$self->verb("Data name '".$name."' matches pattern '".$pattern."'");
return $v;
}
}
return undef;
}
# @DESCRIPTION : This function adds data results
# @LAST CHANGED : 08-27-12 by WL
# @INPUT : ARG1 - name of data variable
# ARG2 - data for this variable
# ARG3 - name of checked variable/parameter corresponding to this data variable
# default undef, assumed to be same as ARG1
# @RETURNS : nothing (future: 1 on success, 0 on error)
# @PRIVACY & USE : PUBLIC, Must be used as an object instance function
sub add_data {
my ($self, $dnam, $dval, $anam) = @_;
my $thresholds = $self->{'_thresholds'};
my $dataresults = $self-> {'_dataresults'};
my $datavars = $self -> {'_datavars'};
my $perfVars = $self->{'_perfVars'};
# determine what plugin options-specified var & threshold this data corresponds to
if (!defined($anam)) {
if ($self->{'enable_regex_match'} == 0) {
$anam = $dnam;
}
else {
$anam = $self->var_pattern_match($dnam);
$anam = $dnam if !defined($anam);
}
}
# set dataresults
if (exists($dataresults->{$dnam})) {
$dataresults->{$dnam}[0] = $dval;
$dataresults->{$dnam}[4] = $anam if defined($anam);
}
else {
$dataresults->{$dnam} = [$dval, 0, 0, '', $anam];
}
# reverse map array
$datavars->{$anam} = [] if !exists($datavars->{$anam});
push @{$datavars->{$anam}}, $dnam;
# setperf if all variables go to perf
if ($self->{'all_variables_perf'} == 1) {
$thresholds->{$anam}={} if !exists($thresholds->{$anam});
$thresholds->{$anam}{'PERF_DATALIST'} = [] if !exists($thresholds->{$anam}{'PERF_DATALIST'});
push @{$thresholds->{$anam}{'PERF_DATALIST'}}, $dnam;
if (!defined($thresholds->{$anam}{'PERF'})) {
push @{$perfVars}, $anam;
$thresholds->{$anam}{'PERF'} = 'YES';
}
}
}
# @DESCRIPTION : Accessor function that gets variable data
# @LAST CHANGED : 08-20-12 by WL
# @INPUT : ARG1 - name of data variable
# @RETURNS : undef if variable does not exist and data otherwise
# @PRIVACY & USE : PUBLIC, Must be used as an object instance function
sub vardata {
my ($self,$dnam) = @_;
my $dataresults = $self->{'_dataresults'};
return undef if !exists($dataresults->{$dnam});
return $dataresults->{$dnam}[0];
}
# @DESCRIPTION : This function parses "WARN:threshold,CRIT:threshold,ABSENT:OK|WARNING|CRITICAL|UNKNOWN" combined threshold string
# Parsing of actual threshold i.e. what is after WARN, CRIT is done by parse_threshold() function
# @LAST CHANGED : 08-27-12 by WL
# @INPUT : ARG1 - String containing threshold line like "WARN:threshold,CRIT:threshold,ABSENT:OK|WARNING|CRITICAL|UNKNOWN"
# Acceptable comma-separated parts threshold specifiers are:
# WARN:<threshold> - warning threshold
# CRIT:<treshold> - critical threshold
# ABSENT:OK|WARNING|CRITICAL|UNKNOWN - nagios exit code if data for this variable is not found
# ZERO:OK|WARNING|CRITICAL|UNKNOWN - nagios exit code if data is 0
# DISPLAY:YES|NO - output data in plugin status line
# PERF:YES|NO - output data as plugin performance data
# SAVED:YES|NO - put results in saved data (this really should not be set manually)
# PATTERN:<regex> - enables regex match allowing more than one real data name to match this threshold
# NAME:<string> - overrides output status and perf name for this variable
# UOM:<string> - unit of measurement symbol to add to perf
# @RETURNS : Returns reference to a hash array, a library's structure for holding processed MULTI-THRESHOLD spec
# Note that this is MULTI-THRESHOLD hash structure, it itself contains threshold hashes returned by parse_threshold()
# @PRIVACY & USE : PUBLIC, but its use is discouraged. Maybe used directly or as an object instance function.
sub parse_thresholds_list {
my ($self,$in) = _self_args(@_);
my $thres = {};
my @tin = undef;
my $t = undef;
my $t2 = undef;
@tin = split(',', $in);
$t = uc $tin[0] if exists($tin[0]);
# old format with =warn,crit thresolds without specifying which one
if (defined($t) && $t !~ /^WARN/ && $t !~ /^CRIT/ && $t !~ /^ABSENT/ && $t !~ /^ZERO/ &&
$t !~ /^DISPLAY/ && $t !~ /^PERF/ && $t !~ /^SAVED/ &&
$t !~ /^PATTERN/ && $t !~ /^NAME/ && $t !~ /^UOM/) {
if (scalar(@tin)==2) {
if (defined($self)) {
$thres->{'WARN'} = $self->parse_threshold($tin[0]);
$thres->{'CRIT'} = $self->parse_threshold($tin[1]);
}
else {
$thres->{'WARN'} = parse_threshold($tin[0]);
$thres->{'CRIT'} = parse_threshold($tin[1]);
}
}
else {
print "Can not parse. Unknown threshold specification: $in\n";
print "Threshold line should be either both warning and critical thresholds separated by ',' or \n";
print "new format of: WARN:threshold,CRIT:threshold,ABSENT:OK|WARNING|CRITICAL|UNKNOWN\n";
print "which allows to specify all 3 (CRIT,WARN,ABSENT) or any one of them in any order\n";
if (defined($self)) { $self->usage(); }
exit $ERRORS{"UNKNOWN"};
}
}
# new format with prefix specifying if its WARN or CRIT and support of ABSENT
else {
foreach $t (@tin) {
$t2 = uc $t;
if ($t2 =~ /^WARN\:(.*)/) {
if (defined($self)) {
$thres->{'WARN'} = $self->parse_threshold($1);
}
else {
$thres->{'WARN'} = parse_threshold($1);
}
}
elsif ($t2 =~ /^CRIT\:(.*)/) {
if (defined($self)) {
$thres->{'CRIT'} = $self->parse_threshold($1);
}
else {
$thres->{'CRIT'} = parse_threshold($1);
}
}
elsif ($t2 =~ /^ABSENT\:(.*)/) {
my $val = $1;
if (defined($ERRORS{$val})) {
$thres->{'ABSENT'} = $val;
}
else {
print "Invalid value $val after ABSENT. Acceptable values are: OK, WARNING, CRITICAL, UNKNOWN\n";
if (defined($self)) { $self->usage(); }
exit $ERRORS{"UNKNOWN"};
}
}
elsif ($t2 =~ /^ZERO\:(.*)/) {
my $val = $1;
if (exists($ERRORS{$val})) {
$thres->{'ZERO'} = $val;
}
else {
print "Invalid value $val after ZERO. Acceptable values are: OK, WARNING, CRITICAL, UNKNOWN\n";
if (defined($self)) { $self->usage(); }
exit $ERRORS{"UNKNOWN"};
}
}
elsif ($t2 =~ /^DISPLAY\:(.*)/) {
if ($1 eq 'YES' || $1 eq 'NO') {
$thres->{'DISPLAY'} = $1;
}
else {