1 2 Previous Next 16 Replies Latest reply on Dec 13, 2019 12:45 PM by Andrew Waters

    NetworkDevices location TPL

    Ajay Purushothaman
      Share This:

      Hi Team,

       

      I just wrote a TPL which fetches a location name for the network devices using 3 letters/characters of their name using regex commands. But i am facing some problem in my TPL. Please try to assist.

       

      In my infra i have many network devices across different Location like.

       

      PHL01-ABC.xyx.com    ----- Phillipines

      AS-PHL01.xyc.com       ------- Phillipines

      AT-01-ABC.xyz.com     ------ Atlanta

       

      I have kept my TPL with regex where first it looks for first 3 character. If not, it moves to first 3 characters after an "-" symbol, where ever it matches my lookup table.

       

      I uploaded the TPL, when i ran it for few network devices it gives me Result of Location as "U". Which so weird for me. Can you please verify my TPL and assist me with the change.

       

      tpl 1.5 module NetworkDevice.Locations;

       

       

      metadata

          origin := "XYZ";

      end metadata;

       

       

       

       

      table DataCenterLocations 1.0

      'phl'  -> 'Philippines';

      'at-'  -> 'Atlanta';

       

      default -> 'UNKNOWN'; // This shouldn't happen!

      end table;    

       

       

      pattern NetworkDeviceLocations 1.0

      '''

          NetworkDevice Location rules

      '''

       

       

          overview

              tags Location;

          end overview;

       

       

          triggers

              on device := NetworkDevice created, confirmed;

          end triggers;

       

       

          body

          

              tmp := regex.extract(device.name, regex'^((?i)([a-z0-9-]{3}))', raw'\1');

              if not tmp then

                tmp := regex.extract(device.name, regex'(\w+)-(?i)([a-z0-9-]{3})', raw'\2');

              end if;

             

             

              dc := DataCenterLocations[tmp];

               

              name := dc[0];

                 

              if name = '' then

                 

                  stop;

              end if;

       

       

              // Get DC Location node

              dc_location := model.Location(

                             

                              name := name

                              );

       

             

             model.uniquerel.Location(

            ElementInLocation := device,  Location := dc_location );

          

          end body;

       

       

      end pattern;

       

      Andrew Waters Please try to assist me on this.

        • 1. Re: NetworkDevices location TPL
          Ajay Purushothaman

          Andrew Waters

           

          Please assist me on above issue

          • 4. Re: NetworkDevices location TPL
            Andrew Waters

            This is what you told it to do. The default value is "UNKNOWN": so

            dc := DataCenterLocations[tmp];

            sets dc to UKNOWN and then for some reason you do

            name := dc[0];

            which picks the first character "U".

             

            As to how you get the location that is difficult because your examples do not match your description. The third example has two characters but your text talks about three.

            • 5. Re: NetworkDevices location TPL
              Ajay Purushothaman

              Hi Andrew,

               

              Ideally it shouldn't turn "UNKNOWN". It should show me "Philippines" as location.

               

              I didn't include two characters. It includes 3 character, it takes "-" also. If you see my regex you will get to know.

               

              Example1: PHL01-ABC.xyx.com    ----- Phillipines

              AT-01-ABC.xyz.com     ------ Atlanta

               

              Here first 3 character is PHL this gives Philippines.

              AT- : this gives Atlanta, as i have included "-" in the regex. So it takes first 3 character.

               

              My regex: regex'^((?i)([a-z0-9-]{3}))', raw'\1'

               

              Example2: AS-PHL01.xyc.com       ------- Phillipines

               

              Here my regex takes 3 character after "-". which is PHL and it should give Philippines as i take the second group to display.

               

              My Regex: regex'(\w+)-(?i)([a-z0-9-]{3})', raw'\2'

               

              Now, Please let me know what changes should i make to get my values.

              • 6. Re: NetworkDevices location TPL
                Brice-Emmanuel Loiseaux

                What about you adding logger right after your regex section to display what you get into tmp variable? You will likely see what you extract does not match 'phl' or 'at-' but at best 'PHL' or 'AT-'.

                • 7. Re: NetworkDevices location TPL
                  Ajay Purushothaman

                  Hi Brice,

                   

                  Can you please let me know at which line should i make the changes.

                   

                  -Thanks

                  • 8. Re: NetworkDevices location TPL
                    Andrew Waters

                    If you set your table to

                    table DataCenterLocations 1.0

                      'phl'  -> 'Philippines';

                      'at-'  -> 'Atlanta';

                      default -> ''; // This shouldn't happen!

                    end table;

                    Then you can just do something like

                    name:= DataCenterLocations[device.name[:3]];

                     

                    if not name then

                        parts := text.split(device.name, '-');

                     

                        if size(parts) > 1 then

                            name := DataCenterLocations[parts[1][:3]];

                        end if;

                     

                        if not name then

                            stop;

                        end if;

                    end if;

                    • 9. Re: NetworkDevices location TPL
                      Bob Anderson

                      When working with regular expressions, it is very handy to use a regular expression testing tool. 

                       

                      The one we use most ofter is RegexCoach.  The Regex Coach - interactive regular expressions

                      There are also several online regular expression testing sites.  Try this one for size : https://regex101.com/

                       

                      Also helpful are logging statements.  I would use something like this following your regular expression exract:

                       

                      log.info("some_meaningful_tag : extract of location : -->%tmp%<--")

                       

                      Using your example data, your first regular expression [ ^((?i)([a-z0-9-]{3})) ] has these matches:

                      (NOTE: It ALWAYS matches the first 3 characters of the name, so the second regex is never used)

                       

                      PHL01-ABC.xyx.com -----> Phillipines from the lookup

                      AS-PHL01.xyc.com  -----> Unknown from the lookup

                      AT-01-ABC.xyz.com -----> Atlanta from the lookup

                       

                      Making an assumption that the 3 character 'location' portion of the name never has digits, and is always followed by 2 digits, try this regular expression

                      If this assumption is wrong, then this regex is also wrong, but based on your limited list of data, this is what I came up with.

                       

                      ([\a-zA-Z-]{3})\d{2}

                      (NOTE: it matches 3 alpha or hyphen characters followed by 2 digits

                       

                      PHL01-ABC.xyx.com -----> Phillipines

                      AS-PHL01.xyc.com  -----> Phillipines

                      AT-01-ABC.xyz.com -----> Atlanta

                       

                      Both these cases are easily seen in the above online testing tool:

                      ^((?i)([a-z0-9-]{3}))

                       

                      ([\a-zA-Z-]{3})\d{2}

                       

                       

                      hth

                       

                      Bob

                      1 of 1 people found this helpful
                      • 10. Re: NetworkDevices location TPL
                        Andrew Waters

                        There really is no need to use regexes to do this.

                        • 11. Re: NetworkDevices location TPL
                          Andrew Waters

                          Note that if you really do have upper case characters then you need to have text.lower otherwise you will not find the entry in the table.

                          name:= DataCenterLocations[text.lower(device.name[:3])];

                           

                          if not name then

                              parts := text.split(device.name, '-');

                           

                              if size(parts) > 1 then

                                  name := DataCenterLocations[text.lower(parts[1][:3])];

                              end if;

                           

                              if not name then

                                  stop;

                              end if;

                          end if;

                          1 of 1 people found this helpful
                          • 12. Re: NetworkDevices location TPL
                            Ajay Purushothaman

                            Hi Andrew,

                             

                            I still get "UNKNOWN" as value from below TPL as you mentioned. Please assist i have kept the TPL below.

                             

                            tpl 1.5 module NetworkDevice.Locations;

                             

                             

                            metadata

                                origin := "TransUnion";

                            end metadata;

                             

                             

                            table DataCenterLocations 1.0

                            'phl'  -> 'Philippines';

                            'atl'  -> 'Atlanta';

                            'ph-'  -> 'Aus';

                             

                             

                            default -> 'UNKNOWN'; // This shouldn't happen!

                            end table;    

                             

                             

                            pattern NetworkDeviceLocations 1.0

                            '''

                                NetworkDevice Location rules

                            '''

                             

                             

                                overview

                                    tags Location;

                                end overview;

                             

                             

                                triggers

                                    on device := NetworkDevice created, confirmed;

                                end triggers;

                             

                             

                            body

                             

                             

                            name:= DataCenterLocations[text.lower(device.name[:3])];

                             

                            if not name then

                                parts := text.split(device.name, '-');

                             

                                if size(parts) > 1 then

                                    name := DataCenterLocations[text.lower(parts[1][:3])];

                                end if;

                             

                                if not name then

                                    stop;

                                end if;

                            end if;

                             

                             

                            dc_location := model.Location(

                                                   

                                                    name := name

                                                    );

                             

                             

                                    // Link Host to DC location

                                   model.uniquerel.Location(

                                  ElementInLocation := device,  Location := dc_location );

                             

                             

                            end body;

                            end pattern;

                            • 13. Re: NetworkDevices location TPL
                              Ajay Purushothaman

                              Hi Bob,

                               

                              Thanks for your valuable information.

                               

                              But i have few device name like AS-PH-ROUTE.xyz.com

                               

                              Here PH- point to Philippines which i have already mentioned in the lookup table.

                               

                              Is there any possibility where you can add this group as well to your existing regex which is : ([\a-zA-Z-]{3})\d{2}

                               

                              My list has device like and where i get values, I have mentioned below.

                               

                              PHL01-ABC.xyx.com -----> Phillipines  (I can get this value)

                              AS-PHL01.xyc.com  -----> Phillipines   (I can get this value)

                              AT-01-ABC.xyz.com -----> Atlanta       (I can get this value)

                              AS-PH-ROUTE.xyz.com  ---> Phillipines   (I can't get this value because there is no digits, Is there any possibility where you can add this group as well)

                               

                               

                              Regards,

                              Ajay

                              • 14. Re: NetworkDevices location TPL
                                Bob Anderson

                                Your lookup into the table will always return something, including the default value of 'UNKNOWN'

                                 

                                Change your default to the empty string like Andrew suggested

                                1 2 Previous Next