Class: Riemann::Tools::Freeswitch

Inherits:
Object
  • Object
show all
Includes:
Riemann::Tools
Defined in:
lib/riemann/tools/freeswitch.rb

Constant Summary

Constants included from Riemann::Tools

VERSION

Instance Attribute Summary

Attributes included from Riemann::Tools

#argv

Instance Method Summary collapse

Methods included from Riemann::Tools

#attributes, #endpoint_name, included, #options, #report, #riemann, #run

Constructor Details

#initializeFreeswitch

Returns a new instance of Freeswitch.



15
16
17
18
19
20
21
# File 'lib/riemann/tools/freeswitch.rb', line 15

def initialize
  super

  @limits = {
    calls: { critical: opts[:calls_critical], warning: opts[:calls_warning] },
  }
end

Instance Method Details

#alert(service, state, metric, description) ⇒ Object



30
31
32
33
34
35
36
37
# File 'lib/riemann/tools/freeswitch.rb', line 30

def alert(service, state, metric, description)
  report(
    service: service.to_s,
    state: state.to_s,
    metric: metric.to_f,
    description: description,
  )
end

#dead_proc?(pid) ⇒ Boolean

Returns:

  • (Boolean)


23
24
25
26
27
28
# File 'lib/riemann/tools/freeswitch.rb', line 23

def dead_proc?(pid)
  Process.getpgid(pid)
  false
rescue Errno::ESRCH
  true
end

#exec_with_timeout(cmd, timeout) ⇒ Object



39
40
41
42
43
44
45
46
47
48
49
50
51
# File 'lib/riemann/tools/freeswitch.rb', line 39

def exec_with_timeout(cmd, timeout)
  pid = Process.spawn(cmd, { %i[err out] => :close, :pgroup => true })
  begin
    Timeout.timeout(timeout) do
      Process.waitpid(pid, 0)
      $CHILD_STATUS.exitstatus.zero?
    end
  rescue Timeout::Error
    Process.kill(15, -Process.getpgid(pid))
    puts "Killed pid: #{pid}"
    false
  end
end

#tickObject



53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
# File 'lib/riemann/tools/freeswitch.rb', line 53

def tick
  # Determine how many current calls I have according to FreeSWITCH
  fs_calls = `fs_cli -x "show calls count"| grep -Po '^\\d+'`.to_i

  # Determine how many current channels I have according to FreeSWITCH
  fs_channels = `fs_cli -x "show channels count"| grep -Po '^\\d+'`.to_i

  # Determine how many conferences I have according to FreeSWITCH
  fs_conferences = `fs_cli -x "conference list"| grep -Pco '^Conference'`.to_i

  # Try to read pidfile. If it fails use Devil's dummy PID
  begin
    fs_pid = File.read(opts[:pid_file]).to_i
  rescue StandardError
    puts "Couldn't read pidfile: #{opts[:pid_file]}"
    fs_pid = -666
  end

  fs_threads = fs_pid.positive? ? `ps huH p #{fs_pid} | wc -l`.to_i : 0

  # Submit calls to riemann
  if fs_calls > @limits[:calls][:critical]
    alert 'FreeSWITCH current calls', :critical, fs_calls, "Number of calls are #{fs_calls}"
  elsif fs_calls > @limits[:calls][:warning]
    alert 'FreeSWITCH current calls', :warning, fs_calls, "Number of calls are #{fs_calls}"
  else
    alert 'FreeSWITCH current calls', :ok, fs_calls, "Number of calls are #{fs_calls}"
  end

  # Submit channels to riemann
  if fs_channels > @limits[:calls][:critical]
    alert 'FreeSWITCH current channels', :critical, fs_channels, "Number of channels are #{fs_channels}"
  elsif fs_channels > @limits[:calls][:warning]
    alert 'FreeSWITCH current channels', :warning, fs_channels, "Number of channels are #{fs_channels}"
  else
    alert 'FreeSWITCH current channels', :ok, fs_channels, "Number of channels are #{fs_channels}"
  end

  # Submit conferences to riemann
  if fs_conferences > @limits[:calls][:critical]
    alert 'FreeSWITCH current conferences', :critical, fs_conferences,
          "Number of conferences are #{fs_conferences}"
  elsif fs_conferences > @limits[:calls][:warning]
    alert 'FreeSWITCH current conferences', :warning, fs_conferences,
          "Number of conferences are #{fs_conferences}"
  else
    alert 'FreeSWITCH current conferences', :ok, fs_conferences, "Number of conferences are #{fs_conferences}"
  end

  # Submit threads to riemann
  alert 'FreeSWITCH current threads', :ok, fs_threads, "Number of threads are #{fs_threads}" if fs_threads

  # Submit status to riemann
  if dead_proc?(fs_pid)
    alert 'FreeSWITCH status', :critical, -1, 'FreeSWITCH service status: not running'
  else
    alert 'FreeSWITCH status', :ok, nil, 'FreeSWITCH service status: running'
  end

  # Submit CLI status to riemann using timeout in case it's unresponsive
  if exec_with_timeout('fs_cli -x status', 2)
    alert 'FreeSWITCH CLI status', :ok, nil, 'FreeSWITCH CLI status: responsive'
  else
    alert 'FreeSWITCH CLI status', :critical, -1, 'FreeSWITCH CLI status: not responding'
  end
end