我有以下日志文件,正在使用NodeJS编写日志监控。我的解析器基于Regex,所以如果我在日志文件中收到可以翻译成多个日志消息的新行,我应该能够提取它们。因为我需要一个rtegex来提取variouys日志消息
Fri Jan 24 05:28:57 2014
MEMORY_TARGET defaulting to 1128267776.
* instance_number obtained from CSS = 1, checking for the existence of node 0...
* node 0 does not exist. instance_number = 1
Starting ORACLE instance (normal)
LICENSE_MAX_SESSION = 0
LICENSE_SESSIONS_WARNING = 0
Initial number of CPU is 48
Number of processor cores in the system is 24
Number of processor sockets in the system is 12
Private Interface 'nxge20:1' configured from GPnP for use as a private interconnect.
abc
[name='nxge20:1', type=1, ip=169.254.121.29, mac=00-21-28-0e-8c-ae-00-00-00-00-00-00-00-00-00-00-00-2f-ff-ff, net=169.254.0.0/16, mask=255.255.0.0, use=haip:cluster_interconnect/62]
Public Interface 'nxge0' configured from GPnP for use as a public interface.
[name='nxge0', type=1, ip=172.20.70.18, mac=00-21-28-0e-94-ce-00-00-00-00-00-00-00-00-00-00-00-2f-ff-ff, net=172.20.70.0/24, mask=255.255.255.0, use=public/1]
Public Interface 'nxge21' configured from GPnP for use as a public interface.
[name='nxge21', type=1, ip=100.100.100.1, mac=00-21-28-0e-8c-af-00-00-00-00-00-00-00-00-00-00-00-2f-ff-ff, net=100.100.100.0/23, mask=255.255.254.0, use=public/1]
Shared memory segment for instance monitoring created
CELL communication is configured to use 0 interface(s):
CELL IP affinity details:
NUMA status: NUMA system w/ 3 process groups
cellaffinity.ora status: cannot find affinity map at '/etc/oracle/cell/network-config/cellaffinity.ora' (see trace file for details)
CELL communication will use 1 IP group(s):
Grp 0:
Picked latch-free SCN scheme 3
Using LOG_ARCHIVE_DEST_1 parameter default value as /dbtop/grid/dbs/arch
Autotune of undo retention is turned on.
LICENSE_MAX_USERS = 0
SYS auditing is disabled
NOTE: Volume support enabled
NUMA system with 3 nodes detected
Starting up:
Oracle Database 11g Enterprise Edition Release 11.2.0.4.0 - 64bit Production
With the Real Application Clusters and Automatic Storage Management options.
ORACLE_HOME = /dbtop/grid
System name: SunOS
Node name: sec33-e
Release: 5.10
Version: Generic_142909-17
Machine: sun4u
Using parameter settings in server-side spfile /dbtop/asm/dbs/spfile+ASM.ora
System parameters with non-default values:
large_pool_size = 12M
instance_type = "asm"
remote_login_passwordfile= "EXCLUSIVE"
asm_diskstring = "/dev/rdsk/*"
asm_diskgroups = "WFREC"
asm_diskgroups = "WFDATA"
asm_power_limit = 7
diagnostic_dest = "/dbtop/app/oracle"
Cluster communication is configured to use the following interface(s) for this instance
169.254.121.29
cluster interconnect IPC version:Oracle UDP/IP (generic)
IPC Vendor 1 proto 2
Fri Jan 24 05:29:03 2014
PMON started with pid=2, OS id=17520
Fri Jan 24 05:29:04 2014
PSP0 started with pid=3, OS id=17521
Fri Jan 24 05:29:05 2014
VKTM started with pid=4, OS id=17522 at elevated priority
VKTM running at (10)millisec precision with DBRM quantum (100)ms
我尝试使用以下正则表达式匹配数据时间并放置在组中:
(\w{1,3} \w{1,3} \d{1,2} \d{1,2}:\d{1,2}:\d{1,2} \d{4})
与时间戳匹配,如何在第二组中抓取其余的日志消息
答案 0 :(得分:1)
您可以使用
/^([a-z]{1,3} [a-z]{1,3} \d{1,2} \d{1,2}:\d{1,2}:\d{1,2} \d{4})\r?\n(.*(?:\r?\n(?![a-z]{1,3} [a-z]{1,3} \d{1,2} \d{1,2}:\d{1,2}:\d{1,2} \d{4}).*)*)/igm
请参阅regex demo。
模式包括您的模式以及与该模式或文本结尾匹配的附加部分。
^
- 开始行([a-z]{1,3} [a-z]{1,3} \d{1,2} \d{1,2}:\d{1,2}:\d{1,2} \d{4})
- 与时间戳匹配的第1组\r?\n
- 换行符(.*(?:\r?\n(?![a-z]{1,3} [a-z]{1,3} \d{1,2} \d{1,2}:\d{1,2}:\d{1,2} \d{4}).*)*)
- 第2组捕获0+序列:
.*
- 任意多个字符,尽可能多的换行符号(?:\r?\n(?![a-z]{1,3} [a-z]{1,3} \d{1,2} \d{1,2}:\d{1,2}:\d{1,2} \d{4}).*)*
- 零次或多次出现:
\r?\n(?![a-z]{1,3} [a-z]{1,3} \d{1,2} \d{1,2}:\d{1,2}:\d{1,2} \d{4})
- 没有时间戳模式的换行符.*
- 任意多个字符,尽可能多的换行符。JS演示:
var rx = /^([a-z]{1,3} [a-z]{1,3} \d{1,2} \d{1,2}:\d{1,2}:\d{1,2} \d{4})\r?\n(.*(?:\r?\n(?![a-z]{1,3} [a-z]{1,3} \d{1,2} \d{1,2}:\d{1,2}:\d{1,2} \d{4}).*)*)/gmi;
var str = "Fri Jan 24 05:28:57 2014\r\nMEMORY_TARGET defaulting to 1128267776.\r\n* instance_number obtained from CSS = 1, checking for the existence of node 0... \r\n* node 0 does not exist. instance_number = 1 \r\nStarting ORACLE instance (normal)\r\nLICENSE_MAX_SESSION = 0\r\nLICENSE_SESSIONS_WARNING = 0\r\nInitial number of CPU is 48\r\nNumber of processor cores in the system is 24\r\nNumber of processor sockets in the system is 12\r\nPrivate Interface 'nxge20:1' configured from GPnP for use as a private interconnect.\r\nabc\r\n [name='nxge20:1', type=1, ip=169.254.121.29, mac=00-21-28-0e-8c-ae-00-00-00-00-00-00-00-00-00-00-00-2f-ff-ff, net=169.254.0.0/16, mask=255.255.0.0, use=haip:cluster_interconnect/62]\r\nPublic Interface 'nxge0' configured from GPnP for use as a public interface.\r\n [name='nxge0', type=1, ip=172.20.70.18, mac=00-21-28-0e-94-ce-00-00-00-00-00-00-00-00-00-00-00-2f-ff-ff, net=172.20.70.0/24, mask=255.255.255.0, use=public/1]\r\nPublic Interface 'nxge21' configured from GPnP for use as a public interface.\r\n [name='nxge21', type=1, ip=100.100.100.1, mac=00-21-28-0e-8c-af-00-00-00-00-00-00-00-00-00-00-00-2f-ff-ff, net=100.100.100.0/23, mask=255.255.254.0, use=public/1]\r\nShared memory segment for instance monitoring created\r\nCELL communication is configured to use 0 interface(s):\r\nCELL IP affinity details:\r\n NUMA status: NUMA system w/ 3 process groups\r\n cellaffinity.ora status: cannot find affinity map at '/etc/oracle/cell/network-config/cellaffinity.ora' (see trace file for details)\r\nCELL communication will use 1 IP group(s):\r\n Grp 0: \r\nPicked latch-free SCN scheme 3\r\nUsing LOG_ARCHIVE_DEST_1 parameter default value as /dbtop/grid/dbs/arch\r\nAutotune of undo retention is turned on. \r\nLICENSE_MAX_USERS = 0\r\nSYS auditing is disabled\r\nNOTE: Volume support enabled\r\nNUMA system with 3 nodes detected\r\nStarting up:\r\nOracle Database 11g Enterprise Edition Release 11.2.0.4.0 - 64bit Production\r\nWith the Real Application Clusters and Automatic Storage Management options.\r\nORACLE_HOME = /dbtop/grid\r\nSystem name: SunOS\r\nNode name: sec33-e\r\nRelease: 5.10\r\nVersion: Generic_142909-17\r\nMachine: sun4u\r\nUsing parameter settings in server-side spfile /dbtop/asm/dbs/spfile+ASM.ora\r\nSystem parameters with non-default values:\r\n large_pool_size = 12M\r\n instance_type = \"asm\"\r\n remote_login_passwordfile= \"EXCLUSIVE\"\r\n asm_diskstring = \"/dev/rdsk/*\"\r\n asm_diskgroups = \"WFREC\"\r\n asm_diskgroups = \"WFDATA\"\r\n asm_power_limit = 7\r\n diagnostic_dest = \"/dbtop/app/oracle\"\r\nCluster communication is configured to use the following interface(s) for this instance\r\n 169.254.121.29\r\ncluster interconnect IPC version:Oracle UDP/IP (generic)\r\nIPC Vendor 1 proto 2\r\nFri Jan 24 05:29:03 2014\r\nPMON started with pid=2, OS id=17520 \r\nFri Jan 24 05:29:04 2014\r\nPSP0 started with pid=3, OS id=17521 \r\nFri Jan 24 05:29:05 2014\r\nVKTM started with pid=4, OS id=17522 at elevated priority\r\nVKTM running at (10)millisec precision with DBRM quantum (100)ms";
var m, res = [];
while ((m = rx.exec(str)) !== null) {
res.push([m[1], m[2]]);
}
console.log(res);