Class: NSCA::Check::Base

Inherits:
Object
  • Object
show all
Defined in:
lib/nsca/check.rb

Class Attribute Summary collapse

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(return_code = nil, status = nil, perfdatas = nil, timestamp = nil) ⇒ Base

Returns a new instance of Base.



86
87
88
89
# File 'lib/nsca/check.rb', line 86

def initialize return_code = nil, status = nil, perfdatas = nil, timestamp = nil
  @perfdatas = {}
  init return_code, status, perfdatas, timestamp || Time.now
end

Class Attribute Details

.hostnameObject (readonly)

Returns the value of attribute hostname.



172
173
174
# File 'lib/nsca/check.rb', line 172

def hostname
  @hostname
end

.perfdatasObject (readonly)

Returns the value of attribute perfdatas.



172
173
174
# File 'lib/nsca/check.rb', line 172

def perfdatas
  @perfdatas
end

.serviceObject (readonly)

Returns the value of attribute service.



172
173
174
# File 'lib/nsca/check.rb', line 172

def service
  @service
end

Instance Attribute Details

#perfdatasObject (readonly)

Returns the value of attribute perfdatas.



84
85
86
# File 'lib/nsca/check.rb', line 84

def perfdatas
  @perfdatas
end

#return_codeObject

Returns the value of attribute return_code.



83
84
85
# File 'lib/nsca/check.rb', line 83

def return_code
  @return_code
end

#statusObject

Returns the value of attribute status.



83
84
85
# File 'lib/nsca/check.rb', line 83

def status
  @status
end

#timestampObject

Returns the value of attribute timestamp.



83
84
85
# File 'lib/nsca/check.rb', line 83

def timestamp
  @timestamp
end

Class Method Details

.clone(opts = nil) ⇒ Object



192
# File 'lib/nsca/check.rb', line 192

def clone( opts = nil) ::Class.new( self).init opts ? to_h.merge( opts) : to_h end

.critical(status = nil, perfdatas = nil) ⇒ Object Also known as: crit



185
# File 'lib/nsca/check.rb', line 185

def critical( status = nil, perfdatas = nil) new.warning status, perfdatas end

.init(*args) ⇒ Object



173
174
175
176
177
178
179
180
# File 'lib/nsca/check.rb', line 173

def init *args
  a, o = args, args.last.is_a?( Hash) ? args.pop : {}
  service, hostname = nil, perfdatas = nil
  @service, @hostname, @perfdatas = a[0]||o[:service], a[1]||o[:hostname]||`hostname`.chomp, {}
  perfdatas = a[2]||o[:perfdatas]
  perfdatas.each {|pd| @perfdatas[pd.to_sym] = pd }  if perfdatas
  self
end

.ok(status = nil, perfdatas = nil) ⇒ Object



182
# File 'lib/nsca/check.rb', line 182

def ok( status = nil, perfdatas = nil) new.ok status, perfdatas end

.to_aObject



189
# File 'lib/nsca/check.rb', line 189

def to_a() [service, hostname, perfdatas.dup] end

.to_hObject



190
# File 'lib/nsca/check.rb', line 190

def to_h() {service: service, hostname: hostname, perfdatas: perfdatas.values} end

.to_symObject



191
# File 'lib/nsca/check.rb', line 191

def to_sym() "#{hostname}|#{service}".to_sym end

.unknown(status = nil, perfdatas = nil) ⇒ Object



187
# File 'lib/nsca/check.rb', line 187

def unknown( status = nil, perfdatas = nil) new.unknown status, perfdatas end

.warning(status = nil, perfdatas = nil) ⇒ Object Also known as: warn



183
# File 'lib/nsca/check.rb', line 183

def warning( status = nil, perfdatas = nil) new.warning status, perfdatas end

Instance Method Details

#[](perfdata_label) ⇒ Object



104
105
106
107
# File 'lib/nsca/check.rb', line 104

def [] perfdata_label
  pd = @perfdatas[perfdata_label.to_sym]
  pd && pd.value
end

#[]=(perfdata_label, value) ⇒ Object



123
124
125
126
127
# File 'lib/nsca/check.rb', line 123

def []= perfdata_label, value
  return push value  if value.is_a? PerformanceData::Base
  perfdata_label = perfdata_label.to_sym
  @perfdatas[perfdata_label] = perfdata_for( perfdata_label).new value
end

#critObject



144
# File 'lib/nsca/check.rb', line 144

def critical( *args) init ReturnCode::CRITICAL, *args end

#critical(*args) ⇒ Object



143
# File 'lib/nsca/check.rb', line 143

def critical( *args) init ReturnCode::CRITICAL, *args end

#determine_return_codeObject



147
148
149
150
151
152
# File 'lib/nsca/check.rb', line 147

def determine_return_code
  self.class.perfdatas.map do |label, pdc|
    pd = @perfdatas[label]
    pd ? pd.return_code : -1
  end.max
end

#hostnameObject



160
# File 'lib/nsca/check.rb', line 160

def hostname() self.class.hostname end

#init(return_code = nil, status = nil, perfdatas = nil, timestamp = nil) ⇒ Object



91
92
93
94
95
96
97
98
99
100
101
102
# File 'lib/nsca/check.rb', line 91

def init return_code = nil, status = nil, perfdatas = nil, timestamp = nil
  @return_code = return_code  if return_code
  @status = status  if status
  case perfdatas
  when Hash
    perfdatas.each &method( :[])
  when Array
    push *perfdatas
  end
  @timestamp = timestamp  if timestamp
  self
end

#measure(perfdata_label, &block) ⇒ Object



135
136
137
# File 'lib/nsca/check.rb', line 135

def measure perfdata_label, &block
  push perfdata_for( perfdata_label).measure( &block)
end

#ok(*args) ⇒ Object



140
# File 'lib/nsca/check.rb', line 140

def ok( *args) init ReturnCode::OK, *args end

#perfdata_for(label) ⇒ Object



114
115
116
117
118
119
120
121
# File 'lib/nsca/check.rb', line 114

def perfdata_for label
  if label.is_a? PerformanceData::Base
    label
  else
    label = label.to_sym
    self.class.perfdatas[label] || PerformanceData::Base.new( label)
  end
end

#push(*perfdatas) ⇒ Object



109
110
111
112
# File 'lib/nsca/check.rb', line 109

def push *perfdatas
  perfdatas.each {|perfdata| @perfdatas[perfdata.label.to_sym] = perfdata }
  @perfdatas
end

#retcodeObject



154
155
156
157
# File 'lib/nsca/check.rb', line 154

def retcode
  rc = return_code || determine_return_code
  (0..3).include?(rc) ? rc : 3
end

#sendObject



138
# File 'lib/nsca/check.rb', line 138

def send() NSCA::send self end

#serviceObject



159
# File 'lib/nsca/check.rb', line 159

def service() self.class.service end

#textObject



129
130
131
132
133
# File 'lib/nsca/check.rb', line 129

def text
  r = "#{status || ReturnCode.find(retcode)}"
  r += " | #{perfdatas.each_value.map( &:to_s).join ' '}"  unless perfdatas.empty?
  r
end

#to_aObject



161
# File 'lib/nsca/check.rb', line 161

def to_a() [timestamp, retcode, hostname, service, text] end

#to_hObject



162
163
164
# File 'lib/nsca/check.rb', line 162

def to_h
  {timestamp: timestamp, return_code: retcode, hostname: hostname, server: service, status: text}
end

#to_packet(version = nil) ⇒ Object



166
167
168
169
# File 'lib/nsca/check.rb', line 166

def to_packet version = nil
  version ||= PacketV3
  version.new timestamp, retcode, hostname, service, text
end

#unknown(*args) ⇒ Object



145
# File 'lib/nsca/check.rb', line 145

def unknown( *args) init ReturnCode::UNKNOWN, *args end

#warnObject



142
# File 'lib/nsca/check.rb', line 142

def warning( *args) init ReturnCode::WARNING, *args end

#warning(*args) ⇒ Object



141
# File 'lib/nsca/check.rb', line 141

def warning( *args) init ReturnCode::WARNING, *args end