Pattern template relating hosts to locationsThis is the Template Relating Hosts to Locations template which is supplied with Tideway Foundation. IntroductionThis is a template pattern module containing a pattern for relating hosts to locations. Names surrounded by double dollar signs like $$pattern_name$$ should all be replaced with values suitable for the pattern. // This is a template pattern module containing a pattern for // relating Hosts to Locations based on hostname. // // Names surrounded by double dollar signs like $$pattern_name$$ // should all be replaced with values suitable for the pattern. // // Text prefixed with // like these lines are comments that extend to // the end of the line. // // This pattern is in the public domain. Module declarationtpl 1.0 module HostLocationTemplate; Table declaration table LocationIdToName 1.0
"$$id1$$" -> "$$name1$$";
"$$id2$$" -> "$$name2$$";
default -> none;
end table;
Pattern declaration pattern HostToLocation 1.0
"""
This is a template pattern for linking Hosts to Locations based on
hostname.
"""
Overview overview
tags $$tags$$;
end overview;
Triggers // We trigger on DeviceInfo, since a new DeviceInfo is created each
// time a Host is discovered.
triggers
on device_info := DeviceInfo where hostname exists;
end triggers;
Body body
host := model.host(device_info);
// There may not be a Host corresponding to the DeviceInfo, in the
// case that the device is a router or printer, for example. If
// there is no Host, stop executing the pattern.
if not host then
stop;
end if;
// Extract location id from the host name. e.g. to extract the
// first two characters, use a regular expression of "^(..)".
location_id := regex.extract(host.name, regex "$$location_regex$$", regex "\1");
// Look up the location name
location_name := LocationIdToName[location_id];
if not location_name then
// Unknown location
stop;
end if;
// Find existing Location relationships so we can destroy any that
// lead to incorrect locations.
already_linked := false;
location_rels := host.#ElementInLocation:Location;
for location_rel in location_rels do
if location_rel.#:Location:Location.name = location_name then
already_linked := true;
else
model.destroy(location_rel);
end if;
end for;
if not already_linked then
// Find the Location node(s) for the location
location := search(Location where name = %location_name%);
// Create relationship
model.rel.Location(ElementInLocation := host, Location := location);
end if;
end body;
Endend pattern; Return to Template Patterns. |
Labels:
None
