sensu-plugins-cpu-usage 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 385500e80aa292fccaf05b20a9466e6a400f99a5
4
+ data.tar.gz: f9b6f16dcb01722a69ba5554673565b74619e575
5
+ SHA512:
6
+ metadata.gz: 64882b65518b853d2715b986e9f24b8bf6c468afe09b496b2a955c2c27cee5d2479c17eb376d18bb351014452e67dd5dce9fa12e6babb3a42138e60adbf80ac1
7
+ data.tar.gz: 79c7c69f46d2b695a206d76127100865ec95144d68c4de86aec3f5198dc934e05ccce160b8088bea0f6bf6e5c98e18a523685fc6360001d42d32bea5f94d7635
data/CHANGELOG.md ADDED
@@ -0,0 +1,10 @@
1
+ # Change Log
2
+ This project adheres to [Semantic Versioning](http://semver.org/).
3
+
4
+ This CHANGELOG follows the format listed at [Keep A Changelog](http://keepachangelog.com/)
5
+
6
+ ## Unreleased
7
+
8
+ ## [0.0.1] - 2016-01-05
9
+ ### Added
10
+ - Initial release
data/LICENSE ADDED
@@ -0,0 +1,23 @@
1
+ Copyright (c) 2015 Matteo Cerutti
2
+
3
+ MIT License
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining
6
+ a copy of this software and associated documentation files (the
7
+ "Software"), to deal in the Software without restriction, including
8
+ without limitation the rights to use, copy, modify, merge, publish,
9
+ distribute, sublicense, and/or sell copies of the Software, and to
10
+ permit persons to whom the Software is furnished to do so, subject to
11
+ the following conditions:
12
+
13
+ The above copyright notice and this permission notice shall be
14
+ included in all copies or substantial portions of the Software.
15
+
16
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
23
+
data/README.md ADDED
@@ -0,0 +1,46 @@
1
+ # Sensu plugin for monitoring the system CPU usage
2
+
3
+ A sensu plugin to monitor the system's CPU usage with the option to generate events on specific metrics (e.g. user time, system time)
4
+
5
+ The plugin generates multiple OK/WARN/CRIT/UNKNOWN events via the sensu client socket (https://sensuapp.org/docs/latest/clients#client-socket-input)
6
+ so that you do not miss state changes when monitoring multiple metrics.
7
+
8
+ ## Usage
9
+
10
+ The plugin accepts the following command line options:
11
+
12
+ ```
13
+ Usage: check-cpu-usage.rb (options)
14
+ -c, --critical <USAGE> Critical if USAGE exceeds the overall system cpu usage (default: 90)
15
+ --crit-guest <USAGE> Critical if USAGE exceeds the current system guest usage
16
+ --crit-guest_nice <USAGE> Critical if USAGE exceeds the current system guest_nice usage
17
+ --crit-idle <USAGE> Critical if USAGE exceeds the current system idle usage
18
+ --crit-iowait <USAGE> Critical if USAGE exceeds the current system iowait usage
19
+ --crit-irq <USAGE> Critical if USAGE exceeds the current system irq usage
20
+ --crit-nice <USAGE> Critical if USAGE exceeds the current system nice usage
21
+ --crit-softirq <USAGE> Critical if USAGE exceeds the current system softirq usage
22
+ --crit-steal <USAGE> Critical if USAGE exceeds the current system steal usage
23
+ --crit-system <USAGE> Critical if USAGE exceeds the current system system usage
24
+ --crit-user <USAGE> Critical if USAGE exceeds the current system user usage
25
+ -i <user,nice,system,idle,iowait,irq,softirq,steal,guest,guest_nice>,
26
+ --ignore-metric Comma separated list of metrics to ignore
27
+ -m <user,nice,system,idle,iowait,irq,softirq,steal,guest,guest_nice>,
28
+ --metric Comma separated list of metrics to monitor (default: ALL)
29
+ -s, --sleep <SECONDS> Sleep N seconds when sampling metrics
30
+ -w, --warn <USAGE> Warn if USAGE exceeds the overall system cpu usage (default: 80)
31
+ --warn-guest <USAGE> Warn if USAGE exceeds the current system guest usage
32
+ --warn-guest_nice <USAGE> Warn if USAGE exceeds the current system guest_nice usage
33
+ --warn-idle <USAGE> Warn if USAGE exceeds the current system idle usage
34
+ --warn-iowait <USAGE> Warn if USAGE exceeds the current system iowait usage
35
+ --warn-irq <USAGE> Warn if USAGE exceeds the current system irq usage
36
+ --warn-nice <USAGE> Warn if USAGE exceeds the current system nice usage
37
+ --warn-softirq <USAGE> Warn if USAGE exceeds the current system softirq usage
38
+ --warn-steal <USAGE> Warn if USAGE exceeds the current system steal usage
39
+ --warn-system <USAGE> Warn if USAGE exceeds the current system system usage
40
+ --warn-user <USAGE> Warn if USAGE exceeds the current system user usage
41
+ ```
42
+
43
+ By default, all metrics are taken into consideration when calculating the overall cpu usage.
44
+
45
+ ## Author
46
+ Matteo Cerutti - <matteo.cerutti@hotmail.co.uk>
@@ -0,0 +1,169 @@
1
+ #!/usr/bin/env ruby
2
+ #
3
+ # check-cpu-usage.rb
4
+ #
5
+ # Author: Matteo Cerutti <matteo.cerutti@hotmail.co.uk>
6
+ #
7
+
8
+ require 'sensu-plugin/check/cli'
9
+ require 'json'
10
+ require 'socket'
11
+
12
+ class CheckCpuUsage < Sensu::Plugin::Check::CLI
13
+ @@metrics = ['user', 'nice', 'system', 'idle', 'iowait', 'irq', 'softirq', 'steal', 'guest', 'guest_nice']
14
+
15
+ option :metric,
16
+ :description => "Comma separated list of metrics to monitor (default: ALL)",
17
+ :short => "-m <#{@@metrics.join(',')}>",
18
+ :long => "--metric <#{@@metrics.join(',')}>",
19
+ :proc => proc { |s| s.split(',') },
20
+ :default => @@metrics
21
+
22
+ option :ignore_metric,
23
+ :description => "Comma separated list of metrics to ignore",
24
+ :short => "-i <#{@@metrics.join(',')}>",
25
+ :long => "--ignore-metric <#{@@metrics.join(',')}>",
26
+ :proc => proc { |s| s.split(',') },
27
+ :default => []
28
+
29
+ @@metrics.each do |metric|
30
+ option "warn_#{metric}".to_sym,
31
+ :description => "Warn if USAGE exceeds the current system #{metric} usage",
32
+ :long => "--warn-#{metric} <USAGE>",
33
+ :proc => proc(&:to_i),
34
+ :default => nil
35
+
36
+ option "crit_#{metric}".to_sym,
37
+ :description => "Critical if USAGE exceeds the current system #{metric} usage",
38
+ :long => "--crit-#{metric} <USAGE>",
39
+ :proc => proc(&:to_i),
40
+ :default => nil
41
+ end
42
+
43
+ option :sleep,
44
+ :description => "Sleep N seconds when sampling metrics",
45
+ :short => "-s <SECONDS>",
46
+ :long => "--sleep <SECONDS>",
47
+ :default => 1,
48
+ :proc => proc(&:to_i)
49
+
50
+ option :warn,
51
+ :description => "Warn if USAGE exceeds the overall system cpu usage (default: 80)",
52
+ :short => "-w <USAGE>",
53
+ :long => "--warn <USAGE>",
54
+ :default => 80,
55
+ :proc => proc(&:to_i)
56
+
57
+ option :crit,
58
+ :description => "Critical if USAGE exceeds the overall system cpu usage (default: 90)",
59
+ :short => "-c <USAGE>",
60
+ :long => "--critical <USAGE>",
61
+ :default => 90,
62
+ :proc => proc(&:to_i)
63
+
64
+ def initialize()
65
+ super
66
+
67
+ raise "Warning CPU usage threshold must be lower than the critical threshold" if config[:warn] >= config[:crit]
68
+
69
+ # sanity checks
70
+ @@metrics.each do |metric|
71
+ raise "Must specify both warning and critical thresholds for CPU #{metric}" if (config["warn_#{metric}".to_sym] and config["crit_#{metric}".to_sym].nil?) or (config["warn_#{metric}".to_sym].nil? and config["crit_#{metric}".to_sym])
72
+
73
+ if config["warn_#{metric}".to_sym] and config["crit_#{metric}".to_sym]
74
+ raise "Warning CPU #{metric} threshold must be lower than the critical threshold" if config["warn_#{metric}".to_sym] >= config["crit_#{metric}".to_sym]
75
+ end
76
+ end
77
+ end
78
+
79
+ def send_client_socket(data)
80
+ sock = UDPSocket.new
81
+ sock.send(data + "\n", 0, "127.0.0.1", 3030)
82
+ end
83
+
84
+ def send_ok(check_name, msg)
85
+ event = {"name" => check_name, "status" => 0, "output" => "OK: #{msg}", "handler" => config[:handler]}
86
+ send_client_socket(event.to_json)
87
+ end
88
+
89
+ def send_warning(check_name, msg)
90
+ event = {"name" => check_name, "status" => 1, "output" => "WARNING: #{msg}", "handler" => config[:handler]}
91
+ send_client_socket(event.to_json)
92
+ end
93
+
94
+ def send_critical(check_name, msg)
95
+ event = {"name" => check_name, "status" => 2, "output" => "CRITICAL: #{msg}", "handler" => config[:handler]}
96
+ send_client_socket(event.to_json)
97
+ end
98
+
99
+ def send_unknown(check_name, msg)
100
+ event = {"name" => check_name, "status" => 3, "output" => "UNKNOWN: #{msg}", "handler" => config[:handler]}
101
+ send_client_socket(event.to_json)
102
+ end
103
+
104
+ def get_cpustats()
105
+ cpu = {}
106
+
107
+ stats = %x[cat /proc/stat]
108
+ values = stats[/^cpu\s*(.*)$/, 1].split(' ').map(&:to_i)
109
+
110
+ # missing guest time (kernel < 2.6.24)
111
+ values << 0 if values.size < 9
112
+
113
+ # missing guest_nice time (kernel < 2.6.33)
114
+ values << 0 if values.size < 10
115
+
116
+ @@metrics.each_with_index do |metric, index|
117
+ cpu[metric] = values[index]
118
+ end
119
+
120
+ cpu
121
+ end
122
+
123
+ def run
124
+ stats_before = get_cpustats()
125
+ sleep(config[:sleep]) if config[:sleep] > 0
126
+ stats_after = get_cpustats()
127
+ stats_diff = {}
128
+ total_diff = 0
129
+
130
+ metrics = config[:metric] - config[:ignore_metric]
131
+ metrics.each do |metric|
132
+ stats_diff[metric] = stats_after[metric] - stats_before[metric]
133
+ total_diff += stats_diff[metric]
134
+ end
135
+
136
+ # calculate percentage of total time for each metric
137
+ usage = {}
138
+ metrics.each do |metric|
139
+ usage[metric] = 100 * stats_diff[metric] / total_diff
140
+ end
141
+
142
+ usage.each do |metric, value|
143
+ check_name = "cpu-usage-#{metric}"
144
+
145
+ if config["warn_#{metric}".to_sym] and config["crit_#{metric}".to_sym]
146
+ if config["crit_#{metric}".to_sym] and value >= config["crit_#{metric}".to_sym]
147
+ send_critical(check_name, "CPU #{metric} time is too high - Current: #{value}% (>= #{config["crit_#{metric}".to_sym]}%)" if value >= config["crit_#{metric}".to_sym])
148
+ elsif config["warn_#{metric}".to_sym] and value >= config["warn_#{metric}".to_sym]
149
+ send_warning(check_name, "High CPU #{metric} time - Current: #{value}% (>= #{config["warn_#{metric}".to_sym]}%)" if value >= config["warn_#{metric}".to_sym])
150
+ else
151
+ send_ok(check_name, "CPU #{metric} time is normal - Current: #{value}% (<= #{config["warn_#{metric}".to_sym]}%)")
152
+ end
153
+ else
154
+ send_ok(check_name, "CPU #{metric} not monitored")
155
+ end
156
+ end
157
+
158
+ config[:ignore_metrics].each do |metric|
159
+ send_ok(check_name, "CPU #{metric} time not monitored")
160
+ end
161
+
162
+ # overall cpu usage
163
+ usage['overall'] = 100 * (total_diff - stats_diff['idle']) / total_diff
164
+
165
+ critical("CPU usage is too high - Current: #{usage['overall']}% (>= #{config[:crit]}%)") if usage['overall'] >= config[:crit]
166
+ warning("High CPU usage - Current: #{usage['overall']}% (>= #{config[:warn]}%)") if usage['overall'] >= config[:warn]
167
+ ok("CPU usage is normal - Current: #{usage['overall']}% (<= #{config[:warn]}%)")
168
+ end
169
+ end
@@ -0,0 +1,9 @@
1
+ module SensuPluginsCpuUsage
2
+ module Version
3
+ MAJOR = 0
4
+ MINOR = 0
5
+ PATCH = 1
6
+
7
+ VER_STRING = [MAJOR, MINOR, PATCH].compact.join('.')
8
+ end
9
+ end
@@ -0,0 +1 @@
1
+ require 'sensu-plugins-cpu-usage/version'
metadata ADDED
@@ -0,0 +1,70 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: sensu-plugins-cpu-usage
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - Matteo Cerutti
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2016-01-05 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: sensu-plugin
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - '='
18
+ - !ruby/object:Gem::Version
19
+ version: 1.2.0
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - '='
25
+ - !ruby/object:Gem::Version
26
+ version: 1.2.0
27
+ description: This plugin provides facilities for monitoring the system cpu usage
28
+ email: "<matteo.cerutti@hotmail.co.uk>"
29
+ executables:
30
+ - check-cpu-usage.rb
31
+ extensions: []
32
+ extra_rdoc_files: []
33
+ files:
34
+ - CHANGELOG.md
35
+ - LICENSE
36
+ - README.md
37
+ - bin/check-cpu-usage.rb
38
+ - lib/sensu-plugins-cpu-usage.rb
39
+ - lib/sensu-plugins-cpu-usage/version.rb
40
+ homepage: https://github.com/m4ce/sensu-plugins-cpu-usage
41
+ licenses:
42
+ - MIT
43
+ metadata:
44
+ maintainer: "@m4ce"
45
+ development_status: active
46
+ production_status: stable
47
+ release_draft: 'false'
48
+ release_prerelease: 'false'
49
+ post_install_message: You can use the embedded Ruby by setting EMBEDDED_RUBY=true
50
+ in /etc/default/sensu
51
+ rdoc_options: []
52
+ require_paths:
53
+ - lib
54
+ required_ruby_version: !ruby/object:Gem::Requirement
55
+ requirements:
56
+ - - ">="
57
+ - !ruby/object:Gem::Version
58
+ version: 1.9.3
59
+ required_rubygems_version: !ruby/object:Gem::Requirement
60
+ requirements:
61
+ - - ">="
62
+ - !ruby/object:Gem::Version
63
+ version: '0'
64
+ requirements: []
65
+ rubyforge_project:
66
+ rubygems_version: 2.4.5.1
67
+ signing_key:
68
+ specification_version: 4
69
+ summary: Sensu plugins for monitoring the system cpu usage
70
+ test_files: []