fluent-plugin-watch-process 0.0.1 → 0.0.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
data/README.md CHANGED
@@ -3,17 +3,17 @@ fluent-plugin-watch-process [![Build Status](https://travis-ci.org/y-ken/fluent-
3
3
 
4
4
  ## Overview
5
5
 
6
- Fluentd Input plugin to collect process information via ps command.
6
+ Fluentd Input plugin to collect continual process information via ps command. It is useful for cron/barch process monitoring.
7
7
 
8
8
  ## Use Cases
9
9
 
10
- * collect cron/batch process for analysis.
10
+ * collect cron/batch process for long term analysis.
11
11
  * high cpu load time
12
12
  * high usage of memory
13
13
  * determine too long running task
14
14
 
15
15
  * output destination example
16
- * Elasticsearch + Kibana to visualize statistics. Example: [example1.conf](https://github.com/y-ken/fluent-plugin-watch-process/blob/master/example1.conf)
16
+ * Elasticsearch + Kibana to visualize cron/batch process statistics. Example: [example1.conf](https://github.com/y-ken/fluent-plugin-watch-process/blob/master/example1.conf)
17
17
  * save process information as audit log into AWS S3 which filename isolated by hostname. Example: [example2.conf](https://github.com/y-ken/fluent-plugin-watch-process/blob/master/example2.conf)
18
18
 
19
19
  ## Installation
@@ -51,37 +51,38 @@ After restarting td-agent, it will output process information to the td-agent.lo
51
51
 
52
52
  `````
53
53
  $ tail -f /var/log/td-agent/td-agent.log
54
+ ...snip...
54
55
  2014-01-16 14:21:34 +0900 debug.batch.localhost: {"start_time":"2014-01-16 14:21:13 +0900","user":"td-agent","pid":17486,"parent_pid":17483,"cpu_time":"00:00:00","cpu_percent":1.5,"memory_percent":3.5,"mem_rss":36068,"mem_size":60708,"state":"S","proc_name":"ruby","command":"/usr/lib64/fluent/ruby/bin/ruby /usr/sbin/td-agent --group td-agent --log /var/log/td-agent/td-agent.log --daemon /var/run/td-agent/td-agent.pid","elapsed_time":21}
55
56
  `````
56
57
 
57
58
  ### Syntax
58
59
 
59
- * tag # Required
60
+ * tag (Required)
60
61
  * record output destination
61
62
  * supported tag placeholders are `${hostname}` and `__HOSTNAME__`.
62
63
 
63
- * command # Optional
64
+ * command (Optional)
64
65
  * execute ps command with some options
65
66
  * [default] Linux: `LANG=en_US.UTF-8 && ps -ewwo lstart,user:20,pid,ppid,time,%cpu,%mem,rss,sz,s,comm,cmd`
66
67
  * [default] MacOSX: `LANG=en_US.UTF-8 && ps -ewwo lstart,user,pid,ppid,time,%cpu,%mem,rss,vsz,state,comm,command`
67
68
 
68
- * keys # Optional
69
+ * keys (Optional)
69
70
  * output record keys of the ps command results
70
71
  * [default] start_time user pid parent_pid cpu_time cpu_percent memory_percent mem_rss mem_size state proc_name command
71
72
 
72
- * types # Optional
73
+ * types (Optional)
73
74
  * settings of converting types from string to integer/float.
74
75
  * [default] pid:integer parent_pid:integer cpu_percent:float memory_percent:float mem_rss:integer mem_size:integer
75
76
 
76
- * interval # Optional
77
+ * interval (Optional)
77
78
  * execute interval time
78
79
  * [default] 5s
79
80
 
80
- * lookup_user # Optional
81
+ * lookup_user (Optional)
81
82
  * filter process owner username with comma delimited
82
83
  * [default] N/A
83
84
 
84
- * hostname_command # Optional
85
+ * hostname_command (Optional)
85
86
  * settings for tag placeholder, `${hostname}` and `__HOSTNAME__`. By default, it using long hostname.
86
87
  * to use short hostname, set `hostname -s` for this option on linux/mac.
87
88
  * [default] `hostname`
@@ -3,11 +3,11 @@ $:.push File.expand_path("../lib", __FILE__)
3
3
 
4
4
  Gem::Specification.new do |s|
5
5
  s.name = "fluent-plugin-watch-process"
6
- s.version = "0.0.1"
6
+ s.version = "0.0.2"
7
7
  s.authors = ["Kentaro Yoshida"]
8
8
  s.email = ["y.ken.studio@gmail.com"]
9
9
  s.homepage = "https://github.com/y-ken/fluent-plugin-watch-process"
10
- s.summary = %q{Fluentd Input plugin to collect process information via ps command.}
10
+ s.summary = %q{Fluentd Input plugin to collect continual process information via ps command. It is useful for cron/barch process monitoring.}
11
11
 
12
12
  s.files = `git ls-files`.split("\n")
13
13
  s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
@@ -10,6 +10,9 @@ module Fluent
10
10
  config_param :lookup_user, :string, :default => nil
11
11
  config_param :hostname_command, :string, :default => 'hostname'
12
12
 
13
+ DEFAULT_KEYS = %w(start_time user pid parent_pid cpu_time cpu_percent memory_percent mem_rss mem_size state proc_name command)
14
+ DEFAULT_TYPES = %w(pid:integer parent_pid:integer cpu_percent:float memory_percent:float mem_rss:integer mem_size:integer)
15
+
13
16
  Converters = {
14
17
  'string' => lambda { |v| v.to_s },
15
18
  'integer' => lambda { |v| v.to_i },
@@ -39,8 +42,8 @@ module Fluent
39
42
  super
40
43
 
41
44
  @command = @command || get_ps_command
42
- @keys = @keys || %w(start_time user pid parent_pid cpu_time cpu_percent memory_percent mem_rss mem_size state proc_name command)
43
- types = @types || %w(pid:integer parent_pid:integer cpu_percent:float memory_percent:float mem_rss:integer mem_size:integer)
45
+ @keys = @keys.nil? ? DEFAULT_KEYS : @keys.to_s.gsub(' ', '').split(',')
46
+ @types = @types || DEFAULT_TYPES
44
47
  @types_map = Hash[types.map{|v| v.split(':')}]
45
48
  @lookup_user = @lookup_user.gsub(' ', '').split(',') unless @lookup_user.nil?
46
49
  @interval = Config.time_value(@interval)
@@ -61,15 +64,20 @@ module Fluent
61
64
  io = IO.popen(@command, 'r')
62
65
  io.gets
63
66
  while result = io.gets
64
- values = result.chomp.strip.split(/\s+/, @keys.size + 4)
65
- time = Time.parse(values[0...5].join(' '))
67
+ keys_size = @keys.size
68
+ if result =~ /(?<lstart>(^\w+ \w+ \d+ \d\d:\d\d:\d\d \d+))/
69
+ lstart = Time.parse($~[:lstart])
70
+ values = result.sub($~[:lstart], '')
71
+ keys_size -= 1
72
+ end
73
+ values = values.chomp.strip.split(/\s+/, keys_size)
66
74
  data = Hash[
67
- @keys.zip([time.to_s, values.values_at(5..15)].flatten).map do |k,v|
75
+ @keys.zip([lstart.to_s, values].reject(&:empty?).flatten).map do |k,v|
68
76
  v = Converters[@types_map[k]].call(v) if @types_map.include?(k)
69
77
  [k,v]
70
78
  end
71
79
  ]
72
- data['elapsed_time'] = (Time.now - Time.parse(data['start_time'])).to_i
80
+ data['elapsed_time'] = (Time.now - Time.parse(data['start_time'])).to_i if data['start_time']
73
81
  next unless @lookup_user.nil? || @lookup_user.include?(data['user'])
74
82
  tag = @tag.gsub(/(\${[a-z]+}|__[A-Z]+__)/, get_placeholder)
75
83
  Engine.emit(tag, Engine.now, data)
@@ -77,6 +85,8 @@ module Fluent
77
85
  io.close
78
86
  sleep @interval
79
87
  end
88
+ rescue StandardError => e
89
+ $log.error "watch_process: error has occured. #{e.message}"
80
90
  end
81
91
 
82
92
  def get_ps_command
@@ -7,11 +7,10 @@ class WatchProcessInputTest < Test::Unit::TestCase
7
7
 
8
8
  CONFIG = %[
9
9
  tag input.watch_process
10
- lookup_user apache, mycron
11
10
  ]
12
11
 
13
- def create_driver(conf=CONFIG,tag='test')
14
- Fluent::Test::OutputTestDriver.new(Fluent::WatchProcessInput, tag).configure(conf)
12
+ def create_driver(conf = CONFIG)
13
+ Fluent::Test::InputTestDriver.new(Fluent::WatchProcessInput).configure(conf)
15
14
  end
16
15
 
17
16
  def test_configure
@@ -27,4 +26,3 @@ class WatchProcessInputTest < Test::Unit::TestCase
27
26
  assert_equal ['apache', 'mycron'], d.instance.lookup_user
28
27
  end
29
28
  end
30
-
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: fluent-plugin-watch-process
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.1
4
+ version: 0.0.2
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2014-01-16 00:00:00.000000000 Z
12
+ date: 2014-01-17 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: rake
@@ -85,7 +85,8 @@ rubyforge_project:
85
85
  rubygems_version: 1.8.23
86
86
  signing_key:
87
87
  specification_version: 3
88
- summary: Fluentd Input plugin to collect process information via ps command.
88
+ summary: Fluentd Input plugin to collect continual process information via ps command.
89
+ It is useful for cron/barch process monitoring.
89
90
  test_files:
90
91
  - test/helper.rb
91
92
  - test/plugin/test_in_watch_process.rb