Class: MonitorTypeProcess

Inherits:
MonitorTypeThreshold show all
Defined in:
lib/monitor_type/process.rb

Overview

A class for checking if a Process is running in Unix based systems

Instance Method Summary collapse

Methods inherited from MonitorTypeThreshold

#check, #initialize, #process

Methods inherited from MonitorType

#alert, #initialize, #process, #run, #teardown

Constructor Details

This class inherits a constructor from MonitorTypeThreshold

Instance Method Details

#derived_valueObject



53
54
55
# File 'lib/monitor_type/process.rb', line 53

def derived_value
  `ps aux | grep #{@process_name}`.length
end

#extract_paramsObject

Extract parameters

Parameters:

  • :process_name (String)

    THe name of the process to monitor



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
# File 'lib/monitor_type/process.rb', line 11

def extract_params
  if @params[:process_name].nil?
    string = "*** Process Name parameter missing, process_name\n" \
             '*** :process_name => <name of the process to ' \
             'be monitored>'
    fail MonitorTypeParameterMissingError, string
  end
  @process_name = @params[:process_name]

  log "#{@process_name}", "result: #{(@process_name =~ /^(.*\[{1}.+\]{1}.*)$|^(\w+)$/) == 0}"

  unless (@process_name =~ /^(.*\[{1}.+\]{1}.*)$|^(\w+)$/) == 0
    string = '*** Process Name parameter doest not match the required ' \
             "pattern, #{@process_name}\n" \
             "*** :process_name => <plain string, or a string " \
             'with one or more characters enclosed in square brackets, ' \
             "i.e. 'foo', '[f]oo' or '[foo]'>"
    fail InvalidProcessNameError, string
  end

  log '*** Min value will be ignored, setting to 1' unless (params[:min].nil? || params[:min] == 0)
  @min = 1

  log '*** Max value will be ignored, setting to nil' unless params[:max].nil?
  @max = nil

  @context_sentence = "Checking that process is running, #{@process_name}"
end

#sanitiseObject



47
48
49
50
51
# File 'lib/monitor_type/process.rb', line 47

def sanitise
  # Ensure that the process name contains a single character surrounded
  #  by square brackets
  @process_name = @process_name.insert(0,'[').insert(2,']') unless @process_name =~ /^.*\[.+\].*/
end

#setupObject



40
41
42
43
44
45
# File 'lib/monitor_type/process.rb', line 40

def setup
  sanitise
rescue MonitorTypeExceptionHandled => e
  puts e.message
  abort
end