-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathcheck_pm2
70 lines (57 loc) · 1.34 KB
/
check_pm2
1
2
3
4
5
6
7
8
9
10
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
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
#!/usr/bin/env ruby -W0
#
# Check pm2 status node.js for Nagios
# Author: Poy <[email protected]>
#
#
# Nagions check_pm2 command
#
# define command {
# command_name check_pm2
# command_line $USER1$/check_pm2 --host=$HOSTADDRESS --name=translation_engine
#
# Nagios service example check for translation_engine
#
# define service{
# use tech-service
# host_name server_name
# service_description pm2 translation_engine
# check_command check_pm2
# }
require 'json'
require 'curb'
require 'thor'
class CLI < Thor
NAGIOS_OK = 0
NAGIOS_WARNING = 1
NAGIOS_CRITICAL = 2
NAGIOS_UNKNOWN = 3
desc "check", "Check pm2"
option :host, :required => true
option :port, :default => '9615'
option :name, :required => true
def check
name = options[:name]
http = Curl.get("#{options[:host]}:#{options[:port]}")
pm2_json = JSON.parse(http.body_str)
processes = pm2_json['processes'].select {|p| p['name'] == name }
if processes.nil? || processes.empty?
puts "Unknown process"
exit NAGIOS_CRITICAL
end
processes.each do |process|
case process['pm2_env']['status']
when 'online'
exit NAGIOS_OK
when 'stopped'
exit NAGIOS_WARNING
when 'errored'
exit NAGIOS_CRITICAL
else
exit NAGIOS_UNKNOWN
end
end
end
default_task :check
end
CLI.start(ARGV)