garbage collector implemented

This commit is contained in:
José Lopes 2020-05-27 18:48:51 -03:00
parent 55c7c7934e
commit cb29536a4c
3 changed files with 21 additions and 7 deletions

View File

@ -10,12 +10,17 @@ netbox-scanner is compatible with **Python 3.7+**, and can be installed like thi
$ cd netbox-scanner
$ pip install -r requirements.txt
After installation, use the `netbox-scanner.conf` file as an example to create your own and put this file in `/opt/netbox` or prepend its name with a dot and put it in your home directory --`~/.netbox-scanner.conf`. Keep reading to learn more about configuration.
## Basics
netbox-scanner reads a user-defined source to discover IP addresses and descriptions, and insert them into NetBox. To control what was previously inserted, netbox-scanner adds tags to each record, so it will know that that item can be handled. In order to guarantee the integrity of manual inputs, records without such tags will not be updated or removed.
It is important to note that if netbox-scanner cannot define the description for a given host, then it will insert the string defined in the `unknown` parameter. Users can change those names at their own will.
### Garbage Collection
If the user marked the `cleanup` option to `yes`, then netbox-scanner will run a garbage collector after the synchronization finishes. Basically, it will get all IP addresses recorded in NetBox under the same tag. Then, both lists will be compared: the one just retrieved from NetBox and the list that was synced. Hosts in the first list that don't appear in the second list will be deleted.
## Configuration
Users can interact with netbox-scanner by command line and configuration file. The latter is pretty simple and straight forward: the only parameter accepted is the module you want to use.

View File

@ -23,7 +23,7 @@ class NetBoxScanner(object):
}
def sync_host(self, host):
'''Syncs a single host to NetBox.
'''Syncs a single host to NetBox
host: a tuple like ('10.0.0.1','Gateway')
returns: True if syncing is good or False for errors
@ -60,9 +60,19 @@ class NetBoxScanner(object):
return True
def garbage_collector(self):
'''Removes records from NetBox not found in last sync'''
nbhosts = self.netbox.ipam.ip_addresses.filter(tag=self.tag)
for nbhost in nbhosts:
nbh = str(nbhost).split('/')[0]
if not any(nbh == addr[0] for addr in self.hosts):
nbhost.delete()
logging.info(f'deleted: {nbhost[0]}')
self.stats['deleted'] += 1
def sync(self):
'''Synchronizes self.hosts to NetBox.
Returns synching statistics.
'''Synchronizes self.hosts to NetBox
Returns synching statistics
'''
for s in self.stats:
self.stats[s] = 0
@ -72,9 +82,9 @@ class NetBoxScanner(object):
self.sync_host(host)
if self.cleanup:
pass
self.garbage_collector()
logging.info('finished: +{} ~{} -{} !{}'.format(
logging.info('finished: .{} +{} ~{} -{} !{}'.format(
self.stats['unchanged'],
self.stats['created'],
self.stats['updated'],

View File

@ -52,10 +52,9 @@ disable_warnings(InsecureRequestWarning)
def cmd_nmap(): # nmap handler
h = Nmap(nmap['path'], nmap['unknown'])
h.run()
print(len(h.hosts));exit(0)
scan = NetBoxScanner(
netbox,
Nmap(nmap['path'], nmap['unknown']).run(),
h.hosts,
nmap['tag'],
nmap.getboolean('cleanup')
)