You are not logged in.

#1 06 Jun 2014 15:57

camerony
New Member
Registered: 01 Jun 2014
Posts: 1

Creating a syntax for arguments passed to a script.

Hi Everyone,
Briefly, I’ve not touch any form of VB code since VB4 was released some 15yrs ago.
Last weekend I created my first script and thankfully it worked (see below).

The script is now being called via a Nagios system & I’m wanting to enhance the script a small bit to enable a bit more flexibility. 
The script contains some hardcoded values that I’d like to be more flexible with. 
As the script has been distributed across a number of systems, any changes to any one of these hardcoded values then makes updating the script a chore.

The goal I have is to have a form of syntax to the arguments presented to the script.
This would mean changes to the script are driven via the Nagios ini file and is performed in one location/server/file.

If an argument isn’t presented, then a default value is used.

By way of example, I currently call the script in the following manner:

cscript check_icom_bookingbngine.vbs //nologo 2

The ‘2’ is the Booking Engine log file that I wish to check, currently there are presently four (4) to check on each server.
The Nagios ini file has been configured to execute the script against each required server and against each Booking Engine log file – so the Nagios ini file has four entries for each required server. 
All good.

I do perform some basic argument checks (lines 38-49) – but they are just that very basic.
I would however like to be able to call the script in the following manner:

cscript check_icom_bookingbngine.vbs //nologo /e 2 /m 5 /u 4

Where  …

  • /e    Refers to the Booking Engine log file required to check.

  • /m    Refers to the number of minutes  (in the past) before an alert is raised - DateLastModified to DateDiff comparison check.

  • /u    Refers to the Upper limit of the number of Booking Engines to check - Lower limit will remain at 1.

If this is possible, could anyone please provide some direction or examples.
Hope the above made sense.

Cheers & thanks in advance,
Cameron

' Script:  check_icom_bookingbngine.vbs [ ICOM Booking Engine Check ]
' **********************************************************************
Option Explicit
Dim strDate, strDay, strMonth, strYear, objFSO, objSHL, StdOut, objArgs
Dim EngNum, Fname, Fpath, MFile, MTime, dtmToday, TimeDiff
'EngNumLowerLimit & EngNumUpperLimit refer to range that cover the
'number of Booking Engine log files available (in use) on the host server.
'I.E.:  1-4 Range = 1-4 Actual Logs ... See use further down.
Const EngNumLowerLimit = 1
Const EngNumUpperLimit = 4
'Number of negative Minutes (in the past) before an alert is raised.
'I.E.:  -x where 'x' is the minutes
Const XMinutes = -5
'Path to location of ICOM Booking Engine log files.
'Remember to include trailing "\" to end path string.
Fpath="D:\TourplaniCom\engine\livemsg\"
'******************************************************************************
'GetFormattedDate Function
Function GetFormattedDate
  strDate = CDate(Date)
  strDay = DatePart("d", strDate)
  strMonth = DatePart("m", strDate)
  strYear = DatePart("yyyy", strDate)
  If strDay < 10 Then
    strDay = "0" & strDay
  End If
  If strMonth < 10 Then
    strMonth = "0" & strMonth
  End If
  GetFormattedDate = strYear & strMonth & strDay
  'StdOut.WriteLine(GetFormattedDate)
End Function
'******************************************************************************
Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objSHL = CreateObject("WScript.Shell")
Set StdOut = objFSO.GetStandardStream(1)
'Store the arguments in a variable
Set objArgs = WScript.Arguments
IF (objArgs.count = 0) OR (objArgs.count > 1) Then _
	StdOut.WriteLine("1;WARN-Missing or too many argument(s)") & WScript.quit
EngNum = int(WScript.Arguments(0))
'- - - - - - - - - - - - - - - - - - - - - - - - - - -
IF (EngNum >= EngNumLowerLimit) AND (EngNum <= EngNumUpperLimit) Then
	'Check Existence of Booking Engine Log file required.
	'Eg:  iComLog_iComBookingImport1_20140508.txt
	Fname = "iComLog_iComBookingImport" & EngNum & "_" & GetFormattedDate & ".txt"
Else
	StdOut.WriteLine("1;WARN-Invalid argument") & WScript.quit
End IF
IF NOT (objFSO.FileExists(Fpath & Fname)) Then _
	StdOut.WriteLine("2;ERROR-FILE: " & Fname & " MISSING!") & WScript.quit

Set MFile = objFSO.GetFile(Fpath & Fname)
MTime = MFile.DateLastModified
dtmToday = Date()
TimeDiff = DateDiff("n", dtmToday & " " & Time, MTime)
IF TimeDiff <= XMinutes Then
	StdOut.WriteLine("2;CRITICAL - ENGINE FAILURE DETECTED") & WScript.quit
ELSE
	StdOut.WriteLine("0;OK")
End IF
'******************************************************************************
'* END of VB Script.

Offline

#2 29 Dec 2014 17:37

JosefZ
New Member
From: CzechLands
Registered: 29 Dec 2014
Posts: 4

Re: Creating a syntax for arguments passed to a script.

Here's a complete script (documented with inset comments):

'
' VB Script Document: Wsh Arguments object (and related) sample
'
' Usage:
' wscript [scriptpath\]scriptname.vbs argument [ arguments] /namedargument[:namedargumentvalue] [/namedargumentn[:"named argument value"]]  
' or
' cscript [scriptpath\]scriptname.vbs argument [ arguments] /namedargument[:namedargumentvalue] [/namedargumentn[:"named argument value"]]
' Note: arguments must be separated by a blank (space or tab) character
'       arguments containing a blank must be enclosed in double quotes: "rabbit ears"
'       The quotes will be removed in the WshNamed (WshUnnamed as well) collection. 
'       For an argument to be in the WshNamed collection, it must have been used 
'       on the command line. 
'       If the argument has no value (such as a simple argument or an empty string), 
'       the Item property returns an empty string. 
'       Requesting a non-existent named argument from the Item property causes an error. 
'       To check if an argument exists, use the Exists method.
'
' e.g.
' wscript scriptname.vbs stringargument "stringarg with blanks" /stringarg:"stringarg with spaces"
'
option explicit

Dim objArgsNamed, objArgsUnnamed, objArgsInall, strResult, iii
strResult = ""

Set objArgsInall = WScript.Arguments
strResult = strResult & "There are " & objArgsInall.Count & " arguments in all:" & vbNewLine
' All arguments are referenced by their order on the command line
For iii = 0 to objArgsInall.Count - 1
  strResult = strResult & objArgsInall(iii) & " = " & Wscript.Arguments.Item(iii) & vbNewLine
Next
strResult = strResult & "-" & vbNewLine

Set objArgsUnnamed = WScript.Arguments.Unnamed
strResult = strResult & "There are " & objArgsUnnamed.Count & " unnamed arguments:" & vbNewLine
' Unnamed arguments are referenced by their order on the command line
For iii = 0 to objArgsUnnamed.Count - 1
  strResult = strResult & objArgsUnnamed(iii) & " = " & Wscript.Arguments.Unnamed.Item(iii) & vbNewLine
Next
strResult = strResult & "-" & vbNewLine

Set objArgsNamed = WScript.Arguments.Named
strResult = strResult & "There are " & objArgsNamed.Count & " named arguments:" & vbNewLine
' Named arguments cannot be referenced by their order on	 the command line
' Named arguments must be referenced by their names
'Expected names of named argumets:
Dim arrExpectArg(1)
arrExpectArg(0) = "stringarg"
arrExpectArg(1) = "otherArg"
For iii = 0 to UBound(arrExpectArg,1)
  If WScript.Arguments.Named.Exists( arrExpectArg(iii)) Then
    strResult = strResult & arrExpectArg(iii) & ": " & objArgsNamed( arrExpectArg(iii)) & " = " & Wscript.Arguments.Named.Item( arrExpectArg(iii)) & vbNewLine
  Else
    strResult = strResult & arrExpectArg(iii) & ": requested argument was not specified on the command line" & vbNewLine  
  End If
Next

WScript.Echo strResult

Offline

Board footer

Powered by