The first question which came to my mind when someone wanted to instrument VBScript was “Why would someone want to instrument a script?”. The answer to that question is that VBScript is not only something used to script small actions when a batch file isn’t the best tool for the job. VBScript is also used within other engines, such as Classic ASP.
If you’re only interested in the end users of a classic ASP application, there is no need to do any code changes as instrumenting IIS with UEM will take care of that for you automatically. But if you need to see how long the different parts of your code is taking, you will have to add instrumentation into the code.
To instrument VBScript we need to use the Native ADK seeing as there is no specialized agent for it. VBScript cannot call a non-ActiveX DLL, which is why I ended up creating an ActiveX wrapper for our Native ADK. The full project can be downloaded here and the ActiveX DLL can be downloaded here. It is created in C# and supports the most commonly used functions such as method instrumentation with arguments and tagging and it can be easily expanded if more functionality is needed.
In this post I’ll go through how to take a simple VBScript and instrument it using the wrapper. The code below is the complete script and while it isn’t doing much, it will be able to show how to start the agent, how to create new PurePaths, capture function timings, arguments and more.
The HandleWebRequest function is the entry point of the script and it prints out whatever argument you pass to it. It could be visualized as a web request handler, which is executed once with every web request. The HandleWebRequest function calls the ExecuteQuery function three times with different arguments, simulating a function executing database queries.
If we execute the above script, we get the following result.
To simplify the instrumentation I’ve created a helper class which will take care of the communication to the ActiveX DLL. The first thing to do is importing the helper class, which you can download here. While ASP has the possibility to import files with a single line of code, VBScript has not, so we will have to add a subroutine which will take care of the import. While one could expand the sub routine to do things such as searching in the PATH, this subroutine is sufficient for the current needs. Once we have added the sub routine we can now call Import “dynatrace.vbs” to get access to the helper class.
The next step is to initialize the agent using Dim Dynatrace : Set Dynatrace = (New DynatraceHelper)(“c:\dtagent.dll”, “VBS_INT”, “localhost”). The initialization only needs to be executed once per process, so normally you would call that during the startup of the script. The init function will set up the connection to the AppMon collector and start monitoring the process. It requires a path to the AppMon agent, the name of the agent and the server name. If you want to pass in additional information, such as log levels, it is quite trivial to change it as the helper class doesn’t need to be compiled and is in clear text.
When the process shuts down we need to, if possible, call Dynatrace. Uninitialize to shut down the agent.
Once the agent is injected we will also need to add instrumentation into the code to, for example, start PurePaths and capture timings. The three functions we will use for this is StartPurePath, Enter and Leave.
The StartPurePath and Enter functions require three arguments, the name of the method, the line number and an array with the arguments to capture. The name of the script will be captured automatically. If you, for example, would like to have a sensor with the name “mySensor”, with the line number 5 and the captured arguments “hello” and “world”, the function would look like this:
This is how the script will look after we added the required lines.
By executing this script we receive the following PurePath in AppMon.
As you can see the instrumentation has captured the function names, arguments, CPU/IO breakdown and name of the file. By looking at the details of the top node we can see that it also recorded the line number and full path to the file.
When registering the ActiveX DLL please keep in mind that it is created using C#, therefore you will need to use RegAsm to register it instead of regsvr32. Please make sure that you register it using the correct architecture as there are two .NET framework folders, one for 32 bit and one for 64 bit. You need to use the correct one depending on the application you are instrumenting or you will get an error about the object not being found. If you, for example, are using a 64-bit IIS this would be the command to use:
The agent ADK, dtadk.dll, can be downloaded from our community downloads and needs to be present in the same folder as the Dynatrace.dll. It needs to be the same architecture as your application.
If you want to test the above example, please make sure you execute the VBScript file using cscript, like this:
Break open the black box and get in control of your applications, even if they are created in VBScript!