jc7 has asked for the wisdom of the Perl Monks concerning the following question:

Hi Monks! I need your wisdom! I am trying to scan SQL 2005 error log using a Perl script. It works with both SQL 7.0 and SQL 2000, but not for SQL 2005. It appears that it is unable to open the SQL 2005 error log correctly. Please help and let me know if you need any more info. Thank you very much in advance for any of your advice!

sub scanSQLErrorlog { …… # now open the errorlog file for scan: unless (open(LOG, "$sqlErrorlog")) { $ref->{log_open_error} = "***Error: could not open $sqlErrorlog for re +ad. "; $ref->{log_open_error} .= Win32::FormatMessage(Win32::GetLastError); return $ref; } # get the very first errorlog line. It has the version info: $_ = <LOG>; if (/^\s*(\d\/\-+\s+\d\:\.+)\s+(?:kernel|Server|spid\d+)\s+(Microsoft\ +s+SQL\s+Server.+)/i) { ($ref->{first_recorded_date}, $ref->{sql_version}) = ($1, $2); } …… }

The first line of SQL server 2000 error log is like:

2007-05-30 08:29:03.17 server Microsoft SQL Server 2000 - 8.00.2040 (I +ntel X86)

After running the script,
$ref->{first_recorded_date} has “2007-05-30 08:29:03.17”
$ref->{sql_version} has “Microsoft SQL Server 2000 - 8.00.2040 (Intel X86)”

The first line of SQL server 2005 error log is like:

2007-05-30 15:49:21.56 Server Microsoft SQL Server 2005 - 9.00.305 +4.00 (Intel X86)

After running the script,
$ref->{first_recorded_date} has nothing.
$ref->{sql_version} has nothing.
It does not grab anything from the log of SQL 2005. There is no error message either.

Edit: g0n - code tags

Replies are listed 'Best First'.
Re: How to open SQL 2005 errorlog?
by almut (Canon) on Jun 06, 2007 at 20:08 UTC

    (Side note: your regex probably should be /^\s*([\d\/\-]+\s+[\d\:\.]+) ... I.e., the square brackets are missing — presumably as a result of not using <code> tags for the code section...)

    As to the open possibly failing... Did you check whether $ref->{log_open_error} is being set to the error message you're generating ("***Error: ...")? If so, what exactly does it say? What's the value of $sqlErrorlog — is it the correct path/filename?

      You are correct! It should be /^\s*([\d\/\-]+\s+[\d\:\.]+) ... It was a result of not using <code> around the script. Sorry for the confusion. Thank you very much for your helping!

      I did check the value of $ref->{log_open_error}. It is blank. And $sqlErrorLog does point to correct path and file name. Please see the sample below. From the output, we see it grabbed the first link of log for SQL 2000 error. For SQL 2005, however, it showed 'yp2'.

      Sample script:

      ... # now open the errorlog file for scan unless (open(LOG, "$sqlErrorlog")) { $ref->{log_open_error} = "***Error: could not open $sqlErrorlo +g for read. "; $ref->{log_open_error} .= Win32::FormatMessage(Win32::GetLastE +rror); return $ref; } print "Error: $ref->{log_open_error}\n"; print "$sqlErrorlog\n"; # get the very first errorlog line. It has the version info. $_ = <LOG>; print "$_\n"; if (/^\s*([\d\/\-]+\s+[\d\:\.]+)\s+(?:kernel|Server|spid\d+)\s+(Micros +oft\s+SQL\s+Server.+)/i) { ($ref->{first_recorded_date}, $ref->{sql_version}) = ($1, $2 +); print " DT: $ref->{first_recorded_date}\n"; print " Version: $2\n"; }
      The output is:
      Error:
      \\SQL2000Server\d$\MSSQL\LOG\ErrorLog
      2007-05-30 08:29:03.17 server Microsoft SQL Server 2000 - 8.00.2040 (Intel X86)
      DT: 2007-05-30 08:29:03.17
      Version: Microsoft SQL Server 2000 - 8.00.2040 (Intel X86)
      ...
      Error:
      \\SQL2005Server\d$\MSSQL\LOG\ERRORLOG
      ˙ž2

        Ahh...  the first two bytes (˙ž = 0xFF 0xFE) most likely is a BOM (Byte Order Mark), indicating that the file is UTF-16le or UCS-2le (le=little-endian) encoded (the distinction between UTF-16 and UCS-2 is probably irrelevant in your case — what's essential is that (at least) two bytes are being used to encode a single character).

        In order to properly read such files, you need to open them like this:

        unless (open LOG, "<:encoding(ucs-2)", $sqlErrorlog) { ...

        or

        unless (open LOG, "<:encoding(utf-16)", $sqlErrorlog) { ...

        (you need Perl 5.8.x for this to work)

        When debug-printing your $_, you should then see the line content as expected.

        Just in case you're still having problems (in particular with line endings), you might want to see this post of mine, which describes the problem, and a workaround. Good luck.