eznemo 0.2.1 → 0.3.0

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: 3e5d349829718d26ca4db2923e6729c16372e2e6
4
- data.tar.gz: 0754641f251dd3220f90e650f62a7c28ec47aa9d
3
+ metadata.gz: 167ab32ba8c4cf65e9714b41289729a61942df54
4
+ data.tar.gz: b4f8ab53fff1342c65d339fc96c22474ede30f26
5
5
  SHA512:
6
- metadata.gz: eaa3255851876e776e773feab656201edfeb026e824ea32408702affbcd8ba46d0d792cface8b6d725366f6f29ea8bafffcb5e6e1385ebd8851c597dccdcd4c1
7
- data.tar.gz: 518f2523ee34c20e60ba53e96878502d53e9a2328f10db2c2a932f5879e966ac28e70f0f55890da2bbdaf950ed75c91d6f8153f34de217564a48b547aef749e0
6
+ metadata.gz: de094143e701ab83c769aa811e811db3b099748abb3d523bed7979818e1a45b1a3488e8a890501a79969cd8a77f7c4b295d18458f2de6a4e6af371d9ab4e89d2
7
+ data.tar.gz: 2ac35029e455ddfb589320ee12ea595130c8e35bfb2504cb08c7290f4861083cc02dcdba58f07306fd16b487cb00995a26659cd2d4bdd071811aa4664e940660
data/README.md CHANGED
@@ -39,20 +39,23 @@ Config file.
39
39
  ---
40
40
  :probe:
41
41
  :name: Probe01
42
+ :logger: 'Logger.new(STDOUT)'
43
+ :log_level: 'Logger::WARN'
42
44
  :datastore:
43
45
  :type: :mysql # currently the only option
44
46
  :queue_size: 20
47
+ :queue_interval: 60 # if present, queue_size will be ignored
45
48
  :options:
46
49
  :host: 127.0.0.1
47
50
  :username: user
48
51
  :password: paSsw0rd
49
52
  :database: eznemo
50
53
  :checks:
51
- :tags:
54
+ :tags: # multiple tags are AND
52
55
  - tag1
53
56
  - tag2
54
57
  :monitor:
55
- :ping: # all optional
58
+ :ping: # below are all optional
56
59
  :path: '/bin/ping'
57
60
  :min_interval: 10
58
61
  :timeout: 5
@@ -75,25 +78,25 @@ Config file.
75
78
  }
76
79
  ```
77
80
 
78
- ### Results
81
+ ### Tags
79
82
 
80
83
  ```ruby
81
84
  {
82
85
  check_id: 123, # from checks
83
- probe: 'Probe01',
84
- timestamp: '2016-04-01 10:00:00 -0700',
85
- status: true, # true means OK
86
- response_ms: 0.012, # in milliseconds
87
- status_desc: 'OK' # short description of the result
86
+ text: 'prod' # tag text
88
87
  }
89
88
  ```
90
89
 
91
- ### Tags
90
+ ### Results
92
91
 
93
92
  ```ruby
94
93
  {
94
+ timestamp: '2016-04-01 10:00:00 UTC',
95
95
  check_id: 123, # from checks
96
- text: 'prod' # tag text
96
+ probe: 'Probe01',
97
+ status: true, # true means OK
98
+ response_ms: 0.012, # in milliseconds
99
+ status_desc: 'OK' # short description of the result
97
100
  }
98
101
  ```
99
102
 
@@ -110,26 +113,11 @@ CREATE TABLE `checks` (
110
113
  `interval` int(11) NOT NULL COMMENT 'in seconds',
111
114
  `type` varchar(255) NOT NULL DEFAULT '',
112
115
  `state` tinyint(1) NOT NULL,
113
- `options` varchar(255) DEFAULT '',
116
+ `options` varchar(255) DEFAULT NULL,
114
117
  PRIMARY KEY (`id`),
115
118
  CLUSTERING KEY `state` (`state`)
116
119
  ) ENGINE=TokuDB DEFAULT CHARSET=utf8;
117
120
 
118
- CREATE TABLE `results` (
119
- `id` int(11) NOT NULL AUTO_INCREMENT,
120
- `check_id` int(11) NOT NULL,
121
- `probe` varchar(255) NOT NULL DEFAULT '',
122
- `timestamp` datetime NOT NULL COMMENT 'in utc',
123
- `status` tinyint(1) NOT NULL,
124
- `response_ms` float NOT NULL DEFAULT '0',
125
- `status_desc` varchar(255) NOT NULL DEFAULT '',
126
- PRIMARY KEY (`id`),
127
- CLUSTERING KEY `check_id` (`check_id`),
128
- KEY `probe` (`probe`),
129
- KEY `timestamp` (`timestamp`),
130
- KEY `status` (`status`)
131
- ) ENGINE=TokuDB DEFAULT CHARSET=utf8;
132
-
133
121
  CREATE TABLE `tags` (
134
122
  `id` int(11) unsigned NOT NULL AUTO_INCREMENT,
135
123
  `check_id` int(11) NOT NULL,
@@ -138,4 +126,15 @@ CREATE TABLE `tags` (
138
126
  KEY `check_id` (`check_id`),
139
127
  CLUSTERING KEY `text` (`text`)
140
128
  ) ENGINE=TokuDB DEFAULT CHARSET=utf8;
129
+
130
+ CREATE TABLE `results` (
131
+ `timestamp` datetime NOT NULL COMMENT 'in utc',
132
+ `check_id` int(11) NOT NULL,
133
+ `probe` varchar(255) NOT NULL DEFAULT '',
134
+ `status` tinyint(1) NOT NULL,
135
+ `response_ms` float NOT NULL DEFAULT '0',
136
+ `status_desc` varchar(255) NOT NULL DEFAULT '',
137
+ PRIMARY KEY (`timestamp`, `check_id`, `probe`),
138
+ CLUSTERING KEY `check_id` (`check_id`)
139
+ ) ENGINE=TokuDB DEFAULT CHARSET=utf8;
141
140
  ```
@@ -7,8 +7,8 @@ Gem::Specification.new do |s|
7
7
  s.version = EzNemo::Version
8
8
  s.authors = ['Ken J.']
9
9
  s.email = ['kenjij@gmail.com']
10
- s.description = %q{Simple network monitoring}
11
- s.summary = %q{Simple network monitoring implemented with Ruby.}
10
+ s.summary = %q{Simple network monitoring}
11
+ s.description = %q{Simple network monitoring implemented with Ruby.}
12
12
  s.homepage = 'https://github.com/kenjij/eznemo'
13
13
  s.license = 'MIT'
14
14
 
@@ -1,3 +1,4 @@
1
+ require 'eznemo/logger'
1
2
  require 'eznemo/config'
2
3
  require 'eznemo/version'
3
4
  require 'eventmachine'
@@ -19,16 +20,21 @@ module EzNemo
19
20
  # Usually called by #self.run!
20
21
  def initialize(opts)
21
22
  c = EzNemo.load_config(opts[:config])
22
-
23
+ p = c[:probe]
24
+ EzNemo.logger = eval(p[:logger]) if p[:logger].class == String
25
+ logger = EzNemo.logger
26
+ logger.level = eval(p[:log_level]) if p[:log_level].class == String
27
+ logger.debug 'Loading datastore adapter...'
23
28
  require "eznemo/#{c[:datastore][:type]}"
24
29
  require 'eznemo/datastore'
25
-
30
+ logger.debug 'Loading monitoring plugins...'
26
31
  require 'eznemo/monitor'
27
32
  require 'eznemo/monitor/ping'
28
33
  end
29
34
 
30
35
  # Usually called by #self.run!
31
36
  def run
37
+ logger = EzNemo.logger
32
38
  ds = EzNemo.datastore
33
39
 
34
40
  Signal.trap('INT') do
@@ -44,7 +50,9 @@ module EzNemo
44
50
  end
45
51
 
46
52
  EM.run do
53
+ logger.debug 'Loading checks...'
47
54
  EzNemo.monitor.start_checks(ds.checks)
55
+ ds.start_loop
48
56
  end
49
57
  end
50
58
 
@@ -0,0 +1,24 @@
1
+ require 'logger'
2
+
3
+
4
+ module EzNemo
5
+
6
+ def self.logger=(logger)
7
+ @logger = logger
8
+ end
9
+
10
+ def self.logger
11
+ @logger ||= NullLogger.new()
12
+ end
13
+
14
+ class NullLogger < Logger
15
+
16
+ def initialize(*args)
17
+ end
18
+
19
+ def add(*args, &block)
20
+ end
21
+
22
+ end
23
+
24
+ end
@@ -29,7 +29,7 @@ module EzNemo
29
29
  p.add_check(c)
30
30
  i += 1
31
31
  end
32
- puts "#{i} checks activated."
32
+ EzNemo.logger.info "#{i} checks activated."
33
33
  end
34
34
 
35
35
  # Report result; usually called by the plugin
@@ -34,6 +34,7 @@ module EzNemo
34
34
  @config[:path] ||= 'ping'
35
35
  @config[:min_interval] ||= DEFAULT_MIN_INTERVAL
36
36
  @config[:timeout] ||= DEFAULT_TIMEOUT
37
+ EzNemo.logger.info 'Ping plugin registered.'
37
38
  end
38
39
 
39
40
  # Add a check using this plugin
@@ -20,6 +20,7 @@ module EzNemo
20
20
  @results = []
21
21
  @queue_size = EzNemo.config[:datastore][:queue_size]
22
22
  @queue_size ||= DEFAULT_QUEUE_SIZE
23
+ @queue_interval = EzNemo.config[:datastore][:queue_interval]
23
24
  @opts = EzNemo.config[:datastore][:options]
24
25
  @opts[:flags] = Mysql2::Client::MULTI_STATEMENTS
25
26
  end
@@ -56,11 +57,24 @@ module EzNemo
56
57
  final_ids
57
58
  end
58
59
 
60
+ # Register EventMachine blocks
61
+ def start_loop
62
+ return unless @queue_interval
63
+ logger = EzNemo.logger
64
+ logger.info 'Registering MySQL EM block...'
65
+ EM.add_periodic_timer(@queue_interval) do
66
+ EzNemo.logger.debug 'Queue interval time arrived.'
67
+ write_results
68
+ end
69
+ end
70
+
59
71
  # Stores a result; into queue first
60
72
  # @param result [Hash] (see {EzNemo::Monitor#report})
61
73
  def store_result(result)
62
74
  @results << result
75
+ return if @queue_interval
63
76
  if @results.count >= @queue_size
77
+ EzNemo.logger.debug 'Queue is full.'
64
78
  write_results
65
79
  end
66
80
  end
@@ -69,16 +83,13 @@ module EzNemo
69
83
  # @param sync [Boolean] use EM (async) if false
70
84
  # @return [Object] Mysql2 client instance
71
85
  def write_results(sync = false)
86
+ logger = EzNemo.logger
72
87
  return nil if @results.empty?
73
88
  if sync
74
- # Sequel won't run after trap; run in another thread
75
- thr = Thread.new do
76
- puts 'Flushing in another thread...'
77
- Result.db.transaction do
78
- @results.each { |r| r.save}
79
- end
89
+ logger.debug 'Writing to DB...'
90
+ Result.db.transaction do
91
+ @results.each { |r| r.save}
80
92
  end
81
- thr.join
82
93
  return true
83
94
  else
84
95
  db = emdatabase
@@ -86,9 +97,10 @@ module EzNemo
86
97
  @results.each { |r| stmt << Result.dataset.insert_sql(r) + ';' }
87
98
  defer = db.query(stmt)
88
99
  defer.callback do
100
+ logger.debug 'Wrote to DB async.'
89
101
  end
90
102
  defer.errback do |r|
91
- puts r.message
103
+ logger.error r.message
92
104
  db.close if db.ping
93
105
  end
94
106
  end
@@ -98,11 +110,17 @@ module EzNemo
98
110
 
99
111
  # Flush queue to storage
100
112
  def flush
101
- if write_results(true)
102
- puts "Flushed."
103
- else
104
- puts "Nothing to flush."
113
+ logger = EzNemo.logger
114
+ # Won't run after trap; run in another thread
115
+ thr = Thread.new do
116
+ logger.debug 'Spawned flushing thread.'
117
+ if write_results(true)
118
+ logger.info "Flushed."
119
+ else
120
+ logger.info "Nothing to flush."
121
+ end
105
122
  end
123
+ thr.join
106
124
  end
107
125
 
108
126
  end
@@ -1,5 +1,5 @@
1
1
  module EzNemo
2
2
 
3
- Version = '0.2.1'
3
+ Version = '0.3.0'
4
4
 
5
5
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: eznemo
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.1
4
+ version: 0.3.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Ken J.
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2016-05-14 00:00:00.000000000 Z
11
+ date: 2016-05-18 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: kajiki
@@ -52,7 +52,7 @@ dependencies:
52
52
  - - "~>"
53
53
  - !ruby/object:Gem::Version
54
54
  version: '4.0'
55
- description: Simple network monitoring
55
+ description: Simple network monitoring implemented with Ruby.
56
56
  email:
57
57
  - kenjij@gmail.com
58
58
  executables:
@@ -67,6 +67,7 @@ files:
67
67
  - lib/eznemo.rb
68
68
  - lib/eznemo/config.rb
69
69
  - lib/eznemo/datastore.rb
70
+ - lib/eznemo/logger.rb
70
71
  - lib/eznemo/monitor.rb
71
72
  - lib/eznemo/monitor/ping.rb
72
73
  - lib/eznemo/mysql.rb
@@ -94,5 +95,5 @@ rubyforge_project:
94
95
  rubygems_version: 2.4.3
95
96
  signing_key:
96
97
  specification_version: 4
97
- summary: Simple network monitoring implemented with Ruby.
98
+ summary: Simple network monitoring
98
99
  test_files: []