Newer
Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
#
# License: GNU General Public License v2
# Author: thl-cmk[at]outlook[dot]com
# URL : https://thl-cmk.hopto.org
# Date : 2023-10-12
# File : create_topology_utils.py
#
# options used
# -d --default
# -o --output-directory
# -s --seed-devices
# -u --user-data-file
# -v --version
# --check-user-data-only
# --data-source
# --debug
# --dont-compare
# --inventory-columns
# --keep-domain
# --keep
# --lldp
# --lowercase
# --min-age
# --path-in-inventory
# --time-format
# --uppercase
from argparse import (
Namespace as arg_Namespace,
ArgumentParser,
RawTextHelpFormatter,
)
from create_topology_utils import(
CREATE_TOPOLOGY_VERSION,
SCRIPT,
SAMPLE_SEEDS,
USER_DATA_FILE,
LABEL_CDP,
COLUMNS_CDP,
LABEL_LLDP,
PATH_LLDP,
COLUMNS_LLDP,
PATH_CDP
)
def parse_arguments() -> arg_Namespace:
parser = ArgumentParser(
prog='create_topology_data.py',
description='This script creates the topology data file needed for the Checkmk "network_visualization"\n'
'plugin by Andreas Boesl and schnetz. For more information see\n'
'the announcement from schnetz: https://forum.checkmk.com/t/network-visualization/41680\n'
'and the plugin on the Exchange: https://exchange.checkmk.com/p/network-visualization .\n'
'\n'
'The required inventory data can be created with my inventory plugins:\n'
'CDP: https://thl-cmk.hopto.org/gitlab/checkmk/vendor-independent/inventory/inv_cdp_cache\n'
'LLDP: https://thl-cmk.hopto.org/gitlab/checkmk/vendor-independent/inventory/inv_lldp_cache\n'
'\n'
f'\nVersion: {CREATE_TOPOLOGY_VERSION} | Written by: thl-cmk\n'
f'for more information see: https://thl-cmk.hopto.org',
formatter_class=RawTextHelpFormatter,
epilog='Usage:\n'
'for CDP (the default):\n'
f'{SCRIPT} -s {SAMPLE_SEEDS} -d\n'
'for LLDP:\n'
f'{SCRIPT} -s {SAMPLE_SEEDS} -d --lldp\n',
)
command_group = parser.add_mutually_exclusive_group()
parser.add_argument(
'-d', '--default', default=False, action='store_const', const=True,
help='Set the created topology data as default',
)
parser.add_argument(
'-m', '--merge',
nargs=2,
choices=['CDP', 'LLDP'],
help=f'Merge topologies. This runs the topology creation for CDP and LLDP.\n'
f'The topologies are then merged in the specified order.\n'
f'I.e. -m CDP LLDP merges the CDP topology into the LLDP topology, overwriting\n'
f'the LLDP data in case there are conflicts.\n'
f'NOTE: static connection data from the user file will always merged with the\n'
f' highest priority',
)
parser.add_argument(
'-o', '--output-directory', type=str,
help='Directory name where to save the topology data.\n'
'I.e.: my_topology. Default is the actual date/time\n'
'in "--time-format" format.\n'
'NOTE: the directory is a sub directory under "~/var/topology_data/"',
)
parser.add_argument(
'-s', '--seed-devices', type=str, nargs='+',
help=f'List of devices to start the topology discovery from.\n'
f'I.e. {SAMPLE_SEEDS}',
)
parser.add_argument(
'-u', '--user-data-file', type=str,
help='Set the name uf the user provided data file\n'
'Default is ~local/bin/topology_data/create_topology_data.toml\n',
)
parser.add_argument(
'-v', '--version', default=False, action='store_const', const=True,
help='Print version of this script and exit',
)
parser.add_argument(
'--check-user-data-only', default=False, action='store_const', const=True,
help=f'Only tries to read/parse the user data from {USER_DATA_FILE} and exits.',
)
parser.add_argument(
'--data-source', type=str,
help=f'The source from which the topology data originates.\n'
f'I.e. {LABEL_CDP} for CDP data from the inventory.\n'
'NOTE: right now this only an unused label.',
)
parser.add_argument(
'--debug', default=False, action='store_const', const=True,
help='Print debug information',
)
parser.add_argument(
'--dont-compare', default=False, action='store_const', const=True,
help='Do not compare the actual topology data with the default topology\n'
'data. By default, the actual topology is compared with the default\n'
'topology. If the data matches, the actual topology is not saved.\n'
'So, if you run this tool in a cron job, a new topology will be\n'
'created only if there was a change, unless you use "--dont-compare".'
)
parser.add_argument(
'--inventory-columns', type=str,
help=f'Columns used from the inventory data.\n'
f'I.e. "{COLUMNS_CDP}"\n'
'NOTE: the columns must be in the order: neighbour, local_port,\n'
'neighbour_port',
)
parser.add_argument(
'--keep-domain', default=False, action='store_const', const=True,
help='Do not remove the domain name from the neighbor name',
)
parser.add_argument(
'--keep', type=int,
help=f'Number of topologies to keep. The oldest topologies above keep\n'
f'max will be deleted. The minimum value for --keep is 1.\n'
f'NOTE: The default topologies will be always kept.\n'
)
parser.add_argument(
'--lldp', default=False, action='store_const', const=True,
help=f'Sets data source to {LABEL_LLDP}, inventory path \n'
f'to "{PATH_LLDP}" and columns\n'
f'to "{COLUMNS_LLDP}"',
)
command_group.add_argument(
'--lowercase', default=False, action='store_const', const=True,
help='Change neighbour names to all lower case',
)
parser.add_argument(
'--min-age', type=int,
help=f'The minimum number of days before a topology is deleted\n'
f'by "--keep".\n'
f'NOTE: Topologies that are not older than 1 days are always kept.',
)
parser.add_argument(
'--path-in-inventory', type=str,
help=f'Checkmk inventory path to the topology data.\n'
f'I.e. "{PATH_CDP}"',
)
parser.add_argument(
'--time-format', type=str,
help='Format string to render the time. (default: %%Y-%%m-%%dT%%H:%%M:%%S.%%m)',
)
command_group.add_argument(
'--uppercase', default=False, action='store_const', const=True,
help='Change neighbour names to all upper case',
)
return parser.parse_args()