def import_user(self): """ Users in JFFNMS become users in RNMS """ add_count = 0 users = {} result = self.db_handle.execute( """SELECT id,username,name FROM clients WHERE id > 2 ORDER BY id""") for row in result: user = model.User() user.user_name = unicode(row[1]) user.display_name = unicode(row[2]) user.email_address = unicode(row[1]) user.password = u'password' try: DBSession.add(user) DBSession.flush() except IntegrityError as errmsg: self.log.error('Error importing users: %s', errmsg) transaction.abort() return None else: users[row[0]] = user.user_id add_count += 1 self.log.info('Users: %d added.', add_count) return users
def import_interface(self): add_count = 0 ifaces = {} try: result = self.db_handle.execute( """SELECT i.id,i.host,f.field,f.value FROM interfaces AS i, interfaces_values AS f WHERE i.type=4 and i.id=f.interface AND f.field IN (3,4,6)""") for row in result: ifid = row[0] if ifid not in ifaces: ifaces[ifid] = model.Iface() ifaces[ifid].host_id = self.host_id(row[1]) if row[2] == 3: ifaces[ifid].ifindex = int(row[3]) elif row[2] == 4: ifaces[ifid].display_name = unicode(row[3]) elif row[2] == 6: ifaces[ifid].speed = int(row[3]) for iface in ifaces.values(): DBSession.add(iface) DBSession.flush() add_count += 1 except IntegrityError as errmsg: self.log.error('Error importing interfaces: %s', errmsg) transaction.abort() return None else: self.log.info('Interfaces: %d added.', add_count) return []
def import_interface(self): add_count = 0 ifaces = {} try: result = self.db_handle.execute( """SELECT i.id,i.host,f.field,f.value FROM interfaces AS i, interfaces_values AS f WHERE i.type=4 and i.id=f.interface AND f.field IN (3,4,6)""") for row in result: ifid = row[0] if ifid not in ifaces: ifaces[ifid] = model.Iface() ifaces[ifid].host_id = self.host_id(row[1]) if row[2] == 3: ifaces[ifid].ifindex = int(row[3]) elif row[2] == 4: ifaces[ifid].display_name = unicode(row[3]) elif row[2] == 6: ifaces[ifid].speed = int(row[3]) for iface in ifaces.values(): DBSession.add(iface) DBSession.flush() add_count += 1 except IntegrityError as errmsg: self.log.error('Error importing interfaces: %s', errmsg) transaction.abort() return None else: self.log.info('Interfaces: %d added.', add_count) return []
def import_user(self): """ Users in JFFNMS become users in RNMS """ add_count = 0 users = {} result = self.db_handle.execute("""SELECT id,username,name FROM clients WHERE id > 2 ORDER BY id""") for row in result: user = model.User() user.user_name = unicode(row[1]) user.display_name = unicode(row[2]) user.email_address = unicode(row[1]) user.password = u'password' try: DBSession.add(user) DBSession.flush() except IntegrityError as errmsg: self.log.error('Error importing users: %s', errmsg) transaction.abort() return None else: users[row[0]] = user.user_id add_count += 1 self.log.info('Users: %d added.', add_count) return users
def setUp(self): """Setup test fixture for each model test method.""" try: new_attrs = {} new_attrs.update(self.attrs) new_attrs.update(self.do_get_dependencies()) self.obj = self.klass(**new_attrs) DBSession.add(self.obj) DBSession.flush() return self.obj except: DBSession.rollback() raise
def import_snmp(self, old_comm): if old_comm is None: comm = model.SnmpCommunity.by_name(u'None') if comm is not None: return comm.id try: (comm_ver, comm_data) = old_comm.split(':') except ValueError: pass else: comm_ver = int(comm_ver[1:]) display_name = unicode(old_comm.split('|')[0]) comm_fields = comm_data.split('|') comm_name = comm_fields[0] comm_id = DBSession.query(model.SnmpCommunity.id).\ select_from(model.SnmpCommunity).\ filter(and_( model.SnmpCommunity.community == comm_name, model.SnmpCommunity.version == comm_ver)).\ scalar() if comm_id is not None: return comm_id new_comm = model.SnmpCommunity() new_comm.display_name = display_name new_comm.version = comm_ver if comm_ver == 3: if comm_fields[1] == 'noAuthNoPriv': new_comm.set_v3auth_none() elif comm_fields[1] in ('authNoPriv', 'authPriv'): if comm_fields[2] == 'md5': new_comm.set_v3auth_md5(comm_name, comm_fields[3]) else: new_comm.set_v3auth_sha(comm_name, comm_fields[3]) if comm_fields[1] != 'authPriv' or comm_fields[5] == '': new_comm.set_v3privacy_none() elif comm_fields[4] == 'des': new_comm.set_v3privacy_des(comm_fields[5]) else: new_comm.set_v3privacy_aes(comm_fields[5]) else: new_comm.community = comm_name DBSession.add(new_comm) DBSession.flush() return new_comm.id return 1
def import_snmp(self, old_comm): if old_comm is None: comm = model.SnmpCommunity.by_name(u'None') if comm is not None: return comm.id try: (comm_ver, comm_data) = old_comm.split(':') except ValueError: pass else: comm_ver = int(comm_ver[1:]) display_name = unicode(old_comm.split('|')[0]) comm_fields = comm_data.split('|') comm_name = comm_fields[0] comm_id = DBSession.query(model.SnmpCommunity.id).\ select_from(model.SnmpCommunity).\ filter(and_( model.SnmpCommunity.community == comm_name, model.SnmpCommunity.version == comm_ver)).\ scalar() if comm_id is not None: return comm_id new_comm = model.SnmpCommunity() new_comm.display_name = display_name new_comm.version = comm_ver if comm_ver == 3: if comm_fields[1] == 'noAuthNoPriv': new_comm.set_v3auth_none() elif comm_fields[1] in ('authNoPriv', 'authPriv'): if comm_fields[2] == 'md5': new_comm.set_v3auth_md5(comm_name, comm_fields[3]) else: new_comm.set_v3auth_sha(comm_name, comm_fields[3]) if comm_fields[1] != 'authPriv' or comm_fields[5] == '': new_comm.set_v3privacy_none() elif comm_fields[4] == 'des': new_comm.set_v3privacy_des(comm_fields[5]) else: new_comm.set_v3privacy_aes(comm_fields[5]) else: new_comm.community = comm_name DBSession.add(new_comm) DBSession.flush() return new_comm.id return 1
def _discovery_found(self, host, atype_id, attribute): """ Autodiscovery has found a new attribute that is not stored in the database. """ if host.autodiscovery_policy.can_add(attribute): self.logger.debug('H:%d AT:%d New Interface Found: %s', host.id, atype_id, attribute.index) if host.autodiscovery_policy.permit_add: if self.print_only: self.logger.debug('H:%d AT:%d Added %s', host.id, atype_id, attribute.index) else: real_att = Attribute.from_discovered(host, attribute) DBSession.add(real_att) DBSession.flush() self.logger.debug('H:%d AT:%d Added %s = %d', host.id, atype_id, attribute.index, real_att.id)
def import_hostconfig(self): conf_count = 0 try: result = self.db_handle.execute("""SELECT date,host,config FROM hosts_config WHERE id > 1 ORDER BY id""") for row in result: conf = model.HostConfig() conf.created = datetime.datetime.fromtimestamp(row[0]) conf.host_id = self.host_id(row[1]) conf.config = row[2] DBSession.add(conf) DBSession.flush() except IntegrityError as errmsg: self.log.error('Error importing host configs: %s', errmsg) transaction.abort() return None else: self.log.info('Hosts Config: %d added.', conf_count) return []
def _run_backends(self, patt): """ Run all the backends for this polling attribute """ attribute = model.Attribute.by_id(patt.id) for poller_row in patt.poller_set: start_time = time.time() if poller_row.backend.command == '': backend_result = '' else: backend_result = poller_row.run_backend( attribute, patt.poller_values[poller_row.position]) self.logger.debug( "A:%d I:%d - %s:%s -> %s:%s (%d:%d)", patt.id, poller_row.position, poller_row.poller.display_name, self._poller_prettyprint( patt.poller_values[poller_row.position]), poller_row.backend.display_name, backend_result, patt.poller_times[poller_row.position], (time.time() - start_time) * 1000) attribute.update_poll_time() DBSession.flush()
def import_hostconfig(self): conf_count = 0 try: result = self.db_handle.execute( """SELECT date,host,config FROM hosts_config WHERE id > 1 ORDER BY id""") for row in result: conf = model.HostConfig() conf.created = datetime.datetime.fromtimestamp(row[0]) conf.host_id = self.host_id(row[1]) conf.config = row[2] DBSession.add(conf) DBSession.flush() except IntegrityError as errmsg: self.log.error('Error importing host configs: %s', errmsg) transaction.abort() return None else: self.log.info('Hosts Config: %d added.', conf_count) return []
def _discovery_found(self, host, atype_id, attribute): """ Autodiscovery has found a new attribute that is not stored in the database. """ if host.autodiscovery_policy.can_add(attribute): self.logger.debug('H:%d AT:%d New Interface Found: %s', host.id, atype_id, attribute.index) if host.autodiscovery_policy.permit_add: if self.print_only: self.logger.debug( 'H:%d AT:%d Added %s', host.id, atype_id, attribute.index) else: real_att = Attribute.from_discovered(host, attribute) DBSession.add(real_att) DBSession.flush() self.logger.debug('H:%d AT:%d Added %s = %d', host.id, atype_id, attribute.index, real_att.id)
def import_host(self): add_count = 0 hosts = {} try: result = self.db_handle.execute( '''SELECT id,ip,name,rocommunity,rwcommunity,zone,tftp, autodiscovery,autodiscovery_default_customer,show_host, poll,creation_date,modification_date,last_poll_date, sysobjectid,config_type FROM hosts WHERE id>1 ORDER by id''') for row in result: host = model.Host(mgmt_address=row[1], display_name=row[2]) host.ro_community_id = self.import_snmp(row[3]) host.trap_community_id = host.ro_community_id host.rw_community_id = self.import_snmp(row[4]) host.zone_id = self.zone_id(row[5]) host.tftp_server = row[6] host.autodiscovery_policy_id = row[7] host.default_user_id = self.user_id(row[8]) host.show_host = (row[9] == 1) host.pollable = (row[10] == 1) host.created = datetime.datetime.fromtimestamp(row[11]) host.updated = datetime.datetime.fromtimestamp(row[12]) host.discovered = datetime.datetime.now() host.next_discover = host.discovered + \ datetime.timedelta(minutes=30) host.sysobjid = row[14] # host.config_backup_type_id = row[15] DBSession.add(host) DBSession.flush() hosts[row[0]] = host.id add_count += 1 except IntegrityError as errmsg: self.log.error('Error importing users: %s', errmsg) transaction.abort() return None else: self.log.info('Hosts: %d added.', add_count) return hosts
def import_host(self): add_count = 0 hosts = {} try: result = self.db_handle.execute( '''SELECT id,ip,name,rocommunity,rwcommunity,zone,tftp, autodiscovery,autodiscovery_default_customer,show_host, poll,creation_date,modification_date,last_poll_date, sysobjectid,config_type FROM hosts WHERE id>1 ORDER by id''') for row in result: host = model.Host(mgmt_address=row[1], display_name=row[2]) host.ro_community_id = self.import_snmp(row[3]) host.trap_community_id = host.ro_community_id host.rw_community_id = self.import_snmp(row[4]) host.zone_id = self.zone_id(row[5]) host.tftp_server = row[6] host.autodiscovery_policy_id = row[7] host.default_user_id = self.user_id(row[8]) host.show_host = (row[9] == 1) host.pollable = (row[10] == 1) host.created = datetime.datetime.fromtimestamp(row[11]) host.updated = datetime.datetime.fromtimestamp(row[12]) host.discovered = datetime.datetime.now() host.next_discover = host.discovered + \ datetime.timedelta(minutes=30) host.sysobjid = row[14] # host.config_backup_type_id = row[15] DBSession.add(host) DBSession.flush() hosts[row[0]] = host.id add_count += 1 except IntegrityError as errmsg: self.log.error('Error importing users: %s', errmsg) transaction.abort() return None else: self.log.info('Hosts: %d added.', add_count) return hosts
def _run_backends(self, patt): """ Run all the backends for this polling attribute """ attribute = model.Attribute.by_id(patt.id) for poller_row in patt.poller_set: start_time = time.time() if poller_row.backend.command == '': backend_result = '' else: backend_result = poller_row.run_backend( attribute, patt.poller_values[poller_row.position]) self.logger.debug( "A:%d I:%d - %s:%s -> %s:%s (%d:%d)", patt.id, poller_row.position, poller_row.poller.display_name, self._poller_prettyprint( patt.poller_values[poller_row.position]), poller_row.backend.display_name, backend_result, patt.poller_times[poller_row.position], (time.time() - start_time)*1000) attribute.update_poll_time() DBSession.flush()
def import_zone(self): add_count = 0 zones = {} try: result = self.db_handle.execute( """SELECT id,zone,shortname,image,show_zone FROM zones WHERE id > 1 ORDER BY id""") for row in result: zone = model.Zone(display_name=unicode(row[1]), short_name=unicode(row[2]), icon=row[3]) zone.showable = (row[4] == 1) DBSession.add(zone) DBSession.flush() zones[row[0]] = zone.id add_count += 1 except IntegrityError as errmsg: self.log.error('Error importing zones: %s', errmsg) transaction.abort() exit() return None else: self.log.info('Zones: %d added.', add_count) return zones
def import_zone(self): add_count = 0 zones = {} try: result = self.db_handle.execute( """SELECT id,zone,shortname,image,show_zone FROM zones WHERE id > 1 ORDER BY id""") for row in result: zone = model.Zone(display_name=unicode(row[1]), short_name=unicode(row[2]), icon=row[3]) zone.showable = (row[4] == 1) DBSession.add(zone) DBSession.flush() zones[row[0]] = zone.id add_count += 1 except IntegrityError as errmsg: self.log.error('Error importing zones: %s', errmsg) transaction.abort() exit() return None else: self.log.info('Zones: %d added.', add_count) return zones