def db_new_host(self, **attrs): self.db_lock.acquire() try: obj = models.Host(**attrs) obj.save() print 'd2sec_django - Saved new host "%s" to database OK' % attrs['title'] except Exception, e: print 'd2sec_django - Failed to save new host "%s" : %s' % (attrs['title'], e) obj = None
def addhost(): method = request.args if request.method == 'GET' else request.form host = models.Host(method) if host.add_host(): ok = True result = '添加主机成功' else: ok = False result = '添加失败' return json.dumps({'ok': True, 'result': result})
def deletehost(): host = models.Host(request.form) if host.del_host(): ok = True result = "删除成功" else: ok = False result = "删除失败" if ok: return json.dumps({'ok': True, 'result': result}) else: return json.dump({'ok': False, 'result': result})
def updatehost(): host = models.Host(request.form) if host.updatehost(): ok = True result = "更新主机信息成功" else: ok = False result = '主机信息无变化' if ok: return json.dumps({'ok': True, 'result': result}) else: return json.dumps({'ok': False, 'result': result})
def get_host_with_nagios(self): url = 'http://%s/nagiosxi/api/v1/objects/host/' % (NAGIOS_URL) params = { 'apikey': NAGIOS_API_KEY, } r = requests.get(url=url, params=params) print r.text print r.json() if r.status_code == 200: models.Host.objects.all().delete() host_list = [] data = r.json()['hostlist']['host'] for i in data: host_list.append( models.Host( # pk=data.index(i), instance_id=i['instance_id'], host_name=i['host_name'], is_active=i['is_active'], config_type=i['config_type'], alias=i['alias'], display_name=i['display_name'], address=i['address'], check_interval=i['check_interval'], retry_interval=i['retry_interval'], max_check_attempts=i['max_check_attempts'], first_notification_delay=i['first_notification_delay'], notification_interval=i['notification_interval'], passive_checks_enabled=i['passive_checks_enabled'], active_checks_enabled=i['active_checks_enabled'], notifications_enabled=i['notifications_enabled'], notes=i['notes'], notes_url=i['notes_url'], action_url=i['action_url'], icon_image=i['icon_image'], icon_image_alt=i['icon_image_alt'], statusmap_image=i['statusmap_image'], )) print host_list models.Host.objects.bulk_create(host_list) # for i in r.json()['hostlist']['host']: # print i # i.pop('@attributes') # models.Host.objects.update_or_create(address=i['address'], defaults=i) return 200 else: print 'nagios get host api failed!' return 'nagios get host api failed!'
def post(self): args, tk = self.parser.parse_args(), request.headers.get('Authorization') uid = token.check_token(tk) date = time.strftime("%Y-%m-%d %H:%M", time.localtime()) new_task = md.Task(uid=uid, tname=args['tname'], date=date, status=0) db.session.add(new_task) db.session.commit() host_list = json.loads(args['hosts']) # convert pure json to python list for host in host_list: if dns.valid_host(host['target']): new_host = md.Host(tid=new_task.id, hname=new_task.tname, target=host['target'], policy=host['policy']) db.session.add(new_host) db.session.commit() return msg.success_msg
def create_hosts(userobj, argvs): ''' 添加主机 :param argvs: :return: ''' if '-f' in argvs: hosts_file = argvs[argvs.index("-f") + 1] else: raise MyException("106") source = mylib.yaml_parser(hosts_file) if source: for key, val in source.items(): print(key, val) #添加主机信息 obj = models.Host(hostname=key, ip_addr=val.get('ip_addr'), port=val.get('port') or 22) session.add(obj) session.commit()
def register(values): session = get_session() with session.begin(): model = models.Host() model.update(values) model.save(session=session)