【问题标题】:Best Way to Trigger an Event When a Specific Error is Thrown in Windows 2003 Server?在 Windows 2003 Server 中引发特定错误时触发事件的最佳方法?
【发布时间】:2012-07-13 00:26:48
【问题描述】:

我有一个 Windows 2003 Server,它使用 IIS 来托管旧版 ASP.NET Web 服务,该服务连接到我无法控制的远程 Oracle 数据库服务器上的数据库。问题是数据库服务器每隔一两周就会关闭一次,但大约 5 分钟后就会恢复。然后我必须重新启动 IIS 以删除任何损坏的连接,然后我的 Web 服务才能再次工作。

发生特定错误代码时触发事件的最佳方法是什么(即给自己发电子邮件和/或重置 IIS)(在这种情况下,它将是 ORA 类型的错误,但我可以获得 Windows 错误代码) ?

IIS 设置?

任务计划程序? (仅限于计划任务,我相信在 Windows 2003 服务器上,例如每天/每周/每月等)

Powershell 脚本?

其他选项?

我知道在 Windows 2008 Server 中,您可以配置任务计划程序以在服务器在其错误日志中遇到某些错误代码时触发事件...但我在 Windows 2003 的任务计划程序中找不到类似的内容服务器。

谢谢。

【问题讨论】:

    标签: windows powershell windows-server-2008 scheduled-tasks windows-server-2003


    【解决方案1】:

    我写了一个 powershell shell 脚本来监控 sql server 错误日志并报告特定的错误。我存储了上次停止阅读的位置,然后在下次运行脚本时从该点继续。这是实际读取日志的部分。然后,您只需将位置存储在某个临时文件中并将其作为计划任务运行。并在发生错误时发送电子邮件,甚至重新启动某些服务。

    $path = $logs.file
    Write-Host $path
    if($currentLog.lastpos -ne $null){$pos = $currentLog.lastpos}
    else{$pos = 0}
    if($logs.enc -eq $null){$br = New-Object System.IO.BinaryReader([System.IO.File]::Open($path, [System.IO.FileMode]::Open, [System.IO.FileAccess]::Read, [System.IO.FileShare]::ReadWrite))}
    else{
        $encoding = $logs.enc.toUpper().Replace('-','')
        if($encoding -eq 'UTF16'){$encoding = 'Unicode'}
        $br = New-Object System.IO.BinaryReader([System.IO.File]::Open($path, [System.IO.FileMode]::Open, [System.IO.FileAccess]::Read, [System.IO.FileShare]::ReadWrite), [System.Text.Encoding]::$encoding)
    }
    $required = $br.BaseStream.Length - $pos
    if($required -lt 0){
        $pos = 0
        $required = $br.BaseStream.Length
    }
    if($required -eq 0){$br.close(); return $null}
    $br.BaseStream.Seek($pos, [System.IO.SeekOrigin]::Begin)|Out-Null
    $bytes = $br.ReadBytes($required)
    $result = [System.Text.Encoding]::Unicode.GetString($bytes)
    $split = $result.Split("`n")
    foreach($s in $split)
    {
        if($s.contains("  Error:"))
        {
            #Filter events here
        }
    }
    $currentLog.lastpos = $br.BaseStream.Position 
    $br.close()
    

    【讨论】:

    • 不错!感谢分享,我会尽快尝试。我认为 Windows Server 2003 需要在其上安装 Powershell,因为它不像 Windows Server 2008 那样预先安装。
    • 我也希望在 Windows 2003 Server 上有一些内置的解决方案,因为服务器不是我的,出于安全原因,我不允许将新软件导入服务器。
    • 然后您可以编写一个小的 Visual Basic 脚本来执行此操作并将其作为计划任务执行。然后,您需要导入的只是您的脚本。我不知道 Windows 中有任何内置功能可以为您执行此操作。
    • 感谢 Gisli,我想用脚本运行计划任务至少是一种解决方案。最好在错误发生的同时捕获错误,但现在必须这样做。干杯
    猜你喜欢
    • 2023-03-07
    • 2010-10-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-12-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多