10 Replies Latest reply on Sep 12, 2013 5:54 AM by Flavio Bonacordi

    TMART Monitor to check a Text File (not CSV)

    Flavio Bonacordi

      Hi Community,

       

      I'm trying to create a custom monitor that will read a text file and look for a specific string.

       

      I found some examples but related to CSV files (https://kb.bmc.com/infocenter/index?page=content&id=KA287501&actp=search&viewlocale=en_US&searchid=1378473459557)

       

      I come up with a very simple script using the File Functions and I'm able to find the "first instance" in the text file...

       

      dcltrans
        transaction TInit
        begin
        end TInit;
      transaction TMain
        var
          sFileName,sLog, sSearch      : string;
          hFile,sSize : number;
       begin
          sFileName:="\\\\hoeapp194\\ARLogs\\[08-22-13] - Thu - 01.30.22 PM\\arerror.log";
          
          FOpen(hFile, sFileName);
      
          sSearch:="fatal error encountered";
          
          if FFindString(hFile, sSearch) then
            print("word found");
          else
            print("word not found")
          end;
      end TMain;
      
      

      And I have a coulple of questions that I would like the Community help.

       

      1 - CSV funtions has options like FileGetNextRow Function FileGetFirstRow Function but I wasn't able to find something related to Plain Text Files. Is there any way to "keep reading" the file and find all the instances of that string?

      2 - My perfect solution would be read a line... analyze the line and go to the next one until the end of the file. Is that possible?

       

      Thanks in advance...

        • 1. Re: TMART Monitor to check a Text File (not CSV)
          Christophe Oggeri

          Maybe you can try to put each line in an array and use the strsearch function for each entries.

          • 2. Re: TMART Monitor to check a Text File (not CSV)
            Flavio Bonacordi

            That sounds promissing... but I will fall back in the my actual problem... how to read the txt file line by line.

            • 4. Re: TMART Monitor to check a Text File (not CSV)
              Flavio Bonacordi

              Hey Dan,

               

              Thanks for the help... after I submit this question... I hit, by my self, a result very close to the first example you found.

               

              But I'm still trying to read the file line by line...

               

              The second example is very interesting... would be perfect if I could set the split to the CRLF (ASCII code 13) and do that multiple times until the EOF.

              • 5. Re: TMART Monitor to check a Text File (not CSV)
                Christophe Oggeri

                Hi Flavio,

                 

                you can check also the strRegexFind..... functions.

                 

                I made a quick script :

                nPos1 := StrSearch(sData, "\r", STR_SEARCH_FIRST);

                writeln("line1:" + substr(sData,test,1,nPos1));

                   

                 

                    cpt := 1;

                    while (nPos1 < strlen(sData)-1) do

                      cpt := cpt + 1;

                      nPos := StrSearch(sData, "\r", STR_SEARCH_NEXT);

                      writeln("line" + string(cpt) + ":" + substr(sData,test,nPos1+2,nPos-nPos1-2));

                      nPos1 := nPos;

                    end;

                 

                the limitation is to have a break line at the end of the file, if not the script never stop...but the script is made to be enhanced

                • 6. Re: TMART Monitor to check a Text File (not CSV)
                  Flavio Bonacordi

                  Christophe... I don't have words to describe the size of the smile that your code bring to my life!!!

                   

                   

                   

                  I will work now enhanced the code and work to analize every line of my file to raise the alerts when I found the string I'm looking for...

                   

                  I will post the code here later when I finish!

                   

                  Thank you very much!!!

                  • 8. Re: TMART Monitor to check a Text File (not CSV)
                    Flavio Bonacordi

                    With Christophe collaboration I was able to come up with a custom script that allows you to verify a text file and look for a specific string. The monitor will Raise a Custom Error returning the entire line where the error was found.

                    Singles TRY SCRIPT in workbench works fine. I will start the testing phase in TMART and I'm wondering what will happend if I schedule the monitor to run every 5 minutes... pretty sure the monitor will "find" the same error over and over again until the log file changes... but it's a starting point and I'm looking for the Comunity assistance to overcame that

                     

                    behavior.

                    Thanks!

                     

                    For some reason that I don't understand the FOpen doesn't work when I use the AttributeGetString. But that can be solved with some research.

                     

                     

                    WebUser-Profile1_1 localhost 22 00:00:00 Function AttributeGetString "attLogFile" = "\\\\ARAdminDev\\ARLogs\\arerror.log"
                    WebUser-Profile1_1 localhost 23 00:00:00 Function AttributeGetString "attSearch" = "AR System server terminated when a signal/exception was received by the server"
                    WebUser-Profile1_1 localhost 25 00:00:00 Info Print \\\\ARAdminDev\\ARLogs\\arerror.log
                    WebUser-Profile1_1 localhost 33 00:00:00 Error FOpen RESERR: 1016 - Cannot find data file, File: \\\\ARAdminDev\\ARLogs\\arerror.log, Reason: The system cannot find the file specified.
                    WebUser-Profile1_1 localhost 37 00:00:00 Error FSizeGet RT: 58 - Invalid file handle: The parameter provided as a file handle is not valid.
                    WebUser-Profile1_1 localhost 38 00:00:00 Info Print file size = 0
                    WebUser-Profile1_1 localhost 41 00:00:00 Error FRead RT: 58 - Invalid file handle: The parameter provided as a file handle is not valid.
                    WebUser-Profile1_1 localhost 42 00:00:00 Info Print 0 bytes read
                    WebUser-Profile1_1 localhost 53 00:00:00 Error Substr RT: 100 - Invalid Usage, Position is beyond string length
                    WebUser-Profile1_1 localhost 53 00:00:00 Info Print line1:
                    WebUser-Profile1_1 localhost 92 00:00:00 Error FClose RT: 58 - Invalid file handle: The parameter provided as a file handle is not valid.
                    WebUser-Profile1_1 localhost 94 00:00:00 Transaction Exec: 0.01; Busy: 0.01 TMain
                    

                     

                    Where is the code...

                     

                     

                     

                    benchmark WebBenchmarkName
                    use"WebAPI.bdh"
                    use"Kernel.bdh"
                        
                    dcluser 
                        user
                          WebUser   
                        transactions
                            TMain : 1;
                    dcltrans
                        transaction
                          TMain
                          var
                            hFile, nRead, nSize,nPos1,cpt,nPos  : number;
                            sData,sFile,sSearch,sSearch1,sLine  : string;
                     
                        begin  
                       
                          // open text file for reading
                          sFile:="\\\\ARAdminDev\\ARLogs\\arerror.log"; 
                         
                           FOpen(hFile, sFile);
                          
                          // calculate and display the number of bytes in the file
                          FSizeGet(hFile, nSize);
                          print("file size = "+string(nSize)); 
                          // read nSize number of bytes into sData
                          FRead(hFile, sData, nSize, nRead);
                          print(string(nRead)+" bytes read");
                          
                          // string to search in every line 
                          
                          sSearch:="AR System server terminated when a signal/exception was received by the server";
                      
                          // breakes every line into a variable
                          
                          nPos1 := StrSearch(sData, "\r", STR_SEARCH_FIRST);
                          print("line1:" + substr(sData,sLine,1,nPos1));
                          cpt := 1;
                          while (nPos1 < strlen(sData)-1) do
                             cpt := cpt + 1;
                             nPos := StrSearch(sData, "\r", STR_SEARCH_NEXT);
                          
                             print("line" + string(cpt) + ": " + substr(sData,sLine,nPos1+2,nPos-nPos1-2)); 
                             sLine:=substr(sData,sLine,nPos1+2,nPos-nPos1-2);
                          
                             nPos1 := nPos;
                             // perform the search in the line for the string defined
                          
                             sSearch1 := Strstr(sLine, sSearch);
                          
                             print("String Found: "+sSearch1);
                          
                             print("Len sSearch1: "+string(strlen(sSearch1)));
                             // if the Strstr function return a lenght of ZERO it didn't find the string
                             if strlen(sSearch1) > 0 then
                                Print(sLine);
                                
                                // Raise a CUSTOM ERROR with the LINE where the string was found.
                                RaiseError(CUSTOM_MESSAGE,sLine,SEVERITY_ERROR);
                             end;
                          end;
                          
                          // close file
                          FClose(hFile);
                         end TMain;
                    
                    

                     

                    Once again.. the "CORE" for this script is the collaboration of Christophe that developed the read line by line routine.

                    • 9. Re: TMART Monitor to check a Text File (not CSV)
                      Christophe Oggeri

                      Hi Flavio,

                       

                      About the issue with the FOpen function, you can look at the antislashes thing, I remember there is a subtility with the use of the projects attributtes. Try without double antislashes.

                       

                      About the problem that the script will detect the same error, again and again, you can try to save somewhere the last line detected and ignore it in the script. You can save it in a text file if you use only one execution server or maybe something else like create a project attribute and insert the value inside...but you will have to use the web services of TMART to update the value in the script.

                      • 10. Re: TMART Monitor to check a Text File (not CSV)
                        Flavio Bonacordi

                        Yeah... thanks again Chris... when I come up with the solution I will post a new version of the code here...