woolen_common 0.0.1
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.
- checksums.yaml +15 -0
- data/README.md +31 -0
- data/ext/woolen_common/extconf.rb +26 -0
- data/ext/woolen_common/linux.h +3 -0
- data/ext/woolen_common/win.c +18 -0
- data/ext/woolen_common/win.h +4 -0
- data/ext/woolen_common/win/puts_color.c +139 -0
- data/ext/woolen_common/win/puts_color.h +5 -0
- data/ext/woolen_common/woolen_common.c +20 -0
- data/ext/woolen_common/woolen_common.h +7 -0
- data/lib/woolen_common.rb +39 -0
- data/lib/woolen_common/abstract_middleware/builder.rb +138 -0
- data/lib/woolen_common/abstract_middleware/map_cfg_manager.rb +52 -0
- data/lib/woolen_common/abstract_middleware/runner.rb +72 -0
- data/lib/woolen_common/action_pool_proxy.rb +28 -0
- data/lib/woolen_common/actionpool.rb +10 -0
- data/lib/woolen_common/actionpool/pool.rb +295 -0
- data/lib/woolen_common/actionpool/queue.rb +41 -0
- data/lib/woolen_common/actionpool/thread.rb +181 -0
- data/lib/woolen_common/addr_helper.rb +93 -0
- data/lib/woolen_common/cache.rb +305 -0
- data/lib/woolen_common/common_helper.rb +42 -0
- data/lib/woolen_common/config_manager.rb +36 -0
- data/lib/woolen_common/drb_helper.rb +125 -0
- data/lib/woolen_common/ffi/win32_kernel32.rb +86 -0
- data/lib/woolen_common/logger.rb +419 -0
- data/lib/woolen_common/pcap/mu/fixnum_ext.rb +8 -0
- data/lib/woolen_common/pcap/mu/pcap/ethernet.rb +164 -0
- data/lib/woolen_common/pcap/mu/pcap/header.rb +76 -0
- data/lib/woolen_common/pcap/mu/pcap/io_pair.rb +68 -0
- data/lib/woolen_common/pcap/mu/pcap/io_wrapper.rb +77 -0
- data/lib/woolen_common/pcap/mu/pcap/ip.rb +62 -0
- data/lib/woolen_common/pcap/mu/pcap/ipv4.rb +274 -0
- data/lib/woolen_common/pcap/mu/pcap/ipv6.rb +149 -0
- data/lib/woolen_common/pcap/mu/pcap/packet.rb +106 -0
- data/lib/woolen_common/pcap/mu/pcap/pkthdr.rb +162 -0
- data/lib/woolen_common/pcap/mu/pcap/reader.rb +62 -0
- data/lib/woolen_common/pcap/mu/pcap/reader/http_family.rb +175 -0
- data/lib/woolen_common/pcap/mu/pcap/sctp.rb +369 -0
- data/lib/woolen_common/pcap/mu/pcap/sctp/chunk.rb +124 -0
- data/lib/woolen_common/pcap/mu/pcap/sctp/chunk/data.rb +135 -0
- data/lib/woolen_common/pcap/mu/pcap/sctp/chunk/init.rb +101 -0
- data/lib/woolen_common/pcap/mu/pcap/sctp/chunk/init_ack.rb +69 -0
- data/lib/woolen_common/pcap/mu/pcap/sctp/parameter.rb +111 -0
- data/lib/woolen_common/pcap/mu/pcap/sctp/parameter/ip_address.rb +49 -0
- data/lib/woolen_common/pcap/mu/pcap/stream_packetizer.rb +74 -0
- data/lib/woolen_common/pcap/mu/pcap/tcp.rb +522 -0
- data/lib/woolen_common/pcap/mu/pcap/udp.rb +81 -0
- data/lib/woolen_common/pcap/mu/scenario/pcap.rb +175 -0
- data/lib/woolen_common/pcap/mu/scenario/pcap/fields.rb +51 -0
- data/lib/woolen_common/pcap/mu/scenario/pcap/rtp.rb +72 -0
- data/lib/woolen_common/pcap/pcap.rb +115 -0
- data/lib/woolen_common/pcap/readme.md +72 -0
- data/lib/woolen_common/ruby_ext/blank.rb +126 -0
- data/lib/woolen_common/ruby_ext/drb_ext.rb +7 -0
- data/lib/woolen_common/ruby_ext/string.rb +116 -0
- data/lib/woolen_common/ruby_ext/win32_ole.rb +4 -0
- data/lib/woolen_common/ruby_proxy.rb +5 -0
- data/lib/woolen_common/ruby_proxy/client.rb +305 -0
- data/lib/woolen_common/ruby_proxy/config.rb +44 -0
- data/lib/woolen_common/ruby_proxy/exceptions.rb +17 -0
- data/lib/woolen_common/ruby_proxy/proxy.rb +157 -0
- data/lib/woolen_common/ruby_proxy/proxy_global_set.rb +44 -0
- data/lib/woolen_common/ruby_proxy/proxy_load.rb +34 -0
- data/lib/woolen_common/ruby_proxy/server.rb +53 -0
- data/lib/woolen_common/splib.rb +36 -0
- data/lib/woolen_common/splib/Array.rb +33 -0
- data/lib/woolen_common/splib/CodeReloader.rb +59 -0
- data/lib/woolen_common/splib/Constants.rb +44 -0
- data/lib/woolen_common/splib/Conversions.rb +47 -0
- data/lib/woolen_common/splib/Exec.rb +132 -0
- data/lib/woolen_common/splib/Float.rb +13 -0
- data/lib/woolen_common/splib/HumanIdealRandomIterator.rb +40 -0
- data/lib/woolen_common/splib/Monitor.rb +214 -0
- data/lib/woolen_common/splib/PriorityQueue.rb +110 -0
- data/lib/woolen_common/splib/Sleep.rb +10 -0
- data/lib/woolen_common/splib/UrlShorteners.rb +48 -0
- data/lib/woolen_common/ssh_proxy.rb +146 -0
- data/lib/woolen_common/system_helper.rb +123 -0
- data/lib/woolen_common/system_monitor.rb +23 -0
- data/lib/woolen_common/system_monitor/linux_monitor.rb +250 -0
- data/lib/woolen_common/system_monitor/windows_monitor.rb +145 -0
- data/lib/woolen_common/type_helper.rb +42 -0
- data/lib/woolen_common/ver_ctrl_middle_ware.rb +92 -0
- data/lib/woolen_common/version.rb +3 -0
- metadata +210 -0
@@ -0,0 +1,250 @@
|
|
1
|
+
# -*- encoding : utf-8 -*-
|
2
|
+
module WoolenCommon
|
3
|
+
module LinuxMonitor
|
4
|
+
include WoolenCommon::ToolLogger
|
5
|
+
LINUX_CPU_STAT='/proc/stat'
|
6
|
+
LINUX_MEM_STAT='/proc/meminfo'
|
7
|
+
LINUX_DISK_STAT='/proc/diskstats'
|
8
|
+
LINUX_DISK_PARTITION_STAT='/proc/partitions'
|
9
|
+
SYS_NET_PATH = '/sys/class/net'
|
10
|
+
|
11
|
+
CPU_STAT_FORMAT = %w{cpu user nice system idle io_wait irq soft_irq steal guest}
|
12
|
+
DISK_STAT_FORMAT = %w{disk r_io/s w_io/s r_kB/s w_kB/s r_size w_size}
|
13
|
+
|
14
|
+
# cpu 432661 13295 86656 422145968 171474 233 5346 0 0
|
15
|
+
# cpu {user} {nice} {system} {idle} {iowait} {irq} {softirq} {stealstolen} {guest}
|
16
|
+
# user (432661) 从系统启动开始累计到当前时刻,用户态的CPU时间(单位:jiffies) ,不包含 nice值为负进程。1jiffies=0.01秒
|
17
|
+
#nice (13295) 从系统启动开始累计到当前时刻,nice值为负的进程所占用的CPU时间(单位:jiffies)
|
18
|
+
#system (86656) 从系统启动开始累计到当前时刻,核心[系统调用]时间(单位:jiffies)
|
19
|
+
#idle (422145968) 从系统启动开始累计到当前时刻,除硬盘IO等待时间以外其它等待时间(单位:jiffies)
|
20
|
+
#iowait (171474) 从系统启动开始累计到当前时刻,硬盘IO等待时间(单位:jiffies) ,
|
21
|
+
#irq (233) 从系统启动开始累计到当前时刻,硬中断时间(单位:jiffies)
|
22
|
+
#softirq (5346) 从系统启动开始累计到当前时刻,软中断时间(单位:jiffies)
|
23
|
+
#stealstolen(0) which is the time spent in other operating systems when running in a virtualized environment(since 2.6.11)
|
24
|
+
#guest(0) which is the time spent running a virtual CPU for guest operating systems under the control of the Linux kernel(since 2.6.24)
|
25
|
+
def get_one_cpu_usage(old, new)
|
26
|
+
ret_hash = {}
|
27
|
+
old_array = old.split(/\s+/)
|
28
|
+
new_array = new.split(/\s+/)
|
29
|
+
# trace "old[#{old}] split #{old_array}"
|
30
|
+
# trace "new[#{new}] split #{new_array}"
|
31
|
+
old_sum = 0
|
32
|
+
new_sum = 0
|
33
|
+
old_array.each { |str| old_sum += str.to_i }
|
34
|
+
new_array.each { |str| new_sum += str.to_i }
|
35
|
+
delta_sum = (new_sum - old_sum).to_f
|
36
|
+
ideal_delta = new_array[4].to_i - old_array[4].to_i
|
37
|
+
# debug "length #{new_array.length} old sum[#{old_sum}] new_sum[#{new_sum}] delta_sum[#{delta_sum}] ideal_delta[#{ideal_delta}]"
|
38
|
+
ret_hash['total'] = ((1 - (ideal_delta) / (delta_sum)) * 100).round(3)
|
39
|
+
0.upto CPU_STAT_FORMAT.length-1 do |cnt|
|
40
|
+
# debug "checking cnt[#{cnt}]"
|
41
|
+
if new_array[cnt]
|
42
|
+
if cnt == 0
|
43
|
+
ret_hash[CPU_STAT_FORMAT[cnt]] = new_array[cnt]
|
44
|
+
else
|
45
|
+
delta = new_array[cnt].to_i - old_array[cnt].to_i
|
46
|
+
# debug "cpu [#{CPU_STAT_FORMAT[cnt]}] delta #{delta}"
|
47
|
+
ret_hash[CPU_STAT_FORMAT[cnt]] = (((delta) / (delta_sum)) * 100).round(3)
|
48
|
+
end
|
49
|
+
else
|
50
|
+
debug "not have #{CPU_STAT_FORMAT[cnt]} cnt[#{cnt}]"
|
51
|
+
end
|
52
|
+
end
|
53
|
+
ret_hash
|
54
|
+
end
|
55
|
+
|
56
|
+
def get_system_cpu_usage(time=1)
|
57
|
+
result = []
|
58
|
+
old_arry = File.open(LINUX_CPU_STAT, 'r') { |f| f.read.split("\n") }
|
59
|
+
sleep time
|
60
|
+
new_arry = File.open(LINUX_CPU_STAT, 'r') { |f| f.read.split("\n") }
|
61
|
+
0.upto old_arry.length do |cnt|
|
62
|
+
if old_arry[cnt] =~ /^cpu/
|
63
|
+
the_cpu_usage = get_one_cpu_usage old_arry[cnt].strip, new_arry[cnt].strip
|
64
|
+
result << the_cpu_usage
|
65
|
+
end
|
66
|
+
end
|
67
|
+
result
|
68
|
+
end
|
69
|
+
|
70
|
+
def get_system_mem_usage
|
71
|
+
mem_array = File.open(LINUX_MEM_STAT, 'r') { |f| f.read.split("\n") }
|
72
|
+
total_mem = 0
|
73
|
+
available_mem = 0
|
74
|
+
mem_array.each do |one_line|
|
75
|
+
line = one_line.strip.downcase
|
76
|
+
case line
|
77
|
+
when /MemTotal/i
|
78
|
+
total_mem = line.gsub(/\D/, '').to_i
|
79
|
+
debug "total mem #{total_mem}"
|
80
|
+
when /MemFree/i
|
81
|
+
available_mem = line.gsub(/\D/, '').to_i
|
82
|
+
debug "available_mem #{available_mem}"
|
83
|
+
else
|
84
|
+
trace "line[#{line}]"
|
85
|
+
end
|
86
|
+
end
|
87
|
+
|
88
|
+
if total_mem > 0
|
89
|
+
((total_mem - available_mem) / total_mem.to_f * 100).round 3
|
90
|
+
else
|
91
|
+
error "can not get total mem from #{mem_array}"
|
92
|
+
nil
|
93
|
+
end
|
94
|
+
end
|
95
|
+
|
96
|
+
|
97
|
+
def get_one_disk_status(old_line, new_line, delta_time)
|
98
|
+
old_arr = old_line.split(/\s+/)
|
99
|
+
new_arr = new_line.split(/\s+/)
|
100
|
+
new = new_arr.drop(2)
|
101
|
+
old = old_arr.drop(2)
|
102
|
+
block_size = File.open("/sys/dev/block/#{new_arr[0]}:#{new_arr[1]}/queue/logical_block_size", 'r') { |f| f.read }.to_i
|
103
|
+
if old[0] != new[0]
|
104
|
+
error "wrong dis check line old[#{old_line}] new[#{new_line}]"
|
105
|
+
end
|
106
|
+
r_iops = ((new[1].to_i - old[1].to_i) / delta_time).round(2)
|
107
|
+
w_iops = ((new[5].to_i - old[5].to_i) / delta_time).round(2)
|
108
|
+
r_sec_s = ((new[3].to_i - old[3].to_i) / delta_time).round(2)
|
109
|
+
w_sec_s = ((new[7].to_i - old[7].to_i) / delta_time).round(2)
|
110
|
+
debug "r_iops[#{r_iops}],w_iops[#{w_iops}],r_sec_s[#{r_sec_s}],w_sec_s[#{w_sec_s}],block_size[#{block_size}]"
|
111
|
+
r_kb_s = r_sec_s * block_size / 8
|
112
|
+
w_kb_s = w_sec_s * block_size / 8
|
113
|
+
if r_iops == 0
|
114
|
+
r_size = 0
|
115
|
+
else
|
116
|
+
r_size = r_kb_s / r_iops
|
117
|
+
end
|
118
|
+
if w_iops == 0
|
119
|
+
w_size = 0
|
120
|
+
else
|
121
|
+
w_size = w_kb_s / w_iops
|
122
|
+
end
|
123
|
+
{
|
124
|
+
'disk' => new[0],
|
125
|
+
'r_io/s' => r_iops,
|
126
|
+
'w_io/s' => w_iops,
|
127
|
+
'r_kB/s' => r_kb_s,
|
128
|
+
'w_kB/s' => w_kb_s,
|
129
|
+
'r_size' => r_size,
|
130
|
+
'w_size' => w_size
|
131
|
+
}
|
132
|
+
end
|
133
|
+
|
134
|
+
def get_disk_hash
|
135
|
+
real_disk = {}
|
136
|
+
disk_array = File.open(LINUX_DISK_PARTITION_STAT, 'r') { |f| f.read.split("\n") }
|
137
|
+
0.upto(disk_array.length-1) do |cnt|
|
138
|
+
unless cnt == 0
|
139
|
+
one_arr = disk_array[cnt].strip.split(/\s+/)
|
140
|
+
trace "str[#{disk_array[cnt].strip}]one arr #{one_arr}"
|
141
|
+
if one_arr[1] == '0'
|
142
|
+
if real_disk[one_arr[3]]
|
143
|
+
warn "already get disk #{one_arr[3]} id #{real_disk[one_arr[3]]}"
|
144
|
+
else
|
145
|
+
real_disk[one_arr[3]] = one_arr[0]
|
146
|
+
end
|
147
|
+
end
|
148
|
+
end
|
149
|
+
end
|
150
|
+
real_disk
|
151
|
+
end
|
152
|
+
|
153
|
+
def get_io_status(time=5)
|
154
|
+
result = []
|
155
|
+
disk_hash = get_disk_hash
|
156
|
+
old_disk_arr = File.open(LINUX_DISK_STAT, 'r') { |f| f.read.split("\n") }.each { |one| one.strip! }
|
157
|
+
old_time = Time.now.to_i
|
158
|
+
sleep time
|
159
|
+
new_disk_arr = File.open(LINUX_DISK_STAT, 'r') { |f| f.read.split("\n") }.each { |one| one.strip! }
|
160
|
+
new_time = Time.now.to_i
|
161
|
+
disk_hash.each do |one_disk, disk_id|
|
162
|
+
0.upto(old_disk_arr.length - 1) do |cnt|
|
163
|
+
if old_disk_arr[cnt] =~ Regexp.new("^#{disk_id}\\s+0\\s+#{one_disk}")
|
164
|
+
result << get_one_disk_status(old_disk_arr[cnt], new_disk_arr[cnt], (new_time - old_time))
|
165
|
+
end
|
166
|
+
end
|
167
|
+
end
|
168
|
+
result
|
169
|
+
end
|
170
|
+
|
171
|
+
def get_net_name(filepath=SYS_NET_PATH)
|
172
|
+
net_name = []
|
173
|
+
if File.directory?(filepath)
|
174
|
+
Dir.foreach(filepath) do |filename|
|
175
|
+
if filename != "." and filename != ".." and filename != "lo"
|
176
|
+
net_name << filename
|
177
|
+
end
|
178
|
+
end
|
179
|
+
else
|
180
|
+
puts "Files:" + filepath
|
181
|
+
end
|
182
|
+
net_name
|
183
|
+
end
|
184
|
+
|
185
|
+
|
186
|
+
def get_net_bytes
|
187
|
+
net_name = get_net_name
|
188
|
+
hash_tmp = {}
|
189
|
+
net_name.each do |one_name|
|
190
|
+
hash_tmp[one_name] = {}
|
191
|
+
File.open("/sys/class/net/#{one_name}/statistics/tx_bytes", 'r') do |f|
|
192
|
+
hash_tmp[one_name]['tx_bytes'] = f.read.strip
|
193
|
+
end
|
194
|
+
File.open("/sys/class/net/#{one_name}/statistics/rx_bytes", 'r') do |f|
|
195
|
+
hash_tmp[one_name]['rx_bytes'] = f.read.strip
|
196
|
+
end
|
197
|
+
end
|
198
|
+
hash_tmp
|
199
|
+
end
|
200
|
+
|
201
|
+
def get_system_net_speed
|
202
|
+
old_hash = get_net_bytes
|
203
|
+
puts old_hash
|
204
|
+
|
205
|
+
sleep 1
|
206
|
+
new_hash = get_net_bytes
|
207
|
+
puts new_hash
|
208
|
+
hash_tmp = {}
|
209
|
+
new_hash.each do |key, value|
|
210
|
+
hash_tmp[key] = {}
|
211
|
+
hash_tmp[key]['tx_bytes'] = value['tx_bytes'].to_i - old_hash[key]['tx_bytes'].to_i
|
212
|
+
hash_tmp[key]['rx_bytes'] = value['rx_bytes'].to_i - old_hash[key]['rx_bytes'].to_i
|
213
|
+
end
|
214
|
+
hash_tmp
|
215
|
+
end
|
216
|
+
|
217
|
+
def get_common_performance(monitor_cfg)
|
218
|
+
performance_hash = {}
|
219
|
+
performance_hash['cpu'] = get_system_cpu_usage[0]['total']
|
220
|
+
performance_hash['memory'] = get_system_mem_usage
|
221
|
+
|
222
|
+
ret_disk_arr = get_io_status
|
223
|
+
if ret_disk_arr
|
224
|
+
performance_hash['r_iops'] = ret_disk_arr[0]['r_io/s']
|
225
|
+
performance_hash['w_iops'] = ret_disk_arr[0]['w_io/s']
|
226
|
+
performance_hash['r_kBps'] = ret_disk_arr[0]['r_kB/s']
|
227
|
+
performance_hash['w_kBps'] = ret_disk_arr[0]['w_kB/s']
|
228
|
+
end
|
229
|
+
|
230
|
+
#get_system_net_speed获取所有网卡的传输速率,前台仅显示eth0的速率
|
231
|
+
ret_net_hash = get_system_net_speed
|
232
|
+
if ret_net_hash
|
233
|
+
monitor_cfg['net_if_ids'].each do |one_if_id|
|
234
|
+
if ret_net_hash["eth#{one_if_id}"]
|
235
|
+
performance_hash['net_tx_bytes'] = ret_net_hash["eth#{one_if_id}"]['tx_bytes']
|
236
|
+
performance_hash['net_rx_bytes'] = ret_net_hash["eth#{one_if_id}"]['rx_bytes']
|
237
|
+
end
|
238
|
+
end
|
239
|
+
end
|
240
|
+
performance_hash
|
241
|
+
end
|
242
|
+
|
243
|
+
|
244
|
+
def self.included(base)
|
245
|
+
base.extend self
|
246
|
+
end
|
247
|
+
|
248
|
+
module_function :get_system_cpu_usage
|
249
|
+
end
|
250
|
+
end
|
@@ -0,0 +1,145 @@
|
|
1
|
+
# -*- encoding : utf-8 -*-
|
2
|
+
require "#{File.join(File.dirname(__FILE__), '../ffi', 'win32_kernel32')}"
|
3
|
+
module WoolenCommon
|
4
|
+
module WindowsMonitor
|
5
|
+
|
6
|
+
MIB_IFROW = 4
|
7
|
+
MIB_IFROW_SIZE = 860 #MIB_IFROW结构体大小
|
8
|
+
MIB_IFROW_dwIndex = 512
|
9
|
+
MIB_IFROW_OutOctets = 576 #dwOutOctets位置偏移
|
10
|
+
MIB_IFROW_InOctets = 552 #dwInOctets位置偏移
|
11
|
+
|
12
|
+
MIB_IFTABLE_SIZE = 28392 #MIB_IFTABLE结构体大小
|
13
|
+
|
14
|
+
IP_ADAPTER_ADDRESSES_SIZE = 4044 #IP_ADAPTER_ADDRESSES结构体大小
|
15
|
+
FRIENDLY_NAME = 26412 #本地连接字符串转成short
|
16
|
+
IP_ADAPTER_ADDRESSES_FriendlyName = 40 #接口名称偏移
|
17
|
+
IP_ADAPTER_ADDRESSES_IfIndex = 4 #接口索引偏移
|
18
|
+
IP_ADAPTER_ADDRESSES_Next = 8 #下一个结构体Next字段偏移
|
19
|
+
|
20
|
+
|
21
|
+
def compare_file_time(time1, time2)
|
22
|
+
a = time1[:dwHighDateTime] << 32 | time1[:dwLowDateTime]
|
23
|
+
b = time2[:dwHighDateTime] << 32 | time2[:dwLowDateTime]
|
24
|
+
b - a
|
25
|
+
end
|
26
|
+
|
27
|
+
def get_system_cpu_usage(time=1)
|
28
|
+
p_idle_time = FFI::MemoryPointer.new(Win32Kernel32::Filetime.size)
|
29
|
+
p_kernel_time = FFI::MemoryPointer.new(Win32Kernel32::Filetime.size)
|
30
|
+
p_user_time = FFI::MemoryPointer.new(Win32Kernel32::Filetime.size)
|
31
|
+
idle_time = Win32Kernel32::Filetime.new p_idle_time
|
32
|
+
kernel_time = Win32Kernel32::Filetime.new p_kernel_time
|
33
|
+
usr_time = Win32Kernel32::Filetime.new p_user_time
|
34
|
+
p_pre_idle_time = FFI::MemoryPointer.new(Win32Kernel32::Filetime.size)
|
35
|
+
p_pre_kernel_time = FFI::MemoryPointer.new(Win32Kernel32::Filetime.size)
|
36
|
+
p_pre_user_time = FFI::MemoryPointer.new(Win32Kernel32::Filetime.size)
|
37
|
+
pre_idle_time = Win32Kernel32::Filetime.new p_pre_idle_time
|
38
|
+
pre_kernel_time = Win32Kernel32::Filetime.new p_pre_kernel_time
|
39
|
+
pre_usr_time = Win32Kernel32::Filetime.new p_pre_user_time
|
40
|
+
Win32Kernel32.getSystemTimes(p_pre_idle_time, p_pre_kernel_time, p_pre_user_time)
|
41
|
+
sleep time
|
42
|
+
Win32Kernel32.getSystemTimes(p_idle_time, p_kernel_time, p_user_time)
|
43
|
+
idle = compare_file_time(pre_idle_time, idle_time)
|
44
|
+
ker = compare_file_time(pre_kernel_time, kernel_time)
|
45
|
+
usr = compare_file_time(pre_usr_time, usr_time)
|
46
|
+
(ker + usr - idle) *100 / (ker + usr)
|
47
|
+
end
|
48
|
+
|
49
|
+
def get_system_mem_usage
|
50
|
+
p_ms_adder= FFI::MemoryPointer.new(Win32Kernel32::MemoryStatus.size)
|
51
|
+
p_ms = Win32Kernel32::MemoryStatus.new p_ms_adder
|
52
|
+
Win32Kernel32.globalMemoryStatus(p_ms);
|
53
|
+
p_ms[:dwMemoryLoad]
|
54
|
+
end
|
55
|
+
|
56
|
+
def get_system_disk_info
|
57
|
+
sz_driver = FFI::MemoryPointer.from_string('\\\\.\\PhysicalDrive0')
|
58
|
+
ptr1 = FFI::MemoryPointer.new(:uint)
|
59
|
+
ptr2 = FFI::MemoryPointer.new(:uint)
|
60
|
+
junk1 = 0
|
61
|
+
junk2 = 0
|
62
|
+
p_disk_perform_adder1= FFI::MemoryPointer.new(Win32Kernel32::Disk_Perfomance.size)
|
63
|
+
p_disk_perform1 = Win32Kernel32::Disk_Perfomance.new p_disk_perform_adder1
|
64
|
+
h_device = Win32Kernel32.createFileA(sz_driver, 0, Win32Kernel32::OPEN_EXISTING, 0, Win32Kernel32::OPEN_EXISTING, 0, 0)
|
65
|
+
Win32Kernel32.deviceIoControl(h_device, Win32Kernel32::IOCTL_DISK_PERFORMANCE, 0, 0, p_disk_perform1, Win32Kernel32::Disk_Perfomance.size, ptr1, 0);
|
66
|
+
puts "BytesRead:#{p_disk_perform1[:BytesRead]} BytesWritten:#{p_disk_perform1[:BytesWritten]} ReadCount:#{p_disk_perform1[:ReadCount]} WriteCount:#{p_disk_perform1[:WriteCount]}"
|
67
|
+
sleep 1
|
68
|
+
p_disk_perform_adder2= FFI::MemoryPointer.new(Win32Kernel32::Disk_Perfomance.size)
|
69
|
+
p_disk_perform2 = Win32Kernel32::Disk_Perfomance.new p_disk_perform_adder2
|
70
|
+
Win32Kernel32.deviceIoControl(h_device, Win32Kernel32::IOCTL_DISK_PERFORMANCE, 0, 0, p_disk_perform2, Win32Kernel32::Disk_Perfomance.size, ptr2, 0);
|
71
|
+
#puts "BytesRead:#{p_disk_perform2[:BytesRead]} BytesWritten:#{p_disk_perform2[:BytesWritten]} ReadCount:#{p_disk_perform2[:ReadCount]} WriteCount:#{p_disk_perform2[:WriteCount]}"
|
72
|
+
ret_disk_arr =[]
|
73
|
+
ret_disk_arr << (p_disk_perform2[:BytesRead] - p_disk_perform1[:BytesRead])/1000.to_f
|
74
|
+
ret_disk_arr << (p_disk_perform2[:BytesWritten] - p_disk_perform1[:BytesWritten])/1000.to_f
|
75
|
+
ret_disk_arr << p_disk_perform2[:ReadCount] - p_disk_perform1[:ReadCount]
|
76
|
+
ret_disk_arr << p_disk_perform2[:WriteCount] - p_disk_perform1[:WriteCount]
|
77
|
+
ret_disk_arr
|
78
|
+
end
|
79
|
+
|
80
|
+
|
81
|
+
#返回数组发送字节/S和接受字节每秒
|
82
|
+
def get_system_network_info(net_if_ids)
|
83
|
+
ret_arr = []
|
84
|
+
net_if_ids.each do |one_if_id|
|
85
|
+
pre_net_arr = get_network_info_by_index(one_if_id.to_i)
|
86
|
+
sleep 1
|
87
|
+
next_net_arr = get_network_info_by_index(one_if_id.to_i)
|
88
|
+
ret_arr << (next_net_arr[0] - pre_net_arr[0]) #发送字节数/秒
|
89
|
+
ret_arr << (next_net_arr[1] - pre_net_arr[1]) #接收字节数/秒
|
90
|
+
end
|
91
|
+
ret_arr
|
92
|
+
end
|
93
|
+
|
94
|
+
#通过本地连接的索引获取本地连接接口发送和接受字节数组
|
95
|
+
def get_network_info_by_index(index=14)
|
96
|
+
net_arr = []
|
97
|
+
ptr_iftable = FFI::MemoryPointer.new(MIB_IFTABLE_SIZE)
|
98
|
+
iftable_size_ptr = FFI::MemoryPointer.new(:uint)
|
99
|
+
iftable_size = MIB_IFTABLE_SIZE
|
100
|
+
iftable_size_ptr.write_uint iftable_size
|
101
|
+
iftable_size_ptr.read_uint
|
102
|
+
dw_ret = Win32Kernel32.getIfTable(ptr_iftable, iftable_size_ptr, 0)
|
103
|
+
dw_num = ptr_iftable.read_uint
|
104
|
+
pifrow_start = (ptr_iftable + MIB_IFROW)
|
105
|
+
pifrow_end = pifrow_start + dw_num * MIB_IFROW_SIZE
|
106
|
+
while pifrow_start.address < pifrow_end.address do
|
107
|
+
if_index = (pifrow_start + MIB_IFROW_dwIndex).read_uint
|
108
|
+
if if_index == index
|
109
|
+
out_bytes = (pifrow_start + MIB_IFROW_OutOctets).read_uint
|
110
|
+
in_bytes = (pifrow_start + MIB_IFROW_InOctets).read_uint
|
111
|
+
net_arr << out_bytes
|
112
|
+
net_arr << in_bytes
|
113
|
+
break
|
114
|
+
end
|
115
|
+
pifrow_start = pifrow_start + MIB_IFROW_SIZE
|
116
|
+
end
|
117
|
+
net_arr
|
118
|
+
|
119
|
+
end
|
120
|
+
|
121
|
+
def get_common_performance(monitor_cfg)
|
122
|
+
performance_hash = {}
|
123
|
+
performance_hash['cpu'] = get_system_cpu_usage
|
124
|
+
performance_hash['memory'] = get_system_mem_usage
|
125
|
+
|
126
|
+
ret_disk = get_system_disk_info
|
127
|
+
if ret_disk
|
128
|
+
performance_hash['r_kBps'] = ret_disk[0]
|
129
|
+
performance_hash['w_kBps'] = ret_disk[1]
|
130
|
+
performance_hash['r_iops'] = ret_disk[2]
|
131
|
+
performance_hash['w_iops'] = ret_disk[3]
|
132
|
+
end
|
133
|
+
net_disk = get_system_network_info(monitor_cfg['net_if_ids'])
|
134
|
+
if net_disk
|
135
|
+
performance_hash['net_tx_bytes'] = net_disk[0]
|
136
|
+
performance_hash['net_rx_bytes'] = net_disk[1]
|
137
|
+
end
|
138
|
+
performance_hash
|
139
|
+
end
|
140
|
+
|
141
|
+
def self.included(base)
|
142
|
+
base.extend self
|
143
|
+
end
|
144
|
+
end
|
145
|
+
end
|
@@ -0,0 +1,42 @@
|
|
1
|
+
# -*- encoding : utf-8 -*-
|
2
|
+
require "#{File.join(File.dirname(__FILE__), 'logger')}"
|
3
|
+
module WoolenCommon
|
4
|
+
class TypeHelper
|
5
|
+
class << self
|
6
|
+
include WoolenCommon::ToolLogger
|
7
|
+
def to_signed(number, bits)
|
8
|
+
mask = (1 << (bits - 1))
|
9
|
+
(number & ~mask) - (number & mask)
|
10
|
+
end
|
11
|
+
|
12
|
+
def to_unsigned(number, bits)
|
13
|
+
mask_high = (1 << (bits - 1))
|
14
|
+
mask_low = 0
|
15
|
+
(bits-1).times do |step_bit|
|
16
|
+
mask_low += (1 << (step_bit))
|
17
|
+
end
|
18
|
+
high = number & mask_high
|
19
|
+
low = number & mask_low
|
20
|
+
#debug "to unsigned #{high},#{low}"
|
21
|
+
high + low
|
22
|
+
end
|
23
|
+
|
24
|
+
def get_high_bit_num(number,all_bits,high_start_bits)
|
25
|
+
bits_mask = 0
|
26
|
+
(high_start_bits..all_bits-1).each do |step_bit|
|
27
|
+
bits_mask += (1 << (step_bit))
|
28
|
+
end
|
29
|
+
number & bits_mask
|
30
|
+
end
|
31
|
+
|
32
|
+
|
33
|
+
def get_low_bit_num(number,hight_start_bits)
|
34
|
+
bits_mask = 0
|
35
|
+
(0..hight_start_bits-1).each do |step_bit|
|
36
|
+
bits_mask += (1 << (step_bit))
|
37
|
+
end
|
38
|
+
number & bits_mask
|
39
|
+
end
|
40
|
+
end
|
41
|
+
end
|
42
|
+
end
|
@@ -0,0 +1,92 @@
|
|
1
|
+
# -*- encoding : utf-8 -*-
|
2
|
+
require "#{File.join(File.dirname(__FILE__), 'abstract_middleware','builder')}"
|
3
|
+
require "#{File.join(File.dirname(__FILE__), 'abstract_middleware','runner')}"
|
4
|
+
require "#{File.join(File.dirname(__FILE__), 'logger')}"
|
5
|
+
module WoolenCommon
|
6
|
+
# 根据某个目录下的源程序,生成中间件调用队列
|
7
|
+
class VerCtrlMiddleWare
|
8
|
+
include WoolenCommon::ToolLogger
|
9
|
+
attr_accessor :ver_match_hash
|
10
|
+
def initialize(class_file_dir,&ver_ctrl_block)
|
11
|
+
debug "init the AbstractMiddleWare with class dir ::#{class_file_dir}"
|
12
|
+
@ver_match_hash = {}
|
13
|
+
if File.directory? class_file_dir
|
14
|
+
@class_dir = Dir.new(class_file_dir)
|
15
|
+
else
|
16
|
+
fatal "目录:#{class_file_dir}不存在,无法从中加载相关中间件代码"
|
17
|
+
end
|
18
|
+
@class_dir.each do |one_file|
|
19
|
+
file_str = File.join(File.realdirpath(@class_dir),one_file)
|
20
|
+
if File.directory? file_str
|
21
|
+
trace "file_str :#{file_str} 是个目录"
|
22
|
+
next
|
23
|
+
end
|
24
|
+
# 只加载ruby源程序
|
25
|
+
if File.extname(file_str) == '.rb' || File.extname(file_str) == '.ruby'
|
26
|
+
base_file_name = File.basename(file_str,(File.extname(file_str)))
|
27
|
+
class_name = base_file_name.split('_').map!{|k| k.capitalize}.join ''
|
28
|
+
trace "加载文件:#{file_str},获得的类名是:#{class_name}"
|
29
|
+
File.open(file_str,'r') do |file|
|
30
|
+
self.instance_eval file.read,file_str
|
31
|
+
end
|
32
|
+
#self.instance_eval "load '#{file_str}',true;"
|
33
|
+
k_class = self.instance_eval class_name
|
34
|
+
if block_given?
|
35
|
+
int_ver = ver_ctrl_block.call k_class::VERSION
|
36
|
+
else
|
37
|
+
int_ver = k_class::VERSION.to_i
|
38
|
+
end
|
39
|
+
trace "file:#{file_str},version:#{k_class::VERSION},int_version:#{int_ver}"
|
40
|
+
if @ver_match_hash[int_ver]
|
41
|
+
warn "int_version :#{int_ver} 已经有类了:#{@ver_match_hash[int_ver]},略过当前类的加入:#{k_class}"
|
42
|
+
next
|
43
|
+
else
|
44
|
+
@ver_match_hash[int_ver] = k_class
|
45
|
+
end
|
46
|
+
else
|
47
|
+
warn "目录:#{@class_dir.to_s}下的文件:#{file_str}不是ruby代码"
|
48
|
+
end
|
49
|
+
end
|
50
|
+
trace "获取到的hash sort后是:#{@ver_match_hash.sort}"
|
51
|
+
end
|
52
|
+
|
53
|
+
def add_ver_class(int_ver,k_class,force=false)
|
54
|
+
if int_ver.is_a? Integer
|
55
|
+
if @ver_match_hash[int_ver]
|
56
|
+
warn "尝试添加一个ver:#{int_ver}已经存在的k_clas:#{@ver_match_hash[int_ver]},尝试添加的class:#{k_class}"
|
57
|
+
if force
|
58
|
+
info "需要强制添加类:#{k_class}"
|
59
|
+
@ver_match_hash[int_ver] = k_class
|
60
|
+
end
|
61
|
+
else
|
62
|
+
@ver_match_hash[int_ver] = k_class
|
63
|
+
end
|
64
|
+
else
|
65
|
+
warn "ver 不是integen:#{int_ver},要添加的class:#{k_class}"
|
66
|
+
end
|
67
|
+
end
|
68
|
+
|
69
|
+
def del_ver_class_by_ver(int_ver)
|
70
|
+
if int_ver.is_a? Integer
|
71
|
+
if @ver_match_hash[int_ver]
|
72
|
+
debug "删除一个ver:#{int_ver}"
|
73
|
+
@ver_match_hash.delete int_ver
|
74
|
+
else
|
75
|
+
debug "ver :#{int_ver} 不存在"
|
76
|
+
end
|
77
|
+
else
|
78
|
+
warn "ver不是integen:#{int_ver}"
|
79
|
+
end
|
80
|
+
end
|
81
|
+
|
82
|
+
def call(*args,&block)
|
83
|
+
middle_ware_stack = Middleware::Builder.new
|
84
|
+
sort_middle_ware_array = @ver_match_hash.sort
|
85
|
+
sort_middle_ware_array.reverse.each do |int_ver,k_class|
|
86
|
+
trace "添加ver:#{int_ver} 的class:#{k_class}"
|
87
|
+
middle_ware_stack.use k_class,*args,&block
|
88
|
+
end
|
89
|
+
middle_ware_stack.call binding
|
90
|
+
end
|
91
|
+
end
|
92
|
+
end
|