# trigger.rancid — RANCID Compatibility Library¶

Parse RANCID db files so they can be converted into Trigger NetDevice objects.

New in version 1.2.

Far from complete. Very early in development. Here is a basic example.

>>> from trigger import rancid
>>> rancid_root = '/path/to/rancid/data'
>>> r = Rancid(rancid_root)
>>> dev = r.devices.get('test1-abc.net.aol.com')
>>> dev
RancidDevice(nodeName='test-abc.net.aol.com', manufacturer='juniper', deviceStatus='up', deviceType=None)


Another option if you want to get the parsed RANCID data directly without having to create an object is as simple as this:

>>> parsed = rancid.parse_rancid_data('/path/to/dancid/data')


Or using multiple RANCID instances within a single root:

>>> multi_parsed = rancid.parse_rancid_data('/path/to/rancid/data', recurse_subdirs=True)

trigger.rancid.parse_rancid_file(rancid_root, filename='router.db', fields=None, delimiter=':')

Parse a RANCID file and return generator representing a list of lists mapped to the fields.

Parameters: rancid_root – Where to find the file filename – Name of the file to parse (e.g. router.db) fields – (Optional) A list of field names used to map to the device data delimiter – (Optional) Field delimiter
trigger.rancid.parse_devices(metadata, parser)

Iterate device metadata to use parser to create and return a list of network device objects.

Parameters: metadata – A collection of key/value pairs (Generally returned from parse_rancid_file) parser – A callabale used to create your objects
trigger.rancid.walk_rancid_subdirs(rancid_root, config_dirname='configs', fields=None)

Walk the rancid_root and parse the included RANCID files.

Returns a dictionary keyed by the name of the subdirs with values set to the parsed data for each RANCID file found inside.

>>> from trigger import rancid
>>> subdirs = rancid.walk_rancid_subdirs('/data/rancid')
>>> subdirs.get('network-security')
{'router.db': <generator object <genexpr> at 0xa5b852c>,
'routers.all': <generator object <genexpr> at 0xa5a348c>,
'routers.down': <generator object <genexpr> at 0xa5be9dc>,
'routers.up': <generator object <genexpr> at 0xa5bea54>}

Parameters: rancid_root – Where to find the file config_dirname – If the ‘configs’ dir is named something else fields – (Optional) A list of field names used to map to the device data
trigger.rancid.parse_rancid_data(rancid_root, filename='router.db', fields=None, config_dirname='configs', recurse_subdirs=False)

Parse single or multiple RANCID instances and return an iterator of the device metadata.

A single instance expects to find ‘router.db’ in rancid_root.

If you set recurise_subdirs, multiple instances will be expected, and a router.db will be expected to be found in each subdirectory.

Parameters: rancid_root – Where to find the file filename – Name of the file to parse (e.g. router.db) fields – (Optional) A list of field names used to map to the device data config_dirname – If the ‘configs’ dir is named something else recurse_subdirs – Whether to recurse directories (e.g. multiple instances)
trigger.rancid.gather_devices(subdir_data, rancid_db_file='router.db')

Returns a chained iterator of parsed RANCID data, based from the results of walk_rancid_subdirs.

This iterator is suitable for consumption by parse_devices or Trigger’s NetDevices.

Parameters: rancid_root – Where to find your RANCID files (router.db, et al.) rancid_db_file – If it’s named other than router.db
class trigger.rancid.Rancid(rancid_root, rancid_db_file='router.db', config_dirname='configs', device_fields=None, device_class=None, recurse_subdirs=False)

Holds RANCID data. INCOMPLETE.

Defaults to a single RANID instance specified as rancid_root. It will parse the file found at rancid_db_file and use this to populate the devices dictionary with instances of device_class.

If you set recurse_subdirs, it is assumed that rancid_root holds one or more individual RANCID instances and will attempt to walk them, parse them, and then aggregate all of the resulting device instances into the devices dictionary.

Still needs:

• Config parsing for metadata (make, model, type, serial, etc.)
• Recursive Config file population/parsing when recurse_subdirs is set
Parameters: rancid_root – Where to find your RANCID files (router.db, et al.) rancid_db_file – If it’s named other than router.db config_dir – If it’s named other than configs device_fields – A list of field names used to map to the device data. These must match the attributes expected by device_class. device_class – If you want something other than RancidDevice recurse_subdirs – Whether you want to recurse directories.
class trigger.rancid.RancidDevice

A simple subclass of namedtuple to store contents of parsed RANCID files.

Designed to support all router.* files. The field names are intended to be compatible with Trigger’s NetDevice objects.

Parameters: nodeName – Hostname of device manufacturer – Vendor/manufacturer name of device deviceStatus – (Optional) Up/down status of device deviceType – (Optional) The device type… determined somehow