def netops_main_status_view(req): host_count = Host.count() bar = DIV(STYLE='width:100%;height:20px;background-color:#888;', TITLE='Unsettled Environment') for host in Host.list(): if host.range_type == '': bar.html( DIV(TITLE='Reserved\n%s' % (host.ip), STYLE= 'float:left;width:calc(100%%/%d);height:20px;background-color:#888;' % host_count)) elif host.range_type == 'dynamic': bar.html( DIV(TITLE='Dynamic DHCP\n%s\n%s' % (host.range_name, host.ip), STYLE= 'float:left;width:calc(100%%/%d);height:20px;background-color:#44ffcc;' % host_count)) elif host.range_type == 'static': sh = DIV( TITLE='Static DHCP\n%s\n%s\n%s' % (host.range_name, host.ip, host.name), STYLE= 'float:left;width:calc(100%%/%d);height:20px;cursor:pointer;color:#fff;background-color:#44ccff;' % host_count) if host.name != '' or host.mac != '': sh.html('!') bar.html(sh) elif host.range_type == 'environment': bar.html( DIV(TITLE='Environment\n%s\n%s' % (host.ip, host.name), STYLE= 'float:left;width:calc(100%%/%d);height:20px;background-color:#f00;' % host_count)) return bar
def api_getHost(req, id=None): if id != None: host = Host.get(id) if host: return { 'id': host.id, 'name': host.name, 'mac': host.mac, 'ip': host.ip, 'model': host.model, 'serial': host.serial, 'range_name': host.range_name, 'desc': host.desc } return None hosts = Host.list() ret = [] for host in hosts: ret.append({ 'id': host.id, 'name': host.name, 'mac': host.mac, 'ip': host.ip }) return ret
def netops_main_static_view(req, sr_id=None, host_id=None): if req.method == 'POST': if 'sr_id' in req.data: sr_id = req.data.pop('sr_id') Host.set(**req.data) elif req.method == 'DELETE': Host.clear(host_id) return DIV().html( netops.table( TABLE.SYNC('Name', 'IP', 'MAC', 'Model', 'Serial', 'Description', 'Action'), 'netops_main_static_table', sr_id), netops.refresh('netops_main_status_view'), netops.refresh('netops_main_total_view'))
def scale_vm(self, cu): self.hosts.sort(key=lambda host: host.cu_remain, reverse=False) for host in self.hosts: if host.cu_remain >= cu: host.cu_used = host.cu_used + cu host.cu_remain = host.cu_remain - cu return True host = Host() if host.cu_remain >= cu: host.cu_used = host.cu_used + cu host.cu_remain = host.cu_remain - cu self.hosts.append(host) return True else: return False
def netops_main_static_table(table, sr_id): if isinstance(sr_id, str): sr_id = int(sr_id) env = Environment.one() sr = StaticRange.get(sr_id) if sr: hosts = Host.list(Host.ip_num >= sr.stt_num, Host.ip_num <= sr.end_num) for host in hosts: sr_id = INPUT.HIDDEN('sr_id', str(sr.id)) host_id = INPUT.HIDDEN('host_id', str(host.id)) name = INPUT.TEXT('name', host.name, CLASS='page-input-in-table') mac = INPUT.TEXT('mac', host.mac, CLASS='page-input-in-table') model_list = [m for m in _host_models] model_list.remove(host.model) model_list.insert(0, host.model) model = INPUT.SELECT('model', *model_list, CLASS='page-input-in-table') serial = INPUT.TEXT('serial', host.serial, CLASS='page-input-in-table') desc = INPUT.TEXT('desc', host.desc, CLASS='page-input-in-table') submit = DIV(STYLE='width:100%;text-align:center;').html( netops.context( BUTTON(CLASS='btn-primary btn-xs', STYLE='margin:0px;padding:0px 5px;font-size:11px;'). html('Save'), 'netops_main_static_view::' + str(sr.id), sr_id, host_id, name, mac, model, serial, desc), netops.signal( BUTTON(CLASS='btn-danger btn-xs', STYLE='margin:0px;padding:0px 5px;font-size:11px;' ).html('Clear'), 'DELETE', 'netops_main_static_view::' + str(sr.id), str(sr.id), str(host.id)), sr_id, host_id) table.record(name, host.ip, mac, model, serial, desc, submit)
def api_setHost(req): host = Host.set(**req.data) if host: return { 'id': host.id, 'name': host.name, 'mac': host.mac, 'ip': host.ip, 'model': host.model, 'serial': host.serial, 'range_name': host.range_name, 'desc': host.desc } return None
def netops_main_total_table(table): env = Environment.one() hosts = Host.list() for host in hosts: if host.name != '' and host.range_name != '' and env.domain != '': dns = '%s.%s.%s' % (host.name.replace( ' ', '-'), host.range_name.replace(' ', '-'), env.domain) else: dns = '' if host.model == 'Unknown': model = '' else: model = host.model table.record( host.name, ANCH(HREF='http://' + dns, TARGET='_blank').html(dns), '%s (%s)' % (host.range_name, host.range_type) if host.range_name != '' else host.range_name, host.ip, host.mac, model, host.serial, host.desc)
def new_agent(self, hostname: str, api_key: str, ip: str) -> dict: if not self.db.session.query(Host).filter_by( hostname=hostname).count() < 1: return {'code': 1} info = (hostname + api_key + ip).encode('utf-8') # cálculo de hash com informações do agent sha = hashlib.sha1(info).hexdigest() host_temp = Host(hostname=hostname, apikey=api_key, ip=ip, sha=sha) logging.warning(' - user "%s:%s" add in database SQLite' % (host_temp.hostname, host_temp.ip)) self.db.session.add(host_temp) try: self.db.session.commit() return {'code': 0} except Exception as err: self.db.session.rollback() logging.error(' - error to add user in database: %s' % err) return {'code': 2}
def storage(self, data_list, page): if data_list is None: return None for row in data_list: ip = row["ip"] host = Host.query.filter_by(ip=ip).first() if host is None: host = Host() host.page = int(page) host.ip = ip host.app = row["app"] host.port = row["port"] host.service = row["service"] host.device = row["device"] host.hostname = row["hostname"] host.os = row["os"] host.city_cn = row["city_cn"] host.city_en = row["city_en"] host.country_cn = row["country_cn"] host.country_en = row["country_en"] db.session.add(host) db.session.commit()
def netops_main_dynamic_table(table): drs = DynamicRange.list() for dr in drs: hosts = Host.list(Host.ip_num >= dr.stt_num, Host.ip_num <= dr.end_num) for host in hosts: table.record(dr.name, host.ip)