はじめに
検証とかするときSyslog受信するのに使うので。
fluentdなどでも大丈夫ですが、簡単に利用できます。
Syslog.ps1
$Udp = New-Object Net.Sockets.UdpClient -ArgumentList 514
$Sender = $null
Add-Type -TypeDefinition @"
public enum Syslog_Facility
{
kern,
user,
mail,
system,
security,
syslog,
lpr,
news,
uucp,
clock,
authpriv,
ftp,
ntp,
logaudit,
logalert,
cron,
local0,
local1,
local2,
local3,
local4,
local5,
local6,
local7,
}
"@
Add-Type -TypeDefinition @"
public enum Syslog_Severity
{
Emergency,
Alert,
Critical,
Error,
Warning,
Notice,
Informational,
Debug
}
"@
while($true) `
{
if($Udp.Available) `
{
$Buffer = $Udp.Receive([ref]$Sender)
$MessageString = [Text.Encoding]::UTF8.GetString($Buffer)
$Priority = [Int]($MessageString -Replace "<|>.*")
[int]$FacilityInt = [Math]::truncate([decimal]($Priority / 8))
$Facility = [Enum]::ToObject([Syslog_Facility], $FacilityInt)
[int]$SeverityInt = $Priority - ($FacilityInt * 8 )
$Severity = [Enum]::ToObject([Syslog_Severity], $SeverityInt)
$MessageString = "$MessageString $Facility $Severity"
$MessageString = $MessageString -Replace "<.*>",""
#Write-Host $MessageString
$MessageString >> c:tempsyslog.log
}
[Threading.Thread]::Sleep(500)
}
最後にFacilityとSeverityを見やすいように付加
上記のスクリプトをバックグラウンドで動作させます。
コマンドプロンプトで実行
powershell -windowsstyle hidden syslog.ps1
まとめ
Windowsでは有料のSyslog Serverはありますが、Powershellだけでできるなら要らないですね。