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
993705ab
Commit
993705ab
authored
6 years ago
by
Erik Reid
Browse files
Options
Downloads
Patches
Plain Diff
move config loading to its own module
parent
fc212abd
Branches
Branches containing commit
Tags
Tags containing commit
No related merge requests found
Changes
3
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
inventory_provider/config.py
+84
-0
84 additions, 0 deletions
inventory_provider/config.py
inventory_provider/constants.py
+0
-31
0 additions, 31 deletions
inventory_provider/constants.py
inventory_provider/router_interfaces.py
+13
-65
13 additions, 65 deletions
inventory_provider/router_interfaces.py
with
97 additions
and
96 deletions
inventory_provider/config.py
0 → 100644
+
84
−
0
View file @
993705ab
import
json
import
logging
import
re
import
jsonschema
CONFIG_SCHEMA
=
{
"
$schema
"
:
"
http://json-schema.org/draft-07/schema#
"
,
"
type
"
:
"
object
"
,
"
properties
"
:
{
"
alarms-db
"
:
{
"
type
"
:
"
object
"
,
"
properties
"
:
{
"
hostname
"
:
{
"
type
"
:
"
string
"
},
"
dbname
"
:
{
"
type
"
:
"
string
"
},
"
username
"
:
{
"
type
"
:
"
string
"
},
"
password
"
:
{
"
type
"
:
"
string
"
}
},
"
required
"
:
[
"
hostname
"
,
"
dbname
"
,
"
username
"
,
"
password
"
],
"
additionalProperties
"
:
False
},
"
oid_list.conf
"
:
{
"
type
"
:
"
string
"
},
"
routers_community.conf
"
:
{
"
type
"
:
"
string
"
},
"
ssh
"
:
{
"
type
"
:
"
object
"
,
"
properties
"
:
{
"
private-key
"
:
{
"
type
"
:
"
string
"
},
"
known-hosts
"
:
{
"
type
"
:
"
string
"
}
},
"
required
"
:
[
"
private-key
"
,
"
known-hosts
"
],
"
additionalProperties
"
:
False
}
},
"
required
"
:
[
"
alarms-db
"
,
"
oid_list.conf
"
,
"
routers_community.conf
"
],
"
additionalProperties
"
:
False
}
def
_load_oids
(
config_file
):
"""
:param config_file: file-like object
:return:
"""
result
=
{}
for
line
in
config_file
:
m
=
re
.
match
(
r
'
^([^=]+)=(.*)\s*$
'
,
line
)
if
m
:
result
[
m
.
group
(
1
)]
=
m
.
group
(
2
)
return
result
def
_load_routers
(
config_file
):
"""
:param config_file: file-like object
:return:
"""
for
line
in
config_file
:
m
=
re
.
match
(
r
'
^([a-z\d]+\.[a-z\d]{3,4}\.[a-z\d]{2}\.(geant|eumedconnect)\d*\.net)\s*=([^,]+)\s*,(.*)\s*$
'
,
line
)
if
not
m
:
logging
.
warning
(
"
malformed config file line:
'
%s
'"
%
line
.
strip
())
continue
yield
{
"
hostname
"
:
m
.
group
(
1
),
"
community
"
:
m
.
group
(
3
),
"
address
"
:
m
.
group
(
4
)
}
def
load
(
f
):
"""
loads, validates and returns configuration parameters
:param f: file-like object that produces the config file
:return:
"""
config
=
json
.
loads
(
f
.
read
())
jsonschema
.
validate
(
config
,
CONFIG_SCHEMA
)
with
open
(
config
[
"
oid_list.conf
"
])
as
f
:
config
[
"
oids
"
]
=
_load_oids
(
f
)
with
open
(
config
[
"
routers_community.conf
"
])
as
f
:
config
[
"
routers
"
]
=
list
(
_load_routers
(
f
))
return
config
This diff is collapsed.
Click to expand it.
inventory_provider/constants.py
+
0
−
31
View file @
993705ab
...
...
@@ -2,34 +2,3 @@ SNMP_LOGGER_NAME = "snmp-logger"
THREADING_LOGGER_NAME
=
"
threading-logger
"
JUNIPER_LOGGER_NAME
=
"
juniper-logger
"
DATABASE_LOGGER_NAME
=
"
database-logger
"
CONFIG_SCHEMA
=
{
"
$schema
"
:
"
http://json-schema.org/draft-07/schema#
"
,
"
type
"
:
"
object
"
,
"
properties
"
:
{
"
alarms-db
"
:
{
"
type
"
:
"
object
"
,
"
properties
"
:
{
"
hostname
"
:
{
"
type
"
:
"
string
"
},
"
dbname
"
:
{
"
type
"
:
"
string
"
},
"
username
"
:
{
"
type
"
:
"
string
"
},
"
password
"
:
{
"
type
"
:
"
string
"
}
},
"
required
"
:
[
"
hostname
"
,
"
dbname
"
,
"
username
"
,
"
password
"
],
"
additionalProperties
"
:
False
},
"
oid_list.conf
"
:
{
"
type
"
:
"
string
"
},
"
routers_community.conf
"
:
{
"
type
"
:
"
string
"
},
"
ssh
"
:
{
"
type
"
:
"
object
"
,
"
properties
"
:
{
"
private-key
"
:
{
"
type
"
:
"
string
"
},
"
known-hosts
"
:
{
"
type
"
:
"
string
"
}
},
"
required
"
:
[
"
private-key
"
,
"
known-hosts
"
],
"
additionalProperties
"
:
False
}
},
"
required
"
:
[
"
alarms-db
"
,
"
oid_list.conf
"
,
"
routers_community.conf
"
],
"
additionalProperties
"
:
False
}
This diff is collapsed.
Click to expand it.
inventory_provider/router_interfaces.py
+
13
−
65
View file @
993705ab
...
...
@@ -9,68 +9,13 @@ import jsonschema
from
inventory_provider
import
constants
from
inventory_provider
import
snmp
from
inventory_provider
import
juniper
from
inventory_provider
import
config
def
_load_oids
(
config_file
):
"""
:param config_file: file-like object
:return:
"""
result
=
{}
for
line
in
config_file
:
m
=
re
.
match
(
r
'
^([^=]+)=(.*)\s*$
'
,
line
)
if
m
:
result
[
m
.
group
(
1
)]
=
m
.
group
(
2
)
return
result
def
_load_routers
(
config_file
):
"""
:param config_file: file-like object
:return:
"""
for
line
in
config_file
:
m
=
re
.
match
(
r
'
^([a-z\d]+\.[a-z\d]{3,4}\.[a-z\d]{2}\.(geant|eumedconnect)\d*\.net)\s*=([^,]+)\s*,(.*)\s*$
'
,
line
)
if
not
m
:
logging
.
warning
(
"
malformed config file line:
'
%s
'"
%
line
.
strip
())
continue
yield
{
"
hostname
"
:
m
.
group
(
1
),
"
community
"
:
m
.
group
(
3
),
"
address
"
:
m
.
group
(
4
)
}
def
_validate_config
(
ctx
,
param
,
value
):
"""
loads, validates and returns configuration parameters
:param ctx:
:param param:
:param value:
:return:
"""
config
=
json
.
loads
(
value
.
read
())
jsonschema
.
validate
(
config
,
constants
.
CONFIG_SCHEMA
)
with
open
(
config
[
"
oid_list.conf
"
])
as
f
:
config
[
"
oids
"
]
=
_load_oids
(
f
)
with
open
(
config
[
"
routers_community.conf
"
])
as
f
:
config
[
"
routers
"
]
=
list
(
_load_routers
(
f
))
return
config
def
get_router_interfaces_q
(
router
,
config
,
q
):
def
get_router_interfaces_q
(
router
,
params
,
q
):
threading_logger
=
logging
.
getLogger
(
constants
.
THREADING_LOGGER_NAME
)
threading_logger
.
debug
(
"
[ENTER>>] get_router_interfaces_q: %r
"
%
router
)
q
.
put
(
list
(
snmp
.
get_router_interfaces
(
router
,
config
)))
q
.
put
(
list
(
snmp
.
get_router_interfaces
(
router
,
params
)))
threading_logger
.
debug
(
"
[<<EXIT] get_router_interfaces_q: %r
"
%
router
)
...
...
@@ -122,14 +67,14 @@ def get_router_details(router, params, q):
threading_logger
.
debug
(
"
[<<EXIT]get_router_details: %r
"
%
router
)
def
load_network_details
(
config
):
def
load_network_details
(
params
):
threading_logger
=
logging
.
getLogger
(
constants
.
THREADING_LOGGER_NAME
)
processes
=
[]
for
r
in
config
[
"
routers
"
]:
for
r
in
params
[
"
routers
"
]:
q
=
Queue
()
p
=
Process
(
target
=
get_router_details
,
args
=
(
r
,
config
,
q
))
p
=
Process
(
target
=
get_router_details
,
args
=
(
r
,
params
,
q
))
p
.
start
()
processes
.
append
({
"
router
"
:
r
,
"
process
"
:
p
,
"
queue
"
:
q
})
...
...
@@ -143,17 +88,20 @@ def load_network_details(config):
return
result
def
_validate_config
(
ctx
,
param
,
value
):
return
config
.
load
(
value
)
@click.command
()
@click.option
(
"
--
config
"
,
"
--
params
"
,
# required=True,
type
=
click
.
File
(),
help
=
"
Configuration filename
"
,
default
=
open
(
"
config.json
"
),
callback
=
_validate_config
)
def
cli
(
config
):
network_details
=
load_network_details
(
config
)
def
cli
(
params
):
network_details
=
load_network_details
(
params
)
filename
=
"
/tmp/router-info.json
"
logging
.
debug
(
"
writing output to:
"
+
filename
)
with
open
(
filename
,
"
w
"
)
as
f
:
...
...
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