mirror of
https://github.com/lopes/netbox-scanner.git
synced 2025-07-23 05:44:53 +02:00
logging improved
This commit is contained in:
parent
5b51e83468
commit
5d3e6972ac
@ -28,7 +28,7 @@ Note that `netbox-scanner` will require Nmap and an instance of NetBox ready to
|
||||
2. For each discovered host it will:
|
||||
1. If host is in NetBox, description is different, and tag is set as defined in `netbox-scanner/config.py/TAG`, it'll be updated.
|
||||
2. If host is not in NetBox, it'll be created.
|
||||
3. It will iterate through each network to find and delete hosts registered in NetBox that are not responsible to scan, and have the tag `netbox-scanner/config.py/TAG`.
|
||||
3. It will iterate through each network to find and delete any hosts registered in NetBox that did not respond to scan, and have the tag `netbox-scanner/config.py/TAG`.
|
||||
|
||||
This way, if some hosts in your networks that are monitored via `netbox-scanner` are eventually down, but you don't want to delete them, just make sure that it doesn't have the tag as set in `netbox-scanner/config.py/TAG`.
|
||||
|
||||
|
@ -40,7 +40,8 @@ class NetBoxScanner(object):
|
||||
for host in nm.all_hosts():
|
||||
address = nm[host]['addresses']['ipv4']
|
||||
try:
|
||||
description = self.get_description(nm[host]['hostnames'][0]['name'],
|
||||
description = self.get_description(
|
||||
nm[host]['hostnames'][0]['name'],
|
||||
nm[host]['osmatch'][0]['osclass'][0]['cpe'])
|
||||
except (KeyError, AttributeError, IndexError):
|
||||
description = self.unknown
|
||||
@ -54,32 +55,39 @@ class NetBoxScanner(object):
|
||||
:return: nothing will be returned
|
||||
'''
|
||||
for net in networks:
|
||||
logging.info('scanning network {}'.format(net))
|
||||
logging.info('scan: {}'.format(net))
|
||||
hosts = self.scan(net)
|
||||
for host in hosts:
|
||||
nbhost = self.netbox.ipam.get_ip_addresses(address=host['address'])
|
||||
nbhost = self.netbox.ipam.get_ip_addresses(
|
||||
address=host['address'])
|
||||
if nbhost:
|
||||
if (self.tag in nbhost[0]['tags']) and (host['description'] !=
|
||||
nbhost[0]['description']):
|
||||
logging.warning('updating host {} ({}) to: {}'.format(
|
||||
if (self.tag in nbhost[0]['tags']) and (
|
||||
host['description'] != nbhost[0]['description']):
|
||||
logging.warning('update: {} "{}" -> "{}"'.format(
|
||||
host['address'], nbhost[0]['description'],
|
||||
host['description']))
|
||||
self.netbox.ipam.update_ip('{}/32'.format(host['address']),
|
||||
description=host['description'])
|
||||
self.netbox.ipam.update_ip('{}/32'.format(
|
||||
host['address']), description=host['description'])
|
||||
else:
|
||||
logging.info('creating host {} ({})'.format(host['address'],
|
||||
logging.info('create: {} "{}"'.format(host['address'],
|
||||
host['description']))
|
||||
self.netbox.ipam.create_ip_address('{}/32'.format(host['address']),
|
||||
self.netbox.ipam.create_ip_address(
|
||||
'{}/32'.format(host['address']),
|
||||
tags=[self.tag], description=host['description'])
|
||||
|
||||
for ipv4 in IPv4Network(net):
|
||||
address = str(ipv4)
|
||||
if not any(h['address'] == address for h in hosts):
|
||||
nbhost = self.netbox.ipam.get_ip_addresses(address=address)
|
||||
nbhost = self.netbox.ipam.get_ip_addresses(
|
||||
address=address)
|
||||
try:
|
||||
if self.tag in nbhost[0]['tags']:
|
||||
logging.warning('deleting host {} ({})'.format(
|
||||
host['address'], host['description']))
|
||||
logging.warning('delete: {} "{}"'.format(
|
||||
nbhost[0]['address'],
|
||||
nbhost[0]['description']))
|
||||
self.netbox.ipam.delete_ip_address(address)
|
||||
else:
|
||||
logging.info('undiscovered: {}'.format(
|
||||
nbhost[0]['address']))
|
||||
except IndexError:
|
||||
pass
|
||||
|
@ -3,13 +3,16 @@
|
||||
import logging
|
||||
from sys import stdout, stderr
|
||||
from argparse import ArgumentParser
|
||||
from datetime import datetime
|
||||
|
||||
import config
|
||||
from nbscan import NetBoxScanner
|
||||
|
||||
|
||||
logging.basicConfig(filename='netbox-scanner.log', level=logging.INFO,
|
||||
format='%(asctime)s\t%(name)s\t%(levelname)s\t%(message)s')
|
||||
logging.basicConfig(filename='netbox-scanner-{}.log'.format(
|
||||
datetime.now().strftime('%Y%m%dT%H%M%SZ')),
|
||||
level=logging.INFO,
|
||||
format='%(asctime)s\tnetbox-scanner\t%(levelname)s\t%(message)s')
|
||||
|
||||
argp = ArgumentParser()
|
||||
argp.add_argument('-a', '--address', help='netbox address',
|
||||
@ -32,7 +35,7 @@ args = argp.parse_args()
|
||||
|
||||
nbs = NetBoxScanner(args.address, args.tls, args.token, args.port,
|
||||
args.tag, args.unknown, args.warnings)
|
||||
|
||||
nbs.sync(args.networks)
|
||||
logging.info('finished')
|
||||
|
||||
exit(0)
|
||||
|
20
setup.py
20
setup.py
@ -2,22 +2,22 @@
|
||||
|
||||
import setuptools
|
||||
|
||||
with open("README.md", "r") as fh:
|
||||
with open('README.md', 'r') as fh:
|
||||
long_description = fh.read()
|
||||
|
||||
setuptools.setup(
|
||||
name="netbox-scanner",
|
||||
version="0.2.0",
|
||||
name='netbox-scanner',
|
||||
version='0.2.1',
|
||||
author='José Lopes de Oliveira Jr.',
|
||||
author_email="jlojunior@gmail.com",
|
||||
description="A scanner util for NetBox",
|
||||
author_email='jlojunior@gmail.com',
|
||||
description='A scanner util for NetBox',
|
||||
long_description=long_description,
|
||||
long_description_content_type="text/markdown",
|
||||
url="https://github.com/forkd/netbox-scanner",
|
||||
long_description_content_type='text/markdown',
|
||||
url='https://github.com/forkd/netbox-scanner',
|
||||
packages=setuptools.find_packages(),
|
||||
classifiers=[
|
||||
"Programming Language :: Python :: 3",
|
||||
"License :: OSI Approved :: MIT License",
|
||||
"Operating System :: OS Independent",
|
||||
'Programming Language :: Python :: 3',
|
||||
'License :: OSI Approved :: MIT License',
|
||||
'Operating System :: OS Independent',
|
||||
],
|
||||
)
|
||||
|
Loading…
x
Reference in New Issue
Block a user