Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
I
inventory-provider
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
dashboardv3
inventory-provider
Commits
d689384e
Commit
d689384e
authored
6 years ago
by
Erik Reid
Browse files
Options
Downloads
Patches
Plain Diff
pass raw console output from ssh process
parent
aedce954
No related branches found
Branches containing commit
No related tags found
Tags containing commit
No related merge requests found
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
inventory_provider/juniper.py
+48
-29
48 additions, 29 deletions
inventory_provider/juniper.py
inventory_provider/router_interfaces.py
+26
-16
26 additions, 16 deletions
inventory_provider/router_interfaces.py
with
74 additions
and
45 deletions
inventory_provider/juniper.py
+
48
−
29
View file @
d689384e
...
...
@@ -8,9 +8,12 @@ import paramiko
from
inventory_provider.constants
import
JUNIPER_LOGGER_NAME
def
neighbors
(
router
,
routing_instances
=
[
"
IAS
"
],
group_expression
=
None
):
def
neighbors
(
parsed_json_output
,
routing_instances
=
[
"
IAS
"
],
group_expression
=
r
'
^GEANT-IX(v6)?[\s-].*$
'
):
for
config
in
router
[
"
bgp
"
]
[
"
configuration
"
]:
for
config
in
parsed_json_output
[
"
configuration
"
]:
for
ri
in
config
[
"
routing-instances
"
]:
for
inst
in
ri
[
"
instance
"
]:
if
inst
[
"
name
"
][
"
data
"
]
not
in
routing_instances
:
...
...
@@ -25,8 +28,8 @@ def neighbors(router, routing_instances=["IAS"], group_expression=None):
yield
n
def
interfaces
(
router
):
for
ifc_info
in
router
[
"
interfaces
"
]
[
"
interface-information
"
]:
def
interfaces
(
parsed_json_output
):
for
ifc_info
in
parsed_json_output
[
"
interface-information
"
]:
for
ifc_list
in
ifc_info
[
"
logical-interface
"
]:
for
ifc
in
ifc_list
:
yield
{
...
...
@@ -40,50 +43,66 @@ def interfaces(router):
@contextlib.contextmanager
def
ssh_connection
(
router
,
ssh_params
):
def
ssh_connection
(
hostname
,
ssh_params
):
import
os
key_filename
=
os
.
path
.
join
(
os
.
path
.
dirname
(
__file__
),
ssh_params
[
"
private-key
"
])
known_hosts
=
os
.
path
.
join
(
os
.
path
.
dirname
(
__file__
),
ssh_params
[
"
known-hosts
"
])
k
=
paramiko
.
DSSKey
.
from_private_key_file
(
key_filename
)
router
[
"
hostname
"
]
=
"
mx1.ams.nl.geant.net
"
with
paramiko
.
SSHClient
()
as
ssh
:
ssh
.
load_host_keys
(
known_hosts
)
ssh
.
connect
(
hostname
=
router
[
"
hostname
"
]
,
hostname
=
hostname
,
username
=
"
Monit0r
"
,
pkey
=
k
)
yield
ssh
def
exec_
router_
commands
_json
(
router
,
ssh_params
,
commands
):
def
ssh_
exec_commands
(
hostname
,
ssh_params
,
commands
):
juniper_logger
=
logging
.
getLogger
(
JUNIPER_LOGGER_NAME
)
with
ssh_connection
(
router
,
ssh_params
)
as
ssh
:
with
ssh_connection
(
hostname
,
ssh_params
)
as
ssh
:
_
,
stdout
,
_
=
ssh
.
exec_command
(
"
set cli screen-length 0
"
)
assert
stdout
.
channel
.
recv_exit_status
()
==
0
def
_dups_to_list
(
pairs
):
counter_map
=
{}
for
k
,
v
in
pairs
:
counter_map
.
setdefault
(
k
,
[]).
append
(
v
)
result
=
{}
for
k
,
v
in
counter_map
.
items
():
if
len
(
v
)
==
1
:
result
[
k
]
=
v
[
0
]
else
:
result
[
k
]
=
v
return
result
for
c
in
commands
:
juniper_logger
.
debug
(
"
command:
'
%s
'"
%
(
c
+
"
| display json
"
)
)
_
,
stdout
,
_
=
ssh
.
exec_command
(
c
+
"
| display json
"
)
juniper_logger
.
debug
(
"
command:
'
%s
'"
%
c
)
_
,
stdout
,
_
=
ssh
.
exec_command
(
c
)
assert
stdout
.
channel
.
recv_exit_status
()
==
0
# TODO: error handling
output
=
stdout
.
read
()
if
output
:
juniper_logger
.
debug
(
"
%r output: [%d] %r
"
%
(
router
,
len
(
output
),
output
[:
20
]))
yield
json
.
loads
(
output
,
object_pairs_hook
=
_dups_to_list
)
yield
stdout
.
read
().
decode
(
"
utf-8
"
)
def
shell_commands
():
def
_dups_to_list
(
pairs
):
counter_map
=
{}
for
k
,
v
in
pairs
:
counter_map
.
setdefault
(
k
,
[]).
append
(
v
)
result
=
{}
for
k
,
v
in
counter_map
.
items
():
if
len
(
v
)
==
1
:
result
[
k
]
=
v
[
0
]
else
:
juniper_logger
.
debug
(
"
%r output empty
"
%
router
)
yield
{}
result
[
k
]
=
v
return
result
yield
{
"
command
"
:
"
set cli screen-length 0
"
,
"
parser
"
:
lambda
_
:
(
None
,
None
)
}
yield
{
"
command
"
:
'
show configuration routing-instances IAS protocols bgp | display json
'
,
"
parser
"
:
lambda
txt
:
(
"
bgp
"
,
list
(
neighbors
(
json
.
loads
(
txt
)))
if
txt
else
{})
}
yield
{
"
command
"
:
'
show configuration logical-systems VRR protocols bgp | display json
'
,
"
parser
"
:
lambda
txt
:
(
"
vrr
"
,
json
.
loads
(
txt
)
if
txt
else
{})
}
yield
{
"
command
"
:
'
show interfaces descriptions | display json
'
,
"
parser
"
:
lambda
txt
:
(
"
interfaces
"
,
list
(
interfaces
(
json
.
loads
(
txt
,
object_pairs_hook
=
_dups_to_list
)))
if
txt
else
{})
}
This diff is collapsed.
Click to expand it.
inventory_provider/router_interfaces.py
+
26
−
16
View file @
d689384e
...
...
@@ -17,11 +17,11 @@ def get_router_interfaces_q(router, params, q):
threading_logger
.
debug
(
"
[<<EXIT] get_router_interfaces_q: %r
"
%
router
)
def
exec_
router_
commands_
json_q
(
router
,
ssh_params
,
commands
,
q
):
def
ssh_
exec_commands_
q
(
hostname
,
ssh_params
,
commands
,
q
):
threading_logger
=
logging
.
getLogger
(
constants
.
THREADING_LOGGER_NAME
)
threading_logger
.
debug
(
"
[ENTER>>] exec_router_commands_q: %r
"
%
router
)
q
.
put
(
list
(
juniper
.
exec_
router_
commands
_json
(
router
,
ssh_params
,
commands
)))
threading_logger
.
debug
(
"
[<<EXIT] exec_router_commands_q: %r
"
%
router
)
threading_logger
.
debug
(
"
[ENTER>>] exec_router_commands_q: %r
"
%
hostname
)
q
.
put
(
list
(
juniper
.
ssh_
exec_commands
(
hostname
,
ssh_params
,
commands
)))
threading_logger
.
debug
(
"
[<<EXIT] exec_router_commands_q: %r
"
%
hostname
)
def
get_router_details
(
router
,
params
,
q
):
...
...
@@ -30,28 +30,38 @@ def get_router_details(router, params, q):
threading_logger
.
debug
(
"
[ENTER>>]get_router_details: %r
"
%
router
)
commands
=
[
'
show configuration routing-instances IAS protocols bgp
'
,
# 'show configuration routing-instances IAS protocols bgp | display set | match neighbor | match description | match "GEANT-IX | GEANT-IX-"',
# 'show configuration routing-instances IAS protocols bgp | display set | match neighbor | match description | match "GEANT-IXv6 | GEANT-IXv6-"',
'
show configuration logical-systems VRR protocols bgp
'
,
# 'show configuration logical-systems VRR protocols bgp group VPN-RR-INTERNAL | match "neigh|desc"',
# 'show configuration logical-systems VRR protocols bgp group VPN-RR | match "neigh|desc"'
'
show interfaces descriptions
'
]
commands
=
list
(
juniper
.
shell_commands
())
snmpifc_proc_queue
=
Queue
()
snmpifc_proc
=
Process
(
target
=
get_router_interfaces_q
,
args
=
(
router
,
params
,
snmpifc_proc_queue
))
snmpifc_proc
=
Process
(
target
=
get_router_interfaces_q
,
args
=
(
router
,
params
,
snmpifc_proc_queue
))
snmpifc_proc
.
start
()
commands_proc_queue
=
Queue
()
commands_proc
=
Process
(
target
=
exec_router_commands_json_q
,
args
=
(
router
,
params
[
"
ssh
"
],
commands
,
commands_proc_queue
))
commands_proc
=
Process
(
target
=
ssh_exec_commands_q
,
args
=
(
router
[
"
hostname
"
],
params
[
"
ssh
"
],
[
c
[
"
command
"
]
for
c
in
commands
],
commands_proc_queue
))
commands_proc
.
start
()
threading_logger
.
debug
(
"
waiting for commands result: %r
"
%
router
)
command_output
=
commands_proc_queue
.
get
()
assert
len
(
command_output
)
==
len
(
commands
)
result
=
dict
(
zip
([
"
bgp
"
,
"
vrr
"
,
"
interfaces
"
],
command_output
))
for
i
,
o
in
enumerate
(
command_output
):
with
open
(
"
/tmp/%s-%d.output
"
%
(
router
[
"
hostname
"
],
i
),
"
w
"
)
as
f
:
f
.
write
(
o
)
result
=
{}
for
c
,
o
in
zip
(
commands
,
command_output
):
parsed
=
c
[
"
parser
"
](
o
)
if
parsed
[
0
]:
result
[
parsed
[
0
]]
=
parsed
[
1
]
commands_proc
.
join
()
threading_logger
.
debug
(
"
... got commands result & joined: %r
"
%
router
)
...
...
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