Getting contents from a text file

All questions related to installations, configurations and maintenance of Advanced Host Monitor (including additional tools such as RMA for Windows, RMA Manager, Web Servie, RCC).
Post Reply
Kapz
Posts: 216
Joined: Mon Dec 06, 2004 2:33 pm
Location: Denmark

Getting contents from a text file

Post by Kapz »

Hi !

Situation:

On a remote server with an RMA installed I would like to read the content of a simple text file.
The text file contains a number, e.g. 36.

Is there any test that can send me this number back as a reply ?

I've been fiddling around with Compare Files and Text Log and even though I'm able to get these tests working (they alert me as they should when the string is found/not fund) my %reply% is always empty.

Thanks in advance for any help !

Kasper :O)
KS-Soft Europe
Posts: 2832
Joined: Tue May 16, 2006 4:41 am
Contact:

Post by KS-Soft Europe »

Could you be more specific? Does this file contain only one string with this number? Does the string contains only number or there are some text, like "Value : 36"? Does text file contains only one entry with this number, or there could be several entries, and you need the last one? Could you provide an example of this file?

Regards,
Max
Kapz
Posts: 216
Joined: Mon Dec 06, 2004 2:33 pm
Location: Denmark

Post by Kapz »

Max,

Yes, the file only contains a number, so currently it looks like this:

36

However, if it makes it easier to parse we can make it look like this:

MyValue=36

The file will always only contain one single line - the only thing that will change is the value (36 might change to 39 or 23 or 77 or ... etc.)

It seems like I'm trying to accomplish what dba asked about on Dec. 8th.

Thanks !

Kasper :O)
KS-Soft Europe
Posts: 2832
Joined: Tue May 16, 2006 4:41 am
Contact:

Post by KS-Soft Europe »

Kapz wrote:Yes, the file only contains a number, so currently it looks like this:
36
The file will always only contain one single line - the only thing that will change is the value (36 might change to 39 or 23 or 77 or ... etc.)
You may try following script:
Start cmd : cmd /c %Script% %Params%
Script:

Code: Select all

@echo off
rem %1 - Full file name with path included

IF "%1"=="" goto filenotfound
IF NOT EXIST %1 goto filenotfound

for /f "eol=; tokens=*" %%i in (%1) do SET value=%%i 
echo ScriptRes:Ok:%value%

goto end

:filenotfound
  echo ScriptRes:Bad:File not found
:end
Kapz wrote:It seems like I'm trying to accomplish what dba asked about on Dec. 8th.
Yes, there is similar script, but much simplier.

Please note, script may not work properly with folders with space in the name, like "\Program files".

Regards,
Max
Kapz
Posts: 216
Joined: Mon Dec 06, 2004 2:33 pm
Location: Denmark

Post by Kapz »

Thanks for the script !

However, I still get no reply, but maybe I set up something wrong ?

My test:

Method = TextLog
;--- Common properties ---
RMAgent = ANet01 (AD/Fil/Print)
Title = TextLog: TSLicenseCount.txt
Comment =
RelatedURL =
ScheduleMode= Regular
Schedule = Mo-Su 0000-2359
Interval = 60
Alerts = ANet01Test
ReverseAlert= No
UnknownIsBad= No
UseCommonLog= Yes
PrivLogMode = Default
CommLogMode = Default
;--- Test specific properties ---
File = D:\SystemMaintenance\LicenseControl\TSLicenseCount.txt
FileMacros = No
LookFor = SomethingThatDoesNotExist
LookMode = String
MatchCase = No
WholeWord = No
UseMacros = No
AlertMode = AllEvents
ReplyMode = Line
ReplyFilter = WholeLine
ReplyRange1 = 10
ReplyRange2 = 15

The Alert Profile for ANet01Test:

Bad Action:

Execute external program by RMA "ANet01 (AD/Fil/Print)": cmd /c D:\SystemMaintenance\LicenseControl\KasperTest.bat D:\SystemMaintenance\LicenseControl\TSLicenseCount.txt

Good Action:

Execute external program by RMA "ANet01 (AD/Fil/Print)": cmd /c D:\SystemMaintenance\LicenseControl\KasperTest.bat D:\SystemMaintenance\LicenseControl\TSLicenseCount.txt

The file TSLicenseCount.txt is the one containing the string I would to see as reply and the file KasperTest.bat contains your script.

I currently check for the string SomethingThatDoesNotExist to make the test fail thus executing my Bad action.

Thanks for your help !

Kasper :O)
KS-Soft Europe
Posts: 2832
Joined: Tue May 16, 2006 4:41 am
Contact:

Post by KS-Soft Europe »

Kapz wrote:However, I still get no reply, but maybe I set up something wrong ?
Method = TextLog
Actually, foregoing script is designed for the Shell Script test method, not for the Text Log.
http://www.ks-soft.net/hostmon.eng/mfra ... m#chkShell
Kapz wrote:D:\SystemMaintenance\LicenseControl\KasperTest.bat
Sorry, I do not know what script KasperTest.bat should do.

It is supposed to be that you setup Shell Script test method, with "Start cmd" as "cmd /c %Script% %Params%", "Script" as foregoing script, and "Params" as full file name, like "D:\SystemMaintenance\LicenseControl\TSLicenseCount.txt ". Script returns the value into %Reply% field and you may assign, for instance Send Email action with %Reply% variable in mail template, to send this number back tou you.

Regards,
Max
Kapz
Posts: 216
Joined: Mon Dec 06, 2004 2:33 pm
Location: Denmark

Post by Kapz »

Max,

Ah, get it know - and it works like a charm, thanks ! :)

Very nice that your code can even be put into the "Script" field and called with %script% in the Start cmd line meaning that I don't need to copy the script to multiple servers.

One question however:

How many characters can %reply% contain ?

Kasper :O)
KS-Soft Europe
Posts: 2832
Joined: Tue May 16, 2006 4:41 am
Contact:

Post by KS-Soft Europe »

Kapz wrote:Ah, get it know - and it works like a charm, thanks ! :)
You are welcome!
Kapz wrote:Very nice that your code can even be put into the "Script" field and called with %script% in the Start cmd line meaning that I don't need to copy the script to multiple servers.
It is not an advantage of my code, it is an advantage of Shell Script test method by design.
Kapz wrote:How many characters can %reply% contain ?
It depends on test. Anyway, I would not recommend you to use long data in files, that are checked by this script. And please note: foregoing script returns only ONE line from the text file. If there are several lines in the file, script returns the last one.

Regards,
Max
Kapz
Posts: 216
Joined: Mon Dec 06, 2004 2:33 pm
Location: Denmark

Post by Kapz »

Max,

> If there are several lines in the file, script returns the last one.
Yes, that's what I noticed so we've now put everything on a single line seperated with commas.
This way 8 KB can be transferred through your script although the %reply% macro beeing used in an action in an Alert profile only shows the first 4 KB.

Thanks again for your help !

Kasper :O)
KS-Soft Europe
Posts: 2832
Joined: Tue May 16, 2006 4:41 am
Contact:

Post by KS-Soft Europe »

Kapz wrote:This way 8 KB can be transferred through your script although the %reply% macro beeing used in an action in an Alert profile only shows the first 4 KB.
Correct. The maximum length of the %Reply% variable is 4KB.

Regards,
Max
Kapz
Posts: 216
Joined: Mon Dec 06, 2004 2:33 pm
Location: Denmark

Post by Kapz »

Max,

Within Script Manager my "Start cmd" field currently looks like this:

cmd /c %script% D:\SystemMaintenance\LicenseControl\TSLicenseCount.txt

The script you provided is then placed in the big "Script" field.

This works great.
However, the script is beeing called with a parameter - in this case "D:\SystemMaintenance\LicenseControl\TSLicenseCount.txt".
Is there a way to set this parameter somewhere else than in the "Start cmd" field so that this can be customized in the individual tests that calls the script ?

Thanks !

Kasper
Kapz
Posts: 216
Joined: Mon Dec 06, 2004 2:33 pm
Location: Denmark

Post by Kapz »

Whops - Never Mind, found that I needed to add %params% to the "Start cmd" field in order to use supplied parameters from my Shell Script test.

Sorry for the noise.

/Kasper
KS-Soft Europe
Posts: 2832
Joined: Tue May 16, 2006 4:41 am
Contact:

Post by KS-Soft Europe »

Kapz wrote:Whops - Never Mind, found that I needed to add %params% to the "Start cmd" field in order to use supplied parameters from my Shell Script test.
Yes, %Params% directive is requred to pass parameters. Quote from the manual:
========================
%Params% - Scripts often require some parameters, you can specify the list of parameters in the Test Properties dialog for each test item separately. This allows using the same script to check different parameters on different target systems. To pass parameters to the script use %Params% variable, HostMonitor will replace this variable by parameters specified for the test item.
==========================

Regards,
Max
Post Reply