Getting contents from a text file
Getting contents from a text file
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)
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)
-
- Posts: 2832
- Joined: Tue May 16, 2006 4:41 am
- Contact:
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
Regards,
Max
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)
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)
-
- Posts: 2832
- Joined: Tue May 16, 2006 4:41 am
- Contact:
You may try following script: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.)
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
Yes, there is similar script, but much simplier.Kapz wrote:It seems like I'm trying to accomplish what dba asked about on Dec. 8th.
Please note, script may not work properly with folders with space in the name, like "\Program files".
Regards,
Max
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)
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)
-
- Posts: 2832
- Joined: Tue May 16, 2006 4:41 am
- Contact:
Actually, foregoing script is designed for the Shell Script test method, not for the Text Log.Kapz wrote:However, I still get no reply, but maybe I set up something wrong ?
Method = TextLog
http://www.ks-soft.net/hostmon.eng/mfra ... m#chkShell
Sorry, I do not know what script KasperTest.bat should do.Kapz wrote:D:\SystemMaintenance\LicenseControl\KasperTest.bat
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
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)
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)
-
- Posts: 2832
- Joined: Tue May 16, 2006 4:41 am
- Contact:
You are welcome!Kapz wrote:Ah, get it know - and it works like a charm, thanks !
It is not an advantage of my code, it is an advantage of Shell Script test method by design.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 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.Kapz wrote:How many characters can %reply% contain ?
Regards,
Max
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)
> 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)
-
- Posts: 2832
- Joined: Tue May 16, 2006 4:41 am
- Contact:
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
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
-
- Posts: 2832
- Joined: Tue May 16, 2006 4:41 am
- Contact:
Yes, %Params% directive is requred to pass parameters. Quote from the manual: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.
========================
%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