Location for switches using SEARCH NetworkDevice WHERE (name HAS SUBWORD ‘?’) |
|
|
| Posted: 31 January 2012 07:38 PM |
[ Ignore ]
|
|
|
Member
Total Posts: 30
Joined: 2011-06-29
|
I know that their have been many post on this subject. But here is what I am looking to do.
I need to assign a location to a switch by its name I.e. (stl0123addm) whst i would like to do is a use a (SEARCH NetworkDevice WHERE (name HAS SUBWORD ‘stl’)) stl being the location, anybody have any suggestions?
|
|
|
|
|
|
| Posted: 03 February 2012 05:53 PM |
[ Ignore ]
[ # 1 ]
|
|
|
Member
Total Posts: 30
Joined: 2011-06-29
|
this is what I have come up with so far.
4 tpl 1.5 module NetworkDevice_Location; 5 6 7 metadata 8 origin := "location"; 9 end metadata; 10 11 12 13 pattern NetworkDevice_Location 1.0 14 ''' 15 Pattern for Location based upon NetworkDevice Name 16 17 This pattern triggers on a NetworkDevice being created and confirmed 18 19 This pattern expects Location nodes to be pre created in ADDM 20 21 22 23 ''' 24 25 // Required overview section. Some tags must be defined. 26 overview 27 tags NetworkDeviceLocation, Location; 28 end overview; 29 30 // We trigger whenever a NetworkDevice is created for the first time, or confirmed on 31 // a subsequent discovery run. 32 33 triggers 34 on NetworkDevice := NetworkDevice created, confirmed; 35 end triggers; 36 37 body 38 39 if NetworkDevice WHERE (name HAS SUBWORD 'stl') then location_name =: 'STL' 40 else if NetworkDevice WHERE (name HAS SUBWORD 'arn') then location_name =: 'ARN' 41 else if NetworkDevice WHERE (name HAS SUBWORD 'blt') then location_name =: 'BLT' 42 43 break; 44 end if; 45 46 47 log.debug("Subnet_2_Locations: location ID is %location_id%"); 48 49 // Relate NetworkDevice to location. Using "uniquerel" rather than "rel" means that 50 // any existing Location relationships between this NetworkDevice (the first 51 // parameter) and any Location nodes other than the one given (the second 52 // parameter) are removed. If a NetworkDevice has changed location, this keeps the 53 // model up-to-date. 54 log.info("Subnet_2_Locations: model.uniquerel.Location %NetworkDevice% %location%"); 55 model.uniquerel.Location( 56 ElementInLocation := NetworkDevice, 57 Location := location 58 ); 59 end body; 60 61 end pattern; 62
|
|
|
|
|
|
| Posted: 03 February 2012 06:35 PM |
[ Ignore ]
[ # 2 ]
|
|
|
Guru
Total Posts: 181
Joined: 2011-03-16
|
And does it work? If not, you could try this….
tpl 1.5 module NDLocation;
// Map Name to location
table NameToLocation 1.0 'stl' -> 'STL'; 'arn' -> 'ARN'; 'blt' -> 'BLT'; default -> 'Location unknown'; end table;
pattern NetworkDeviceToLocation 1.0 '''Pattern to determine and record location of a NetworkDevice.'''
overview tags Extension; end overview;
triggers on MyNetworkDevice := NetworkDevice created, confirmed; end triggers;
body // Get the NetworkDevice name. name := regex.extract(MyNetworkDevice.name, ^(\w{3}), raw "\1"); if name in NameToLocation then // Found a location. Ensure we have a Location node.
location := NameToLocation[name]; location_node := model.Location(key := text.hash(location), name := location);
// Relation NetworkDevice to location using uniquerel to ensure single location.
model.uniquerel.Location(ElementInLocation := MyNetworkDevice, Location := location_node);
end if; end body; end pattern;
|
|
|
|
|
|
| Posted: 03 February 2012 06:42 PM |
[ Ignore ]
[ # 3 ]
|
|
|
BMC ADDM Staff
Administrator
Total Posts: 876
Joined: 2008-02-12
|
It depends upon how good your naming system is. For example if you are using the first 3 letters, as in this example the first 3 characters.
// Trigger on creation and confirmation of NetworkDevices that have names.
triggers on device := NetworkDevice created, confirmed where name exists; end triggers;
body // Extract the first 3 characters of the name for location mapping. // Normalise the case to make is consistent.
location_name := text.upper(device.name[:3]);
Though slicing is only available in ADDM 8.3. You could use a regex to effectively do the same thing.
|
|
|
|
|
|
| Posted: 03 February 2012 06:54 PM |
[ Ignore ]
[ # 4 ]
|
|
|
Member
Total Posts: 30
Joined: 2011-06-29
|
the problem is that we have a large infrastructure and while we have a set standard now it has not made it to all the devices. But all the names do contain a 3 letter location some where in the naming convention. it is just not always located in the same spot. that is why I was tiring to do a contain type query but i cant seem to get it to work
|
|
|
|
|
|
| Posted: 03 February 2012 07:34 PM |
[ Ignore ]
[ # 5 ]
|
|
|
Guru
Total Posts: 181
Joined: 2011-03-16
|
Instead of SUBWORD, change it to SUBSTRING.
SUBWORD
a HAS SUBWORD b = Case-insensitive subword test.
a is split on white space and other delimiter characters. The condition is true if b is in the list of split words.
|
|
|
|
|
|
| Posted: 03 February 2012 07:54 PM |
[ Ignore ]
[ # 6 ]
|
|
|
Member
Total Posts: 30
Joined: 2011-06-29
|
i keep getting
Change to package NetworkDevice_Locations Failed because module NetworkDevice_Location ‘Syntax error at or near ‘WHERE’ at line 39’.
|
|
|
|
|
|
| Posted: 03 February 2012 07:58 PM |
[ Ignore ]
[ # 7 ]
|
|
|
Guru
Total Posts: 181
Joined: 2011-03-16
|
Try this
39 if NetworkDevice.name HAS SUBSTRING 'stl' then 40 location_name := 'STL'; 41 elif NetworkDevice.name HAS SUBSTRING 'arn' then 42 location_name := 'ARN'; 43 elif NetworkDevice.name HAS SUBSTRING 'blt' then 44 location_name := 'BLT'; 45 else 46 location_name := 'No clue'; 47 end if;
|
|
|
|
|
|
| Posted: 03 February 2012 08:02 PM |
[ Ignore ]
[ # 8 ]
|
|
|
Member
Total Posts: 30
Joined: 2011-06-29
|
i got this ( I am using 8.3)
Change to package NetworkDevice_Locations Failed because module NetworkDevice_Location ‘Syntax error at or near ‘HAS’ at line 38’.
// This is a pattern module containing a pattern for relating NetworkDevices // to Locations based on name. // tpl 1.5 module NetworkDevice_Location;
metadata origin := "location"; end metadata;
pattern NetworkDevice_Location 1.0 ''' Pattern for Location based upon NetworkDevice Name
This pattern triggers on a NetworkDevice being created and confirmed This pattern expects Location nodes to be pre created in ADDM
'''
// Required overview section. Some tags must be defined. overview tags NetworkDeviceLocation, Location; end overview; // We trigger whenever a NetworkDevice is created for the first time, or confirmed on // a subsequent discovery run. triggers on NetworkDevice := NetworkDevice created, confirmed; end triggers; body if NetworkDevice.name HAS SUBSTRING 'stl' then location_name := 'STL'; elif NetworkDevice.name HAS SUBSTRING 'arn' then location_name := 'ARN'; elif NetworkDevice.name HAS SUBSTRING 'blt' then location_name := 'BLT'; else location_name := 'No clue'; end if; log.debug("Subnet_2_Locations: location ID is %location_id%"); // Relate NetworkDevice to location. Using "uniquerel" rather than "rel" means that // any existing Location relationships between this NetworkDevice (the first // parameter) and any Location nodes other than the one given (the second // parameter) are removed. If a NetworkDevice has changed location, this keeps the // model up-to-date. log.info("Subnet_2_Locations: model.uniquerel.Location %NetworkDevice% %location%"); model.uniquerel.Location( ElementInLocation := NetworkDevice, Location := location ); end body;
end pattern;
|
|
|
|
|
|
| Posted: 03 February 2012 08:17 PM |
[ Ignore ]
[ # 9 ]
|
|
|
Guru
Total Posts: 181
Joined: 2011-03-16
|
Sorry. Change any of the UPPERCASE command words to lower case. I had that problem at one time as well. If that does not work, change the “has substring” to “matches regex”
Also, your log entry will be nasty unless you use the NetworkDevice.name in it instead of NetworkDevice
|
|
|
|
|
|
| Posted: 03 February 2012 09:02 PM |
[ Ignore ]
[ # 10 ]
|
|
|
Member
Total Posts: 30
Joined: 2011-06-29
|
So I got the code to load and ran the pattern against the network devices and it told me that all devices met the trigger and it completed good. When I check the location it still dose not register/ any suggestions.
// This is a pattern module containing a pattern for relating NetworkDevices // to Locations based on IP Subnet Range. // tpl 1.5 module NetworkDevice_Location;
metadata origin := "location"; end metadata;
pattern NetworkDevice_Location 1.0 ''' Pattern for Location based upon NetworkDevice Name
This pattern triggers on a NetworkDevice being created and confirmed This pattern expects Location nodes to be pre created in ADDM
'''
// Required overview section. Some tags must be defined. overview tags NetworkDeviceLocation, Location; end overview; // We trigger whenever a NetworkDevice is created for the first time, or confirmed on // a subsequent discovery run. triggers on NetworkDevice := NetworkDevice created, confirmed; end triggers; body if NetworkDevice.name has substring 'stl' then location_name := 'STL'; elif NetworkDevice.name has substring 'arn' then location_name := 'ARN'; elif NetworkDevice.name has substring 'blt' then location_name := 'BLT'; else location_name := 'No clue'; end if; end body;
end pattern;
|
|
|
|
|
|
| Posted: 03 February 2012 09:26 PM |
[ Ignore ]
[ # 11 ]
|
|
|
Guru
Total Posts: 181
Joined: 2011-03-16
|
You are missing the most important part…
location_node := model.Location(key := blah blah blah, name := location_name); //Do this is location don't already exist.
// Relation NetworkDevice to location using uniquerel to ensure single location. model.uniquerel.Location(ElementInLocation := NetworkDevice, Location := location_node);
|
|
|
|
|
|
| Posted: 03 February 2012 09:46 PM |
[ Ignore ]
[ # 12 ]
|
|
|
Member
Total Posts: 30
Joined: 2011-06-29
|
something like this ?
// This is a pattern module containing a pattern for relating NetworkDevices // to Locations based on IP Subnet Range. // tpl 1.5 module NetworkDevice_Location;
metadata origin := "location"; end metadata;
pattern NetworkDevice_Location 1.0 ''' Pattern for Location based upon NetworkDevice Name
This pattern triggers on a NetworkDevice being created and confirmed This pattern expects Location nodes to be pre created in ADDM
'''
// Required overview section. Some tags must be defined. overview tags NetworkDeviceLocation, Location; end overview; // We trigger whenever a NetworkDevice is created for the first time, or confirmed on // a subsequent discovery run. triggers on NetworkDevice := NetworkDevice created, confirmed; end triggers; body if NetworkDevice.name has substring 'stl' then location_name := 'STL'; elif NetworkDevice.name has substring 'arn' then location_name := 'ARN'; elif NetworkDevice.name has substring 'blt' then location_name := 'BLT'; else location_name := 'No clue'; end if; location := NameToLocation[name]; location_node := model.Location(key := text.hash(location), name := location);
// Relation NetworkDevice to location using uniquerel to ensure single location.
model.uniquerel.Location(ElementInLocation := MyNetworkDevice, Location := location_node); end body;
end pattern;
|
|
|
|
|
|
| Posted: 04 February 2012 01:22 AM |
[ Ignore ]
[ # 13 ]
|
|
|
Guru
Total Posts: 181
Joined: 2011-03-16
|
You can leave off the:
location := NameToLocation[name];
As this is if you had a table at the top that you reference to get the location.
You also need to change location to location_name, and MyNetworkDevice to NetworkDevice.
This is the full thing…
// This is a pattern module containing a pattern for relating NetworkDevices // to Locations based on IP Subnet Range. // tpl 1.5 module NetworkDevice_Location;
metadata origin := "location"; end metadata;
pattern NetworkDevice_Location 1.0 ''' Pattern for Location based upon NetworkDevice Name
This pattern triggers on a NetworkDevice being created and confirmed This pattern expects Location nodes to be pre created in ADDM
'''
// Required overview section. Some tags must be defined. overview tags NetworkDeviceLocation, Location; end overview; // We trigger whenever a NetworkDevice is created for the first time, or confirmed on // a subsequent discovery run. triggers on NetworkDevice := NetworkDevice created, confirmed; end triggers; body if NetworkDevice.name has substring 'stl' then location_name := 'STL'; elif NetworkDevice.name has substring 'arn' then location_name := 'ARN'; elif NetworkDevice.name has substring 'blt' then location_name := 'BLT'; else location_name := 'No clue'; end if; location_node := model.Location(key := text.hash(location_name), name := location_name);
// Relation NetworkDevice to location using uniquerel to ensure single location.
model.uniquerel.Location(ElementInLocation := NetworkDevice, Location := location_node); end body;
end pattern;
|
|
|
|
|
|
| Posted: 06 February 2012 02:47 PM |
[ Ignore ]
[ # 14 ]
|
|
|
Member
Total Posts: 30
Joined: 2011-06-29
|
It is still not working. when i run the pattern against the network nodes then check the locations there is nothing there.
|
|
|
|
|
|
| Posted: 06 February 2012 02:54 PM |
[ Ignore ]
[ # 15 ]
|
|
|
Member
Total Posts: 30
Joined: 2011-06-29
|
in the history it is adding the node then removing it.
Mon Feb 6 06:34:10 2012 Reasoning Related Location (InferredElement) removed ARN
Mon Feb 6 06:34:10 2012 Reasoning Related Location added Between ARN & ARN
Mon Feb 6 06:34:10 2012 Reasoning Related Location (InferredElement) added ARN
Mon Feb 6 06:34:10 2012 Reasoning Related Location (Location) added ARN
Mon Feb 6 06:34:10 2012 Reasoning Related Pattern Execution (PatternExecution) added NetworkDevice_Location.NetworkDevice_Location run by me @ 2012-02-06 06:34:10.61
|
|
|
|
|