Module: Remon::Helper

Includes:
Logger
Included in:
Metrics::Yum, Sysinfo
Defined in:
lib/remon/helper.rb

Instance Method Summary collapse

Methods included from Logger

logger, #logger

Instance Method Details

#cmd(command, error_msg: nil, return_output: true, env: {}, shell: false) ⇒ Object



7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
# File 'lib/remon/helper.rb', line 7

def cmd(command, error_msg: nil, return_output: true, env: {}, shell: false)
  if command.is_a? Array
    command_arr = command
    command_str = command.join(" ")
  else
    command_arr = command.split
    command_str = command
  end
  logger.debug command_str

  run_command = shell ? command_str : command_arr
  output = if return_output
    IO.popen(env, run_command) { |f| f.read }
  else
    system(env, run_command, 2 => 1)
  end
  exitstatus = $?.exitstatus

  if exitstatus != 0
    error_msg ||= "non zero exit for \"#{command_str}\""
    raise Error, error_msg
  end
  return output
end

#safe_cmd(*args, **kwargs) ⇒ Object



32
33
34
35
36
37
38
# File 'lib/remon/helper.rb', line 32

def safe_cmd(*args, **kwargs)
  output = cmd(*args, **kwargs)
  return $?.exitstatus, output
rescue => e
  logger.debug e.message
  return -1, nil
end