要使用 Ruby 处理一份日志,提取出来里面的 duration 字段的值,并且相加,得到总的数值。日志需要先处理,找出里面的 kafka 字段,取出其中的 json 串,再做处理。作为新手,跌跌撞撞的使用 Ruby 完成了一部分功能,越看越别扭,希望能指出其中的问题,是否还有其他更简洁优雅的实现方式,多谢。
日志文件为 test.log,内容已经做了处理,参考如下:
/disk/sata13/xxx-platform/2017_01_03/0.0.0.0/platform.07_30.log.0.0.0.0.2017_01_03.gz: /naga/v0.5.3-small: {"log":"[Naga 0.5.3 I 17-01-03 07:19:43] ddc7c32422a7a2e413346258034c39e6 - kafka:{\"media_uris\": [\"\/uploads\/audio\/120\/14833991837592.amr\"], \"status\": 200, \"tasks\": [\"transcode\"], \"request_time\": 1483399183, \"task_id\": \"ddc7c32422a7a2e413346258034cxxx39e6\", \"timestamp\": 1483399183, \"bucket\": \"xxx\", \"convtime\": 0.18000006675720215, \"notify_url\": \"http:\/\/www.xxx.net\/api\/upload\/xxx\", \"input_info\": {\"streams\": [{\"index\": 0, \"audio_channels\": 1, \"audio_samplerate\": 8000.0, \"bitrate\": 8800, \"codec_desc\": \"AMR-NB (Adaptive Multi-Rate NarrowBand)\", \"codec\": \"amrnb\", \"duration\": 0.029125000000000002, \"type\": \"audio\"}], \"format\": {\"duration\": 0.029125000000000002, \"fullname\": \"3GPP AMR\", \"bitrate\": 10437.0, \"filesize\": 38.0, \"format\": \"amr\"}}, \"uploads\": [\"\/uploads\/audio\/120\/14833991837592.mp3\"], \"starttime\": 1483399183, \"async\": true, \"media_info\": {\"streams\": [{\"index\": 0, \"audio_channels\": 1, \"audio_samplerate\": 8000.0, \"bitrate\": 64000, \"codec_desc\": \"MP3 (MPEG audio layer 3)\", \"codec\": \"mp3\", \"duration\": 0.216, \"type\": \"audio\"}], \"format\": {\"duration\": 0.216, \"fullname\": \"MP2\/3 (MPEG audio layer 2\/3)\", \"bitrate\": 87000.0, \"filesize\": 2349.0, \"format\": \"mp3\"}}, \"type\": \"audio\", \"options\": {\"ab\": \"64\", \"f\": \"mp3\"}, \"errmsg\": \"\"}","time":"2017\/01\/02 23:19:43 UTC","hostname":"bd39a20ecef9@xxx"}
/disk/sata13/xxxx-platform/2017_01_03/0.0.0.0/platform.07_30.log.0.0.0.0.2017_01_03.gz: /naga/v0.5.3-small: {"log":"[Naga 0.5.3 I 17-01-03 07:19:43] ddc7c32422a7a2e413346258034c39e6 - notify:message:{'bucket': 'xxx', 'notify_url': 'http:\/\/www.xxx.net\/api\/upload\/xxx', 'content_type': 'application\/x-www-form-urlencoded', 'operator': 'xxx', 'password': 'xxx', 'data': 'xxx'}","time":"2017\/01\/02 23:19:43 UTC","hostname":"bd39a20ecef9@xxx"}
/disk/sata13/xxx-platform/2017_01_03/0.0.0.0/platform.07_30.log.0.0.0.0.2017_01_03.gz: /naga/v0.5.3-small: {"log":"[Naga 0.5.3 I 17-01-03 07:19:43] ddc7c32422a7a2e413346258034c39e6 - kafka:{\"media_uris\": [\"\/uploads\/audio\/120\/14833991837592.amr\"], \"status\": 200, \"tasks\": [\"transcode\"], \"request_time\": 1483399183, \"task_id\": \"ddc7c32422a7a2e413346258034cxxx39e6\", \"timestamp\": 1483399183, \"bucket\": \"xxx\", \"convtime\": 0.18000006675720215, \"notify_url\": \"http:\/\/www.xxx.net\/api\/upload\/xxx\", \"input_info\": {\"streams\": [{\"index\": 0, \"audio_channels\": 1, \"audio_samplerate\": 8000.0, \"bitrate\": 8800, \"codec_desc\": \"AMR-NB (Adaptive Multi-Rate NarrowBand)\", \"codec\": \"amrnb\", \"duration\": 0.029125000000000002, \"type\": \"audio\"}], \"format\": {\"duration\": 0.029125000000000002, \"fullname\": \"3GPP AMR\", \"bitrate\": 10437.0, \"filesize\": 38.0, \"format\": \"amr\"}}, \"uploads\": [\"\/uploads\/audio\/120\/14833991837592.mp3\"], \"starttime\": 1483399183, \"async\": true, \"media_info\": {\"streams\": [{\"index\": 0, \"audio_channels\": 1, \"audio_samplerate\": 8000.0, \"bitrate\": 64000, \"codec_desc\": \"MP3 (MPEG audio layer 3)\", \"codec\": \"mp3\", \"duration\": 0.216, \"type\": \"audio\"}], \"format\": {\"duration\": 0.216, \"fullname\": \"MP2\/3 (MPEG audio layer 2\/3)\", \"bitrate\": 87000.0, \"filesize\": 2349.0, \"format\": \"mp3\"}}, \"type\": \"audio\", \"options\": {\"ab\": \"64\", \"f\": \"mp3\"}, \"errmsg\": \"\"}","time":"2017\/01\/02 23:19:43 UTC","hostname":"bd39a20ecef9@xxx"}
/disk/sata13/docker-platform/2017_01_03/0.0.0.0/platform.07_30.log.0.0.0.0.2017_01_03.gz: /naga/v0.5.3-small: {"log":"[Naga 0.5.3 I 17-01-03 07:19:43] ddc7c32422a7a2e413346258034c39e6 - notify:message:{'bucket': 'xxx', 'notify_url': 'http:\/\/www.xxx.net\/api\/upload\/xxx', 'content_type': 'application\/x-www-form-urlencoded', 'operator': 'xxx', 'password': 'xxx', 'data': 'xxx'}","time":"2017\/01\/02 23:19:43 UTC","hostname":"bd39a20ecef9@xxx"}
/disk/sata13/docker-platform/2017_01_03/0.0.0.0/platform.07_30.log.0.0.0.0.2017_01_03.gz: /naga/v0.5.3-small: {"log":"[Naga 0.5.3 I 17-01-03 07:19:43] ddc7c32422a7a2e413346258034c39e6 - notify:message:{'bucket': 'xxx', 'notify_url': 'http:\/\/www.xxx.net\/api\/upload\/xxx', 'content_type': 'application\/x-www-form-urlencoded', 'operator': 'xxx', 'password': 'xxx', 'data': 'xxx'}","time":"2017\/01\/02 23:19:43 UTC","hostname":"bd39a20ecef9@xxx"}
/disk/sata13/xxx-platform/2017_01_03/0.0.0.0/platform.07_30.log.0.0.0.0.2017_01_03.gz: /naga/v0.5.3-small: {"log":"[Naga 0.5.3 I 17-01-03 07:19:43] ddc7c32422a7a2e413346258034c39e6 - kafka:{\"media_uris\": [\"\/uploads\/audio\/120\/14833991837592.amr\"], \"status\": 200, \"tasks\": [\"transcode\"], \"request_time\": 1483399183, \"task_id\": \"ddc7c32422a7a2e413346258034cxxx39e6\", \"timestamp\": 1483399183, \"bucket\": \"xxx\", \"convtime\": 0.18000006675720215, \"notify_url\": \"http:\/\/www.xxx.net\/api\/upload\/xxx\", \"input_info\": {\"streams\": [{\"index\": 0, \"audio_channels\": 1, \"audio_samplerate\": 8000.0, \"bitrate\": 8800, \"codec_desc\": \"AMR-NB (Adaptive Multi-Rate NarrowBand)\", \"codec\": \"amrnb\", \"duration\": 0.029125000000000002, \"type\": \"audio\"}], \"format\": {\"duration\": 0.029125000000000002, \"fullname\": \"3GPP AMR\", \"bitrate\": 10437.0, \"filesize\": 38.0, \"format\": \"amr\"}}, \"uploads\": [\"\/uploads\/audio\/120\/14833991837592.mp3\"], \"starttime\": 1483399183, \"async\": true, \"media_info\": {\"streams\": [{\"index\": 0, \"audio_channels\": 1, \"audio_samplerate\": 8000.0, \"bitrate\": 64000, \"codec_desc\": \"MP3 (MPEG audio layer 3)\", \"codec\": \"mp3\", \"duration\": 0.216, \"type\": \"audio\"}], \"format\": {\"duration\": 0.216, \"fullname\": \"MP2\/3 (MPEG audio layer 2\/3)\", \"bitrate\": 87000.0, \"filesize\": 2349.0, \"format\": \"mp3\"}}, \"type\": \"audio\", \"options\": {\"ab\": \"64\", \"f\": \"mp3\"}, \"errmsg\": \"\"}","time":"2017\/01\/02 23:19:43 UTC","hostname":"bd39a20ecef9@xxx"}
/disk/sata13/docker-platform/2017_01_03/0.0.0.0/platform.07_30.log.0.0.0.0.2017_01_03.gz: /naga/v0.5.3-small: {"log":"[Naga 0.5.3 I 17-01-03 07:19:43] ddc7c32422a7a2e413346258034c39e6 - notify:message:{'bucket': 'xxx', 'notify_url': 'http:\/\/www.xxx.net\/api\/upload\/xxx', 'content_type': 'application\/x-www-form-urlencoded', 'operator': 'xxx', 'password': 'xxx', 'data': 'xxx'}","time":"2017\/01\/02 23:19:43 UTC","hostname":"bd39a20ecef9@xxx"}
代码如下:
#! /usr/bin/ruby
# something
require 'json'
File.open('test.log', 'r') do |f|
f.each do |line|
re = /(kafka:)({.+}(?=",))+/
match_data = re.match(line)
data = $2.gsub('\\', '') if match_data.class == MatchData
data1 = JSON.parse(data,{ symbolize_names: true }) if data.class == String
if data1.is_a? Hash
puts data1[:media_info][:format][:duration].to_f
end
end
end