Code Monkey home page Code Monkey logo

sensu-plugins-memory-checks's Introduction

Sensu Bonsai Asset Build Status Gem Version Code Climate Test Coverage Dependency Status

Sensu Memory Check Plugin

Overview

This plugin provides native memory instrumentation for monitoring and metrics collection, including memory usage via free and vmstat, including metrics. NOTE: This plugin may have cross-platform issues.

Files

  • bin/check-memory.rb
  • bin/check-memory.sh
  • bin/check-memory-percent.rb
  • bin/check-memory-percent.sh
  • bin/check-ram.rb
  • bin/check-swap-percent.rb
  • bin/check-swap.sh
  • bin/check-swap.rb
  • bin/metrics-memory-percent.rb
  • bin/metrics-memory.rb

check-memory Evaluate free system memory for Linux-based systems.

check-memory-percent Evaluate the percentage of free system memory for Linux-based systems.

check-ram Check the percentage of free RAM.

check-swap-percent Checks swap usage as a percentage of the total swap.

check-swap Evaluate swap memory usage for Linux-based systems.

metrics-memory-percent Read memory statistics as a percentage and put them in a form usable by Graphite. Metrics borrowed from HoardD.

metrics-memory-vmstat Read memory statistics and put them in a form usable by vmstat.

metrics-memory Read memory statistics and put them in a form usable by Graphite. Metrics borrowed from HoardD.

Usage examples

Help

check-ram.rb

Usage: /opt/sensu/embedded/bin/check-ram.rb (options)
    -c CRIT
    -f, --free                       checks free threshold, defaults to true
    -m, --megabytes                  Unless --megabytes is specified the thresholds are in percentage of memory used as opposed to MB of ram left
    -u, --used                       checks used threshold, defaults to false
    -w WARN

metrics-memory-percent.rb

Usage: /opt/sensu/embedded/bin/metrics-memory-percent.rb (options)
    -s, --scheme SCHEME              Metric naming scheme, text to prepend to metric

Configuration

Sensu Go

Asset registration

Assets are the best way to make use of this plugin. If you're not using an asset, please consider doing so! If you're using sensuctl 5.13 or later, you can use the following command to add the asset:

sensuctl asset add sensu-plugins/sensu-plugins-memory-checks

If you're using an earlier version of sensuctl, you can download the asset definition from this project's Bonsai Asset Index page.

Asset definition

---
type: Asset
api_version: core/v2
metadata:
  name: sensu-plugins-memory-checks
spec:
  url: https://assets.bonsai.sensu.io/c5391d4ae186484226732344b35cf95c0b07b8ec/sensu-plugins-memory-checks_4.0.0_centos_linux_amd64.tar.gz
  sha512: ea297a85aa3612da7f78d948f9784443fffac511040c5130a2dcde7191a0004044c2ef881e665520cbc64431955ab19920d84de6b5fed85c63da7091c4b93bf0

Check definition

---
type: CheckConfig
spec:
  command: "check-memory.rb"
  handlers: []
  high_flap_threshold: 0
  interval: 10
  low_flap_threshold: 0
  publish: true
  runtime_assets:
  - sensu-plugins/sensu-plugins-memory-checks
  - sensu/sensu-ruby-runtime
  subscriptions:
  - linux

Sensu Core

Check definition

{
  "checks": {
    "metrics-disk-usage": {
      "command": "metric-disk-usage.rb",
      "subscribers": ["linux"],
      "interval": 10,
      "refresh": 10,
      "handlers": ["influxdb"]
    }
  }
}

Installation from source

Sensu Go

See the instructions above for asset registration

Sensu Core

Install and setup plugins on Sensu Core

Additional notes

bin/check-ram.rb

Ruby does not have a good native way to grab information on memory usage (without using C extensions or running shell commands and parsing out). For this reason, check-ram.rb uses a gem called vmstat that has a dependency on a GCC to compile the C extensions.

To avoid errors that occur when trying to compile the C extensions without GCC present, we do not install the vmstat gem by default.

Users who need to use check-ram.rb must install build tools, including GCC. This usually requires two steps but can vary based on your distribution and Ruby setup:

  1. Install gcc. If you are on a Debian system, GCC is located in the build-essential package. On RHEL systems, yum groupinstall "Development tools" will provide the required packages.
  2. Install the vmstat gem into the path from which Sensu gems are expected to run, sudo /opt/sensu/embedded/bin/gem install vmstat --no-rdoc --no-ri, or the equivalent for installing Sensu gems in your configuration management system.

Certification verification

If you are verifying certificates in the gem install you will need the certificate for the sys-filesystem gem loaded in the gem certificate store.

Contributing

See CONTRIBUTING.md for information about contributing to this plugin.

sensu-plugins-memory-checks's People

Contributors

amaltson avatar amdprophet avatar analytically avatar bleuchtang avatar brandt avatar corro avatar cwjohnston avatar dependabot-preview[bot] avatar dependabot-support avatar eheydrick avatar eslamelhusseiny avatar hillaryfraley avatar jpoizat avatar jspaleta avatar kszarek avatar losisin avatar majormoses avatar mattyjones avatar max-ar avatar mjulian avatar pchambino avatar portertech avatar rostyslavfridman avatar rs-mrichmond avatar scalp42 avatar sge-babrams avatar sstarcher avatar stefan-walluhn avatar tas50 avatar ttarczynski avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

sensu-plugins-memory-checks's Issues

Error while executing script check-memory.rb

I am getting following error will executing
[root@localhost bin]# /opt/sensu/embedded/bin/ruby check-memory.rb -w 700 -c 800

/opt/sensu/embedded/lib/ruby/gems/2.4.0/gems/sensu-plugins-memory-checks-3.0.0/bin/check-memory.sh: line 44: [: missing `]'

Its because there is no space on line 44
Instead of [ -z "$WARN"] || [ -z "$CRIT" ]; then it should be if [ -z "$WARN" ] || [ -z "$CRIT" ]; then

Add instructions for how to install `vmstat` Ruby package in Sensu's Ruby

Your changelog mentions that the Ruby vmstat package has been removed as a dependency, but there are no instructions on how to install it in Sensu.

With the new 1.0 release, I'm getting this error:

/opt/sensu/embedded/lib/ruby/site_ruby/2.3.0/rubygems/core_ext/kernel_require.rb:55:in `require': cannot load such file -- vmstat (LoadError) from
/opt/sensu/embedded/lib/ruby/site_ruby/2.3.0/rubygems/core_ext/kernel_require.rb:55:in `require' from
/opt/sensu/embedded/lib/ruby/gems/2.3.0/gems/sensu-plugins-memory-checks-1.0.2/bin/check-ram.rb:36:in `<top (required)>' from /opt/sensu/embedded/bin/check-ram.rb:22:in `load' from
/opt/sensu/embedded/bin/check-ram.rb:22:in `<main>' Sensu::Plugin::CLI: ["Not implemented! You should override Sensu::Plugin::CLI#run."] 

Proposal: switch thresholds to be memory used instead of memory free

I think setting a threshold on memory left vs memory used does not seem as natural as most plugins are based on used threshold.

If there is any chance of it passing I am more than happy to submit a PR. Either to adding additional doc as to how its used or changing it to be a usage threshold.

FreeBSD - free: command not found

/home/USER/.gem/gems/sensu-plugins-memory-checks-1.0.2/bin/check-memory-percent.sh: line 49: free: command not found
/home/USER/.gem/gems/sensu-plugins-memory-checks-1.0.2/bin/check-memory-percent.sh: line 52: free: command not found
/home/USER/.gem/gems/sensu-plugins-memory-checks-1.0.2/bin/check-memory-percent.sh: line 55: free: command not found

Release 2.1.0

There was a major release that includes some major changes but it looks like it was never actually released. Can we get a release please? @eheydrick

vmstat gem required ?

I didn't need to install the vmstat gem manually until now. Just installing this plugin worked by itself.

What changed ? Why can't the plugin installation pull the vmstat gem by itself if it is now required ?

remove "bc" as dependency in check-swap-percent.sh

It should be done as it is in check-memory-percent.sh. bc command is not installed by default on some systems. I've already rewritten it for my installation of Sensu and I can create pull requests.

Hardcoded threshold values in check-ram.rb override command line values

Following line override warning and critical threshold set when using "-u" or "--used" memory values.

L88 config[:warn] = 90
L89 config[:crit] = 95

If the current memory usage is at 91% and threshold as set as follows, it will not alert as threshold set on command line are overridden by above two lines

/opt/sensu/embedded/bin/ruby check-ram.rb --used -w 80 -c 90

Expected behaviour:

CheckRAM CRITICAL: 91% RAM used

Actual

CheckRAM WARNING: 91% RAM used

check-swap-percent.rb fails on FreeBSD

/opt/sensu/embedded/lib/ruby/gems/2.3.0/gems/sensu-plugins-memory-checks-1.0.2/bin/check-swap-percent.rb:6:in `exec': No such file or directory - /opt/sensu/embedded/lib/ruby/gems/2.3.0/gems/sensu-plugins-memory-checks-1.0.2/bin/check-swap-percent.sh (Errno::ENOENT)
    from /opt/sensu/embedded/lib/ruby/gems/2.3.0/gems/sensu-plugins-memory-checks-1.0.2/bin/check-swap-percent.rb:6:in `<main>'

Metric memory wrong when checking on Centos 7

Hi,
When client run on Centos 7 (systemd), the script metrics-memory-percent.rb returns wrong values

  • run free command :

    free -b
    total used free shared buff/cache available
    Mem: 3880316928 424787968 145653760 33890304 3309875200 3145891840
    Swap: 0 0 0

  • run script :

metrics-memory.rb
foo.memory.total 3880316928 1474542059
foo.memory.free 160362496 1474542059
foo.memory.buffers 0 1474542059
foo.memory.cached 387592192 1474542059
foo.memory.swapTotal 0 1474542059
foo.memory.swapFree 0 1474542059
foo.memory.dirty 40960 1474542059
foo.memory.swapUsed 0 1474542059
foo.memory.used 3719954432 1474542059
foo.memory.usedWOBuffersCaches 3332362240 1474542059
foo.memory.freeWOBuffersCaches 547954688 1474542059

memory-check.sh thresholds should not default to zero

As other check plugins within the repo have defaults which will send an alert. There should be a default for memory-check.sh or an error which prevents the check running without defined thresholds. The lack of thresholds will continue to show a healthy status until the system falls over. Even an arbitrary Threshold which will raise an alert seems better than none.

check-ram.rb not computing megabytes correctly.

It appears that under the config[:megabytes], the math for free_ram, used_ram, and total_ram is actually dividing by 1 instead of the 1024's as intended. Here's the change I made that seemed to work:

 free_ram /= 1024 * 1024
 used_ram /= 1024 * 1024
 total_ram /= 1024 * 1024

Memory check resource definition is not valid in README

The README currently has the following for a memory check resource definition example:

---
type: CheckConfig
spec:
  command: "check-memory.rb"
  handlers: []
  high_flap_threshold: 0
  interval: 10
  low_flap_threshold: 0
  publish: true
  runtime_assets:
  - sensu-plugins/sensu-plugins-memory-checks
  - sensu/sensu-ruby-runtime
  subscriptions:
  - linux

This isn't valid for a few reasons:

  • No metadata, so no check name can be set.
  • Missing the api_version attribute.
  • The memory check command throws an error that both warning and critical need to be set.
    The following worked for me:
---
type: CheckConfig
api_version: core/v2
metadata:
  name: check-memory
  namespace: default
spec:
  command: "check-memory.rb -w 80 -c 90"
  high_flap_threshold: 0
  interval: 10
  low_flap_threshold: 0
  publish: true
  runtime_assets:
  - sensu-plugins/sensu-plugins-memory-checks
  - sensu/sensu-ruby-runtime
  subscriptions:
  - linux

Asset builds should include vmstat gem

Per the readme, check-ram.rb requires vmstat but that gem is not part of this gem's runtime requirements (not installed by default).

In order to make check-ram.rb functional for those consuming releases as Sensu Go asset builds, we need to explicitly install the vmstat gem as part of the build process.

Suddenly getting error: "cannot load such file -- vmstat"

I have a system which launches VMs and installs this sensu plugin, as well, as several others. I haven't launched any VMs for a while (2 or 3 weeks) and I started launching some yesterday, and noticed that on each machine check-ram.rb produces this stack trace:

Sensu::Plugin::CLI: ["Not implemented! You should override Sensu::Plugin::CLI#run."]
/usr/lib/ruby/1.9.1/rubygems/custom_require.rb:36:in `require': cannot load such file -- vmstat (LoadError)
    from /usr/lib/ruby/1.9.1/rubygems/custom_require.rb:36:in `require'
    from /var/lib/gems/1.9.1/gems/sensu-plugins-memory-checks-1.0.2/bin/check-ram.rb:36:in `<top (required)>'
    from /usr/local/bin/check-ram.rb:23:in `load'
    from /usr/local/bin/check-ram.rb:23:in `<main>'

The process which provisions these VMs will install these plugins from scratch on each VM so I imagine that the version running right now is not the same as it was a few weeks ago. The base image that the VM uses was updated but I don't think there was anything ruby/sensu-related in that set of updates.

The metrics-memory.rb plugin seems to be working fine, as well as all the sensu plugins.

Is there anything else I need to install/update/tweak to get this working again?

Needs to be updated for dependency 'sensu-plugins' 1.3.0 ?

Hi,

My sensu-plugins gem was updated to latest version 1.3.0, making this plugins fail.
Could it be updated to support the updated dependency ?

/opt/sensu/embedded/lib/ruby/site_ruby/2.3.0/rubygems/dependency.rb:310:into_specs': Could not find 'sensu-plugin' (= 1.2.0) - did find: sensu-plugin-1.3.0
Checked in 'GEM_PATH=/opt/sensu/embedded/lib/ruby/gems/2.3.0:/opt/sensu/.gem/ruby/2.3.0', execute gem env for more information
from /opt/sensu/embedded/lib/ruby/site_ruby/2.3.0/rubygems/specification.rb:1439:in block in activate_dependencies' from /opt/sensu/embedded/lib/ruby/site_ruby/2.3.0/rubygems/specification.rb:1428:ineach'
from /opt/sensu/embedded/lib/ruby/site_ruby/2.3.0/rubygems/specification.rb:1428:in activate_dependencies' from /opt/sensu/embedded/lib/ruby/site_ruby/2.3.0/rubygems/specification.rb:1410:inactivate'
from /opt/sensu/embedded/lib/ruby/site_ruby/2.3.0/rubygems.rb:279:in block in activate_bin_path' from /opt/sensu/embedded/lib/ruby/site_ruby/2.3.0/rubygems.rb:279:insynchronize'
from /opt/sensu/embedded/lib/ruby/site_ruby/2.3.0/rubygems.rb:279:in activate_bin_path' from /opt/sensu/embedded/bin/check-memory.rb:22:in

'`

Check-memory-percent.sh not returning correct values for Linux systems

FWIW, I am using both Ubuntu 16/18 and CentOS 7 systems.

So near as I can tell this issues is caused by an incorrect parsing of the values returned by the free -m command as well as not calculating the free memory correctly.

Using the following output from the free -m command as an example,

                total        used        free      shared  buff/cache   available
     Mem:        3951        2915         224          32         812         731
     Swap:       1951         404        1547

The following code does not make much sense:

elif [ $os = "Linux" ]; then
  # Get total memory available on machine
  TotalMem=$(free -m | grep Mem | awk '{ print $2 }')
  # Determine amount of free memory on the machine
  set -o pipefail
  FreeMem=$(free -m | grep buffers/cache | awk '{ print $4 }')
  if [ $? -ne 0 ]; then
    FreeMem=$(free -m | grep Mem | awk '{ print $7 }')
  fi
fi

The first issue is where the code attempts to set the value for FreeMem.

This command:

FreeMem=$(free -m | grep buffers/cache | awk '{ print $4 }')
# does not actually return anything which causes the program bail out to the line after the "if [ $? -ne 0 ]; then" statement

# It probably ought to  be:
FreeMem$(free -m | grep Mem | awk '{ print $6 }')

However, this is also an issue as is it not the full picture of the free memory on the system.

FreeMem should be defined as:

Free Memory = Free + Buffers + Cached

So, using the the example data above we would have
Free Memory = Free (224 MB) + Buffers & Cache (812) = 1,036 MB.

A possible solution might look something like this:

elif [ $os = "Linux" ]; then`
  # Get total memory available on machine
  TotalMem=$(free -m | grep Mem | awk '{ print $2 }')
  # Determine amount of free memory on the machine
  set -o pipefail
  FreeMem=$(free -m | grep Mem | awk '{ print $4 }')
  BuffCache=$(free -m | grep Mem | awk '{ print $6 }')
 if [ $? -ne 0 ]; then
   UsedMem=$(free -m | grep Mem | awk '{ print $3 }')
 fi
fi
# Get percentage of free memory
TotalFree=$(($FreeMem+$BuffCache))
FreePer=$(awk -v total="$TotalMem" -v free="$TotalFree" 'BEGIN { printf("%-10f\n", (free / total) * 100) }' | cut -d. -f1)

Thank you for your effort on this project,
-Thomas

EDIT BY @majormoses: used triple backticks to form code blocks so its easier to read

Units Clarification

Can anyone tell what units these metrics are in? For instance the '4356' from root used and the number after it.

[mesocool@blueserver bin]$ sudo ./metrics-disk-usage.rb
myserver.laptop.disk_usage.root.used 4356 1490035728
myserver.laptop.disk_usage.root.avail 25415 1490035728
myserver.laptop.disk_usage.root.used_percentage 15 1490035728
myserver.laptop.disk_usage.root.boot.used 221 1490035728
myserver.laptop.disk_usage.root.boot.avail 1693 1490035728
myserver.laptop.disk_usage.root.boot.used_percentage 12 1490035728
myserver.laptop.disk_usage.root.tmp.used 3 1490035728
myserver.laptop.disk_usage.root.tmp.avail 1849 1490035728
myserver.laptop.disk_usage.root.tmp.used_percentage 1 1490035728
myserver.laptop.disk_usage.root.var.used 722 1490035728
myserver.laptop.disk_usage.root.var.avail 2953 1490035728
myserver.laptop.disk_usage.root.var.used_percentage 20 1490035728
myserver.laptop.disk_usage.root.var.log.used 184 1490035728
myserver.laptop.disk_usage.root.var.log.avail 9047 1490035728
myserver.laptop.disk_usage.root.var.log.used_percentage 2 1490035728
myserver.laptop.disk_usage.root.var.log.audit.used 20 1490035728
myserver.laptop.disk_usage.root.var.log.audit.avail 3654 1490035728
myserver.laptop.disk_usage.root.var.log.audit.used_percentage 1 1490035728
myserver.laptop.disk_usage.root.disks.data304.used 46439 1490035728
myserver.laptop.disk_usage.root.disks.data304.avail 4253943 1490035728
myserver.laptop.disk_usage.root.disks.data304.used_percentage 2 1490035728
myserver.laptop.disk_usage.root.data.starfs1.used 197290 1490035728
myserver.laptop.disk_usage.root.data.starfs1.avail 705438 1490035728
myserver.laptop.disk_usage.root.data.starfs1.used_percentage 22 1490035728
myserver.laptop.disk_usage.root.data.fo.used 103321 1490035728
myserver.laptop.disk_usage.root.data.fo.avail 4795028 1490035728
myserver.laptop.disk_usage.root.data.fo.used_percentage 3 1490035728

Use /proc instead of free -m

Hi,

there is one serious problem with check-ram.rb:

`free -m`.split("\n").drop(1).each do |line|
free_ram = line.split[3].to_i if line =~ /^-\/\+ buffers\/cache:/ # rubocop:disable RegexpLiteral

It is not best way to relate on free -m. Why? Output can be different on different distributions. f.e.
Centos 6:

[root@centos6]# free -m
             total       used       free     shared    buffers     cached
Mem:          8192       1191       7000          2          0        953
-/+ buffers/cache:        237       7954
Swap:         4096          0       4096

Centos 7:

[root@centos7]# free -m
              total        used        free      shared  buff/cache   available
Mem:           8192         429         956           3        6806        7633
Swap:          4096         198        3897

This is causing false-positives in monitoring, because calling script gives output:

[root@centos7]# /opt/sensu/embedded/bin/check-ram.rb
CheckRAM CRITICAL: 0% free RAM left

I think it will be better to use /proc/meminfo - there is very small chance to change of file format.

vmstat gem installation path

Install vmstat gem into the path that sensu gems are expected to run from:
sudo /opt/sensu/embedded/bin/gem install vmstat --no-rdoc --no-ri

following above instruction, what is the expected destination this would install the gem file to?

I am able to install this gem as user "sensu", and gem installation destination is

/opt/sensu/.gem/ruby/2.3.0/gems

But rest of sensu gem dependencies are at

/opt/sensu/embedded/lib/ruby/gems/2.3.0/gems/

I am not too happy about dependencies spread across multiple locations...

Rewrite shell scripts to pure Ruby

Shell scripts should be rewritten to Pure Ruby in a way that is cross platform if available.

[ ] check-mem.sh
[ ] check-memory-pcnt.sh
[ ] check-swap-percentage.sh
[ ] check-swap.sh

MacOS Support

Hi, Could MacOS support get added to this since Sensu also has a Mac client now?

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.