【发布时间】:2019-08-21 16:05:10
【问题描述】:
我正在尝试处理包含多行事件的大型文本文件(500 MB - 2+ GB)并将它们发送到 VIA 系统日志。到目前为止,我的脚本似乎在很长一段时间内都运行良好,但过了一段时间,它导致 ISE(64 位)没有响应并耗尽了所有系统内存。
我也很好奇是否有办法提高速度,因为当前脚本仅以每秒约 300 个事件发送到 syslog。
示例数据
START--random stuff here
more random stuff on this new line
more stuff and things
START--some random things
additional random things
blah blah
START--data data more data
START--things
blah data
代码
Function SendSyslogEvent {
$Server = '1.1.1.1'
$Message = $global:Event
#0=EMERG 1=Alert 2=CRIT 3=ERR 4=WARNING 5=NOTICE 6=INFO 7=DEBUG
$Severity = '10'
#(16-23)=LOCAL0-LOCAL7
$Facility = '22'
$Hostname= 'ServerSyslogEvents'
# Create a UDP Client Object
$UDPCLient = New-Object System.Net.Sockets.UdpClient
$UDPCLient.Connect($Server, 514)
# Calculate the priority
$Priority = ([int]$Facility * 8) + [int]$Severity
#Time format the SW syslog understands
$Timestamp = Get-Date -Format "MMM dd HH:mm:ss"
# Assemble the full syslog formatted message
$FullSyslogMessage = "<{0}>{1} {2} {3}" -f $Priority, $Timestamp, $Hostname, $Message
# create an ASCII Encoding object
$Encoding = [System.Text.Encoding]::ASCII
# Convert into byte array representation
$ByteSyslogMessage = $Encoding.GetBytes($FullSyslogMessage)
# Send the Message
$UDPCLient.Send($ByteSyslogMessage, $ByteSyslogMessage.Length) | out-null
}
$LogFiles = Get-ChildItem -Path E:\Unzipped\
foreach ($File in $LogFiles){
$EventCount = 0
$global:Event = ''
switch -Regex -File $File.fullname {
'^START--' { #Regex to find events
if ($global:Event) {
# send previous events' lines to syslog
write-host "Send event to syslog........................."
$EventCount ++
SendSyslogEvent
}
# Current line is the start of a new event.
$global:Event = $_
}
default {
# Event-interior line, append it.
$global:Event += [Environment]::NewLine + $_
}
}
# Process last block.
if ($global:Event) {
# send last event's lines to syslog
write-host "Send last event to syslog-------------------------"
$EventCount ++
SendSyslogEvent
}
}
【问题讨论】:
-
1) 停止附加到全局变量,2) 处理您的
$UDPCLient,也许 3) 重新使用 UDPClient 连接,而不是重新创建它时间:) -
这些听起来不错。你有机会提供例子吗?我不确定我应该怎么做。我在功能范围内挣扎,我不知道如何做其他两件事。真的很感激。
-
最大的文件是
2GB? -
到目前为止,我已经看到它们大到 2.5 GB。
-
如果是这种情况,那么使用开关可能是错误的方法。您可能需要考虑使用流。
标签: powershell text syslog