Home > Scripting, Windows Server 2003 > KiXtart script to generate and email uptime report

KiXtart script to generate and email uptime report

March 10th, 2009 Leave a comment Go to comments

As part of my Service Level Agreement at work I have to produce a monthly uptime report for each server. Rather than generate individual reports for each server I decided to write a KiXtart script that would generate a report for all servers and email it to me each month.

The following script uses two other applications:

BMail - A command line SMTP mailing tool
Uptime - Allows you to estimate Server availability

Full credit to Craig Peacock for BMail.

Here is the script:

;===============================================
;===============================================
;
; Clear Existing Uptime Report
;
;===============================================
;===============================================

IF EXIST ("Report")
DEL "Report\*.txt"
ENDIF

;===============================================
;===============================================
;
; Produce Uptime Report
;
;===============================================
;===============================================

;Convert YYYY/MM/DD to DD-MM-YYYY
$temp = @DATE
$todaysdate = SUBSTR($temp, 9, 2)
$todaysdate = $todaysdate + "-" + SUBSTR($temp, 6, 2)
$todaysdate = $todaysdate + "-" + SUBSTR($temp, 1, 4)

;Array containing server names
Dim $ComputerNames[5]
$ComputerNames[0] = "Server01"
$ComputerNames[1] = "Server02"
$ComputerNames[2] = "Server03"
$ComputerNames[3] = "Server04"
$ComputerNames[4] = "Server05"

;If Report directory doesn't exist then create it
IF NOT EXIST ("Report")
MD "Report"
ENDIF

;Output uptime report for all computers in the array
For Each $Element In $ComputerNames

IF $Element = ""
GoTo "Email"
ENDIF

shell "%comspec% /c " + chr(34) + "Uptime.exe" + " " + $Element + " " + chr(34) + " /s /p:31 >> Report\$todaysdate-UptimeReport.txt"
shell "%comspec% /c echo. >> Report\$todaysdate-UptimeReport.txt"
shell "%comspec% /c echo. >> Report\$todaysdate-UptimeReport.txt"
shell "%comspec% /c echo ******************************************************************************** >> Report\$todaysdate-UptimeReport.txt"
shell "%comspec% /c echo ******************************************************************************** >> Report\$todaysdate-UptimeReport.txt"
shell "%comspec% /c echo ******************************************************************************** >> Report\$todaysdate-UptimeReport.txt"
shell "%comspec% /c echo. >> Report\$todaysdate-UptimeReport.txt"
shell "%comspec% /c echo. >> Report\$todaysdate-UptimeReport.txt"

Next

:Email
;===============================================
;===============================================
;
; Email Uptime Report
;
;===============================================
;===============================================

$emailserver = "someemailserver"
$toaddress = "james" + chr(64) + "somedomain.com"
$fromaddress = "uptime" + chr(64) + "somedomain.com"
$subject = "Uptime Report For all London Servers"

;Form email command
$emailcommand = "bmail -s " + $emailserver + " -t " + $toaddress + " -f " + $fromaddress + " -h -a " + chr(34) + $subject + chr(34) + " -m " + "Report\" + $todaysdate + "-UptimeReport.txt" + " -c"

RUN ($emailcommand)

;===============================================

The script works by generating a file that contains the results of uptime.exe for each server specified. The contents of the file are then emailed using bmail.

This script obviously requires a few variables to be modified to get it to work in another environment. The following will need to be altered:

Dim $ComputerNames[5] - you will need to change the numeric value to the number of servers you want to generate a report for

NOTE: the example above is for 5 servers. If you wanted to monitor 4 servers then you will need to modify the array to change the number of elements to 4. So your array would look like (the last line has been removed):

$ComputerNames[0] = "Server01"
$ComputerNames[1] = "Server02"
$ComputerNames[2] = "Server03"
$ComputerNames[3] = "Server04"

$ComputerNames[0] = "Server01" - you need to change Server01 to the server you want to generate a report for
$emailserver - the name of your email server (the server running this script must be allowed to relay through it)
$toaddress - the address you want the email alert to go to
$fromaddress - the (spoof) address you want the email to appear to come from
$subject - the subject line of the email

I use a scheduled task on a Windows Server 2003 to run this script once a month. It generates a report for the last 31 days of uptime for each server in the array and then emails me the report.

NOTE: you can change 31 day period that the script gathers uptime information for by modifying /p:31 on the following line:

(31 represents the number of days in the past to gather uptime statistics for)

shell "%comspec% /c " + chr(34) + "Uptime.exe" + " " + $Element + " " + chr(34) + " /s /p:31 >> Report\$todaysdate-UptimeReport.txt"

To use this:

1) Save the above script as kixtart.kix in a directory containing the kixtart application
2) Save bmail.exe and uptime.exe (available from the links above) in the same directory
3) Modify variables as appropriate
4) Make sure that the server running the script can relay email through your email server
5) Test by running the script manually and/or create the scheduled task

Alternatively, download the above script and necessary files - HERE

The end result is an email with a breakdown of uptime availability for each server including any dates and times where necessary.

Categories: Scripting, Windows Server 2003 Tags:
  1. No comments yet.

*

code