metrics-memory.rb
1.91 KB
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
#! /usr/bin/env ruby
# encoding: UTF-8
#
# metrics-memory
#
# DESCRIPTION:
#
# OUTPUT:
# metric data
#
# PLATFORMS:
# Linux
#
# DEPENDENCIES:
# gem: sensu-plugin
#
# USAGE:
# ./metrics-memory.rb
#
# 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/metric/cli'
require 'socket'
class MemoryGraphite < Sensu::Plugin::Metric::CLI::Graphite
option :scheme,
description: 'Metric naming scheme, text to prepend to metric',
short: '-s SCHEME',
long: '--scheme SCHEME',
default: "#{Socket.gethostname}.memory"
def run
# Metrics borrowed from hoardd: https://github.com/coredump/hoardd
mem = metrics_hash
mem.each do |k, v|
output "#{config[:scheme]}.#{k}", v
end
ok
end
def metrics_hash
mem = {}
meminfo_output.each_line do |line|
mem['total'] = line.split(/\s+/)[1].to_i * 1024 if line =~ /^MemTotal/
mem['free'] = line.split(/\s+/)[1].to_i * 1024 if line =~ /^MemFree/
mem['buffers'] = line.split(/\s+/)[1].to_i * 1024 if line =~ /^Buffers/
mem['cached'] = line.split(/\s+/)[1].to_i * 1024 if line =~ /^Cached/
mem['swapTotal'] = line.split(/\s+/)[1].to_i * 1024 if line =~ /^SwapTotal/
mem['swapFree'] = line.split(/\s+/)[1].to_i * 1024 if line =~ /^SwapFree/
mem['dirty'] = line.split(/\s+/)[1].to_i * 1024 if line =~ /^Dirty/
end
mem['swapUsed'] = mem['swapTotal'] - mem['swapFree']
mem['used'] = mem['total'] - mem['free']
mem['usedWOBuffersCaches'] = mem['used'] - (mem['buffers'] + mem['cached'])
mem['freeWOBuffersCaches'] = mem['free'] + (mem['buffers'] + mem['cached'])
mem['swapUsedPercentage'] = 100 * mem['swapUsed'] / mem['swapTotal'] if mem['swapTotal'] > 0
mem
end
def meminfo_output
File.open('/proc/meminfo', 'r')
end
end