kstats-node 1.0.1 → 1.0.2
Sign up to get free protection for your applications and to get access to all the features.
- data/bin/kstats-node +18 -10
- data/lib/kstats/node/config.rb +34 -1
- data/lib/kstats/node/database.rb +2 -2
- data/lib/kstats/node/probe.rb +1 -1
- data/lib/kstats/node/version.rb +1 -1
- data/lib/kstats/node/worker.rb +1 -1
- data/test/conf.yml +7 -1
- metadata +1 -1
data/bin/kstats-node
CHANGED
@@ -2,13 +2,6 @@
|
|
2
2
|
lib = File.expand_path('../../lib', __FILE__)
|
3
3
|
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
4
|
|
5
|
-
|
6
|
-
class Time
|
7
|
-
def to_db
|
8
|
-
strftime "%Y%m%d%H%M%S"
|
9
|
-
end
|
10
|
-
end
|
11
|
-
|
12
5
|
require 'optparse'
|
13
6
|
require 'yaml'
|
14
7
|
require 'kstats/node'
|
@@ -20,18 +13,31 @@ optparse = OptionParser.new do |opts|
|
|
20
13
|
|
21
14
|
opts.on('-h', '--help', 'Show help screen'){ puts opts; exit }
|
22
15
|
|
23
|
-
opts.on('-c', '--config CONF', 'Set configuration file') do |config|
|
16
|
+
opts.on('-c', '--config CONF', 'Set configuration file (required)') do |config|
|
24
17
|
options[:config] = config
|
25
18
|
end
|
26
19
|
end
|
27
20
|
|
28
21
|
optparse.parse!
|
29
|
-
require 'sinatra'
|
30
22
|
|
31
23
|
#Parse the configuration file:
|
24
|
+
if options[:config].nil?
|
25
|
+
puts optparse
|
26
|
+
exit
|
27
|
+
end
|
28
|
+
|
29
|
+
require 'sinatra'
|
30
|
+
|
31
|
+
puts "KStats::Node"
|
32
|
+
|
33
|
+
puts "Load configuration file"
|
32
34
|
config = YAML::load(File.read(options[:config]))
|
33
|
-
Kstats::Node::
|
35
|
+
Kstats::Node::Config.init(config)
|
36
|
+
|
37
|
+
set :port, Kstats::Node::Config['server.port', 4567].to_i
|
38
|
+
set :bind, Kstats::Node::Config['server.bind', '0.0.0.0']
|
34
39
|
|
40
|
+
puts "Initialize database"
|
35
41
|
Kstats::Node::Database.init
|
36
42
|
|
37
43
|
get '/probes/list' do
|
@@ -82,3 +88,5 @@ get '/probe/:id/:target' do
|
|
82
88
|
end
|
83
89
|
|
84
90
|
Kstats::Node::Worker.launch!
|
91
|
+
|
92
|
+
puts "Service is launched"
|
data/lib/kstats/node/config.rb
CHANGED
@@ -1,5 +1,38 @@
|
|
1
1
|
module Kstats
|
2
2
|
module Node
|
3
|
-
|
3
|
+
module Config
|
4
|
+
def self.init config_file
|
5
|
+
@config ||= {}
|
6
|
+
@config.merge!(config_file)
|
7
|
+
end
|
8
|
+
|
9
|
+
def self.[] key, default = nil
|
10
|
+
path = key.to_s.split(/\./)
|
11
|
+
|
12
|
+
explore_path(path) || default
|
13
|
+
end
|
14
|
+
|
15
|
+
private
|
16
|
+
|
17
|
+
def self.explore_path path, current = nil, idx=0
|
18
|
+
if idx == 0 && current.nil?
|
19
|
+
current = @config
|
20
|
+
end
|
21
|
+
|
22
|
+
if current.nil?
|
23
|
+
nil
|
24
|
+
else
|
25
|
+
if idx == path.count-1
|
26
|
+
if current.instance_of?(Hash)
|
27
|
+
current[path[idx]]
|
28
|
+
else
|
29
|
+
nil
|
30
|
+
end
|
31
|
+
else
|
32
|
+
explore_path path, current[path[idx]], idx+1
|
33
|
+
end
|
34
|
+
end
|
35
|
+
end
|
36
|
+
end
|
4
37
|
end
|
5
38
|
end
|
data/lib/kstats/node/database.rb
CHANGED
@@ -7,7 +7,7 @@ module Kstats
|
|
7
7
|
attr_reader :db
|
8
8
|
|
9
9
|
def init
|
10
|
-
@db = SQLite3::Database.new( Kstats::Node::
|
10
|
+
@db = SQLite3::Database.new( Kstats::Node::Config['db_dir'] )
|
11
11
|
@db.execute [
|
12
12
|
"CREATE TABLE IF NOT EXISTS probe_data (id INTEGER PRIMARY KEY ASC, date DATETIME, period_type STRING, probe_id STRING, probe_key STRING, probe_value NUMBER)",
|
13
13
|
"CREATE INDEX IF NOT EXISTS probe_data_period_type ON probe_data(period_type)",
|
@@ -26,7 +26,7 @@ module Kstats
|
|
26
26
|
|
27
27
|
probes.each do |name, values|
|
28
28
|
@db.close
|
29
|
-
@db = SQLite3::Database.new( Kstats::Node::
|
29
|
+
@db = SQLite3::Database.new( Kstats::Node::Config['db_dir'] )
|
30
30
|
|
31
31
|
sql = <<SQL
|
32
32
|
INSERT INTO probe_data (date, period_type, probe_id, probe_key, probe_value)
|
data/lib/kstats/node/probe.rb
CHANGED
data/lib/kstats/node/version.rb
CHANGED
data/lib/kstats/node/worker.rb
CHANGED
data/test/conf.yml
CHANGED