4 Replies Latest reply on Jul 12, 2018 6:05 AM by Matt Lambie

    How to read specific record in a file using TPL

    Mahesh VP

      Hello,

       

      I need a help on how to read a specific record in a file.

      My use case is as below:

       

      I am running a command to read the file

       

      command: sudo /ttc/ans/gats.o/base_gats.gat

      Output of the command is : {"cb_cluster": 125864, "cb_cluster_mem": false, "cb_socket": "ddpttr"}

      In the above output I want to separate value of cb_socket(only this value), split that value and store it into two different variables like below;

       

      if "cb_socket": "ddpttr"

       

      Env= ddp

      partion = ttr

       

      And display above two attributes in the software instance.

       

      Please find my TPL attached.

       

      Thanks in advance.

       

      Thank you,

      Mahesh V

        • 1. Re: How to read specific record in a file using TPL
          Andrew Waters

          Within the if matching the expected format you could just slice the value without the for loop

          Environment := RTU[:3];

          Partition := RTU[3:];

          You fail to initialise Environment and Partition if RTU does not match the expected format which will cause an ECAError.

          2 of 2 people found this helpful
          • 2. Re: How to read specific record in a file using TPL
            Mahesh VP

            Thanks for the reply Andrew,

             

            It is matching the expected format(tested using regex test) but getting below ECA error after scanning the server.

             

            RuleError on rule tpl_build_SI_RTU_Couch_body_1 due to: Error while executing a rule -- TypeError: string indices must be integers, not str

             

            File "./__init__.py", line 770, in processEvent File "./rule.py", line 129, in doExecute File "/usr/tideway/var/code/generated_code/software_instance_model/RTU_Couch/__init__.py", line 179, in execute llr_tpl_RTU = llr_tpl_record['cb_bucket']

             

            My TPL below:

             

            body

                    RTU := "";

                    RTU_Couch := "";

                   

                    file := discovery.runCommand(host, "sudo /ttc/ans/gats.o/base_gats.gat");

                    if file and file.result then

                       decoded := json.decode(file.result);

                       //log.debug ("%decoded%");

                        

                         if decoded then

                           for record in decoded do

                              if 'cb_socket' in record then

                                RTU := record['cb_socket'];

                                //log.debug ("%RTU%");

             

             

                                if RTU matches regex '^([a-zA-Z0-9]{3})([a-zA-Z0-9]{3})$' then

                                          Environment := RTU[:3];

                                          Partition := RTU[3:];

                                end if;

                           

                                si := model.SoftwareInstance(key := 'RTU_Couch + /%host.key% + /%RTU%',

                                                             type := 'RTU_Couch',

                                                             name := 'RTU_Couch' + ' %RTU%' + ' on %host.name%',

                                                             short_name := RTU_Couch,

                                                             Environment := Environment,

                                                             Partition := Partition,

                                                             _tw_meta_data_attrs := ['Environment', 'Partition']

                                                             );

                           

                               end if;

                             end for;

                          end if;

                      end if;

             

             

                end body;

             

             

            Thank you,

            Mahesh V

            • 3. Re: How to read specific record in a file using TPL
              Andrew Waters

              That would be because you JSON output just contains a dictionary, not a list of dictionaries.

               

              You just want

              if decoded then

                if 'cb_scoket' in decoded then

                  RTU := decoded['cb_scoket'];

               

              2 of 2 people found this helpful
              • 4. Re: How to read specific record in a file using TPL
                Matt Lambie

                This one pulls multiple results hence the for loop...

                 

                reg_extracts := regex.extractAll(svcPath,regex'"(.*\\PASS.*)\\(\S+).exe"');

                for reg_extract in reg_extracts do

                     progFolder := reg_extract[0];

                     procName := reg_extract[1];

                2 of 2 people found this helpful