wakame-dolphin 0.0.2

Sign up to get free protection for your applications and to get access to all the features.
Files changed (59) hide show
  1. checksums.yaml +7 -0
  2. data/.gitignore +9 -0
  3. data/.rspec +4 -0
  4. data/.travis.yml +25 -0
  5. data/Gemfile +18 -0
  6. data/Makefile +56 -0
  7. data/README.md +77 -0
  8. data/Rakefile +67 -0
  9. data/bin/dolphin_server +98 -0
  10. data/config/db/cassandra_clear.txt +1 -0
  11. data/config/db/cassandra_schema.txt +14 -0
  12. data/config/db/sequel/migrations/0001_add_notification.rb +15 -0
  13. data/config/db/sequel/migrations/0002_add_event.rb +15 -0
  14. data/config/dolphin-mysql.conf.travis +26 -0
  15. data/config/dolphin.conf.example +26 -0
  16. data/lib/dolphin.rb +148 -0
  17. data/lib/dolphin/data_store.rb +55 -0
  18. data/lib/dolphin/data_stores/base_rdb.rb +57 -0
  19. data/lib/dolphin/data_stores/cassandra.rb +98 -0
  20. data/lib/dolphin/data_stores/mysql.rb +31 -0
  21. data/lib/dolphin/helpers/message/zabbix_helper.rb +16 -0
  22. data/lib/dolphin/helpers/request_helper.rb +72 -0
  23. data/lib/dolphin/mailer.rb +83 -0
  24. data/lib/dolphin/manager.rb +35 -0
  25. data/lib/dolphin/message_builder.rb +98 -0
  26. data/lib/dolphin/models/base.rb +8 -0
  27. data/lib/dolphin/models/cassandra/base.rb +11 -0
  28. data/lib/dolphin/models/cassandra/event.rb +42 -0
  29. data/lib/dolphin/models/cassandra/notification.rb +28 -0
  30. data/lib/dolphin/models/rdb/base.rb +12 -0
  31. data/lib/dolphin/models/rdb/event.rb +47 -0
  32. data/lib/dolphin/models/rdb/notification.rb +27 -0
  33. data/lib/dolphin/models/rdb/orm/base.rb +10 -0
  34. data/lib/dolphin/models/rdb/orm/event.rb +8 -0
  35. data/lib/dolphin/models/rdb/orm/notification.rb +8 -0
  36. data/lib/dolphin/query_processor.rb +50 -0
  37. data/lib/dolphin/request_handler.rb +150 -0
  38. data/lib/dolphin/sender.rb +47 -0
  39. data/lib/dolphin/util.rb +18 -0
  40. data/lib/dolphin/version.rb +3 -0
  41. data/lib/dolphin/worker.rb +149 -0
  42. data/script/console +13 -0
  43. data/spec/files/cassandra_models_spec.rb +127 -0
  44. data/spec/files/dolphin_spec.rb +110 -0
  45. data/spec/files/endpoint/event_spec.rb +123 -0
  46. data/spec/files/endpoint/notification_spec.rb +54 -0
  47. data/spec/files/message_builder_spec.rb +15 -0
  48. data/spec/helpers/test_helper.rb +21 -0
  49. data/spec/helpers/web_request_helper.rb +40 -0
  50. data/spec/spec_helper.rb +18 -0
  51. data/templates/email/alert_port.erb +24 -0
  52. data/templates/email/default.erb +3 -0
  53. data/tests/test_dolphin +10 -0
  54. data/tests/test_get_event +31 -0
  55. data/tests/test_get_notification +27 -0
  56. data/tests/test_post_event +37 -0
  57. data/tests/test_post_notification +43 -0
  58. data/wakame-dolphin.gemspec +40 -0
  59. metadata +311 -0
@@ -0,0 +1,110 @@
1
+ # -*- coding: utf-8 -*-
2
+
3
+ require 'rubygems'
4
+ require 'spec_helper'
5
+ require 'net/http'
6
+
7
+ require File.join(File.expand_path('../../../', __FILE__), 'lib/dolphin')
8
+
9
+ describe 'Notification Service' do
10
+
11
+ describe Dolphin::RequestHandler do
12
+ context "リクエストパラメータを入力したとき" do
13
+ it "リクエストに成功すること" do
14
+ end
15
+
16
+ describe "リクエストに失敗すること" do
17
+ end
18
+ end
19
+ end
20
+
21
+ describe Dolphin::Manager do
22
+ it "開始できること" do
23
+ end
24
+
25
+ it "停止できること" do
26
+ end
27
+
28
+ it "Workerを起動できること" do
29
+ end
30
+
31
+ it "Workerを停止できること" do
32
+ end
33
+
34
+ it "RequestHandlerを起動できること" do
35
+ end
36
+
37
+ it "RequestHandlerを停止できること" do
38
+ end
39
+
40
+ end
41
+
42
+ describe Dolphin::Worker do
43
+
44
+ context "EventObjectを入力したとき" do
45
+ it "Senderにメッセージが送れること" do
46
+ end
47
+ end
48
+ end
49
+
50
+ describe Dolphin::QueryProcessor do
51
+
52
+ it "宛先情報を取得できること" do
53
+ end
54
+
55
+ it "イベントを記録できること" do
56
+ end
57
+ end
58
+
59
+ describe Dolphin::Sender do
60
+
61
+ context "ファイル出力を選択したとき" do
62
+ it "ファイルに保存できること" do
63
+ end
64
+ end
65
+
66
+ context "メールサーバーが設定されているとき", :smtp => true do
67
+ it "送信できること" do
68
+ end
69
+ end
70
+
71
+ end
72
+
73
+ describe Dolphin::DataBase do
74
+
75
+ context "設定ファイルを入力したとき" do
76
+ it "Cassandraデータベースに接続できること" do
77
+ end
78
+ end
79
+
80
+ context "Cassandraデータベースに接続できたとき" do
81
+
82
+ it "Eventを保存できること" do
83
+ end
84
+
85
+ it "Eventを取得できること" do
86
+ end
87
+
88
+ it "Notificationを保存できること" do
89
+ end
90
+
91
+ it "Notificationを取得できること" do
92
+ end
93
+
94
+ end
95
+
96
+ end
97
+
98
+ describe Dolphin::Util do
99
+ context "ログタイプにstdoutを選択したとき" do
100
+ it "ログが書き込めること" do
101
+ end
102
+ end
103
+
104
+ context "ログタイプにltsvを選択したとき" do
105
+ it "ログが書き込めること" do
106
+ end
107
+ end
108
+ end
109
+
110
+ end
@@ -0,0 +1,123 @@
1
+ # -*- coding: utf-8 -*-
2
+
3
+ require 'spec_helper'
4
+
5
+ describe 'Event API' do
6
+
7
+ before(:all) do
8
+
9
+ # Mail Setting
10
+ output_mail_file_location = '/var/tmp/'
11
+
12
+ @mail_to = "test_to@example.com"
13
+ @temporary_mail = "#{output_mail_file_location}#{@mail_to}"
14
+
15
+ @connection = Dolphin::DataStore.current_store
16
+ @connection.connect
17
+
18
+ if @connection.closed?
19
+ pending "Failed cannect to database"
20
+ else
21
+ @notification_id = 'system'
22
+ @notification_values = {
23
+ "email"=> {
24
+ "to" => @mail_to,
25
+ }
26
+ }
27
+ @notification_id = 'test'
28
+ @connection.put_notification(@notification_id, @notification_values)
29
+
30
+ @message_type = "default"
31
+ @event_values = {
32
+ 'notification_id' => @notification_id,
33
+ 'message_type' => @message_type,
34
+ 'messages' => {
35
+ "message"=>"Alert!!!!"
36
+ }
37
+ }
38
+
39
+ @connection.put_event({:messages => @event_values})
40
+ end
41
+ end
42
+
43
+ it 'expect to get events' do
44
+ res = get('/events', :headers => {
45
+ 'Content-Type' =>'application/json',
46
+ })
47
+ expect(res['message']).to eql 'OK'
48
+ end
49
+
50
+ it 'expect to get last event' do
51
+ events = get('/events', :headers => {
52
+ 'Content-Type' =>'application/json',
53
+ })
54
+ last_event = events['results'][-1]
55
+ last_event_id = last_event['id']
56
+
57
+ query_string = '?' + [
58
+ "count=1",
59
+ "start_id=#{SimpleUUID::UUID.new(last_event_id).to_guid}"
60
+ ].join('&')
61
+
62
+ res = get('/events' + query_string, :headers => {
63
+ 'Content-Type' =>'application/json',
64
+ })['results'][0]
65
+ expect(res['id']).to eql last_event_id
66
+ end
67
+
68
+ it 'expect to post event and send email' do
69
+ message_subject = 'subject'
70
+ message_body = 'body'
71
+ res = post('/events',
72
+ :headers => {
73
+ 'Content-Type' =>'application/json',
74
+ 'X-Notification-Id' => @notification_id,
75
+ 'X-Message-Type' => @message_type
76
+ },
77
+ :body => {
78
+ 'subject' => message_subject,
79
+ 'body' => message_body,
80
+ 'message' => 'Alert!!!!'
81
+ }.to_json)
82
+
83
+ expect(res['message']).to eql 'OK'
84
+
85
+ wait_for_sending_mail_to_be_ready {
86
+ break if File.exists?(@temporary_mail)
87
+ }
88
+
89
+ mb = Dolphin::MessageBuilder::Mail.new
90
+ template = mb.build(@message_type, {
91
+ 'messages' => {
92
+ 'subject' => message_subject,
93
+ 'body' => message_body,
94
+ },
95
+ 'mail' => {
96
+ 'from' => Dolphin.settings['mail']['from']
97
+ },
98
+ 'to' => @mail_to
99
+ })
100
+
101
+ mail = Dolphin::Mailer.read_iso2022_jp_mail(@temporary_mail)
102
+ expect(mail[:from]).to eql Dolphin.settings['mail']['from']
103
+ expect(mail[:to]).to eql @mail_to
104
+ expect(mail[:subject]).to eql template.subject
105
+ expect(mail[:body]).to eql template.body
106
+ end
107
+
108
+ it 'expect to post event only' do
109
+ res = post('/events',
110
+ :headers => {
111
+ 'Content-Type' =>'application/json',
112
+ },
113
+ :body => {
114
+ 'message' => 'Alert!!!!'
115
+ }.to_json)
116
+
117
+ expect(res['message']).to eql 'OK'
118
+ end
119
+
120
+ before(:all) do
121
+ FileUtils.rm(@temporary_mail) if File.exists?(@temporary_mail)
122
+ end
123
+ end
@@ -0,0 +1,54 @@
1
+ # -*- coding: utf-8 -*-
2
+
3
+ require 'spec_helper'
4
+
5
+ describe 'Notification API' do
6
+ before(:all) do
7
+
8
+ @connection = Dolphin::DataStore.current_store
9
+ @connection.connect
10
+ if @connection.closed?
11
+ pending "Failed cannect to database"
12
+ else
13
+ @notification_values = {
14
+ "email"=> {
15
+ "to" => "foo@example.com,bar@example.com",
16
+ "cc" => "foo@example.com,bar@example.com",
17
+ "bcc" =>"foofoo@example.com,barbar@example.com"
18
+ }
19
+ }
20
+ @notification_id = 'system'
21
+ @connection.put_notification(@notification_id, @notification_values)
22
+ end
23
+ end
24
+
25
+ it 'expect to get notifications' do
26
+ res = get('/notifications', :headers => {
27
+ 'Content-Type' =>'application/json',
28
+ 'X-Notification-Id' => @notification_id,
29
+ })
30
+
31
+ expect(res['results']).to eql @notification_values
32
+ expect(res['message']).to eql 'OK'
33
+ end
34
+
35
+ it 'expect to post notification' do
36
+ res = post('/notifications',
37
+ :headers => {
38
+ 'Content-Type' =>'application/json',
39
+ 'X-Notification-Id' => @notification_id
40
+ },
41
+ :body => @notification_values.to_json
42
+ )
43
+ expect(res['message']).to eql 'OK'
44
+ end
45
+
46
+ it 'expect to delete notification' do
47
+ res = delete('/notifications', :headers => {
48
+ 'Content-Type' =>'application/json',
49
+ 'X-Notification-Id' => @notification_id,
50
+ })
51
+ expect(res['message']).to eql 'OK'
52
+ end
53
+
54
+ end
@@ -0,0 +1,15 @@
1
+ # -*- coding: utf-8 -*-
2
+
3
+ require 'spec_helper'
4
+
5
+ describe Dolphin::TemplateBuilder do
6
+ describe ".build" do
7
+ it 'expect to build template' do
8
+ tb = Dolphin::TemplateBuilder.new
9
+
10
+ template_str = 'hello <%= @foo %>'
11
+ bind_params = {:foo => 'world'}
12
+ expect(tb.build(template_str, bind_params)).to eql 'hello world'
13
+ end
14
+ end
15
+ end
@@ -0,0 +1,21 @@
1
+ module TestHelper
2
+ def wait_for_sending_mail_to_be_ready(timeout = 5, sleep_time = 1, &block)
3
+ if block_given? &block
4
+ waiting_count = 0
5
+ loop do
6
+ block.call
7
+
8
+ if waiting_count > timeout
9
+ puts "wait timeout #{timeout}"
10
+ break
11
+ end
12
+
13
+ puts "sleep #{sleep_time} wait..#{waiting_count}"
14
+ sleep sleep_time
15
+ waiting_count +=1
16
+ end
17
+ else
18
+ sleep sleep_time
19
+ end
20
+ end
21
+ end
@@ -0,0 +1,40 @@
1
+ require "net/http"
2
+ require "uri"
3
+
4
+ module WebRequestHelper
5
+ def get(path, params)
6
+ uri = build_uri(path)
7
+ net = Net::HTTP::Get.new(uri.request_uri, params[:headers])
8
+ request(net, uri)
9
+ end
10
+
11
+ def post(path, params)
12
+ uri = build_uri(path)
13
+ net = Net::HTTP::Post.new(uri.request_uri, params[:headers])
14
+ net.body = params[:body]
15
+ request(net, uri)
16
+ end
17
+
18
+ def delete(path, params)
19
+ uri = build_uri(path)
20
+ net = Net::HTTP::Delete.new(uri.request_uri, params[:headers])
21
+ request(net, uri)
22
+ end
23
+
24
+ private
25
+
26
+ def build_uri(path)
27
+ server = Dolphin.settings['server']
28
+ endpoint = "http://#{server['host']}:#{server['port']}"
29
+ URI.parse(endpoint + path)
30
+ end
31
+
32
+ def request(net, uri)
33
+ http = Net::HTTP.new(uri.host, uri.port)
34
+ # http.set_debug_output $stderr
35
+ http.start do |h|
36
+ response = h.request(net)
37
+ JSON.parse(response.body)
38
+ end
39
+ end
40
+ end
@@ -0,0 +1,18 @@
1
+ # -*- coding: utf-8 -*-
2
+
3
+ require 'rubygems'
4
+ require 'rspec'
5
+ require File.join(File.expand_path('../../', __FILE__), 'lib/dolphin')
6
+
7
+ # Setup Bundler
8
+ ENV['BUNDLE_GEMFILE'] ||= File.expand_path(File.dirname(__FILE__) + '/Gemfile')
9
+ require 'bundler/setup' if File.exists?(ENV['BUNDLE_GEMFILE'])
10
+ Bundler.require :default if defined?(Bundler)
11
+
12
+ # Load helper files
13
+ Dir.glob('./spec/helpers/*_helper.rb').each {|f| require File.expand_path(f) }
14
+
15
+ RSpec.configure do |c|
16
+ c.include WebRequestHelper
17
+ c.include TestHelper
18
+ end
@@ -0,0 +1,24 @@
1
+ 監視アラート
2
+ ----------------------------------------
3
+
4
+ 監視アラート
5
+
6
+ <%= @message %>
7
+
8
+ 【コンポーネントID】
9
+ <%= @instance_name %>(<%= @instance_id %>)
10
+
11
+ 【IPアドレス】
12
+ <%= @instance_ip %>
13
+
14
+ 【ステータス】
15
+ 異常
16
+
17
+ 【検知日時】
18
+ <%= @event_datetime %>
19
+
20
+ 【補足】
21
+ 監視概要:監視間隔5分、応答待ち時間30秒、リトライ回数2回
22
+
23
+ ※このメールは送信専用アドレスから送信されています。
24
+  本メールにご返信いただいてもお答えできませんのでご了承下ください。
@@ -0,0 +1,3 @@
1
+ <%= @subject %>
2
+ ----------------------------------------
3
+ <%= @body %>
@@ -0,0 +1,10 @@
1
+ #!/bin/bash
2
+ #
3
+ # Example:
4
+ # DOLPHIN_HOST=127.0.0.1 DOLPHIN_PORT=9004 ./test_dolphin.sh
5
+ #
6
+
7
+ ./test_post_notification
8
+ ./test_post_event
9
+ ./test_get_event
10
+ ./test_delete_notification
@@ -0,0 +1,31 @@
1
+ #!/usr/bin/env ruby
2
+ # -*- coding: utf-8 -*-
3
+
4
+ require 'net/http'
5
+ require 'uri'
6
+ require 'json'
7
+ require 'time'
8
+
9
+ start_time = URI.encode((Time.now - 60).iso8601)
10
+
11
+ host = ENV['DOLPHIN_HOST'] || '127.0.0.1'
12
+ port = ENV['DOLPHIN_PORT'] || 9004
13
+
14
+ path = "http://#{host}:#{port}/events"
15
+ # path = "http://#{host}:#{port}/events?limit=10&start_time=#{start_time}"
16
+ # path = 'http://#{host}:#{port}/events?start_id=12174a14-87c1-11e2-927c-31035db8d436'
17
+
18
+ uri = URI.parse(path)
19
+
20
+ headers = {
21
+ 'Content-Type' =>'application/json',
22
+ }
23
+
24
+ request = Net::HTTP::Get.new(uri.request_uri, headers)
25
+ http = Net::HTTP.new(uri.host, uri.port)
26
+ http.set_debug_output $stderr
27
+ http.start do |h|
28
+ response = h.request(request)
29
+ res = JSON.parse(response.body)
30
+ p res
31
+ end