This report lets you easily spot uptime anomalies
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.)
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
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.
With the scripts and tools in place, you’re now ready to adapt UptimeReport.bat for use in your environment. All the variables needing configuration are grouped together at top of the script. To configure the variables, open the script in Notepad or another script editor and follow these steps:
- Set the path to the target OU if you want to use Dsquery to obtain the list of servers. You must enclose the path in quotes. If you’re using an input file instead of Dsquery, don’t include a path.
- Set the path to the exclusions file, if applicable. Quotes aren’t needed, even if the path includes spaces. If you don’t have any exclusions or you’re using an input file instead of Dsquery, don’t include a path.
- Set the path to the list of target servers if you’re using an input list instead of Dsquery. Quotes aren’t needed, even if the path includes spaces. If you’re using Dsquery, don’t include a path. Note that if you specify a target list and an OU target, the script will use the target list and not the OU target or exclusions list.
- Set the type of report you want. Specify CSV to obtain a CSV report or HTM to obtain an HMTL report. These options are case insensitive.
- Set the path to PsInfo. Quotes aren’t needed. However, PsInfo is sensitive to spaces in the path, so the path can’t contain any spaces.
- Set the path to Dsquery. The default location is C:\WINDOWS\sys tem32\dsquery.exe on Windows 2003 machines. Quotes aren’t needed. However, Dsquery is sensitive to spaces in the path, so the path can’t contain any spaces.
- Set the path to FSort. Quotes aren’t needed, even if the path includes spaces
- Set the path to Csv2Html. Quotes aren’t needed, even if the path includes spaces.
- Set the path to Blat. Quotes aren’t needed, even if the path includes spaces.
- Specify the recipients who should receive the email message containing the report. Use commas to separate addresses, with no spaces preceding or following the commas.
- Specify the email address that should appear in the email message’s From field.
- Specify your company’s SMTP mail server.
After you’ve made these configurations, you’re ready to test UptimeReport.bat. To run UptimeReport.bat, you simply need to run the command
in the command-shell window. If the results are satisfactory, you’re ready to use the script in your production environment.
An Easy Way to Get a Handle on Uptime Anomalies
With UptimeReport.bat, you can easily get a handle on potential uptime anomalies in your environment. This information, in turn, can help you improve your servers’ performance, discover whether any inappropriate reboots have taken place, and verify whether important hotfixes have been correctly applied.