def handle(self, *args, **options): csvheader = ['device_id', 'vendor', 'ip_address', 'source', 'sysname', 'description', 'contact', 'location', 'last_update_by_cli', 'chassis_type', 'hardware_version', 'hardware_id', 'rom_version', 'rom_system_version', 'configuration_register', 'memory', 'memory', 'configuration_memory_in_use', 'boot_image', 'processor', 'os_version', 'mgmt_ip_address', 'hw_model', 'os_family', 'last_update_by_snmp', 'hostname'] if not args[0]: self.stdout.write('Please specify the realm this import belongs to') sys.exit() realm = args[0] if not os.path.exists(options['importfile']): self.stdout.write('File does not exist. Please specify an existing csv file') sys.exit() csvfile = csv.DictReader(open(options['importfile'], 'rb'), fieldnames=csvheader, delimiter=',') for dev in csvfile: unique_id = '%s-%s' % (realm, dev['device_id']) # These values can be empty string so we need to convert that to a normal datetime or None if empty dev['last_update_by_snmp'] = normalize_datetimefield(dev['last_update_by_snmp']) dev['last_update_by_cli'] = normalize_datetimefield(dev['last_update_by_cli']) try: Device.objects.filter(pk=unique_id).update(last_seen=timezone.now(), **dev) self.stdout.write('Device already exists. Updating device "%s"\n' % dev['device_id']) except Device.DoesNotExist: d = Device(realm=realm, first_seen=timezone.now(), last_seen=timezone.now(), pk=unique_id, **dev) d.save() self.stdout.write('Successfully added device "%s"\n' % dev['device_id'])
def handle(self, *args, **options): profile_unique_id = '%s-%s' % (args[1].lower(), args[0].lower()) try: p = Profile.objects.get(unique_id=profile_unique_id) self.stdout.write('Profile already exists "%s"\n' % (p)) except Profile.DoesNotExist: p = Profile(unique_id=profile_unique_id, name=args[0], realm=args[1], first_seen=timezone.now(), last_seen=timezone.now(), active=True) p.save() self.stdout.write('Profile did not exist. Created profile "%s"\n' % (p)) fi = p.task_set.filter(uuid=args[2]) if len(fi) == 0: try: t = p.task_set.create(uuid=args[2], status=args[3], polling_server=args[4], start=normalize_datetimefield(args[5]), end=normalize_datetimefield(args[6])) self.stdout.write( 'Successfully added task "%s" to profile %s\n' % (t.uuid, p)) except: pdb.set_trace() self.stdout.write('Failed to add task "%s" to profile %s\n' % (t.uuid, p)) else: self.stdout.write('Task already exists "%s %s"\n' % (p, fi[0].uuid))
def handle(self, *args, **options): csvheader = [ 'device_id', 'vendor', 'ip_address', 'source', 'sysname', 'description', 'contact', 'location', 'last_update_by_cli', 'chassis_type', 'hardware_version', 'hardware_id', 'rom_version', 'rom_system_version', 'configuration_register', 'memory', 'memory', 'configuration_memory_in_use', 'boot_image', 'processor', 'os_version', 'mgmt_ip_address', 'hw_model', 'os_family', 'last_update_by_snmp', 'hostname' ] if not args[0]: self.stdout.write( 'Please specify the realm this import belongs to') sys.exit() realm = args[0] if not os.path.exists(options['importfile']): self.stdout.write( 'File does not exist. Please specify an existing csv file') sys.exit() csvfile = csv.DictReader(open(options['importfile'], 'rb'), fieldnames=csvheader, delimiter=',') for dev in csvfile: unique_id = '%s-%s' % (realm, dev['device_id']) # These values can be empty string so we need to convert that to a normal datetime or None if empty dev['last_update_by_snmp'] = normalize_datetimefield( dev['last_update_by_snmp']) dev['last_update_by_cli'] = normalize_datetimefield( dev['last_update_by_cli']) try: Device.objects.filter(pk=unique_id).update( last_seen=timezone.now(), **dev) self.stdout.write( 'Device already exists. Updating device "%s"\n' % dev['device_id']) except Device.DoesNotExist: d = Device(realm=realm, first_seen=timezone.now(), last_seen=timezone.now(), pk=unique_id, **dev) d.save() self.stdout.write('Successfully added device "%s"\n' % dev['device_id'])
def handle(self, *args, **options): profile_unique_id = '%s-%s' % (args[1].lower(), args[0].lower()) try: p = Profile.objects.get(unique_id=profile_unique_id) self.stdout.write('Profile already exists "%s"\n' % (p)) except Profile.DoesNotExist: p = Profile(unique_id=profile_unique_id, name=args[0], realm=args[1], first_seen=timezone.now(), last_seen=timezone.now(), active=True) p.save() self.stdout.write('Profile did not exist. Created profile "%s"\n' % (p)) fi = p.task_set.filter(uuid=args[2]) if len(fi) == 0: try: t = p.task_set.create(uuid=args[2], status=args[3], polling_server=args[4], start=normalize_datetimefield(args[5]), end=normalize_datetimefield(args[6])) self.stdout.write('Successfully added task "%s" to profile %s\n' % (t.uuid, p)) except: pdb.set_trace() self.stdout.write('Failed to add task "%s" to profile %s\n' % (t.uuid, p)) else: self.stdout.write('Task already exists "%s %s"\n' % (p, fi[0].uuid))