[PACKAGE]: ../../raw/master/mkp/create_topology_data-0.1.0-20231028.mkp "create_topology_data-0.1.0-20231028.mkp"
# PoC for Network Visualization data creation from inventory data

This script creates the topology data file needed for the [Checkmk Exchange Network visualization](https://exchange.checkmk.com/p/network-visualization) plugin by Andreas Boesl and [schnetz](https://exchange.checkmk.com/u/schnetz).\
For more information see [Checkmk forum Network Visualization](https://forum.checkmk.com/t/network-visualization/41680). 

Features:
- Reading connection data from the checkmk HW/SW inventory.
- Handling differences between inventory neighbor names and checkmk host names.
- Map layer 2 port names to interface service items
- Add custom connections for connections that are not in the inventory
- Optimized for my [CDP](https://thl-cmk.hopto.org/gitlab/checkmk/vendor-independent/inventory/inv_cdp_cache/) (default) and [LLDP](https://thl-cmk.hopto.org/gitlab/checkmk/vendor-independent/inventory/inv_lldp_cache/) cache plugins
- Read and merge CDP and LLDP topologies
- Can also be used with custom inventory plugins
- Clean up old topologies

The necessary HW/SW inventory data can be created with my CDP/LLDP inventory plugins.
- [CDP inventory plugin](https://thl-cmk.hopto.org/gitlab/checkmk/vendor-independent/inventory/inv_cdp_cache/)
- [LLDP inventory plugin](https://thl-cmk.hopto.org/gitlab/checkmk/vendor-independent/inventory/inv_lldp_cache/)

---
### Acknowledgements

Andreas Boesl and schnetz for the Network visualization plugin\
detlev[dot]gerke[at]siegfried[dot]ch, who was my beta tester ;-)

---
### Download
* [Download latest mkp file][PACKAGE]

**Note**: before you update to a newer version check the [CHANGELOG](/CHANGELOG "CHANGELOG"). There could always be an incompatible change.
                        
---
### Installation

You can install the package by uploading it to your CheckMK site and as site user run 
```
mkp add PAKAGE_NAME.mkp
mkp enable PAKAGE_NAME VERSION
```

In the Enterprise/Free/Cloud edition of CheckMK you can use the GUI to install the package (_Setup_ -> _Extension Packages_ -> _Upload package_)

---
### Want to Contribute?

Nice ;-) Have a look at the [contribution guidelines](CONTRIBUTING.md "Contributing")

---
### Requirements

To use this tool:
- your Checkmk Site must be running at least CMK version 2.2.0p1
- the HW/SW inventory must be enabled
- you need to install/enable an inventory plugin to collect the connection data I.e. my [CDP cache](https://thl-cmk.hopto.org/gitlab/checkmk/vendor-independent/inventory/inv_cdp_cache/) plugin
- the [Network visualization plugin](https://exchange.checkmk.com/p/network-visualization) must be installed

**Note**: if you are using my CDP/LLDP plugins update at least to version:
- 0.6.1-20231013 for CDP
- 0.9.0-20231013 for LLDP

---
### Usage

Usage with CDP and STATIC (this is the default):
```
~/local/lib/topology_data/create_topology_data.py -s Core01 -d
```
Usage with LLDP and STATIC:
```
~/local/lib/topology_data/create_topology_data.py -s Core01 -d -l LLDP STATIC
```

<details><summary>Help output from create_topology_data.py --help</summary>

```
OMD[build]:~$ ~/local/bin/topology_data/create_topology_data.py --help
usage: create_topology_data.py [-h] [-d] [-o OUTPUT_DIRECTORY] [-s SEED_DEVICES [SEED_DEVICES ...]]
                               [-l {CDP,CUSTOM,LLDP,STATIC} [{CDP,CUSTOM,LLDP,STATIC} ...]]
                               [-u USER_DATA_FILE] [-v] [--check-user-data-only] [--debug] [--dont-compare]
                               [--keep-domain] [--keep KEEP] [--lowercase] [--min-age MIN_AGE]
                               [--time-format TIME_FORMAT] [--uppercase]

This script creates the topology data file needed for the Checkmk "network_visualization"
plugin by Andreas Boesl and schnetz. For more information see
the announcement from schnetz: https://forum.checkmk.com/t/network-visualization/41680
and the plugin on the Exchange: https://exchange.checkmk.com/p/network-visualization .

The required inventory data can be created with my inventory plugins:
CDP: https://thl-cmk.hopto.org/gitlab/checkmk/vendor-independent/inventory/inv_cdp_cache
LLDP: https://thl-cmk.hopto.org/gitlab/checkmk/vendor-independent/inventory/inv_lldp_cache

Version: 0.1.0-202310128 | Written by: thl-cmk
for more information see: https://thl-cmk.hopto.org

options:
  -h, --help            show this help message and exit
  -d, --default         Set the created topology data as default
  -o OUTPUT_DIRECTORY, --output-directory OUTPUT_DIRECTORY
                        Directory name where to save the topology data.
                        I.e.: my_topology. Default is the actual date/time
                        in "--time-format" format.
                        NOTE: the directory is a sub directory under "~/var/topology_data/"
  -s SEED_DEVICES [SEED_DEVICES ...], --seed-devices SEED_DEVICES [SEED_DEVICES ...]
                        List of devices to start the topology discovery from.
                        I.e. Core01 Core02
  -l {CDP,CUSTOM,LLDP,STATIC} [{CDP,CUSTOM,LLDP,STATIC} ...], --layers {CDP,CUSTOM,LLDP,STATIC} [{CDP,CUSTOM,LLDP,STATIC} ...]
                        Layers with least significant layer first. Listed layers
                        will be merged automatically. Default is "-l CDP STATIC"
  -u USER_DATA_FILE, --user-data-file USER_DATA_FILE
                        Set the name uf the user provided data file
                        Default is ~local/bin/topology_data/create_topology_data.toml
  -v, --version         Print version of this script and exit
  --check-user-data-only
                        Only tries to read/parse the user data from create_topology_data.toml and exits.
  --debug               Print debug information
  --dont-compare        Do not compare the actual topology data with the default topology
                        data. By default, the actual topology is compared with the default
                        topology. If the data matches, the actual topology is not saved.
                        So, if you run this tool in a cron job, a new topology will be
                        created only if there was a change, unless you use "--dont-compare".
  --keep-domain         Do not remove the domain name from the neighbor name
  --keep KEEP           Number of topologies to keep. The oldest topologies above keep
                        max will be deleted.
                        NOTE: The default topologies will be always kept.
  --lowercase           Change neighbour names to all lower case
  --min-age MIN_AGE     The minimum number of days before a topology is deleted by "--keep".
  --time-format TIME_FORMAT
                        Format string to render the time. (default: %Y-%m-%dT%H:%M:%S.%m)
  --uppercase           Change neighbour names to all upper case

Usage:
for CDP (the default):
~/local/bin/network-topology/create_topology_data.py -s Core01 Core02 -d
OMD[build]:~$ 
```

</details>

---
### Limitations

- ~~local/neighbour interface names have to match the service names in Checkmk~~

---
### FAQ

<details><summary>If i open the Network visualization, there is no default topology</summary>

\
To make the topology created by this tool the default use the option `-d` / `--default`

```
$ ~/local/bin/topology_data/create_topology_data.py -s Core01 -d
```

</details>

<details><summary>Can i automate the creation of the topology data</summary>

\
Yes. Add a cron job for the site user in `~/etc/cron.d`. I.e. to create a topology every hour create the file `create_topoloky_data` in `~/etc/cron.d`

```
0 * * * * $OMD_ROOT/local/bin/topology_data/create_topology_data.py -s C9540-7-1 -d --keep 10 --min-age 14
```

This will run the tool every hour and create a new topology if there was a changee. It keeps 10 topologies and deletes old topologies only if they are older than 14 days.

Don't forget to restart the crontab service after creating the cron job.

```
OMD[build]:~/etc/cron.d$ omd restart crontab 
Temporary filesystem already mounted
Removing Crontab...OK
Initializing Crontab...OK
OMD[build]:~/etc/cron.d$ 
```

With crontab -l you get the list of active cron jobs

```
OMD[build]:~/etc/cron.d$ crontab -l | grep -A 3 -B 1  create_topoloky_data 
# ------------------------------------------------------------
# /omd/sites/build/etc/cron.d/create_topoloky_data
0 * * * * $OMD_ROOT/local/bin/topology_data/create_topology_data.py -s Core01 d --keep 10

# ------------------------------------------------------------
```

**Note**: By default it will only create a new topology if the new topology differs from the default topology. The name for the new topology is by default the creation date and time.

</details>

<details><summary>Can I limit the number of topology saved changes</summary>

\
Yes. If you use the option --keep <number of topologies to keep> the tool will delete all topologies over this number. I.e.

```
$ ~/local/bin/topology_data/create_topology_data.py -s Core01 --keep 10
```
will delete the oldest topologies until there only 10 topologies left.

**Note**: the default topology will never be deleted.
</details>

<details><summary>I will not delete topologies from the last X days</summary>

\
No problem. Just use the option `--min-age` togeher with `--keep`. I.e. 
```
$ ~/local/bin/topology_data/create_topology_data.py -s Core01 --keep 10 --min-age 30
``` 
will only delete a topology if it older than 30 days.
</details>

<details><summary>Can the topology data creation be customized</summary>

\
Yes. You can customize the topology data creation by modifying the  `~/local/bin/topology_data/create_topology_data.toml` file. Or provide your own data file (see Option -u, --user-data-file)

**Note**: This file uses [Tom's Obvious Minimal Language](https://toml.io/en/)

```
~/local/bin/topology_data/create_topology_data.py -u ~/local/bin/topology_data/my.toml
```

</details>

<details><summary>My Neighbor names from the inventory and checkmk host names do not match</summary>

\
If the neighbor names from the inventory and the checkmk host names do not match, the topology does not work. To fix this, add a mapping between the neighbor name and the checkmk host name to the **HOST_MAP** in the  data file (by default `create_topology_data.toml`). I.e.

```
# map inventory neighbour name to Checkmk host name
[HOST_MAP]
inventory_neighbour1 = "cmk_host1"
inventory_neighbour2 = "cmk_host2"
inventory_neighbour3 = "cmk_host3"
```
**NOTE**: The script will automatically remove the domain name form the neighbour names. Use the option `--keep-domain` if you want to keep the domain name.

</details>

<details><summary>In my inventory are invalid neighbor names</summary>

\
If you have invalid neighbor names in the inventory (i.e. "not advertised"), you can drop them by adding them to the **DROP_HOSTS** list in the data file (by default `create_topology_data.toml`). I.e..

```
# drop neighbours with invalid names
DROP_HOSTS = [
    "not advertised",
    "a nother invalid name",
]
```

</details>


<details><summary>Not all connections are included in the inventory</summary>

\
If your inventory data does not contain all the required topology information, you can add static connections in the data file (by default `create_topology_data.toml`). This may be the case if not all of your devices use CDP/LLDP. The connections defined in **STATIC_CONNECTIONS** are added to the topology from host to neighbor and vice versa. I.e..

```
# user defined static connections
# connections will be added from host to neighbour and in reverese
STATIC_CONNECTIONS = [
    ["host1", "local-port1", "neighbour-port1", "neighbour1", "label"],
    ["host1", "local-port2", "neighbour-port2", "neighbour2", "label"],
]
```

</details>

<details><summary>Can I use the tool with other inventory data plugins</summary>

\
Yes. If you don\`t use my plugins you can still use this tool to create your topology data, for this you need the path to your topology data in the inventory. For example for my CDP plugin this is _Networking_ -> _CDP Cache_ internally known as `networking` -> `cdp_cache`. The data must form a table in the inventory that contains at least the three columns for the neighbor name, neighbor port, and local port. For these columns you need the internal names. I.e. for my CDP plugin the columns _Neighbor device_, _Neighbor port_ and _Local port_ are known internally as `device_id`, `local_port` and `device_port`. With this information, you can edit the CUSTOM_LAYERS section in the user data file like this
```
# optional custom layers use option -l/--layers CUSTOM to include this layers
CUSTOM_LAYERS = [
    { path = "networking,lldp_cache", columns = "system_name,local_port_num,port_id", label = "custom_LLDP" },
    { path = "networking,cdp_cache", columns = "device_id,local_port,device_port", label = "custom_CDP" },
]
```
then run the tool with the option `-l CUSTOM`
```
~/local/bin/network-topology/create_topology_data.py -s Core01 -l CUSTOM
```

</details>

<details><summary>How are port names mapped to interface service names?</summary>

\
When mapping Layer 2 port names, the first step is to check whether the port name is in the host's list of interface items; if it is, the port name is used; if it is not, the port name is searched for in the inventory interface table. If found it tries to find the correct item by comparing the list of items with the fields index, description, alias, physical address (MAC) and (if available) [name](https://thl-cmk.hopto.org/gitlab/checkmk/vendor-independent/inventory/inv_ifname). If we still can't find the item, we pad the index and try the search again. 
So most of the time we should find the correct interface service for a port.

</details>

---
### Sample Output

Sample output

```
OMD[build]:~$ ~/local/bin/topology_data/create_topology_data.py -d -m CDP LLDP

Start time: 2023-10-20T19:31:34.10
Devices added: 17, source inv_LLDP
Devices added: 80, source inv_CDP
Devices added: 29, source static
Time taken: 0.457689695/s
End time: 2023-10-20T19:31:35.10

OMD[build]:~$ 
```

Sample network topology

![sample output details](/img/sample-details.png?raw=true "sample output details")