Class: Nagios::MkLiveStatus::Stats::Attr

Inherits:
Nagios::MkLiveStatus::Stats show all
Includes:
Nagios::MkLiveStatus, QueryHelper::Comparator, QueryHelper::Deviation
Defined in:
lib/nagios_mklivestatus/stats/attr.rb

Overview

This class symbolise a stats unit. This unit represents a small stat predicate like, in the nagios query, this :

Stats: host_name = name
Author

Esco-lan Team ([email protected])

Copyright

Copyright © 2012 GIP RECIA

License

General Public Licence

Constant Summary

Constants included from QueryHelper::Deviation

QueryHelper::Deviation::AVG, QueryHelper::Deviation::AVGINV, QueryHelper::Deviation::MAX, QueryHelper::Deviation::MIN, QueryHelper::Deviation::STD, QueryHelper::Deviation::SUM, QueryHelper::Deviation::SUMINV

Constants included from QueryHelper::Comparator

QueryHelper::Comparator::EQUAL, QueryHelper::Comparator::EQUAL_IGNCASE, QueryHelper::Comparator::GREATER, QueryHelper::Comparator::GREATER_EQUAL, QueryHelper::Comparator::LESSER, QueryHelper::Comparator::LESSER_EQUAL, QueryHelper::Comparator::NOT_EQUAL, QueryHelper::Comparator::NOT_EQUAL_IGNCASE, QueryHelper::Comparator::NOT_SUBSTR, QueryHelper::Comparator::NOT_SUBSTR_IGNCASE, QueryHelper::Comparator::SUBSTR, QueryHelper::Comparator::SUBSTR_IGNCASE

Instance Method Summary collapse

Methods included from QueryHelper::Deviation

#get_all_deviations

Methods included from QueryHelper::Comparator

#get_all_comparators

Methods included from Nagios::MkLiveStatus

init, #logger

Constructor Details

#initialize(attr_name, attr_comp = nil, attr_value = nil, attr_mod = nil) ⇒ Attr

Create the unit predicate with the deviation, the column name, the operator use to compare, and the value :

Stats: [dev] name [op value]
attr_name

name of the column

attr_comp

operator, one of the constant of the class

attr_value

the value to compare to

attr_mod

deviation of the predicate like avg, max, min



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
# File 'lib/nagios_mklivestatus/stats/attr.rb', line 23

def initialize (attr_name, attr_comp=nil, attr_value=nil, attr_mod=nil)
  
  list_comparator = get_all_comparators
    
  list_deviation = get_all_deviations
  
  if attr_name == nil or attr_name.empty?
    raise QueryException.new("The name of the attribute must be set in order to create the stats.")
  else
    @attr_name = attr_name
  end
  
  if attr_comp == nil and attr_value == nil and attr_mod != nil and not attr_mod.empty? 
  
    if list_deviation.index(attr_mod) == nil
      raise QueryException.new("The deviation #{attr_mod} of the attribute must be set to one of : #{list_deviation.join(", ")} in order to create the stats")
    else
      @attr_mod = attr_mod
    end
    
  elsif attr_comp != nil and attr_value != nil and attr_mod == nil
  
    if list_comparator.index(attr_comp) == nil
      raise QueryException.new("The comparator \"#{attr_comp}\" is not recognized.\n Please use one of : #{list_comparator.join(", ")}")
    else
      @attr_comp = attr_comp
    end
  
    @attr_value = attr_value
  else
    raise QueryException.new("The stats can't have both deviation and comparator.")
  end
  
end

Instance Method Details

#to_column_name(has_parent = false) ⇒ Object

transform predicate to column name same as the to_s but without “Stats: ” (ex: “state = 0”)



87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
# File 'lib/nagios_mklivestatus/stats/attr.rb', line 87

def to_column_name(has_parent=false)
  if @attr_name == nil or @attr_name.empty?
    raise QueryException.new("The stats cannot be converted into string column name because the name of the attribute is not set.")
  end
  
  if (@attr_comp == nil or @attr_comp.empty?) and @attr_mod == nil
    raise QueryException.new("The stats cannot be converted into string column name because the comparator of the attribute and the deviation are not set.")
  end
  
  if @attr_mod != nil
    return @attr_mod+" "+@attr_name
  end
  
  if @attr_value == nil or @attr_value.empty?
    return @attr_name+" "+@attr_comp 
  end
  
  return @attr_name+" "+@attr_comp+" "+@attr_value;
end

#to_sObject

Convert the Stats class to the nagios query string.

Stats: name comp value


62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
# File 'lib/nagios_mklivestatus/stats/attr.rb', line 62

def to_s
  if @attr_name == nil or @attr_name.empty?
    raise QueryException.new("The stats cannot be converted into string because the name of the attribute is not set.")
  end
  
  if (@attr_comp == nil or @attr_comp.empty?) and @attr_mod == nil
    raise QueryException.new("The stats cannot be converted into string because the comparator of the attribute and the deviation are not set.")
  end
  
  stats = "Stats: "
  
  if @attr_mod != nil
    stats+=@attr_mod+" "+@attr_name
    return stats
  end
  
  if @attr_value == nil or @attr_value.empty?
    return stats+@attr_name+" "+@attr_comp 
  end
  
  return stats+@attr_name+" "+@attr_comp+" "+@attr_value;
end