Skip to content

Commit 745000b

Browse files
awangptcmajormoses
authored andcommitted
Support 'I' (TASK_IDLE) process state introduced by Linux kernel 4.14
- default behavior counts I in the same bucket as D (uninterruptible) Add --idle-state, -I option to split out TASK_IDLE processes into a .idle metric
1 parent c076d20 commit 745000b

3 files changed

Lines changed: 45 additions & 1 deletion

File tree

CHANGELOG.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,12 @@ This CHANGELOG follows the format listed [here](https://github.com/sensu-plugins
55

66

77
## [Unreleased]
8+
### Fixed
9+
- metrics-processes-threads-count.rb: Backward compatible support for TASK_IDLE process state in Linux 4.14+ (@awangptc)
10+
11+
### Added
12+
- metrics-processes-threads-count.rb: Add option to separate out TASK_IDLE process into it's own metric (@awangptc)
13+
814

915
## [3.1.0] - 2018-05-02
1016
### Added

bin/metrics-processes-threads-count.rb

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,13 @@ class ProcessesThreadsCount < Sensu::Plugin::Metric::CLI::Graphite
5858
boolean: true,
5959
default: false
6060

61+
option :idle_state,
62+
description: 'If specified, count TASK_IDLE (I) state in separate .idle metric. Note: TASK_IDLE is only available on Linux Kernels 4.14 and higher',
63+
short: '-I',
64+
long: '--idle-state',
65+
boolean: true,
66+
default: false
67+
6168
# Takes a value to be tested as an integer. If a new Integer instance cannot be created from it, return 1.
6269
# See the comments on get_process_threads() for why 1 is returned.
6370
def test_int(i)
@@ -95,9 +102,14 @@ def count_processes_by_status(ps_table)
95102
%w[S R D T t X Z].each do |v|
96103
list_proc[v] = 0
97104
end
105+
list_proc['I'] = 0 if config[:idle_state]
98106
if ps_table.first.respond_to?(:state)
99107
ps_table.each do |pr|
100-
list_proc[pr[:state]] += 1
108+
state = pr[:state]
109+
if state == 'I'
110+
state = 'D' unless config[:idle_state]
111+
end
112+
list_proc[state] += 1
101113
end
102114
end
103115
list_proc
@@ -131,6 +143,8 @@ def run
131143
output "#{[config[:scheme], 'dead'].join('.')} #{proc_count} #{timestamp}"
132144
when 'Z'
133145
output "#{[config[:scheme], 'zombie'].join('.')} #{proc_count} #{timestamp}"
146+
when 'I'
147+
output "#{[config[:scheme], 'idle'].join('.')} #{proc_count} #{timestamp}"
134148
end
135149
end
136150
end

test/metrics-processes-threads-count_spec.rb

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,30 @@
4545
ptcount = ProcessesThreadsCount.new
4646
expect(ptcount.count_threads(table)).to eq(10)
4747
end
48+
49+
it 'should be able to count processes in each state - count I toward D for 4.14+ kernels' do
50+
states = %w[S R D T t X Z I]
51+
ps_entry = Struct.new(:state)
52+
table = states.map { |state| ps_entry.new(state) }
53+
ptcount = ProcessesThreadsCount.new
54+
counts = ptcount.count_processes_by_status(table)
55+
states.each do |state|
56+
expect(counts[state]).to eq(2) if state == 'D'
57+
expect(counts[state]).to be_nil if state == 'I'
58+
end
59+
end
60+
61+
it 'should be able to count processes in each state with :idle_state enabled - count I separate' do
62+
states = %w[S R D T t X Z I]
63+
ps_entry = Struct.new(:state)
64+
table = states.map { |state| ps_entry.new(state) }
65+
ptcount = ProcessesThreadsCount.new
66+
ptcount.config[:idle_state] = true
67+
counts = ptcount.count_processes_by_status(table)
68+
states.each do |state|
69+
expect(counts[state]).to eq(1)
70+
end
71+
end
4872
end
4973

5074
describe ThreadsCount, 'run' do

0 commit comments

Comments
 (0)