Number of entries in a textfile

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
Christian
Posts: 3
Joined: Thu Nov 29, 2012 8:15 am

Number of entries in a textfile

Post by Christian »

Hi!

I have a text file with the following entries:

p5872 rdp-tcp#1 2 Aktiv . 29.11.2012 10:08
p5871 rdp-tcp#1 2 Aktiv . 29.11.2012 11:08
p5870 rdp-tcp#1 2 Aktiv . 29.11.2012 12:08
p5869 rdp-tcp#1 2 Aktiv . 29.11.2012 13:08
p5868 rdp-tcp#1 2 Aktiv . 29.11.2012 14:08

I would like to count the number of entries with the string 'rdp-tcp' and send an alert to an email adress every hour.

Is this possible?

Thank you!

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

Post by KS-Soft Europe »

In some cases you may use Text Log test for this purpose.
Could you please give more information:
1. File name is static or dynamic (new day - new file, e.g. 2012-11-29.txt) ?
2. Do you want to count new/added specific records (lines), or count specific records(lines) in entire file ?
I would like to count the number of entries with the string 'rdp-tcp' and send an alert to an email adress every hour.
Every hour ?
or, maybe you want to get alert when records count is less or more then expected ?
Christian
Posts: 3
Joined: Thu Nov 29, 2012 8:15 am

Post by Christian »

Hi!

Thanks for your reply!

The filename is static and the file is newly created every 15 min. So we have to count the whole number of entries.
I want to be informed about the number of entries every hour.

Best regards!
Christian
KS-Soft Europe
Posts: 2832
Joined: Tue May 16, 2006 4:41 am
Contact:

Post by KS-Soft Europe »

The filename ... is newly created every 15 min. ...
I want to be informed about the number of entries every hour.
This is a little bit confusing.
If file is created/overwriten every 15 minutes, how do you want do count records (every hour) in files (4 files per hour) that have been already overwritten ?
E.g.:
10:15 file.txt contain 12 Bad records
10:30 file.txt is overwritten and now contains 11 Bad records
10:45 file.txt is overwritten and now contains 6 Bad records
11:00 file.txt is overwritten and now contains 2 Bad records
HostMonitor can only detect 2 Bad records in last file.txt, because
3 previous files have been overwritten.

Or maybe I haven't understood you correctly ?
KS-Soft Europe
Posts: 2832
Joined: Tue May 16, 2006 4:41 am
Contact:

Post by KS-Soft Europe »

If you need just count lines that contains some text in entire file, you may use Shell Script test with custom made script.
E.g. Script may look like the following:

Code: Select all

Option Explicit
On Error Resume Next

const statusUnknown     = "scriptRes:Unknown:"
const statusOk          = "scriptRes:Ok:"
const statusBad         = "scriptRes:Bad:"

dim objArgs, FileName, Find, objFSO, objTextFile, cnt, strtext, MaxCount
Set objArgs = WScript.Arguments

IF objArgs.Count < 2 Then
 WScript.StdOut.Write statusUnknown & "Expected parameters: <file path> <text to search> [<max count>]"
 Wscript.Quit
End if

FileName = objArgs(0)
Find = objArgs(1)
If objArgs.Count > 2 Then
  MaxCount = objArgs(2)
Else
  MaxCount = -1
End If

Set objFSO = CreateObject("Scripting.FileSystemObject")
if objFSO.FileExists(FileName)=False then
  WScript.StdOut.Write statusUnknown & "File not found: " & FileName
  WScript.Quit
End if

Set objTextFile = objFSO.OpenTextFile(FileName, 1)

cnt=0

Do While (objTextFile.AtEndOfStream <> True)
  strtext = objTextFile.ReadLine
  if InStr(1, strtext, Find,0) Then
    cnt=cnt+1
  End If
Loop

if (cnt > int(MaxCount)) AND (MaxCount <> -1) then
  WScript.StdOut.Write statusBad & cnt
Else
  WScript.StdOut.Write statusOk & cnt
End if
Start CMD: cmd /c cscript /B /E:VBScript %Script% %Params%
This script requires 2 or 3 parameters: <file path> <text to search> [<max count>].
Please note, if you are using file path or search text that contain spaces, you should quote them.
e.g. Script parameters may look like the following:
"C:\Program Files(x86)\Logs\log.txt" rdp-tcp 20
Christian
Posts: 3
Joined: Thu Nov 29, 2012 8:15 am

Post by Christian »

Hi!

Sorry for confusing you.
The number of entries (=active sessions on rdp sessionhosts) mainly should be displayed on Hostmonitor Web Interface.
I just want to receive an email with the number of entries once an hour (could be every 15 min too).

Thank you and best regards!
Christian
KS-Soft Europe
Posts: 2832
Joined: Tue May 16, 2006 4:41 am
Contact:

Post by KS-Soft Europe »

Have tried the script ?
Does it work as expected ?
Thank you and best regards!
You are welcome!
Kris
Posts: 375
Joined: Wed May 12, 2010 3:22 am

Post by Kris »

Just my 2 cents, but why not check the number of rdp session directly from the Performance Counter? Assuming you have a Windows Terminal server of course :-)

\\<SERVER_NAME>\Terminal Services\Active Sessions
Post Reply