super-test 1.0.9

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.
Files changed (108) hide show
  1. checksums.yaml +7 -0
  2. data/CHANGE.txt +9 -0
  3. data/README.txt +13 -0
  4. data/bin/idb +19 -0
  5. data/bin/ii +113 -0
  6. data/bin/istart_mock +36 -0
  7. data/bin/istop_mock +16 -0
  8. data/bin/kk +181 -0
  9. data/bin/rubyatp +42 -0
  10. data/bin/start_mock +36 -0
  11. data/bin/stop_mock +16 -0
  12. data/bin/xgo +45 -0
  13. data/conf/conf.rb +11 -0
  14. data/conf/db03.yml +31 -0
  15. data/conf/db13.yml +37 -0
  16. data/conf/db19.yml +33 -0
  17. data/conf/online.yml +32 -0
  18. data/conf/super_test.yml +40 -0
  19. data/conf/system.yml +26 -0
  20. data/conf/sz00.yml +31 -0
  21. data/conf/sz06.yml +34 -0
  22. data/conf/wise00.yml +11 -0
  23. data/data/case.house +0 -0
  24. data/data/char.house +0 -0
  25. data/data/images/Bluehills.jpg +0 -0
  26. data/data/images/Sunset.jpg +0 -0
  27. data/data/images/Waterlilies.jpg +0 -0
  28. data/data/images/Winter.jpg +0 -0
  29. data/data/query.house +18595 -0
  30. data/data/user.house +8 -0
  31. data/lib/action/module/check_action.rb +58 -0
  32. data/lib/action/module/cnnt_action.rb +161 -0
  33. data/lib/action/module/datacenter_action.rb +48 -0
  34. data/lib/action/module/http_pack_helper.rb +163 -0
  35. data/lib/action/module/httpserver_action.rb +297 -0
  36. data/lib/action/module/inc.rb +21 -0
  37. data/lib/action/module/mcpack_action.rb +57 -0
  38. data/lib/action/module/sql_action.rb +84 -0
  39. data/lib/action/module/ssh_action.rb +37 -0
  40. data/lib/action/system/user_action.rb +180 -0
  41. data/lib/common/appium_helper.rb +217 -0
  42. data/lib/common/atp_helper.rb +74 -0
  43. data/lib/common/atp_report.rb +344 -0
  44. data/lib/common/data_house.rb +405 -0
  45. data/lib/common/data_model.rb +87 -0
  46. data/lib/common/http/assert_helper.rb +343 -0
  47. data/lib/common/http/fix_hpricot.rb +118 -0
  48. data/lib/common/http/html_helper.rb +362 -0
  49. data/lib/common/http/http_helper.rb +469 -0
  50. data/lib/common/http/multipart.rb +81 -0
  51. data/lib/common/isandbox_helper.rb +195 -0
  52. data/lib/common/json_helper.rb +43 -0
  53. data/lib/common/mock_helper.rb +168 -0
  54. data/lib/common/mtop_helper.rb +141 -0
  55. data/lib/common/myconf_helper.rb +49 -0
  56. data/lib/common/mylog_helper.rb +77 -0
  57. data/lib/common/pairwise.rb +121 -0
  58. data/lib/common/query_house.rb +89 -0
  59. data/lib/common/report_helper.rb +97 -0
  60. data/lib/common/robot_helper.rb +85 -0
  61. data/lib/common/socket/check.rb +325 -0
  62. data/lib/common/socket/conf_modifier.rb +149 -0
  63. data/lib/common/socket/context.rb +55 -0
  64. data/lib/common/socket/data.rb +392 -0
  65. data/lib/common/socket/data_helper.rb +41 -0
  66. data/lib/common/socket/env.rb +317 -0
  67. data/lib/common/socket/inc.rb +24 -0
  68. data/lib/common/socket/log.rb +213 -0
  69. data/lib/common/socket/log4s.rb +58 -0
  70. data/lib/common/socket/log_helper.rb +332 -0
  71. data/lib/common/socket/my_sql.rb +77 -0
  72. data/lib/common/socket/net.rb +74 -0
  73. data/lib/common/socket/network.rb +115 -0
  74. data/lib/common/socket/rlib/conf_modifier.rb +130 -0
  75. data/lib/common/socket/rlib/data_helper.rb +33 -0
  76. data/lib/common/socket/rlib/log_helper.rb +303 -0
  77. data/lib/common/socket/rlib/process_helper.rb +91 -0
  78. data/lib/common/socket/stub.rb +276 -0
  79. data/lib/common/socket/util.rb +266 -0
  80. data/lib/patterns/inc.rb +10 -0
  81. data/lib/patterns/mc_base_pattern.rb +474 -0
  82. data/lib/patterns/some_pattern.rb +145 -0
  83. data/lib/super_test.rb +114 -0
  84. data/log/super_test.log +0 -0
  85. data/log/super_test.log.wf +0 -0
  86. data/test/cover_me.rb +6 -0
  87. data/tool/jenny +0 -0
  88. data/tool/mcsend/mcsend2 +0 -0
  89. data/tool/mcsend/mcsend2_shead +0 -0
  90. data/tool/mcserver/conf/mcserver.conf +77 -0
  91. data/tool/mcserver/conf/mcserverauthip +17 -0
  92. data/tool/mcserver/mcserver +0 -0
  93. data/tool/mcserver/mcserver_trans +0 -0
  94. data/tool/mysql +0 -0
  95. data/tool/net/mcsend/mcsend +0 -0
  96. data/tool/net/mcsend/mcsend_trans +0 -0
  97. data/tool/net/mcsend/nshead.data +7 -0
  98. data/tool/net/mcsend/res_nshead.data +7 -0
  99. data/tool/net/my_client/my_client +0 -0
  100. data/tool/net/tsclient/client.tcl +205 -0
  101. data/tool/others/kk +190 -0
  102. data/tool/others/rubyatp +42 -0
  103. data/tool/others/supertest_install.sh +34 -0
  104. data/tool/trip/idb +0 -0
  105. data/tool/ts-agent/nshead.bft +12 -0
  106. data/tool/ts-agent/server.conf +10 -0
  107. data/tool/ts-agent/server.tcl +376 -0
  108. metadata +275 -0
@@ -0,0 +1,74 @@
1
+ #!/usr/bin/env ruby
2
+ # coding: utf-8
3
+
4
+
5
+ #============================================================
6
+ # 网络交互服务顶层接口
7
+ # e.g. nshead_send, shead_send
8
+ # Author:: chenjie
9
+ # Data:: 2009-4-30
10
+ #============================================================
11
+
12
+ module Net
13
+
14
+ # ===功能
15
+ #向指定目标发送指定内容的mcpack包(mcsend)
16
+ #===params:
17
+ #ip: 目标机器ip
18
+ #port: 目标机器端口
19
+ #nshead hash: 定制的nshead文件路径(需要指定nshead头参数时使用,NULL表示使用默认值)
20
+ #send_data_file: 待发送mcpack文本路径
21
+ #recv_data_file: 待接收另存的mcpack文本路径
22
+ # ===example
23
+ # nshead_send "127.0.0.1", 3390, nsheadhash, sendfile, recvfile
24
+ def nshead_send(ip, port, nshead_hash, send_data_file, recv_data_file)
25
+ ms_path=TOOL_ROOT+"net/mcsend/"
26
+ nshead_file = "nshead.data"
27
+ if nil != nshead_hash then
28
+ #TODO: modify nshead_file according to the nshead_hash
29
+ Env.modify_conf('127.0.0.1',USERNAME, PASSWORD, ms_path + nshead_file, nshead_hash)
30
+ end
31
+ send_cmd = "cd #{ms_path} && ./mcsend -s -i #{ip} -p #{port} -n #{nshead_file} -b #{send_data_file} -o #{recv_data_file} "
32
+ $log.debug send_cmd
33
+ system "#{send_cmd}"
34
+ end
35
+
36
+
37
+ def shead_send(ip, port, send_data_file, recv_data_file, diver=nil)
38
+ end
39
+
40
+ def g_send(host, querykey, driver, headtype, datapath, datafile, resultpath, resultfile)
41
+ my_client_path=TOOL_ROOT+"net/my_client/"
42
+ send_cmd="cd #{my_client_path} && ./my_client -q #{querykey} -d #{driver} -t #{headtype} -a #{host} -p #{datapath} -c #{datafile} -r #{resultpath} -f #{resultfile}"
43
+
44
+ system "#{send_cmd}"
45
+
46
+ end
47
+
48
+ def send_mcpack(host, headtype, datapath, datafile, resultpath, resultfile)
49
+ my_client_path=TOOL_ROOT+"net/my_client/"
50
+ send_cmd="cd #{my_client_path} && ./my_client -t #{headtype} -a #{host} -p #{datapath} -c #{datafile} -r #{resultpath} -f #{resultfile}"
51
+
52
+ system "#{send_cmd}"
53
+ end
54
+
55
+ =begin
56
+ def shead_struct_send(host, querykey, driver, datafile, resultfile)
57
+
58
+ datapath=CASE_ROOT+Context.get("MODULE_NAME")
59
+ resultpath=casepath+"/tmp/"
60
+ send(host, querykey, driver, "shead", datapath, datafile, resultpath, resultfile)
61
+ end
62
+
63
+ def shead_struct_send(host, querykey, datafile)
64
+
65
+ driver_path=TOOL_ROOT+"net/my_client/driver/"
66
+ #or find the driver by "grep".
67
+ driver=driver_path+"lib#{querykey}driver.so"
68
+ datapath=CASE_ROOT+Context.get("MODULE_NAME")
69
+ resultpath=casepath+"/tmp/"
70
+ resultfile="#{datafile}.ret"
71
+ send(host, querykey, driver, "shead", datapath, datafile, resultpath, resultfile)
72
+ end
73
+ =end
74
+ end
@@ -0,0 +1,115 @@
1
+ #!/usr/bin/env ruby
2
+ # coding: utf-8
3
+ require 'inc'
4
+
5
+ #============================================================
6
+ # 网络交互服务顶层接口
7
+ # e.g. nshead_send, shead_send
8
+ # Author:: chenjie
9
+ # Data:: 2009-4-30
10
+ #============================================================
11
+
12
+ module Network
13
+
14
+ # ===功能
15
+ #向指定目标发送指定内容的mcpack包(mcsend)
16
+ # ===参数
17
+ # params:
18
+ # ip: 目标机器ip
19
+ # port: 目标机器端口
20
+ # nshead hash: 定制的nshead文件路径(需要指定nshead头参数时使用,NULL表示使用默认值)
21
+ # send_data: 待发送mcpack文本路径
22
+ # recv_data: 待接收另存的mcpack文本路径
23
+ # interval: 两次发送之间的时间间隔,单位为秒
24
+ # ===example
25
+ # nshead_send "127.0.0.1", 3390, nsheadhash, sendfile, recvfile
26
+ def nshead_send ip, port, nshead_hash, send_data, recv_data, interval=0
27
+ ms_path=TOOL_ROOT+"net/mcsend/"
28
+ nshead_file = "nshead.data"
29
+ if nil != :nshead_hash then
30
+ Env.modify_conf('127.0.0.1', USERNAME, PASSWORD, ms_path + nshead_file, nshead_hash)
31
+ end
32
+ send_cmd = "cd #{ms_path} && ./mcsend -s -i #{ip} -p #{port} -n #{nshead_file} -b #{send_data} -o #{recv_data} -t #{interval}"
33
+ $log.debug send_cmd
34
+ system "#{send_cmd}"
35
+ end
36
+
37
+
38
+ # ===功能
39
+ #向指定目标发送指定内容的mcpack包(mcsend_trans)
40
+ # ===参数
41
+ # params:
42
+ # ip: 目标机器ip
43
+ # port: 目标机器端口
44
+ # nshead hash: 定制的nshead文件路径(需要指定nshead头参数时使用,NULL表示使用默认值)
45
+ # send_data: 待发送mcpack文本路径
46
+ # recv_data: 待接收另存的mcpack文本路径
47
+ # interval: 两次发送之间的时间间隔,单位为秒
48
+ # ===example
49
+ # nshead_trans_send "127.0.0.1", 3390, nsheadhash, sendfile, recvfile
50
+ def nshead_trans_send ip, port, nshead_hash, send_data, recv_data, interval=0
51
+ ms_path=TOOL_ROOT+"net/mcsend/"
52
+ nshead_file = "nshead.data"
53
+ if nil != :nshead_hash then
54
+ Env.modify_conf('127.0.0.1', USERNAME, PASSWORD, ms_path + nshead_file, nshead_hash)
55
+ end
56
+ send_cmd = "cd #{ms_path} && ./mcsend_trans -s -i #{ip} -p #{port} -n #{nshead_file} -b #{send_data} -o #{recv_data} -t #{interval}"
57
+ $log.debug send_cmd
58
+ system "#{send_cmd}"
59
+ end
60
+
61
+
62
+ def shead_send(ip, port, send_data_file, recv_data_file, diver=nil)
63
+ end
64
+
65
+ # ===功能
66
+ # 使用my_clinet发送数据
67
+ # ===example
68
+ # g_send host, "spanti", "spanti_driver.so", "nshead", path1, file1, path2, file2
69
+ def g_send(host, querykey, driver, headtype, datapath, datafile, resultpath, resultfile)
70
+ my_client_path=TOOL_ROOT+"net/my_client/"
71
+ send_cmd="cd #{my_client_path} && ./my_client -q #{querykey} -d #{driver} -t #{headtype} -a #{host} -p #{datapath} -c #{datafile} -r #{resultpath} -f #{resultfile}"
72
+
73
+ system "#{send_cmd}"
74
+ end
75
+
76
+ # === 功能
77
+ # 使用my_client发送数据
78
+ # ===example
79
+ # send_mcpack host, "nshead", path, file, rpath, rfile
80
+ def send_mcpack(host, headtype, datapath, datafile, resultpath, resultfile)
81
+ my_client_path=TOOL_ROOT+"net/my_client/"
82
+ send_cmd="cd #{my_client_path} && ./my_client -t #{headtype} -a #{host} -p #{datapath} -c #{datafile} -r #{resultpath} -f #{resultfile}"
83
+
84
+ system "#{send_cmd}"
85
+ end
86
+ =begin
87
+ def shead_struct_send(host, querykey, driver, datafile, resultfile)
88
+
89
+ datapath=CASE_ROOT+Context.get("MODULE_NAME")
90
+ resultpath=casepath+"/tmp/"
91
+ send(host, querykey, driver, "shead", datapath, datafile, resultpath, resultfile)
92
+ end
93
+
94
+ def shead_struct_send(host, querykey, datafile)
95
+
96
+ driver_path=TOOL_ROOT+"net/my_client/driver/"
97
+ #or find the driver by "grep".
98
+ driver=driver_path+"lib#{querykey}driver.so"
99
+ datapath=CASE_ROOT+Context.get("MODULE_NAME")
100
+ resultpath=casepath+"/tmp/"
101
+ resultfile="#{datafile}.ret"
102
+ send(host, querykey, driver, "shead", datapath, datafile, resultpath, resultfile)
103
+ end
104
+ =end
105
+ end
106
+
107
+
108
+ if __FILE__ == $0
109
+ include Network
110
+ class EnvTest < BaseCase
111
+ def test
112
+ nshead_send_2 :ip=>'127.0.0.1'
113
+ end
114
+ end
115
+ end
@@ -0,0 +1,130 @@
1
+ #!/usr/bin/env ruby
2
+ # coding: utf-8
3
+
4
+ #============================================================
5
+ # This class supply conf modification related service
6
+ # e.g. modify single/batch conf items
7
+ # Author:: chenjie
8
+ # Data:: 2009-4-30
9
+ #============================================================
10
+ class ConfModifier
11
+ @lines = 0
12
+
13
+ def initialize(confPath)
14
+ @confPath = confPath
15
+ #lineNum : key
16
+ @dic= Hash.new
17
+ #key : value
18
+ @conf = Hash.new
19
+
20
+ file = File.open(@confPath)
21
+ cnt = 0
22
+
23
+ file.each{ |line|
24
+ #print line
25
+ cnt+=1
26
+
27
+ if line.strip[0, 1] == '#' or line.strip.length == 0
28
+ @dic[ cnt ] = line.strip
29
+ else
30
+ list = line.strip.split(":")
31
+ key = list[0].strip
32
+ if list[1] != nil then
33
+ list.delete_at(0)
34
+ value = list.join(":").strip
35
+ else
36
+ #只有key没有value的情况
37
+ value = ''
38
+ end
39
+
40
+ @dic[ cnt ] = key
41
+ @conf[key] = value
42
+ end
43
+ }
44
+
45
+ @lines = cnt
46
+
47
+ file.close
48
+ end
49
+
50
+ # 修改单个配置项
51
+ def modify(key, newValue)
52
+ if @conf.has_key?(key)
53
+ puts "modify from [" + key + "=>" + @conf[key] + "] to [" + key + "=>" + newValue + " ]"
54
+ @conf[key] = newValue
55
+ else
56
+ puts "add new conf " + key + "=>" + newValue
57
+ @lines += 1
58
+ @dic[@lines] = key
59
+ @conf[key] = newValue
60
+ end
61
+ end
62
+
63
+ # 批量修改配置项
64
+ def modifyall(hash)
65
+ if hash != nil then
66
+ hash.each{|key,value|
67
+ modify(key, value)
68
+ }
69
+ end
70
+ end
71
+
72
+ # 按顺序回写配置文件
73
+ def writeconf
74
+ file = File.open(@confPath, 'w')
75
+ @dic.sort_by { |key, value| key }
76
+ @dic.each_key { |ki|
77
+ if @dic[ki].strip.length != 0
78
+ #key, value
79
+ if @conf.has_key?(@dic[ki])
80
+ file.print(@dic[ki] , " : ",@conf[@dic[ki]] , "\n")
81
+ else
82
+ file.print( @dic[ki] , "\n")
83
+ end
84
+ else
85
+ file.print( "\n")
86
+ end
87
+ }
88
+
89
+ file.close
90
+ end
91
+
92
+ # 获取某个配置项的值
93
+ def get_conf(key)
94
+ return @conf[key]
95
+ end
96
+ end
97
+
98
+ if ARGV.length > 0 then
99
+ case ARGV[0]
100
+ when "modify_conf_single"
101
+ conf_path = ARGV[1]
102
+ key = ARGV[2]
103
+ new_value = ARGV[3]
104
+ cm = ConfModifier.new(conf_path)
105
+ cm.modify(key, new_value)
106
+ cm.writeconf
107
+ when "get_conf"
108
+ conf_path = ARGV[1]
109
+ key = ARGV[2]
110
+ cm = ConfModifier.new(conf_path)
111
+ puts cm.get_conf(key)
112
+ else
113
+ puts "ConfModifer: bad method name? #{ARGV[0]}"
114
+ end
115
+ end
116
+
117
+
118
+ =begin
119
+ if __FILE__ == $0
120
+ require "#{File.dirname(__FILE__)}/../../lib/base_case"
121
+ class MyTest < BaseCase
122
+ def no_test
123
+ conf_path = "#{File.dirname(__FILE__)}/../../test/env/dataminer.conf"
124
+ cm = ConfModifier.new conf_path
125
+ cm.modify('index_mode', '1')
126
+ cm.writeconf
127
+ end
128
+ end
129
+ end
130
+ =end
@@ -0,0 +1,33 @@
1
+ #$LOAD_PATH << File.join(File.dirname(__FILE__), './')
2
+ #!/usr/bin/env ruby
3
+ # coding: utf-8
4
+
5
+ #require 'log4s'
6
+
7
+ class DataHelper
8
+ def self.file_exist? file_path
9
+ return File.exists? file_path
10
+ end
11
+
12
+ def self.file_size? file_path
13
+ return File.size? file_path
14
+ end
15
+
16
+ end
17
+
18
+ if __FILE__ == $0
19
+ if ARGV.length > 0 then
20
+ case ARGV[0]
21
+ when 'file_exist?'
22
+ file_path = ARGV[1]
23
+ puts DataHelper.file_exist? file_path
24
+
25
+ when 'file_size?'
26
+ file_path = ARGV[1]
27
+ puts DataHelper.file_size? file_path
28
+
29
+ else
30
+ puts 'DataHelper: bad method name? #{ARGV[0]}'
31
+ end
32
+ end
33
+ end
@@ -0,0 +1,303 @@
1
+ #!/usr/bin/env ruby
2
+ # coding: utf-8
3
+ LOG_LEVEL=["DEBUG", "NOTICE", "WARNING", "FATAL"]
4
+
5
+ #===========================================================
6
+ #class for scan local log file
7
+ #support log level : ["DEBUG", "NOTICE", "WARNING", "FATAL"]
8
+ #===========================================================
9
+
10
+ class LogHelper
11
+
12
+ #get the total lines of the log file
13
+ #params
14
+ #log_path: logfile path
15
+ #log_name: logfile name
16
+ #return: the total lines of the log file
17
+ def LogHelper.get_lines(log_path, log_name)
18
+ log_file = log_path + log_name
19
+
20
+ #param check
21
+ if File::exists?(log_file) && File::file?(log_file) then
22
+ File.open(log_file) do |f|
23
+ lines = f.readlines.size
24
+ return lines
25
+ end
26
+ else
27
+ raise "logfile #{log_file} not exists or is not a regular file"
28
+ end
29
+ end
30
+
31
+
32
+ #get the last n lines's contents of the log file
33
+ #param
34
+ #log_path
35
+ #log_name
36
+ #start_lines: from which line we search the log content, newlog=[start_lines, log_end]
37
+ #return: an array contains the last n lines' contents
38
+ def LogHelper.get_tail_contents(log_path, log_name, start_lines)
39
+ last_lines = LogHelper.get_lines(log_path, log_name) - start_lines + 1
40
+ #param check
41
+ if last_lines <= 0 then
42
+ raise "params error: last_lines should bigger than 0"
43
+ end
44
+ log_file = log_path + log_name
45
+
46
+ contents = Array.new
47
+ if File::exists?(log_file) && File::file?(log_file) then
48
+ system "tail -#{last_lines} #{log_file} > tmp_file"
49
+ File.open("tmp_file") do |f|
50
+ contents = f.readlines
51
+ #remove the tmp file
52
+ system "rm tmp_file"
53
+ return contents
54
+ end
55
+ else
56
+ raise "file #{log_file} not exists or is not a regular file"
57
+ end
58
+
59
+ end
60
+
61
+ def LogHelper.get_match_lines log_path, log_name, start_lines, log_level, pattern
62
+ contents = get_tail_contents log_path, log_name, start_lines
63
+ lines = []
64
+
65
+ if contents == nil
66
+ return lines
67
+ else
68
+ contents.each{|line|
69
+ checkable = false
70
+ if log_level.size > 0
71
+ log_level.each{|level|
72
+ if line.include? level
73
+ checkable = true
74
+ break
75
+ end
76
+ }
77
+ else
78
+ checkable = true
79
+ end
80
+
81
+
82
+ if checkable
83
+ #match = line.scan pattern
84
+ #if match != nil && match.size > 0
85
+ # lines << line
86
+ #end
87
+ match = pattern.match line
88
+ if match != nil
89
+ lines << line
90
+ end
91
+ end
92
+ }
93
+
94
+ return lines
95
+ end
96
+ end
97
+
98
+ # get the match times with the giving log level and regex pattern
99
+ #
100
+ # -log_path:
101
+ # -log_name:
102
+ # -start_lines:from which line we search the log content, newlog=[start_lines, log_end]
103
+ # -log_level: an array whoes value from ["DEBUG", "NOTICE", "WARNING", "FATAL"]; nil for all levels
104
+ # -pattern: a string or a regex pattern
105
+ # return: int, the match times with the giving log level and regex pattern
106
+ #
107
+ # example:
108
+ # LogHelper.get_match_times("./", "chenjie.log", 1, nil, "openapptrans")
109
+ # LogHelper.get_match_times("./", "chenjie.log", 1, nil, /(\d{4})-(\d{2})-(\d{2})/)
110
+ def LogHelper.get_match_times(log_path, log_name, start_lines, log_level, pattern)
111
+ contents = get_tail_contents(log_path, log_name, start_lines)
112
+ count = 0
113
+ if contents == nil then
114
+ return 0
115
+ else
116
+ contents.each{|line|
117
+ checkable = false
118
+ if log_level.size > 0 then
119
+ #log_level is not nil, only the line contains the log_level, then should be check
120
+ log_level.each{|level|
121
+ if line.include?(level) then
122
+ checkable = true
123
+ break;
124
+ end
125
+ }
126
+ else
127
+ #log_level is nil, the line should be check
128
+ checkable = true
129
+ end
130
+
131
+ if checkable then
132
+ matches = line.scan(pattern)
133
+ count += matches.size
134
+ end
135
+ }
136
+ end
137
+
138
+ return count
139
+ end
140
+
141
+ #get the match values with the giving log level and regex pattern
142
+ #param
143
+ #log_path:
144
+ #log_name:
145
+ #last_lines:the lines read from tail to head
146
+ #log_level: an array whoes value from ["DEBUG", "NOTICE", "WARNING", "FATAL"]; nil for all levels
147
+ #pattern: a string or a regex pattern
148
+ #return: an array contains the match values with the giving log level and regex pattern
149
+ #
150
+ #example:
151
+ #LogHelper.get_match_values("./", "chenjie.log", 1, nil, /(\d{4})-(\d{2})-(\d{2})/)
152
+ #LogHelper.get_match_values("./", "chenjie.log", 1, nil, "openapptrans")
153
+ def LogHelper.get_match_values(log_path, log_name, start_lines, log_level, pattern)
154
+ contents = get_tail_contents(log_path, log_name, start_lines)
155
+ values = Array.new
156
+ if contents == nil then
157
+ return 0
158
+ else
159
+ contents.each{|line|
160
+ checkable = false
161
+ if log_level.size > 0 then
162
+ #log_level is not nil, only the line contains the log_level, then should be check
163
+ log_level.each{|level|
164
+ if line.include?(level) then
165
+ checkable = true
166
+ break;
167
+ end
168
+ }
169
+ else
170
+ #log_level is nil, the line should be check
171
+ checkable = true
172
+ end
173
+
174
+ if checkable then
175
+ matches = line.scan(pattern)
176
+ matches.each{|m|
177
+ values << m
178
+ }
179
+ end
180
+ }
181
+ end
182
+
183
+ return values
184
+ end
185
+
186
+ #get the first match value, nil if no match found
187
+ #param
188
+ #log_path:
189
+ #log_name:
190
+ #last_lines:the lines read from tail to head
191
+ #log_level: an array whoes value from ["DEBUG", "NOTICE", "WARNING", "FATAL"]; nil for all levels
192
+ #pattern: a string or a regex pattern
193
+ #return: a string firstly matches the pattern, nil if no match found
194
+ #
195
+ #example:
196
+ #LogHelper.get_match_value("./", "chenjie.log", 1, nil, /(\d{4})-(\d{2})-(\d{2})/)
197
+ #LogHelper.get_match_value("./", "chenjie.log", 1, nil, "openapptrans")
198
+ def LogHelper.get_match_value(log_path, log_name, start_lines, log_level, pattern)
199
+ contents = get_tail_contents(log_path, log_name, start_lines)
200
+ if contents == nil then
201
+ return nil
202
+ else
203
+ contents.each{|line|
204
+ checkable = false
205
+ if log_level.size > 0 then
206
+ #log_level is not nil, only the line contains the log_level, then should be check
207
+ log_level.each{|level|
208
+ if line.include?(level) then
209
+ checkable = true
210
+ break;
211
+ end
212
+ }
213
+ else
214
+ #log_level is nil, the line should be check
215
+ checkable = true
216
+ end
217
+
218
+ if checkable then
219
+ match = pattern.match(line)
220
+ if match != nil then
221
+ return match[0]
222
+ end
223
+ end
224
+ }
225
+ end
226
+
227
+ return nil
228
+ end
229
+
230
+ #turn log_level code into log level array(helper method)
231
+ #used to solve remote parameter passing problem(pass string format parameter)
232
+ #param
233
+ #code: "0:0:0:0" to "1:1:1:1"
234
+ #return: [] to ["DEBUG", "NOTICE", "WARNING", "FATAL"]
235
+ def LogHelper.level_decode(code)
236
+ levels = Array.new
237
+ arr = code.split(":")
238
+
239
+ i = 0
240
+ arr.each{|a|
241
+ if a == "1" then
242
+ levels << LOG_LEVEL[i]
243
+ end
244
+ i += 1
245
+ }
246
+ return levels
247
+ end
248
+
249
+ #translate log_level array into log level code(helper method)
250
+ #used to solve remote parameter passing problem(pass string format parameter)
251
+ #param
252
+ #code: "0:0:0:0" to "1:1:1:1"
253
+ #return: [] to ["DEBUG", "NOTICE", "WARNING", "FATAL"]
254
+ def LogHelper.level_encode arr
255
+ code = ""
256
+ if arr.include?("DEBUG") then
257
+ code += "1:"
258
+ else
259
+ code += "0:"
260
+ end
261
+
262
+ if arr.include?("NOTICE") then
263
+ code += "1:"
264
+ else
265
+ code += "0:"
266
+ end
267
+
268
+ if arr.include?("WARNING") then
269
+ code += "1:"
270
+ else
271
+ code += "0:"
272
+ end
273
+
274
+ if arr.include?("FATAL") then
275
+ code += "1"
276
+ else
277
+ code += "0"
278
+ end
279
+
280
+ return code
281
+ end
282
+
283
+ end
284
+
285
+ if ARGV.length > 0 then
286
+ case ARGV[0]
287
+ when "get_lines"
288
+ puts LogHelper.get_lines(ARGV[1], ARGV[2])
289
+ when "get_tail_contents"
290
+ puts LogHelper.get_tail_contents(ARGV[1], ARGV[2], ARGV[3].to_i)
291
+ when "get_match_times"
292
+ puts LogHelper.get_match_times(ARGV[1], ARGV[2], ARGV[3].to_i, LogHelper.level_decode(ARGV[4]) , Regexp.new(ARGV[5]))
293
+ when "get_match_lines"
294
+ puts LogHelper.get_match_lines(ARGV[1], ARGV[2], ARGV[3].to_i, LogHelper.level_decode(ARGV[4]) , Regexp.new(ARGV[5]))
295
+ when "get_match_values"
296
+ puts LogHelper.get_match_values(ARGV[1], ARGV[2], ARGV[3].to_i, LogHelper.level_decode(ARGV[4]) , Regexp.new(ARGV[5]))
297
+ when "get_match_value"
298
+ puts LogHelper.get_match_value(ARGV[1], ARGV[2], ARGV[3].to_i, LogHelper.level_decode(ARGV[4]) , Regexp.new(ARGV[5]))
299
+ else
300
+ puts "LogHelper: bad method name? #{ARGV[0]}"
301
+ end
302
+ end
303
+