-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcheck-pm2.rb
executable file
·125 lines (113 loc) · 3.38 KB
/
check-pm2.rb
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
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
119
120
121
122
123
124
125
#! /usr/bin/env ruby
#
# Check PM2
#
#
# DESCRIPTION:
# Check check if all the processes managed by the PM2 manager are running.
#
# OUTPUT:
# plain-text
#
# PLATFORMS:
# all
#
# DEPENDENCIES:
#
# Add the following 3 lines into your sensu file in the /etc/sudorers.d/ folder
#
# Defaults:sensu !requiretty
# Defaults:sensu secure_path = /opt/sensu/embedded/bin:/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin
#
# sensu ALL=(ALL)NOPASSWD: ALL
#
# USAGE:
# ./check-pm2.rb -u <username> [--status | -memory | -cpu ]] -w <seconds> -c <seconds>
#
# EXAMPLES:
#
# ./check_pm2.rb -u my_user -w 1 -c 1 -s online
#
# If one process managed by PM2 are not in online state, it throws a critical.
#
# ./check_pm2.rb -u my_user -w 1 -c 2 -m 60
#
# If one process is consuming 60 MB or more then the check throws a warning.
# If 2 o more process are consuming 60 MB or more, then it throws a critical.
#
# LICENSE:
# Copyright Marcos Martínez <frommelmak@gmail.com>
#
# Released under the same terms as Sensu (the MIT license); see LICENSE
# for details.
#
require 'sensu-plugin/check/cli'
require 'json'
class CheckPM2 < Sensu::Plugin::Check::CLI
option :critical,
:description => "Max allowed value to consider critical",
:short => '-c VALUE',
:long => '--critical=VALUE',
:proc => proc {|a| a.to_i },
:required => true
option :warning,
:description => "Max allowed value to consider warning",
:short => '-w VALUE',
:long => '--warning=VALUE',
:proc => proc {|a| a.to_i },
:required => true
option :username,
:description => "The user running pm2",
:short => '-u USER',
:long => '--username=USER',
:required => true
option :status,
:description => "Look for the status to throw alerts\n \
\t\t Available status: online, stopping, stopped, launching, errored, one-launch-status",
:short => '-s STATUS',
:long => '--status=STATUS',
:required => false
option :memory,
:description => "Look for the max amount of memory allowed for a process to throw alerts",
:short => '-m MEM',
:long => '--memory=MEM',
:required => false
option :cpu,
:description => "Look for the % of CPU usage to throw alerts",
:short => '-l LOAD',
:long => '--load=LOAD',
:required => false
def run
json = `sudo -iu #{config[:username]} pm2 jlist`
processes_hash = JSON.parse(json)
n = 0
limit = 'none'
if config[:warning] > config[:critical]
puts "ERROR: Warning threshold can not be greater than Critical!"
exit 1
end
bad_metric_counter =0
processes_hash.each do |process|
if config[:status]
limit="not #{config[:status]}"
if config[:status] != process["pm2_env"]["status"]
bad_metric_counter += 1
end
elsif config[:memory]
limit="memory > #{config[:memory]} MB"
if process["monit"]["memory"].fdiv(1024*1024).round >= config[:memory].to_i
bad_metric_counter += 1
end
elsif config[:load]
limit="load > #{config[:cpu]} %"
if process["monit"]["cpu"] >= config[:load].to_i
bad_metric_counter += 1
end
end
end
msg = "There are #{bad_metric_counter} processes in bad status (#{limit})"
critical msg if bad_metric_counter >= config[:critical]
warning msg if bad_metric_counter >= config[:warning]
ok msg
end
end