Server uptime is an important
metric to review in your environment. An extremely long uptime might mean that a server isn’t
responding to remote shutdown and
restart commands or has been overlooked in the regular maintenance
cycle. An extremely short uptime might
mean a server is experiencing blue
screens and automatically restarting
itself. Uptime statistics can even be
helpful in determining whether an
off-hours hotfix installation actually
took place.
Realizing the importance of
reviewing servers’ uptime statistics,
you might already be using a tool to
get this information. If you’re responsible for many servers, you’re probably
painfully aware that reviewing the
uptime statistics for many servers can
take a lot of time. To ease the burden
of this task, I wrote a script named
UptimeReport.bat. This script obtains
the uptime statistics for the specified
servers, sorts the statistics, and reports
them in a way that makes it easy for
you to see the uptime numbers that
don’t look right. You can have the
uptime report emailed to you in a
comma-separated value (CSV) or
HTML file. At the top of the report,
you’ll find the most recently rebooted
servers (i.e., those with the shortest
uptime). At the bottom of the report,
you’ll find the servers that have been
on for the longest period of time. The
report also includes a section that
specifies any servers that were inaccessible when the script ran. When
you get this script implemented in
your environment, a quick glance can
give you clues to potential uptime
problems.
Listing 1 contains an
excerpt from UptimeReport.bat,
which you can run on Windows Server
2003 and Windows XP machines.
Before I describe how to use this
script in your environment, you first
need to know how the script works
and the necessary prerequisites.
How the Script Works
Pseudocoding is the process of writing, in sentence form, the tasks that
you want the script to accomplish.
(For more information about pseudocode, see “Getting Started in NT Shell
Scripting, Part 1,” March 2000,
InstantDoc ID 8182, and “Getting
Started in NT Shell Scripting, Part 2,”
April 2000, InstantDoc ID 8384.) What
follows is the pseudocode I created for
UptimeReport.bat. I’ve added pointers to the parts of the script that
accomplish the specified tasks.
- Obtain the list of target servers by
running the Dsquery tool against a
specified organizational unit (OU) or
by using an input list. Callout E in Listing 1 shows this code. (Note that you’ll
find information about Dsquery and
the other tools I mention in the
pseudocode in the next section.)
- If using Dsquery, check to see
whether there’s a list of servers to
exclude. If so, create a search string of
excluded servers so that Dsquery
won’t be run against these servers.
(See callout D.)
- Ping each server to see whether
it’s offline or online. When offline,
write an error message in the report
and continue to the next server. When
online, use the PsInfo tool to capture
the uptime data. When PsInfo fails,
write an error message in the report
and continue to the next server. (See
callout F.)
- When PsInfo succeeds, use a Set
/A math operation so that each
server’s uptime statistic is reported in
terms of minutes. (See callout H.)
- After all the PsInfo results are
written to a holding file, use the FSort
tool to sort the entries by the number
of minutes of uptime. (See callout A.)
- If users specify they want the
uptime report in HTML format, use the Csv2Html tool to change the
default CSV output to HTML output.
(See callout B.)
- Use Blat to send a notification email message, with the HTML or CSV report as an attachment. (See callout C.)
The Prerequisites
Before you run UptimeReport.bat,
you need to get the necessary tools in
place. To begin, you first need to
download this script from the Download the Code box at the top of the Web page. Next, you need to download and
install the third-party tools that the
script uses:
Blat. Blat is a free utility that lets
you send email messages from a command-shell script. This freeware is
available from SourceForge.net at
http://sourceforge.net/project/show
files.php?group_id=81910.
PsInfo. PsInfo is a tool that provides a variety of Windows system
information. PsInfo is a part of Sysinternals’ PsTools suite, which is a great
collection of free command-line
scripting tools. You can download
PsInfo from http://www.sysinternals.com/utilities/psinfo.html.
FSort and Csv2Html. FSort is a
sorting utility that gives you more
options than the built-in Sort command. Csv2Html converts a CSV file
to HTML file. Both tools are part of
Kilowatt Software’s Poof! suite. In the
past, you had to purchase Poof! but
the company recently made it freeware. You can download Poof! at http://www.kilowattsoftware.com/poofpage.htm.
You must install Poof! on the server
from which you plan to run UptimeReport.bat. By default, the default
installation folder includes the exclamation point (!). I found out the hard
way that this character can cause
problems when a script uses the SetLocal EnableDelayedExpansion command. (For other lessons I learned while
writing UptimeReport.bat, see the
sidebar “Valuable Lessons Learned.”)
In this situation, the exclamation
point becomes a reserved character
for variable designation. For example,
if you run a test file that contains the
code
Set PoofLoc=C:\Poof!
Echo %PoofLoc%
SetLocal EnableDelayedExpansion
Echo %PoofLoc%
you’ll find that the path doesn’t work
correctly because the exclamation
point is dropped when the second
Echo command executes.
You can deal with this problem two
ways. The ideal solution is to delete
the exclamation point from the installation folder’s name when you’re
installing Poof! Alternatively, you can
strategically place the SetLocal EnableDelayedExpansion and SetLocal DisableDelayedExpansion commands.
Typically, you place SetLocal EnableDelayedExpansion at the beginning of
a script and SetLocal DisableDelayedExpansion at the end. In this case, you
place SetLocal EnableDelayedExpansion immediately preceding the code
that needs variable expansion, then
place SetLocal DisableDelayedExpansion immediately after that code. As
callout D in Listing 1 shows, I used this
approach in UptimeReport.bat in case
you already have Poof! installed on
your servers.
After you installed Poof!, you can
open its installation directory and
view the HTML help files that correspond to each of the 145 tools. The
HTML files contain additional information on switch options and usage
examples that you might not see
when you use the /? Switch to access
the online help. Note that when you
run a Poof! tool with the /? switch or
without any parameters, it enters a
console input mode, which can be
confusing. You can press Ctrl+C to exit
out of this mode.
UptimeReport.bat also uses the
Dsquery tool. Dsquery is part of a
suite of six directory service tools
included with Windows 2003 and XP.