kanamei-keystone 0.0.19 → 0.0.20

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.
@@ -0,0 +1,52 @@
1
+ #!/usr/bin/ruby
2
+
3
+ require 'rubygems'
4
+ require 'keystone'
5
+ require 'pit'
6
+ require 'pp'
7
+
8
+ include Keystone::Batch::Base
9
+
10
+ config = Pit.get("disk_size_check")
11
+
12
+ SMTP_SERVER = config["smtp"]
13
+ MAIL_TO = [config["mailto"]]
14
+ ERROR_MAIL_TO = MAIL_TO
15
+ MAIL_FROM = config["mailfrom"]
16
+
17
+ @os = Keystone::Os.get()
18
+ pp @os
19
+
20
+ def send_alert_mail(additional_body = "")
21
+ title = 'disk size alert'
22
+ body = <<-BODY
23
+ disk size alert at #{@os.hostname}(#{@os.ip_address.join(" , ")})
24
+ #{@df_result}
25
+ #{additional_body}
26
+ BODY
27
+
28
+ Keystone::Mail::Send.send(
29
+ MAIL_FROM,
30
+ MAIL_TO,
31
+ title,
32
+ body,
33
+ {:retry_cnt=>3}
34
+ )
35
+ end
36
+
37
+ execute() do
38
+ info "start batch"
39
+ limit = 80
40
+ limit = Integer(ARGV[0]) if ARGV.size > 0
41
+
42
+ @df_result = @os.disk
43
+ @df_result.gsub(/(\d+)%/){|s|
44
+ if $1.to_i >= limit
45
+ warn 'log size alert'
46
+ send_alert_mail
47
+ break
48
+ end
49
+ }
50
+ info "finish batch"
51
+ end
52
+
data/lib/keystone.rb CHANGED
@@ -11,7 +11,7 @@ autoload :Moji , 'moji'
11
11
 
12
12
  module Keystone
13
13
 
14
- VERSION = '0.0.19'
14
+ VERSION = '0.0.20'
15
15
 
16
16
  autoload :StringUtil , 'keystone/string_util'
17
17
  autoload :Batch , 'keystone/batch'
@@ -0,0 +1,84 @@
1
+ require 'kconv'
2
+
3
+ module Keystone
4
+ module Mail
5
+ class Message
6
+ attr_accessor :subject, :body, :mail_to, :mail_from, :mail_from_text
7
+
8
+ #
9
+ # 普通なことをしたいならTMailを使いましょう
10
+ #
11
+ def initialize(opt={})
12
+ @subject, @body, @mail_from_text = "", "", nil
13
+ set_option(opt)
14
+ end
15
+
16
+ def to_src
17
+ header = ""
18
+ header += create_mailfrom_header
19
+ header += create_mailto_header
20
+ header += create_subject_header
21
+ header += "\n"
22
+ header += encode_body
23
+ return header
24
+ end
25
+
26
+ def set_option(opt)
27
+ @subject = opt[:subject] if opt.key?(:subject)
28
+ @body = opt[:body] if opt.key?(:body)
29
+ @body = opt[:mail_to] if opt.key?(:mail_to)
30
+ @body = opt[:mail_from] if opt.key?(:mail_from)
31
+ @mail_from_text = opt[:mail_from_text] if opt.key?(:mail_from_text)
32
+ end
33
+
34
+ private
35
+
36
+ def create_mailfrom_header
37
+ if @mail_from_text.blank?
38
+ return "From: #{@mail_from}\n"
39
+ else
40
+ return to_mail_header("From", @mail_from_text).chomp + "<#{@mail_from}>\n"
41
+ end
42
+ end
43
+
44
+ def create_subject_header
45
+ to_mail_header("Subject",@subject)
46
+ end
47
+
48
+ def create_mailto_header
49
+ if @mail_to.kind_of?(Array)
50
+ return "To: #{@mail_to.join(',')}\n"
51
+ else
52
+ return "To: #{@mail_to.to_s}\n"
53
+ end
54
+ end
55
+
56
+ def encode_mime
57
+ raise "mime_encode must be implemented!!"
58
+ end
59
+
60
+ def encode_body
61
+ raise "encoded_body must be implemented!!"
62
+ end
63
+
64
+ def mime_header_string
65
+ raise "mime_header_string must be implemented!!"
66
+ end
67
+
68
+ private
69
+
70
+ def to_mail_header(header_name, plain_text)
71
+ ret = ""
72
+ str = plain_text.clone
73
+ str.gsub!(/(\r|\n)/,"")
74
+ str.split(//).split_by(10).each do |ar|
75
+ st = encode_mime(ar.join(''))
76
+ st = st.split(//,1).pack('m')
77
+ st = st.chomp
78
+ ret += " #{mime_header_string}#{st}?=\n"
79
+ end
80
+ return "#{header_name}:"+ret
81
+ end
82
+ end
83
+ end
84
+ end
@@ -0,0 +1,18 @@
1
+ module Keystone
2
+ module Mail
3
+ class MessageFactory
4
+ def self.create(opt)
5
+ message = MessageIso2022jp.new(opt)
6
+ if opt.key?(:encoding)
7
+ case opt[:encoding]
8
+ when :sjis
9
+ message = MessageSjis.new(opt)
10
+ else
11
+ raise "encoding '#{opt[:encoding]}' is not supported"
12
+ end
13
+ end
14
+ return message
15
+ end
16
+ end
17
+ end
18
+ end
@@ -0,0 +1,126 @@
1
+ require 'kconv'
2
+
3
+ module Keystone
4
+ module Mail
5
+ #
6
+ # encoding 未実装
7
+ #
8
+ #
9
+ class MessageIso2022jp < Keystone::Mail::Message
10
+ def encode_mime(st)
11
+ Kconv.tojis(st)
12
+ end
13
+
14
+ def mime_header_string
15
+ "=?ISO-2022-JP?B?"
16
+ end
17
+
18
+ def encode_body
19
+ Kconv.tojis(@body)
20
+ end
21
+
22
+ # #
23
+ # # 普通なことをしたいならTMailを使いましょう
24
+ # # class Send
25
+ # def initialize(opt={})
26
+ # @smtp_addr, @smtp_port = "127.0.0.1", 25
27
+ # @subject, @mail_from_text = "" ,nil
28
+ # @retry_cnt, @encoding = 0, :iso2022jp
29
+ # set_option(opt)
30
+ # end
31
+ #
32
+ # #
33
+ # # from could be String or Array
34
+ # # opt
35
+ # # :smtp_addr
36
+ # # :smtp_port
37
+ # # :retry
38
+ # # :mail_from_text
39
+ # # :encoding
40
+ # #
41
+ # def self.send(from, to, subject, body, opt={})
42
+ # queue = self.new(opt)
43
+ # queue.mail_to = to
44
+ # queue.mail_from = from
45
+ # queue.subject = subject
46
+ # queue.body = body
47
+ # queue.send
48
+ # end
49
+ #
50
+ # def send
51
+ # raise "mail_to must not be blank!" if @mail_to.blank?
52
+ # raise "mail_from must not be blank!" if @mail_from.blank?
53
+ # header = ""
54
+ # header += create_mailfrom_header(@mail_from,@mail_from_text)
55
+ # header += create_mailto_header(@mail_to)
56
+ # header += create_subject_header(@subject)
57
+ #
58
+ # case @encoding
59
+ # when :iso2022jp
60
+ # body_encoded = Kconv.tojis(@body)
61
+ # else
62
+ # raise "encoding #{encoding.to_s} can not use"
63
+ # end
64
+ #
65
+ # src = header + "\n" + body_encoded
66
+ # debug src
67
+ # try_cnt = 0
68
+ # begin
69
+ # m = Net::SMTPSession.new(smtp_addr, smtp_port)
70
+ # m.start()
71
+ # m.sendmail(src ,@mail_from ,@mail_to)
72
+ # m.finish
73
+ # rescue => e
74
+ # debug "try_cnt:#{try_cnt}"
75
+ # try_cnt += 1
76
+ # sleep 1
77
+ # retry if @retry_cnt >= try_cnt
78
+ # raise e
79
+ # end
80
+ # end
81
+ #
82
+ # def set_option(opt)
83
+ # @smtp_addr = opt[:smtp_addr] if opt.key?(:smtp_addr)
84
+ # @smtp_port = opt[:smtp_port] if opt.key?(:smtp_port)
85
+ # @retry_cnt = Integer(opt[:retry_cnt]) if opt.key?(:retry_cnt)
86
+ # @mail_from_text = opt[:mail_from_text] if opt.key?(:mail_from_text)
87
+ # @encoding = opt[:encoding] if opt.key?(:encoding)
88
+ # end
89
+ #
90
+ # private
91
+ #
92
+ # def to_mail_header(header_name, plain_text)
93
+ # ret = ""
94
+ # str = plain_text.clone
95
+ # str.gsub!(/(\r|\n)/,"")
96
+ # str.split(//).split_by(10).each do |ar|
97
+ # st = ar.join('')
98
+ # st = Kconv.tojis(st)
99
+ # st = st.split(//,1).pack('m'); st = st.chomp
100
+ # ret += " =?ISO-2022-JP?B?#{st}?=\n"
101
+ # end
102
+ # return "#{header_name}:"+ret
103
+ # end
104
+ #
105
+ # def create_mailfrom_header(from, from_text)
106
+ # if from_text.blank?
107
+ # return "From: #{from}\n"
108
+ # else
109
+ # return to_mail_header("From", from_text).chomp + "<#{from}>\n"
110
+ # end
111
+ # end
112
+ #
113
+ # def create_subject_header(subject)
114
+ # to_mail_header("Subject",subject)
115
+ # end
116
+ #
117
+ # def create_mailto_header(to)
118
+ # if to.class.to_s == 'Array'
119
+ # return "To: #{to.join(',')}\n"
120
+ # else
121
+ # return "To: #{to.to_s}\n"
122
+ # end
123
+ # end
124
+ end
125
+ end
126
+ end
@@ -0,0 +1,23 @@
1
+ require 'kconv'
2
+
3
+ module Keystone
4
+ module Mail
5
+ #
6
+ # encoding 未実装
7
+ #
8
+ #
9
+ class MessageSjis < Keystone::Mail::Message
10
+ def encode_mime(st)
11
+ Kconv.tosjis(st)
12
+ end
13
+
14
+ def mime_header_string
15
+ "=?ShiftJIS?B?"
16
+ end
17
+
18
+ def encode_body
19
+ Kconv.tosjis(@body)
20
+ end
21
+ end
22
+ end
23
+ end
@@ -0,0 +1,15 @@
1
+ require 'rubygems'
2
+ require 'redgreen'
3
+ require 'lib/keystone/core_ext'
4
+
5
+ class TestCoreExt < Test::Unit::TestCase
6
+ def setup
7
+ end
8
+
9
+ def test_array_split_by
10
+ assert_equal([1,2,3,4,5,6,7,8,9].split_by(3),[[1, 2, 3], [4, 5, 6], [7, 8, 9]])
11
+ assert_equal([1,2,3,4,5,6,7,8,9,10].split_by(2),[[1,2],[3,4],[5,6],[7,8],[9,10]])
12
+ assert_equal([].split_by(3),[[]])
13
+ assert_equal([[1,2,3,4],[1,23],[2,33],[[4]]].split_by(3),[[[1, 2, 3, 4], [1, 23], [2, 33]], [[[4]]]])
14
+ end
15
+ end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: kanamei-keystone
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.19
4
+ version: 0.0.20
5
5
  platform: ruby
6
6
  authors:
7
7
  - kanamei
@@ -22,9 +22,11 @@ extensions: []
22
22
  extra_rdoc_files: []
23
23
 
24
24
  files:
25
+ - bin/disk_size_check.rb
25
26
  - example/batch_sample.rb
26
27
  - example/os.rb
27
28
  - example/string_util.rb
29
+ - test/test_core_ext.rb
28
30
  - test/test_string_util.rb
29
31
  - lib/keystone
30
32
  - lib/keystone/base.rb
@@ -40,6 +42,10 @@ files:
40
42
  - lib/keystone/core_ext/uri.rb
41
43
  - lib/keystone/core_ext.rb
42
44
  - lib/keystone/mail
45
+ - lib/keystone/mail/message.rb
46
+ - lib/keystone/mail/message_factory.rb
47
+ - lib/keystone/mail/message_iso2022jp.rb
48
+ - lib/keystone/mail/message_sjis.rb
43
49
  - lib/keystone/mail/send.rb
44
50
  - lib/keystone/mail.rb
45
51
  - lib/keystone/os