Module: Log

Includes:
DATA
Included in:
Check
Defined in:
lib/common/socket/log.rb

Overview

Instance Method Summary collapse

Methods included from DATA

#_init_file_, #file_exist?, #file_match?, #file_match_times, #file_size?, #get_all_pack, #get_mcpack_value, #include_file_str, #same_content_value, #same_file_contents, #same_file_md5, #same_file_str

Instance Method Details

#_monitor_logObject

功能

监控被测模块日志简化版方法,减少参数传递约束: helper中必须定义 IP, PORT, LOG_PATH, LOG, WFLOG等变量

example

_monitor_log



56
57
58
# File 'lib/common/socket/log.rb', line 56

def _monitor_log 
	monitor_log IP, LOG_PATH, LOG, USERNAME, PASSWORD
end

#_monitor_wflogObject

功能

监控被测模块异常日志简化版方法,减少参数传递约束: helper中必须定义 IP, PORT, LOG_PATH, LOG, WFLOG等变量

example

_monitor_wflog



66
67
68
# File 'lib/common/socket/log.rb', line 66

def _monitor_wflog 
	monitor_log IP, LOG_PATH, WFLOG, USERNAME, PASSWORD
end

#get_match_lines(host, log_path, log_name, log_level, pattern, user = USERNAME, password = PASSWORD) ⇒ Object

get_match_lines host, “/home/space/space/log/”, “log1”,



134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
# File 'lib/common/socket/log.rb', line 134

def get_match_lines host, log_path, log_name, log_level, pattern, user=USERNAME, password=PASSWORD
	log_pathname = "#{log_path}/#{log_name}"
	start_lines = Context.get("#{host}:#{log_path}:#{log_name}") + 1
	lines = []
	if Util.is_localhost? host
	  	return LogHelper.get_match_lines log_path, log_name, start_lines, log_level, pattern
	else 
	  	#level_code = LogHelper.level_encode log_level
	  	#lib_path = Util.get_lib_path host
	  	#cmd_str = "cd #{lib_path} && ruby log_helper.rb get_match_lines #{log_path} #{log_name} #{start_lines} #{level_code} #{pattern}"
	  	#lines = Util.exe_ssh_cmd! host, cmd_str
	  	#return lines
		Util.change_in_local(host, "#{log_pathname}", user, password){ | local_path |
                               lines = LogHelper.get_match_lines File.dirname(local_path), File.basename(local_path), start_lines, log_level, pattern
                       }
		return lines
	end	
end

#get_match_times(host, log_path, log_name, log_level, pattern, user = USERNAME, password = PASSWORD) ⇒ Object

功能

get the match times with the giving log level and regex pattern

参数

  • host the remote host ip, nil means the log is on localhost

  • log_path

  • log_name

  • log_level an array whoes value from [“DEBUG”, “NOTICE”, “WARNING”, “FATAL”]; nil for all levels

  • pattern a string or a regex pattern

return int, the match times with the giving log level and regex pattern

example

get_match_times host, logpath, “log1”, [“DEBUG”], “time”



111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
# File 'lib/common/socket/log.rb', line 111

def get_match_times host, log_path, log_name, log_level, pattern, user=USERNAME, password=PASSWORD
	log_pathname = "#{log_path}/#{log_name}"
	start_lines = Context.get("#{host}:#{log_path}:#{log_name}") + 1
	lines = 0
	if Util.is_localhost? host
	  	return LogHelper.get_match_times log_path, log_name, start_lines, log_level, pattern
	else 
	  	#level_code = LogHelper.level_encode log_level
	  	#lib_path = Util.get_lib_path host
	  	#cmd_str = "cd #{lib_path} && ruby log_helper.rb get_match_times #{log_path} #{log_name} #{start_lines} #{level_code} #{pattern}"
		#times = Util.exe_ssh_cmd! host, cmd_str
		#return times.to_i
		Util.change_in_local(host, "#{log_pathname}", user, password){ | local_path |
                               lines = LogHelper.get_match_times File.dirname(local_path), File.basename(local_path), start_lines, log_level, pattern
                       }
		return lines
	end	   
end

#get_match_value(host, log_path, log_name, log_level, pattern, user = USERNAME, password = PASSWORD) ⇒ Object

功能

get the first match value, nil if no match found

参数

  • log_path

  • log_name

  • last_lines the lines read from tail to head

  • log_level an array whoes value from [“DEBUG”, “NOTICE”, “WARNING”, “FATAL”]; nil for all levels

  • pattern a string or a regex pattern

return a string firstly matches the pattern

example

get_match_value host, logpath, “log1”, [“DEBUG”], “time”



194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
# File 'lib/common/socket/log.rb', line 194

def get_match_value host, log_path, log_name, log_level, pattern, user=USERNAME, password=PASSWORD
	log_pathname = "#{log_path}/#{log_name}"
	start_lines = Context.get("#{host}:#{log_path}:#{log_name}") + 1
	lines = nil
	if Util.is_localhost? host
	  	return LogHelper.get_match_value log_path, log_name, start_lines, log_level, pattern
	else      
	  	#level_code = LogHelper.level_encode log_level
	  	#lib_path = Util.get_lib_path host
	  	#cmd_str = "cd #{lib_path} && ruby log_helper.rb get_match_value #{log_path} #{log_name} #{start_lines} #{level_code} #{pattern}"
		#value = Util.exe_ssh_cmd! host, cmd_str
	  	#return value
		Util.change_in_local(host, "#{log_pathname}", user, password){ | local_path |
                               lines = LogHelper.get_match_value File.dirname(local_path), File.basename(local_path), start_lines, log_level, pattern
                       }
	end
end

#get_match_values(host, log_path, log_name, log_level, pattern, user = USERNAME, password = PASSWORD) ⇒ Object

功能

get the match values with the giving log level and regex pattern

参数

  • log_path

  • log_name

  • last_lines the lines read from tail to head

  • log_level an array whoes value from [“DEBUG”, “NOTICE”, “WARNING”, “FATAL”]; nil for all levels

  • pattern a string or a regex pattern

  • return an array contains the match values with the giving log level and regex pattern

example

get_match_values host, logpath, “log1”, [“DEBUG”, “WARNING”], “time”



164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
# File 'lib/common/socket/log.rb', line 164

def get_match_values host, log_path, log_name, log_level, pattern, user=USERNAME, password=PASSWORD
	log_pathname = "#{log_path}/#{log_name}"
	start_lines = Context.get("#{host}:#{log_path}:#{log_name}") + 1
	lines = []
	if Util.is_localhost? host
	  	return LogHelper.get_match_values log_path, log_name, start_lines, log_level, pattern
	else      
	 	#level_code = LogHelper.level_encode log_level
	  	#lib_path = Util.get_lib_path host
	  	#cmd_str = "cd #{lib_path} && ruby log_helper.rb get_match_values #{log_path} #{log_name} #{start_lines} #{level_code} #{pattern}"
		#values = Util.exe_ssh_cmd! host, cmd_str
	  	#return values
		Util.change_in_local(host, "#{log_pathname}", user, password){ | local_path |
                               lines = LogHelper.get_match_values File.dirname(local_path), File.basename(local_path), start_lines, log_level, pattern
                       }
		return lines
	end
end

#has_new_log?(host, log_path, log_name, user = USERNAME, password = PASSWORD) ⇒ Boolean

has_new_log? “127.0.0.1”, “/home/space/space/log/”, “log1”

Returns:

  • (Boolean)


74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
# File 'lib/common/socket/log.rb', line 74

def has_new_log? host, log_path, log_name, user=USERNAME, password=PASSWORD
	log_pathname = "#{log_path}/#{log_name}"
	last_lines = Context.get "#{host}:#{log_path}:#{log_name}"
	
	lines = 0
	if Util.is_localhost? host
	  	lines =  LogHelper.get_lines log_path, log_name
	else
	  	#lib_path = Util.get_lib_path host
	  	#cmd_str = "cd #{lib_path} && ruby log_helper.rb get_lines #{log_path} #{log_name}"
		#lines = Util.exe_ssh_cmd!(host, cmd_str).to_i
		Util.change_in_local(host, "#{log_pathname}", user, password){ | local_path |
                               lines = LogHelper.get_lines File.dirname(local_path), File.basename(local_path)
                       }	
	end
	
	if lines == last_lines then
	  	return false
	elsif lines > last_lines  then
	  	return true
	else
	  	raise "params error: lines is more than last lines"
	end
end

#monitor_log(host, log_path, log_name, user = USERNAME, password = PASSWORD) ⇒ Object

功能

在使用以下所有函数前使用注意:一次只能monitor一个日志文件. monitor the log file and record the lines into context

参数

  • host the remote host ip, nil means the log is on localhost

  • log_path

  • log_name

return the lines of the log; -1 if the log not exist

example

monitor_log host, logpath, “log1”



28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
# File 'lib/common/socket/log.rb', line 28

def monitor_log host, log_path, log_name, user=USERNAME, password=PASSWORD
	lines=0
	log_pathname = "#{log_path}/#{log_name}"
	# 为了解决日志文件过大导致的性能问题,首先将日志清空
	if Util.is_localhost? host
		`> #{log_pathname}`
		lines =  LogHelper.get_lines log_path, log_name
	else
		#lib_path = Util.get_lib_path host
		#cmd_str = "cd #{lib_path} && ruby log_helper.rb get_lines #{log_path} #{log_name}"
		#lines = Util.exe_ssh_cmd!(host, cmd_str).to_i
		cmd_str = "> #{log_pathname}"
		Util.exe_ssh_cmd!(host, cmd_str, user, password)
		Util.change_in_local(host, "#{log_pathname}", user, password){ | local_path |
			lines = LogHelper.get_lines File.dirname(local_path), File.basename(local_path)
		}
	end
	
	#record in context
	Context.set("#{host}:#{log_path}:#{log_name}", lines)
end