malevich 0.0.2 → 0.0.3

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.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: e70253bb89c1f108af088c3c442e0ef69049db6e
4
- data.tar.gz: ff956877ab483fd1c4e4a0473a92a5d8a9380f12
3
+ metadata.gz: 325d8a8c4611108b8280d60ae54db8be295f4e18
4
+ data.tar.gz: c260c8bb5f244505a138a1235f5b873d85eee541
5
5
  SHA512:
6
- metadata.gz: 04603ae1935fee81416cc6860c5f288facf3144f79a4d53bc6983a04ccdd7f935f5341f6ad5d65b73749e2d481b17c541c419ed9d73492385534d4511affcc22
7
- data.tar.gz: ef094efa3de57927a2b9e0a641b440faea651c67ec2cbec3139a3c44053864f676bf726983f3eb3741bcad9d57635ce2a6f5b41f313e979e5b4e6bb5d46e6eb4
6
+ metadata.gz: 08feb43a589b177bcbf1070807987cb1050b9032f9420a4f7b3e57f022d6e5d9c48d05340192942a4087f4f364294336c792a15fb8cb4be430d552a684102ce2
7
+ data.tar.gz: 59c067fbd61348d2a1972fbde4e66c15cc28d8d5e0c9ee957e70d62b8f09365dfb5722dfe1fd176414bf8cdb7db67d30fda02184d8eef0b6ab8fd2fcde1523e7
data/bin/malevich CHANGED
@@ -26,10 +26,11 @@ where [options] are:
26
26
  opt :'http-responder', 'Bind http api', :short => '-h', :default => '0.0.0.0:55755'
27
27
  opt :'udp-responder', 'Bind udp api', :short => '-u', :default => '127.0.0.1:55955'
28
28
  opt :'test', 'Test plugin file', :type => :string
29
+ opt :'profiling', 'Profiling ruby', :default => false
29
30
  end
30
31
 
32
+ include Malevich::Mixin
31
33
  malevich.cmd.deep_merge!(cmd)
32
- #malevich.cmd[:pwd] = Dir.pwd
33
34
  malevich.log_level = cmd[:'log-level']
34
35
  malevich.test_plugin(cmd[:'test'])
35
36
  malevich.load_plugins(cmd[:'plugins'], cmd[:'config'])
data/example/cpu.rb CHANGED
@@ -15,13 +15,13 @@ collect 'linux' do
15
15
  _, u2, n2, s2, i2 = [$1, $2, $3, $4, $5].map { |e| e.to_i }
16
16
  unless @old_cpu[cpu_number].nil?
17
17
  u1, n1, s1, i1 = @old_cpu[cpu_number]
18
- used = (u2+n2+s2) - (u1+n1+s1)
19
- total = used + i2-i1
20
- fraction = used.to_f / total
18
+ used = (u2+n2+s2) - (u1+n1+s1)
19
+ total = used + i2-i1
20
+ fraction = used.to_f / total
21
21
  end
22
22
  @old_cpu[cpu_number] = [u2, n2, s2, i2]
23
- service = "cpu usage cpu#{cpu_number}"
24
- description = "Cpu#{cpu_number} usage"
23
+ service = "cpu usage cpu#{cpu_number}"
24
+ description = "Cpu#{cpu_number} usage"
25
25
  if cpu_number == '_total'
26
26
  event(:service => service, :metric => fraction, :description => description)
27
27
  else
data/example/disk.rb CHANGED
@@ -41,7 +41,7 @@ collect 'linux' do
41
41
  end
42
42
 
43
43
  get_monit_points_for_size.each do |point|
44
- point_stat = Sys::Filesystem.stat(point)
44
+ point_stat = Sys::Filesystem.stat(point)
45
45
  human_point = point == '/' ? '/root' : point
46
46
  human_point = human_point.gsub(/^\//, '').gsub(/\//, '_')
47
47
  event(:service => "disk #{human_point} % block", :description => "Disk usage #{point}, %", :metric => (1- point_stat.blocks_available.to_f/point_stat.blocks).round(2) * 100) unless point_stat.blocks == 0
data/example/disk_stat.rb CHANGED
@@ -1,11 +1,11 @@
1
1
  always_start true
2
2
  interval 60
3
3
 
4
- settings :words, [ 'reads reqs', 'reads merged', 'reads sector', 'reads time',
5
- 'writes reqs', 'writes merged', 'writes sector', 'writes time',
6
- 'io reqs', 'io time', 'io weighted' ]
4
+ settings :words, ['reads reqs', 'reads merged', 'reads sector', 'reads time',
5
+ 'writes reqs', 'writes merged', 'writes sector', 'writes time',
6
+ 'io reqs', 'io time', 'io weighted']
7
7
 
8
- settings :filter, [ 'reads reqs', 'writes reqs' ]
8
+ settings :filter, ['reads reqs', 'writes reqs']
9
9
 
10
10
  run_if 'linux' do
11
11
  File.exists? '/proc/diskstats'
@@ -15,7 +15,7 @@ collect 'linux' do
15
15
  f = File.read('/proc/diskstats')
16
16
  f.split("\n").reject { |d| d =~ /(ram|loop)/ }.inject({}) do |_, line|
17
17
  if line =~ /^(?:\s+\d+){2}\s+([\w\d]+) (.*)$/
18
- dev = $1
18
+ dev = $1
19
19
  values = $2.split(/\s+/).map { |str| str.to_i }
20
20
  next if !!(dev.match /\d+$/ || !(dev.match =~ /^xvd/))
21
21
  settings.filter.each do |filter|
data/example/dns_check.rb CHANGED
@@ -2,6 +2,6 @@ interval 60
2
2
 
3
3
  collect 'linux', 'mac_os_x' do
4
4
  event(
5
- :state => Resolv::DNS.new.getresources(ohai[:fqdn], Resolv::DNS::Resource::IN::A).count == 1,
6
- :description => 'Check resolv self FQDN')
5
+ :state => Resolv::DNS.new.getresources(ohai[:fqdn], Resolv::DNS::Resource::IN::A).count == 1,
6
+ :description => 'Check resolv self FQDN')
7
7
  end
data/example/exim.rb CHANGED
@@ -10,6 +10,6 @@ run_if do
10
10
  end
11
11
 
12
12
  collect do
13
- event(:service => 'exim', :metric => shell(settings.exim, ' -bpc').to_i,
13
+ event(:service => 'exim', :metric => shell(settings.exim, ' -bpc').to_i,
14
14
  :description => 'Exim: count frozen mails')
15
15
  end
@@ -9,7 +9,7 @@ settings :age, 24 * 60 * 60
9
9
  collect do
10
10
  if File.directory?(settings.dir)
11
11
  count_files = 0
12
- file_mask = Regexp.new(settings.file_mask)
12
+ file_mask = Regexp.new(settings.file_mask)
13
13
  Find.find(settings.dir).each do |file|
14
14
  next unless File.file? file
15
15
  next unless file_mask.match file
data/example/http.rb CHANGED
@@ -14,9 +14,9 @@ collect 'linux', 'mac_os_x' do
14
14
 
15
15
  @cmd ||= begin
16
16
  "curl -X#{settings.http_method} -s --connect-timeout #{settings.connect_timeout}" +
17
- " #{'--insecure' if settings.insecure} " +
18
- " -w '%{http_code}\\n' --retry #{settings.retry} --retry-delay #{settings.retry_delay}" +
19
- " --max-time #{settings.max_time} --fail #{settings.url} -o /dev/null"
17
+ " #{'--insecure' if settings.insecure} " +
18
+ " -w '%{http_code}\\n' --retry #{settings.retry} --retry-delay #{settings.retry_delay}" +
19
+ " --max-time #{settings.max_time} --fail #{settings.url} -o /dev/null"
20
20
  end
21
21
 
22
22
  out = shell_out(@cmd).stdout.to_i
data/example/iptables.rb CHANGED
@@ -20,8 +20,8 @@ collect 'linux' do
20
20
  end.compact.join("\n")
21
21
 
22
22
  event(
23
- :service => "iptables #{settings.rule_file}",
24
- :state => current_rules == saved_rules,
25
- :description => "iptables rules different between file: #{settings.rule_file} and iptables-save"
23
+ :service => "iptables #{settings.rule_file}",
24
+ :state => current_rules == saved_rules,
25
+ :description => "iptables rules different between file: #{settings.rule_file} and iptables-save"
26
26
  )
27
27
  end
data/example/la.rb CHANGED
@@ -6,16 +6,16 @@ state :critical, 50
6
6
 
7
7
  collect 'mac_os_x' do
8
8
  event(
9
- :metric => shell("sysctl -n vm.loadavg | awk '{print $2}'").to_f,
10
- :description => 'LA averaged over 1 minute',
11
- :service => 'la la_1'
9
+ :metric => shell("sysctl -n vm.loadavg | awk '{print $2}'").to_f,
10
+ :description => 'LA averaged over 1 minute',
11
+ :service => 'la la_1'
12
12
  )
13
13
  end
14
14
 
15
15
  collect 'linux' do
16
16
  event(
17
- :metric => File.read('/proc/loadavg').scan(/[\d\.]+/)[0].to_f,
18
- :description => 'LA averaged over 1 minute',
19
- :service => 'la la_1'
17
+ :metric => File.read('/proc/loadavg').scan(/[\d\.]+/)[0].to_f,
18
+ :description => 'LA averaged over 1 minute',
19
+ :service => 'la la_1'
20
20
  )
21
21
  end
data/example/mdadm.rb CHANGED
@@ -34,7 +34,7 @@ collect 'linux' do
34
34
  mdstat = File.read('/proc/mdstat').split("\n")
35
35
  mdstat.each_with_index do |line, index|
36
36
  next unless line.include?('blocks')
37
- device = file[index-1].split(':')[0].strip
37
+ device = file[index-1].split(':')[0].strip
38
38
  mdstatus = rm_bracket(line.split(' ').last) # UUU
39
39
  next if status_well?(mdstatus) # пропускаем все збс
40
40
  next if mdstatus == settings.states.send(device).to_s # disabled in config
data/example/memory.rb CHANGED
@@ -6,18 +6,18 @@ warning 85
6
6
 
7
7
  collect 'linux' do
8
8
  m = File.read('/proc/meminfo').split(/\n/).inject({}) do |info, line|
9
- x = line.split(/:?\s+/)
9
+ x = line.split(/:?\s+/)
10
10
  info[x[0]] = x[1].to_i
11
11
  info
12
12
  end
13
13
 
14
- free = m['MemFree'].to_i * 1024
15
- cached = m['Cached'].to_i * 1024
16
- buffers = m['Buffers'].to_i * 1024
17
- total = m['MemTotal'].to_i * 1024
18
- used = total - free
19
- free_bc = free + buffers + cached
20
- fraction = 1 - (free_bc.to_f / total)
14
+ free = m['MemFree'].to_i * 1024
15
+ cached = m['Cached'].to_i * 1024
16
+ buffers = m['Buffers'].to_i * 1024
17
+ total = m['MemTotal'].to_i * 1024
18
+ used = total - free
19
+ free_bc = free + buffers + cached
20
+ fraction = 1 - (free_bc.to_f / total)
21
21
  swap_fraction = m['SwapTotal'] == 0 ? 0 : 1 - m['SwapFree'].to_f/m['SwapTotal']
22
22
 
23
23
  event(:service => 'memory % free', :description => 'Memory usage, %', :metric => fraction.round(2) * 100)
data/example/net.rb CHANGED
@@ -2,10 +2,10 @@ interval 60
2
2
  always_start true
3
3
 
4
4
  settings :include_alias, false
5
- settings :filter, [ 'rx bytes', 'rx errs', 'rx drop', 'tx bytes', 'tx errs', 'tx drop' ]
6
- settings :words, [ 'rx bytes', 'rx packets', 'rx errs', 'rx drop', 'rx fifo', 'rx frame',
7
- 'rx compressed', 'rx multicast', 'tx bytes', 'tx packets', 'tx drops',
8
- 'tx fifo', 'tx colls', 'tx carrier', 'tx compressed' ]
5
+ settings :filter, ['rx bytes', 'rx errs', 'rx drop', 'tx bytes', 'tx errs', 'tx drop']
6
+ settings :words, ['rx bytes', 'rx packets', 'rx errs', 'rx drop', 'rx fifo', 'rx frame',
7
+ 'rx compressed', 'rx multicast', 'tx bytes', 'tx packets', 'tx drops',
8
+ 'tx fifo', 'tx colls', 'tx carrier', 'tx compressed']
9
9
 
10
10
  collect 'linux' do
11
11
  File.read('/proc/net/dev').each_line do |line|
@@ -16,7 +16,7 @@ collect 'linux' do
16
16
  settings.words.map do |service|
17
17
  service
18
18
  end.zip(
19
- $2.split(/\s+/).map { |str| str.to_i }
19
+ $2.split(/\s+/).map { |str| str.to_i }
20
20
  ).each do |service, value|
21
21
  next unless settings.filter.include? service
22
22
  event(:service => "net #{iface} #{service}", :metric => value.to_f/interval, :diff => true)
data/example/net_stat.rb CHANGED
@@ -12,14 +12,14 @@ collect 'linux' do
12
12
  end
13
13
  end
14
14
  filter += " \\) and not dst 127.0.0.1:*"
15
- cmd = 'ss -t -4 -n state established ' + filter + ' | wc -l'
15
+ cmd = 'ss -t -4 -n state established ' + filter + ' | wc -l'
16
16
 
17
17
  count = shell!(cmd).to_i - 1
18
18
 
19
19
  event(
20
- :service => "netstat tcp #{settings.ports.join(', ')}",
21
- :metric => count,
22
- :description => "count established connects: #{count} to ports #{settings.ports.join(', ')}"
20
+ :service => "netstat tcp #{settings.ports.join(', ')}",
21
+ :metric => count,
22
+ :description => "count established connects: #{count} to ports #{settings.ports.join(', ')}"
23
23
  )
24
24
 
25
25
  end
data/example/ntp.rb CHANGED
@@ -8,8 +8,8 @@ settings :timeout, 30
8
8
 
9
9
  collect 'linux', 'mac_os_x' do
10
10
  event(
11
- :service => "ntp #{settings.host}",
12
- :description => "Ntp lag with host #{settings.host}",
13
- :metric => (::Net::NTP.get(plugin.host, 'ntp', settings.timeout).time.to_f - Time.now.to_f).abs
11
+ :service => "ntp #{settings.host}",
12
+ :description => "Ntp lag with host #{settings.host}",
13
+ :metric => (::Net::NTP.get(plugin.host, 'ntp', settings.timeout).time.to_f - Time.now.to_f).abs
14
14
  )
15
15
  end
data/example/pgsql.rb CHANGED
@@ -31,10 +31,10 @@ collect 'linux' do
31
31
  def run_master_sql
32
32
  run_sql("create database #{settings.db4monit}") unless db4monit_exists?
33
33
  run_sql(
34
- "drop table if exists timestamp; \
34
+ "drop table if exists timestamp; \
35
35
  create table timestamp ( id int primary key, value timestamp default now() ); \
36
36
  insert into timestamp (id) values (1); \
37
- ", settings.db4monit)
37
+ ", settings.db4monit)
38
38
  end
39
39
 
40
40
  def repl_lag
data/example/runit.rb CHANGED
@@ -28,7 +28,7 @@ collect 'linux' do
28
28
  Dir.glob('/etc/service/*').each do |srv|
29
29
  srv_uptime = uptime(srv)
30
30
  srv_runned = runned?(srv)
31
- srv_name = human_srv(srv)
31
+ srv_name = human_srv(srv)
32
32
 
33
33
  # сервис запущен и работает дольше чем мы приходили к нему в прошлый раз
34
34
  if srv_runned && srv_uptime > interval
@@ -10,8 +10,8 @@ critical 1
10
10
  collect 'linux' do
11
11
  content = File.read(settings.file).split("\n").delete_if { |x| x.strip.empty? }
12
12
  event(
13
- :service => "#{settings.service} #{settings.file}",
14
- :description => content.last(settings.report_lines).join("\n"),
15
- :metric => content.count
13
+ :service => "#{settings.service} #{settings.file}",
14
+ :description => content.last(settings.report_lines).join("\n"),
15
+ :metric => content.count
16
16
  )
17
17
  end
data/example/tw_cli.rb CHANGED
@@ -10,8 +10,8 @@ end
10
10
 
11
11
  collect 'linux' do
12
12
  event(
13
- :service => 'twcli',
14
- :metric => shell!(settings.cmd).to_i,
15
- :description => 'Hardware raid tw_cli status'
13
+ :service => 'twcli',
14
+ :metric => shell!(settings.cmd).to_i,
15
+ :description => 'Hardware raid tw_cli status'
16
16
  )
17
17
  end
data/lib/malevich/dsl.rb CHANGED
@@ -16,6 +16,7 @@ module Malevich
16
16
  def always_start(val)
17
17
  plugins.last.always_start = !!val
18
18
  end
19
+
19
20
  alias :auto :always_start
20
21
  alias :auto_start :always_start
21
22
 
@@ -27,21 +28,25 @@ module Malevich
27
28
  def state(attr, val)
28
29
  plugins.last.settings[attr] = val
29
30
  end
31
+
30
32
  alias :states :state
31
33
 
32
34
  def critical(val)
33
35
  plugins.last.settings[:critical] = val
34
36
  end
37
+
35
38
  alias :critical= :critical
36
39
 
37
40
  def warning(val)
38
41
  plugins.last.settings[:warning] = val
39
42
  end
43
+
40
44
  alias :warning= :warning
41
45
 
42
46
  def settings(attr, val)
43
47
  plugins.last.settings[attr] = val
44
48
  end
49
+
45
50
  alias :config :settings
46
51
  alias :defaults :settings
47
52
  alias :plugin :settings
data/lib/malevich/init.rb CHANGED
@@ -14,4 +14,3 @@ require 'malevich/plugin'
14
14
  require 'malevich/monitor'
15
15
  require 'malevich/loader'
16
16
  require 'malevich/responders/init'
17
-
@@ -2,7 +2,7 @@ module Malevich
2
2
  module Loader
3
3
 
4
4
  def load_responders
5
- Malevich::Responder.constants.select do |c|
5
+ Malevich::Responder.constants.select do |c|
6
6
  next unless Class === Malevich::Responder.const_get(c)
7
7
  monitor << Malevich::Responder.const_get(c).new
8
8
  end
@@ -11,7 +11,7 @@ module Malevich
11
11
  def load_plugins(plugin_path, config_file)
12
12
 
13
13
  def find_plugin(name)
14
- @all_plugins.find {|p| p.name == name}
14
+ @all_plugins.find { |p| p.name == name }
15
15
  end
16
16
 
17
17
  @all_plugins = Malevich::DSL.load(plugin_path)
@@ -29,7 +29,7 @@ module Malevich
29
29
  Kernel::log :error, "Unable to find parent plugin for #{name}"
30
30
  next
31
31
  end
32
- val.each_with_index do |p_settings, i|
32
+ val.each_with_index do |p_settings, i|
33
33
  child = parent.dup
34
34
  child.name = "#{name}_#{i}"
35
35
  child.settings.deep_merge!(parent.settings.dup)
@@ -39,7 +39,7 @@ module Malevich
39
39
  end
40
40
  # delete parent plugin
41
41
  @all_plugins.delete(parent)
42
- next
42
+ next
43
43
  end
44
44
 
45
45
  # dup plugin with parent
@@ -59,7 +59,7 @@ module Malevich
59
59
  next
60
60
  end
61
61
  # over plugins merge
62
- @all_plugins.each {|p| p.settings.deep_merge!(val) if name == p.name }
62
+ @all_plugins.each { |p| p.settings.deep_merge!(val) if name == p.name }
63
63
  end
64
64
  # add plugin if it always_start or get settings and runnable
65
65
  @all_plugins.each do |p|
@@ -67,12 +67,12 @@ module Malevich
67
67
  Kernel::log(:info, "Plugin '#{p.name}' not started, because it not 'always_start' and not in config")
68
68
  next
69
69
  end
70
- @plugins_to_run << p if p.runnable?
70
+ @plugins_to_run << p if p.runnable?
71
71
  end
72
72
  @all_plugins = nil
73
73
  @all_names = nil
74
74
  # start plugins!
75
- @plugins_to_run.each {|p| monitor << p }
75
+ @plugins_to_run.each { |p| monitor << p }
76
76
  @plugins_to_run = nil
77
77
  end
78
78
 
@@ -10,7 +10,7 @@ module Malevich
10
10
  end
11
11
 
12
12
  def plugins
13
- @tasks.select {|t| t[1].is_a?(Malevich::Plugin) }.map {|x| {x[1].name => x[1].settings.to_hash} }
13
+ @tasks.select { |t| t[1].is_a?(Malevich::Plugin) }.map { |x| {x[1].name => x[1].settings.to_hash} }
14
14
  end
15
15
 
16
16
  def <<(obj)
@@ -32,6 +32,7 @@ module Malevich
32
32
  self << task[1]
33
33
  end
34
34
  log :debug, "Check alive #{@tasks.count} threads"
35
+ ObjectSpace.garbage_collect
35
36
  sleep CHECK_ALIVE
36
37
  end
37
38
  end
@@ -2,10 +2,13 @@ module Malevich
2
2
  module PluginHelpers
3
3
 
4
4
  def error(e)
5
- errors.last_at = Time.now
6
- errors.msg = "#{e.class}: #{e}\n #{e.backtrace.join("\n")}"
7
- errors.reported = false
8
- log :error, "Plugin '#{name}' has a error: #{e.class}: #{e}\n #{e.backtrace.join("\n")}"
5
+ msg = "Plugin '#{name}' has errors\n\n #{e.class}: #{e}\n #{e.backtrace.join("\n")}"
6
+ # for http api
7
+ errors.last_at = Time.now
8
+ errors.msg = msg
9
+ # send message todo: state error?
10
+ event(:service => 'plugin errors', :state => 'critical', :description => msg)
11
+ log :error, msg
9
12
  end
10
13
 
11
14
  end
@@ -61,7 +61,7 @@ module Malevich
61
61
  return if hash[:state] == 'ok' && hash[:metric].nil? && histories[hash[:service]] &&
62
62
  histories[hash[:service]][:state] == 'ok'
63
63
  histories[hash[:service]] = hash
64
- malevich.events << hash
64
+ malevich.riemann_events << hash
65
65
  end
66
66
 
67
67
  end
@@ -1,6 +1,5 @@
1
1
  require 'rest_client'
2
2
  require 'uri'
3
- require 'open-uri'
4
3
 
5
4
  module Malevich
6
5
  module PluginHelpers
@@ -8,6 +7,7 @@ module Malevich
8
7
  def http_get(url)
9
8
  RestClient.get(url)
10
9
  end
10
+
11
11
  alias :rest_get :http_get
12
12
 
13
13
  def body_get(url)
@@ -4,7 +4,7 @@ module Malevich
4
4
  module PluginHelpers
5
5
 
6
6
  def shell_out(*command_args)
7
- cmd = Mixlib::ShellOut.new(*command_args)
7
+ cmd = Mixlib::ShellOut.new(*command_args)
8
8
  #cmd.live_stream = STDOUT if STDOUT.tty?
9
9
  cmd.run_command
10
10
  cmd
@@ -4,6 +4,7 @@ module Malevich
4
4
  class Plugin
5
5
 
6
6
  include Malevich::PluginHelpers
7
+ include Malevich::Mixin
7
8
 
8
9
  attr_accessor :name, :always_start, :interval,
9
10
  :run_if, :collect, :settings, :histories, :errors
@@ -11,9 +12,9 @@ module Malevich
11
12
  alias :plugin :settings
12
13
 
13
14
  def initialize(name)
14
- @name = name
15
- @interval = 60
16
- @run_if = Proc.new { true }
15
+ @name = name
16
+ @interval = 60
17
+ @run_if = Proc.new { true }
17
18
  @always_start = false
18
19
  end
19
20
 
@@ -27,6 +28,7 @@ module Malevich
27
28
  log level, msg
28
29
  false
29
30
  end
31
+
30
32
  return log_and_false("'#{name}' not started, because have not 'collect'") if collect.nil?
31
33
  return log_and_false("'#{name}' disabled in config") if settings.disable? && settings.disable
32
34
  return log_and_false("'#{name} disabled by run_if statement") unless !!self.instance_eval(&run_if)
@@ -55,7 +57,7 @@ module Malevich
55
57
 
56
58
  def self.make_container(container)
57
59
  define_method(container) do |&block|
58
- malevich.plugins[container] ||= {}
60
+ malevich.plugins[container] ||= {}
59
61
  malevich.plugins[container][self.name] ||= Hashie::Mash.new
60
62
  malevich.plugins[container][self.name]
61
63
  end
@@ -2,6 +2,8 @@ module Malevich
2
2
  module Responder
3
3
  class Http
4
4
 
5
+ include Malevich::Mixin
6
+
5
7
  attr_reader :name
6
8
 
7
9
  def initialize
@@ -30,16 +32,16 @@ module Malevich
30
32
 
31
33
  def info
32
34
  {
33
- :cmd => malevich.cmd,
34
- :config => malevich.config,
35
- :plugins => malevich.monitor.plugins,
36
- :monitor => malevich.monitor.tasks.map {|x| { x[1].name => x[0].alive? } },
37
- :errors => (malevich.plugins.errors rescue {}),
38
- :histories => (malevich.plugins.histories rescue {}),
39
- :version => Malevich::VERSION,
40
- :ruby => "#{RUBY_VERSION}-p#{RUBY_PATCHLEVEL}",
41
- :uptime => Time.now.to_i - @started_at,
42
- :queue_size => malevich.events.size
35
+ :cmd => malevich.cmd,
36
+ :config => malevich.config,
37
+ :plugins => malevich.monitor.plugins,
38
+ :monitor => malevich.monitor.tasks.map { |x| {x[1].name => x[0].alive?} },
39
+ :errors => (malevich.plugins.errors rescue {}),
40
+ :histories => (malevich.plugins.histories rescue {}),
41
+ :version => Malevich::VERSION,
42
+ :ruby => "#{RUBY_VERSION}-p#{RUBY_PATCHLEVEL}",
43
+ :uptime => Time.now.to_i - @started_at,
44
+ :queue_size => malevich.riemann_events.size
43
45
  }.to_json + "\n"
44
46
  end
45
47
 
@@ -1,4 +1,3 @@
1
1
  require_relative 'riemann'
2
2
  require_relative 'http'
3
3
  require_relative 'udp'
4
- require_relative 'error'
@@ -5,6 +5,8 @@ module Malevich
5
5
  module Responder
6
6
  class Riemann
7
7
 
8
+ include Malevich::Mixin
9
+
8
10
  INTERVAL_FLUSH = 0.5
9
11
 
10
12
  attr_reader :name
@@ -30,28 +32,28 @@ module Malevich
30
32
  riemann, port = host.split(':')
31
33
  port ||= 5555
32
34
  client = ::Riemann::Client.new({
33
- :host => Resolv.new.getaddress(riemann),
34
- :port => port,
35
- :timeout => 10
36
- })
35
+ :host => Resolv.new.getaddress(riemann),
36
+ :port => port,
37
+ :timeout => 10
38
+ })
37
39
  client = client.tcp if malevich.cmd['riemann-tcp']
38
40
  log :debug, "Add new riemann client: #{client.host}:#{client.port}"
39
41
  @riemanns << client
40
- @name = @riemanns.map {|c| "riemann client [#{c.host}:#{c.port}]" }.join(' , ')
42
+ @name = @riemanns.map { |c| "riemann client [#{c.host}:#{c.port}]" }.join(' , ')
41
43
  end
42
44
  @riemanns
43
45
  end
44
46
 
45
47
  def flush
46
- until malevich.events.empty?
47
- event = malevich.events.pop
48
+ until malevich.riemann_events.empty?
49
+ event = malevich.riemann_events.pop
48
50
  @riemanns.each do |riemann|
49
51
  Timeout::timeout(10) {
50
52
  log :debug, "Sent message #{event} for #{riemann.host}:#{riemann.port}"
51
53
  riemann << event
52
54
  }
53
55
  end
54
- end
56
+ end
55
57
  end
56
58
 
57
59
  end
@@ -4,6 +4,8 @@ module Malevich
4
4
  module Responder
5
5
  class Udp
6
6
 
7
+ include Malevich::Mixin
8
+
7
9
  RIEMANN_RESERVED_FIELDS = [
8
10
  :time,
9
11
  :state,
@@ -24,7 +26,7 @@ module Malevich
24
26
 
25
27
  def process(data, src)
26
28
  begin
27
- malevich.events << event_from_json(data)
29
+ malevich.riemann_events << event_from_json(data)
28
30
  src.reply "sended\n\n"
29
31
  rescue
30
32
  log :error, "Failed to send message: #{data.inspect}"
@@ -1,3 +1,3 @@
1
1
  module Malevich
2
- VERSION = '0.0.2'
2
+ VERSION = '0.0.3'
3
3
  end
data/lib/malevich.rb CHANGED
@@ -1,19 +1,19 @@
1
- require 'malevich/init'
1
+ require 'malevich/loader'
2
2
 
3
3
  module Malevich
4
4
  class Kernel
5
5
 
6
6
  include Malevich::Loader
7
7
 
8
- attr_reader :config, :cmd, :plugins, :ohai, :monitor, :events, :dsl
8
+ attr_reader :config, :cmd, :plugins, :ohai, :monitor, :riemann_events, :dsl
9
9
 
10
10
  def initialize
11
- @config = Hashie::Mash.new # for config from file
12
- @cmd = Hashie::Mash.new
11
+ @config = Hashie::Mash.new # for config from file
12
+ @cmd = Hashie::Mash.new
13
13
  @plugins = Hashie::Mash.new # for plugin containers (errors, settings)
14
- @logger = Logger.new(STDOUT)
14
+ @logger = Logger.new(STDOUT)
15
15
  @monitor = Malevich::Monitor.new
16
- @events = ::Queue.new
16
+ @riemann_events = ::Queue.new
17
17
  end
18
18
 
19
19
  def test_plugin(file)
@@ -31,29 +31,33 @@ module Malevich
31
31
  end
32
32
 
33
33
  def ohai
34
- if (Time.now.to_i - @ohai_timestamp.to_i > 60 * 60) && !$lock_ohai
35
- $lock_ohai = true
34
+ unless @ohai
36
35
  Kernel::log :info, 'Load ohai data'
37
- @real_ohai ||= Ohai::System.new
38
- @real_ohai.all_plugins
36
+ real_ohai = Ohai::System.new
37
+ real_ohai.all_plugins
39
38
  @ohai_timestamp = Time.now
40
- $lock_ohai = false
39
+ @ohai = real_ohai.data.dup
40
+ real_ohai = nil
41
41
  end
42
- @real_ohai.data
42
+ @ohai
43
43
  end
44
-
45
44
  end
46
- end
47
45
 
48
- module Kernel
46
+ module Mixin
49
47
 
50
- def malevich
51
- $__malevich_utils ||= Malevich::Kernel.new
52
- end
48
+ class << self; attr_accessor :instance; end
49
+
50
+ def malevich
51
+ @instance ||= Malevich::Mixin.instance ||= Malevich::Kernel.new
52
+ end
53
+
54
+ def log(level = :info, message)
55
+ speaker = self.class == Class ? "C: #{self.to_s}" : "I: #{self.class.to_s}"
56
+ malevich.log(level, "[#{speaker}] #{message}")
57
+ end
53
58
 
54
- def log(level = :info, message)
55
- speaker = self.class == Class ? "C: #{self.to_s}" : "I: #{self.class.to_s}"
56
- malevich.log(level, "[#{speaker}] #{message}")
57
59
  end
58
60
 
59
61
  end
62
+
63
+ require 'malevich/init'
data/malevich.gemspec CHANGED
@@ -4,18 +4,18 @@ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
4
  require 'malevich/version'
5
5
 
6
6
  Gem::Specification.new do |spec|
7
- spec.name = 'malevich'
8
- spec.version = Malevich::VERSION
9
- spec.authors = ['Vasiliev Dmitry']
10
- spec.email = ['vadv.mkn@gmail.com']
7
+ spec.name = 'malevich'
8
+ spec.version = Malevich::VERSION
9
+ spec.authors = ['Vasiliev Dmitry']
10
+ spec.email = ['vadv.mkn@gmail.com']
11
11
  spec.description = %q{Monitoring tool}
12
- spec.summary = %q{Monitoring tool}
13
- spec.homepage = ''
14
- spec.license = 'MIT'
12
+ spec.summary = %q{Monitoring tool}
13
+ spec.homepage = ''
14
+ spec.license = 'MIT'
15
15
 
16
- spec.files = `git ls-files`.split($/)
17
- spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
18
- spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
16
+ spec.files = `git ls-files`.split($/)
17
+ spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
18
+ spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
19
19
  spec.require_paths = ['lib']
20
20
 
21
21
  spec.add_dependency 'riemann-client', '~> 0.2.2'
@@ -29,4 +29,5 @@ Gem::Specification.new do |spec|
29
29
  spec.add_development_dependency 'bundler', '~> 1.3'
30
30
  spec.add_development_dependency 'rake'
31
31
  spec.add_development_dependency 'pry'
32
+ spec.add_development_dependency 'ruby-prof'
32
33
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: malevich
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.2
4
+ version: 0.0.3
5
5
  platform: ruby
6
6
  authors:
7
7
  - Vasiliev Dmitry
@@ -136,6 +136,20 @@ dependencies:
136
136
  - - '>='
137
137
  - !ruby/object:Gem::Version
138
138
  version: '0'
139
+ - !ruby/object:Gem::Dependency
140
+ name: ruby-prof
141
+ requirement: !ruby/object:Gem::Requirement
142
+ requirements:
143
+ - - '>='
144
+ - !ruby/object:Gem::Version
145
+ version: '0'
146
+ type: :development
147
+ prerelease: false
148
+ version_requirements: !ruby/object:Gem::Requirement
149
+ requirements:
150
+ - - '>='
151
+ - !ruby/object:Gem::Version
152
+ version: '0'
139
153
  description: Monitoring tool
140
154
  email:
141
155
  - vadv.mkn@gmail.com
@@ -182,7 +196,6 @@ files:
182
196
  - lib/malevich/plugin/init.rb
183
197
  - lib/malevich/plugin/shell_out.rb
184
198
  - lib/malevich/plugin/time.rb
185
- - lib/malevich/responders/error.rb
186
199
  - lib/malevich/responders/http.rb
187
200
  - lib/malevich/responders/init.rb
188
201
  - lib/malevich/responders/riemann.rb
@@ -1,30 +0,0 @@
1
- module Malevich
2
- module Responder
3
- class Error
4
-
5
- attr_reader :name
6
-
7
- def initialize
8
- @name = 'plugin error responder'
9
- end
10
-
11
- def run!
12
- loop do
13
- if malevich.plugins.errors
14
- errors = 0
15
- malevich.plugins.errors.each { |_, val| errors += 1 unless val.reported? }
16
- if errors == 0
17
- malevich.events << {:service => 'plugins errors', :state => 'ok'}
18
- else
19
- malevich.events << {:service => 'plugin errors', :metric => errors,
20
- :description => "Plugins has errors \n #{malevich.plugins.errors}"}
21
- malevich.plugins.errors.each { |_, val| val.reported = true unless val.reported? }
22
- end
23
- end
24
- sleep 60
25
- end
26
- end
27
-
28
- end
29
- end
30
- end