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) ⇒ Base

Returns a new instance of Base.



67
68
69
70
# File 'lib/nsca/check.rb', line 67

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

Class Attribute Details

.hostnameObject (readonly)

Returns the value of attribute hostname.



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

def hostname
  @hostname
end

.perfdatasObject (readonly)

Returns the value of attribute perfdatas.



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

def perfdatas
  @perfdatas
end

.serviceObject (readonly)

Returns the value of attribute service.



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

def service
  @service
end

Instance Attribute Details

#perfdatasObject (readonly)

Returns the value of attribute perfdatas.



66
67
68
# File 'lib/nsca/check.rb', line 66

def perfdatas
  @perfdatas
end

#return_codeObject (readonly)

Returns the value of attribute return_code.



66
67
68
# File 'lib/nsca/check.rb', line 66

def return_code
  @return_code
end

#statusObject (readonly)

Returns the value of attribute status.



66
67
68
# File 'lib/nsca/check.rb', line 66

def status
  @status
end

#timestampObject (readonly)

Returns the value of attribute timestamp.



66
67
68
# File 'lib/nsca/check.rb', line 66

def timestamp
  @timestamp
end

Class Method Details

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



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

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

.init(service, hostname = nil, perfdatas = nil) ⇒ Object



157
158
159
160
161
# File 'lib/nsca/check.rb', line 157

def init service, hostname = nil, perfdatas = nil
	@service, @hostname, @perfdatas = service, hostname || `hostname -f`, {}
	perfdatas.each {|pd| @perfdatas[pd.label.to_sym] = pd }
	self
end

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



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

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

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



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

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

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



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

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

Instance Method Details

#[](perfdata_label) ⇒ Object



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

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

#[]=(perfdata_label, value) ⇒ Object



95
96
97
98
99
100
101
# File 'lib/nsca/check.rb', line 95

def []= perfdata_label, value
	return push value  if value.is_a? PerformanceData::Base
	perfdata_label = perfdata_label.to_sym
	cl = self.class.perfdatas[perfdata_label]
	cl ||= PerformanceData::Base.new perfdata_label
	@perfdatas[perfdata_label] = cl.new value
end

#critObject



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

def critical status = nil, perfdatas = nil
	init ReturnCode::CRITICAL, status, perfdatas
	send
end

#critical(status = nil, perfdatas = nil) ⇒ Object



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

def critical status = nil, perfdatas = nil
	init ReturnCode::CRITICAL, status, perfdatas
	send
end

#determine_return_codeObject



136
137
138
139
140
141
142
143
144
145
# File 'lib/nsca/check.rb', line 136

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

#hostnameObject



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

def hostname() self.class.hostname end

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



72
73
74
75
76
77
78
79
80
81
82
83
# File 'lib/nsca/check.rb', line 72

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



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

def measure perfdata_label, &block
	@perfdatas[perfdata_label.to_sym].measure &block
end

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



114
115
116
117
# File 'lib/nsca/check.rb', line 114

def ok status = nil, perfdatas = nil
	init ReturnCode::OK, status, perfdatas
	send
end

#push(*perfdatas) ⇒ Object



90
91
92
93
# File 'lib/nsca/check.rb', line 90

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

#retcodeObject



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

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

#sendObject



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

def send() NSCA::send self end

#serviceObject



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

def service() self.class.service end

#textObject



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

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

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



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

def unknown status = nil, perfdatas = nil
	init ReturnCode::UNKNOWN, status, perfdatas
	send
end

#warnObject



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

def warning status = nil, perfdatas = nil
	init ReturnCode::WARNING, status, perfdatas
	send
end

#warning(status = nil, perfdatas = nil) ⇒ Object



119
120
121
122
# File 'lib/nsca/check.rb', line 119

def warning status = nil, perfdatas = nil
	init ReturnCode::WARNING, status, perfdatas
	send
end