-
Notifications
You must be signed in to change notification settings - Fork 51
/
check-ram.rb
executable file
·126 lines (116 loc) · 3.22 KB
/
check-ram.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
126
#! /usr/bin/env ruby
# encoding: UTF-8
#
# check-ram
#
# DESCRIPTION:
#
# OUTPUT:
# plain text
#
# PLATFORMS:
# Linux
#
# DEPENDENCIES:
# gem: sensu-plugin
# gem: vmstat
# compiler: gcc
#
# USAGE:
# check-ram.rb --help
#
# EXTRA INSTALL INSTRUCTIONS:
# You must install gcc. This is needed to compile the vmstat gem
# which you must put in a path that sensu can reach. See the README for
# more details.
# NOTES:
# The default behavior is to check % of RAM free. This can easily
# be overwritten via args please see `check-ram.rb --help` for details
# on each option.
#
# LICENSE:
# Copyright 2012 Sonian, Inc <chefs@sonian.net>
# Released under the same terms as Sensu (the MIT license); see LICENSE
# for details.
#
require 'sensu-plugin/check/cli'
require 'vmstat'
class CheckRAM < Sensu::Plugin::Check::CLI
option :megabytes,
short: '-m',
long: '--megabytes',
description: 'Unless --megabytes is specified the thresholds are in percentage of memory used as opposed to MB of ram left',
boolean: true,
default: false
option :free,
short: '-f',
long: '--free',
description: 'checks free threshold, defaults to true',
boolean: true,
default: true
option :used,
short: '-u',
long: '--used',
description: 'checks used threshold, defaults to false',
boolean: true,
default: false
option :warn,
short: '-w WARN',
proc: proc(&:to_i),
default: 10
option :crit,
short: '-c CRIT',
proc: proc(&:to_i),
default: 5
def run
begin
require 'vmstat'
rescue LoadError => e
raise unless e.message =~ /vmstat/
unknown "Error unable to load vmstat gem: #{e}"
end
# calculating free and used ram based on: https://github.com/threez/ruby-vmstat/issues/4 to emulate free -m
mem = Vmstat.snapshot.memory
free_ram = mem.inactive_bytes + mem.free_bytes
used_ram = mem.wired_bytes + mem.active_bytes
total_ram = mem.total_bytes
# only free or used should be defined, change defaults to mirror free
if config[:used]
config[:free] = false
end
if config[:megabytes]
# free_ram is returned in Bytes. see: https://github.com/threez/ruby-vmstat/blob/master/lib/vmstat/memory.rb
free_ram /= 1024 * 1024
used_ram /= 1024 * 1024
total_ram /= 1024 * 1024
if config[:free]
ram = free_ram
message "#{ram} megabytes of RAM left"
# return used ram
elsif config[:used]
ram = used_ram
message "#{ram} megabytes of RAM used"
end
# use percentages
else
unknown 'invalid percentage' if config[:crit] > 100 || config[:warn] > 100
if config[:free]
ram = (free_ram / total_ram.to_f * 100).round(2)
message "#{ram}% RAM free"
elsif config[:used]
ram = (used_ram / total_ram.to_f * 100).round(2)
message "#{ram}% RAM used"
end
end
# determine state
if config[:free]
critical if ram <= config[:crit]
warning if ram <= config[:warn]
ok
elsif config[:used]
critical if ram >= config[:crit]
warning if ram >= config[:warn]
ok
end
end
end