sensu-plugins-process-checks 2.6.0 → 2.7.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: a359a27504b2f6f1129f1221775b3e7d081be20a4b9bc092683f9a4f112ed961
4
- data.tar.gz: 9a0665cdde92261c3726d9a4dc641fffb3196dc0c19d5468bf7050d3b1e76ac7
3
+ metadata.gz: 876caee1acc9312011319211530a65c0277ba8827397e5b0efb1855417e95f64
4
+ data.tar.gz: 7d353e6e88a47b7d9edb50586f08e4569a2b4ebe578df3a91ececdf5c9cb01fb
5
5
  SHA512:
6
- metadata.gz: 820865ef5103241ff3d1a4747239b6c7444d0bde906bcea7a248ee6549795a55bbb60b58d90326823be549a302f1ad56d5c52c2ff408ac9695038965a4bfecac
7
- data.tar.gz: 9496e6fa7679ad4afb56134260f6e453bf10b15a935595ea959b91e91d85a59baebbb15d9842427adec5a80b1ab23281236b8c6eb658dfeece23e4975a2103fc
6
+ metadata.gz: e9cb951943643d96a22cd791b742db5dc7c5b1540a966373597149fabd365e5ae3e4fdbcf4a36844def13ce0a6c85106afef2bab9fb302eaba226b717e00743a
7
+ data.tar.gz: 8a07af4a8bc3eacb1b8ed6829a08db70cdc392cdc8e25bdf7bcbcf4460d6971ab451fdb048f40ef0cbe8b9308872930822ebb339a19fd9979f3d9ff9ea2691df
data/CHANGELOG.md CHANGED
@@ -6,6 +6,10 @@ This CHANGELOG follows the format listed [here](https://github.com/sensu-plugins
6
6
 
7
7
  ## [Unreleased]
8
8
 
9
+ ## [2.7.0] - 2018-01-06
10
+ ### Changed
11
+ - metrics-per-processes.py: Add option to find processes by username (@rthouvenin)
12
+
9
13
  ## [2.6.0] - 2017-12-05
10
14
  ### Changed
11
15
  - loosen dependency of `sys-proctable` (@majormoses)
@@ -156,7 +160,8 @@ This CHANGELOG follows the format listed [here](https://github.com/sensu-plugins
156
160
  - built against 1.9.3, 2.0, 2.1
157
161
  - cryptographically signed
158
162
 
159
- [Unreleased]: https://github.com/sensu-plugins/sensu-plugins-process-checks/compare/2.6.0...HEAD
163
+ [Unreleased]: https://github.com/sensu-plugins/sensu-plugins-process-checks/compare/2.7.0...HEAD
164
+ [2.7.0]: https://github.com/sensu-plugins/sensu-plugins-process-checks/compare/2.6.0...2.7.0
160
165
  [2.6.0]: https://github.com/sensu-plugins/sensu-plugins-process-checks/compare/2.5.0...2.6.0
161
166
  [2.5.0]: https://github.com/sensu-plugins/sensu-plugins-process-checks/compare/2.4.0...2.5.0
162
167
  [2.4.0]: https://github.com/sensu-plugins/sensu-plugins-process-checks/compare/2.3.0...2.4.0
@@ -16,7 +16,7 @@
16
16
  #
17
17
  # USAGE:
18
18
  #
19
- # metrics-per-process.py -n <process_name> | -p <path_to_process_pid_file> [-s <graphite_scheme>] [-m <metrics_regexes>]
19
+ # metrics-per-process.py -n <process_name> | -p <path_to_process_pid_file> | -u <username> [-s <graphite_scheme>] [-m <metrics_regexes>]
20
20
  #
21
21
  # NOTES:
22
22
  # The plugin requires to read files in the /proc file system, make sure the owner
@@ -66,6 +66,7 @@
66
66
  import os
67
67
  import optparse
68
68
  import psutil
69
+ import pwd
69
70
  import re
70
71
  import sys
71
72
  import time
@@ -92,23 +93,46 @@ TCP_CONN_STATUSES = [
92
93
  ]
93
94
  MEMORY_STATS = ['rss', 'vms', 'shared', 'text', 'lib', 'data', 'dirty']
94
95
 
95
- def find_pids_from_name(process_name):
96
- '''Find process PID from name using /proc/<pids>/comm'''
96
+ def find_pids(matcher):
97
+ '''Find process PID using /proc/<pids> with given matcher'''
97
98
 
98
99
  pids_in_proc = [ pid for pid in os.listdir(PROC_ROOT_DIR) if pid.isdigit() ]
99
100
  pids = []
100
101
  for pid in pids_in_proc:
101
102
  path = PROC_ROOT_DIR + pid
102
103
  try:
103
- if 'comm' in os.listdir(path):
104
- file_handler = open(path + '/comm', 'r')
105
- if file_handler.read().rstrip() == process_name:
106
- pids.append(int(pid))
104
+ if matcher(path):
105
+ pids.append(int(pid))
107
106
  except OSError, e:
108
- if e.errno == 2:
109
- pass
107
+ if e.errno == 2:
108
+ pass
110
109
  return pids
111
110
 
111
+ def find_pids_from_name(process_name):
112
+ '''Find process PID from name using /proc/<pids>/comm'''
113
+
114
+ def matcher(path):
115
+ if 'comm' in os.listdir(path):
116
+ file_handler = open(path + '/comm', 'r')
117
+ return file_handler.read().rstrip() == process_name
118
+
119
+ return find_pids(matcher)
120
+
121
+ def find_pids_from_user(username):
122
+ '''Find process PID from username using ownership of /proc/<pids>'''
123
+
124
+ try:
125
+ user = pwd.getpwnam(username)
126
+ uid = user.pw_uid
127
+ except KeyError:
128
+ return []
129
+
130
+ def matcher(path):
131
+ stat = os.stat(path)
132
+ return stat.st_uid == uid
133
+
134
+ return find_pids(matcher)
135
+
112
136
  def additional_stats(process_handler, metrics_regexp):
113
137
  stats = {}
114
138
 
@@ -250,15 +274,20 @@ def main():
250
274
  parser = optparse.OptionParser()
251
275
 
252
276
  parser.add_option('-n', '--process-name',
253
- help = 'name of process to collect stats (imcompatible with -p)',
277
+ help = 'name of process to collect stats (imcompatible with -p or -u)',
254
278
  dest = 'process_name',
255
279
  metavar = 'PROCESS_NAME')
256
280
 
257
281
  parser.add_option('-p', '--pid-file',
258
- help = 'path to pid file for process to collect stats (imcompatible with -n)',
282
+ help = 'path to pid file for process to collect stats (imcompatible with -n or -u)',
259
283
  dest = 'process_pid_file',
260
284
  metavar = 'PROCESS_PID_FILE')
261
285
 
286
+ parser.add_option('-u', '--user',
287
+ help = 'username of user running the process to collect stats (incompatible with -n or -p)',
288
+ dest = 'username',
289
+ metavar = 'USERNAME')
290
+
262
291
  parser.add_option('-s', '--graphite_scheme',
263
292
  help = 'graphite scheme to prepend, default to <process_stats>',
264
293
  default = 'per_process_stats',
@@ -272,13 +301,15 @@ def main():
272
301
  metavar = 'METRICS_REGEXES')
273
302
 
274
303
  (options, args) = parser.parse_args()
304
+ options_list = [options.process_name, options.process_pid_file, options.username]
305
+ options_count = len(filter(lambda x: x is not None, options_list))
275
306
 
276
- if options.process_name and options.process_pid_file:
277
- print 'Specify a process name or a process pid file path, but not both'
307
+ if options_count > 1:
308
+ print 'Specify a process name or a process pid file path or username, but only one of them'
278
309
  sys.exit(1)
279
310
 
280
- if not options.process_name and not options.process_pid_file:
281
- print 'A process name or a process pid file path is needed'
311
+ if options_count == 0:
312
+ print 'A process name or a process pid file path or username is needed'
282
313
  sys.exit(1)
283
314
 
284
315
  options.metrics_regexes = [re.compile(regex) for regex in options.metrics_regexes.split(',')]
@@ -287,6 +318,10 @@ def main():
287
318
  pids = find_pids_from_name(options.process_name)
288
319
  graphite_printer(multi_pid_process_stats(pids, options.metrics_regexes), options.graphite_scheme)
289
320
 
321
+ if options.username:
322
+ pids = find_pids_from_user(options.username)
323
+ graphite_printer(multi_pid_process_stats(pids, options.metrics_regexes), options.graphite_scheme)
324
+
290
325
  if options.process_pid_file:
291
326
  pid = get_pid_from_pid_file(options.process_pid_file)
292
327
  graphite_printer(stats_per_pid(pid, options.metrics_regexes), options.graphite_scheme)
@@ -1,7 +1,7 @@
1
1
  module SensuPluginsProcessChecks
2
2
  module Version
3
3
  MAJOR = 2
4
- MINOR = 6
4
+ MINOR = 7
5
5
  PATCH = 0
6
6
 
7
7
  VER_STRING = [MAJOR, MINOR, PATCH].compact.join('.')
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: sensu-plugins-process-checks
3
3
  version: !ruby/object:Gem::Version
4
- version: 2.6.0
4
+ version: 2.7.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Sensu-Plugins and contributors
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2017-12-05 00:00:00.000000000 Z
11
+ date: 2018-01-07 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: english
@@ -236,7 +236,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
236
236
  version: '0'
237
237
  requirements: []
238
238
  rubyforge_project:
239
- rubygems_version: 2.7.3
239
+ rubygems_version: 2.7.4
240
240
  signing_key:
241
241
  specification_version: 4
242
242
  summary: Sensu plugins for checking running processes