Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
B
brian-polling-manager
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Releases
Package registry
Container registry
Model registry
Operate
Environments
Terraform modules
Monitor
Incidents
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
geant-swd
brian
brian-polling-manager
Commits
158b5236
Commit
158b5236
authored
1 year ago
by
Erik Reid
Browse files
Options
Downloads
Patches
Plain Diff
removed inventory option, only one router
parent
6f9850a3
No related branches found
No related tags found
No related merge requests found
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
brian_polling_manager/interface_stats/cli.py
+31
-32
31 additions, 32 deletions
brian_polling_manager/interface_stats/cli.py
test/test_interface_stats.py
+21
-24
21 additions, 24 deletions
test/test_interface_stats.py
with
52 additions
and
56 deletions
brian_polling_manager/interface_stats/cli.py
+
31
−
32
View file @
158b5236
...
...
@@ -2,12 +2,13 @@ import contextlib
from
datetime
import
datetime
from
functools
import
partial
import
json
import
socket
import
click
import
jsonschema
from
brian_polling_manager.interface_stats
import
config
,
brian
,
errors
,
juniper
from
brian_polling_manager
import
inventory
,
influx
from
brian_polling_manager
import
influx
import
logging.config
...
...
@@ -99,20 +100,13 @@ def _validate_config(_unused_ctx, _unused_param, file):
raise
click
.
BadParameter
(
e
)
def
_managed_routers
(
inventory_base_urls
):
return
{
_ifc
[
'
router
'
]
for
_ifc
in
inventory
.
load_interfaces
(
inventory_base_urls
)}
def
fqdn_netconf_doc_map
(
app_config_params
):
_result
=
{}
for
fqdn
in
_managed_routers
(
inventory_base_urls
=
app_config_params
[
'
inventory
'
]):
_result
[
fqdn
]
=
{
'
timestamp
'
:
datetime
.
now
(),
'
doc
'
:
juniper
.
get_interface_info_ncrpc
(
fqdn
)
}
return
_result
def
_validate_hostname
(
_unused_ctx
,
_unused_param
,
hostname
):
try
:
socket
.
gethostbyname
(
hostname
)
except
socket
.
error
:
raise
click
.
BadParameter
(
f
'
{
hostname
}
is not resolveable
'
)
def
_brian_points
(
router_fqdn
,
netconf_doc
,
timestamp
,
measurement_name
):
_ctr2point
=
partial
(
ctr2point
,
measurement_name
,
timestamp
)
...
...
@@ -137,7 +131,7 @@ def _error_points(router_fqdn, netconf_doc, timestamp, measurement_name):
yield
from
map
(
_ctr2point
,
counters
)
def
_main
(
app_config_params
:
dict
):
def
_main
(
router_fqdn
:
str
,
app_config_params
:
dict
):
"""
callable entry point, without click
...
tmp
,
for
testing
...
...
@@ -148,29 +142,28 @@ def _main(app_config_params: dict):
setup_logging
()
nc_doc_map
=
fqdn_netconf_doc_map
(
app_config_params
)
netconf_doc
=
juniper
.
get_interface_info_ncrpc
(
router_fqdn
)
netconf_timestamp
=
datetime
.
now
()
influx_params
=
app_config_params
[
'
influx
'
][
'
brian-counters
'
]
for
_fqdn
,
_ncdoc
in
nc_doc_map
.
items
():
points
=
_brian_points
(
router_fqdn
=
_fqdn
,
netconf_doc
=
_ncdoc
[
'
doc
'
],
timestamp
=
_ncdoc
[
'
timestamp
'
],
measurement_name
=
influx_params
[
'
measurement
'
])
points
=
_brian_points
(
router_fqdn
=
router_fqdn
,
netconf_doc
=
netconf_doc
,
timestamp
=
netconf_timestamp
,
measurement_name
=
influx_params
[
'
measurement
'
])
with
contextlib
.
closing
(
influx
.
influx_client
(
influx_params
))
as
client
:
client
.
write_points
(
points
)
with
contextlib
.
closing
(
influx
.
influx_client
(
influx_params
))
as
client
:
client
.
write_points
(
points
)
influx_params
=
app_config_params
[
'
influx
'
][
'
error-counters
'
]
for
_fqdn
,
_ncdoc
in
nc_doc_map
.
items
():
points
=
_error_points
(
router_fqdn
=
_fqdn
,
netconf_doc
=
_ncdoc
[
'
doc
'
],
timestamp
=
_ncdoc
[
'
timestamp
'
],
measurement_name
=
influx_params
[
'
measurement
'
])
points
=
_error_points
(
router_fqdn
=
router_fqdn
,
netconf_doc
=
netconf_doc
,
timestamp
=
netconf_timestamp
,
measurement_name
=
influx_params
[
'
measurement
'
])
with
contextlib
.
closing
(
influx
.
influx_client
(
influx_params
))
as
client
:
client
.
write_points
(
points
)
with
contextlib
.
closing
(
influx
.
influx_client
(
influx_params
))
as
client
:
client
.
write_points
(
points
)
@click.command
()
...
...
@@ -180,6 +173,12 @@ def _main(app_config_params: dict):
type
=
click
.
File
(
'
r
'
),
help
=
'
Config filename
'
,
callback
=
_validate_config
)
@click.option
(
'
--router
'
,
'
router_fqdn
'
,
required
=
True
,
type
=
click
.
STRING
,
help
=
'
router fqdn
'
,
callback
=
_validate_hostname
)
def
main
(
app_config_params
:
dict
):
_main
(
app_config_params
)
...
...
This diff is collapsed.
Click to expand it.
test/test_interface_stats.py
+
21
−
24
View file @
158b5236
...
...
@@ -19,7 +19,7 @@ import pytest
import
yaml
from
brian_polling_manager.interface_stats
import
brian
,
errors
,
juniper
,
cli
from
brian_polling_manager
import
inventory
,
influx
,
interface_stats
from
brian_polling_manager
import
influx
,
interface_stats
DATA_DIRNAME
=
os
.
path
.
join
(
os
.
path
.
dirname
(
__file__
),
'
data
'
,
'
interface-info-snapshots
'
)
DATA_FILENAME_EXTENSION
=
'
-interface-info.xml
'
...
...
@@ -72,22 +72,16 @@ def test_validate_logical_counters_schema(router_fqdn, ifc_netconf_rpc):
for
ifc
in
juniper
.
logical_interface_counters
(
doc
):
jsonschema
.
validate
(
ifc
,
interface_stats
.
LOGICAL_INTERFACE_COUNTER_SCHEMA
)
@pytest.fixture
def
mocked_poller_interfaces
():
with
patch
(
'
brian_polling_manager.inventory.load_interfaces
'
)
as
rpc
:
with
open
(
os
.
path
.
join
(
DATA_DIRNAME
,
'
poller-interfaces.json
'
))
as
f
:
rpc
.
return_value
=
json
.
load
(
f
)
yield
# keep the patch in place until caller context finishe
def
poller_interfaces
():
with
open
(
os
.
path
.
join
(
DATA_DIRNAME
,
'
poller-interfaces.json
'
))
as
f
:
return
json
.
load
(
f
)
@pytest.fixture
def
polled_interfaces
(
mocked_poller_interfaces
):
polled
=
{}
for
ifc
in
inventory
.
load_interfaces
([
'
https://bogus-hostname
'
]
):
for
ifc
in
poller_interfaces
(
):
if
ifc
[
'
dashboards
'
]:
polled
.
setdefault
(
ifc
[
'
router
'
],
set
()).
add
(
ifc
[
'
name
'
])
return
polled
...
...
@@ -201,11 +195,6 @@ def app_config_params(free_host_port):
with
tempfile
.
NamedTemporaryFile
()
as
f
:
yield
{
'
ssh-config
'
:
f
.
name
,
'
inventory
'
:
[
'
https://bogus-hostname-1
'
,
'
https://bogus-hostname-2
'
,
'
https://bogus-hostname-3
'
,
],
'
influx
'
:
{
'
brian-counters
'
:
{
'
hostname
'
:
'
localhost
'
,
...
...
@@ -418,15 +407,23 @@ def _use_docker_compose():
@pytest.mark.skipif
(
not
_use_docker_compose
(),
reason
=
'
docker compose not found or disabled
'
)
def
test_e2e
(
app_config_params
,
ifc_netconf_rpc
,
testenv_containers
,
mocked_poller_interfaces
):
def
_mocked_managed_routers
(
inventory_base_urls
):
all_routers
=
{
_ifc
[
'
router
'
]
for
_ifc
in
inventory
.
load_interfaces
([
'
https://abc
'
])}
return
[
_ifc
for
_ifc
in
all_routers
if
not
_ifc
.
startswith
(
'
srx
'
)]
def
test_e2e
(
app_config_params
,
ifc_netconf_rpc
,
testenv_containers
):
"""
load all router interfaces into a tmp influx container, check that
all potential counter fields are populated
with
patch
(
'
brian_polling_manager.interface_stats.cli._managed_routers
'
)
as
rpc
:
rpc
.
side_effect
=
_mocked_managed_routers
cli
.
_main
(
app_config_params
)
:param app_config_params:
:param ifc_netconf_rpc:
:param testenv_containers:
:return:
"""
managed_routers
=
{
_h
[
'
router
'
]
for
_h
in
poller_interfaces
()
# TODO: srx's are excluded for now, since there's no test data
if
not
_h
[
'
router
'
].
startswith
(
'
srx
'
)
}
for
router_fqdn
in
managed_routers
:
cli
.
_main
(
router_fqdn
=
router_fqdn
,
app_config_params
=
app_config_params
)
def
verify_influx_content
(
influx_config
,
expected_fields
):
# for each expected field, verify that there's
...
...
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment