def get_all_hostids(): values = { "jsonrpc": "2.0", "method": "host.get", "params": { "output": ["hostid"] }, "auth": auth, "id": '1' } output = requestJson(values) all_hostids = [i['hostid'] for i in output] return all_hostids
def update_map_host(): map_selements = [] values = { "jsonrpc": "2.0", "method": "map.get", "params": { "output": ["selements"], "selectSelements": "extend" }, "auth": auth, "id": 1 } result = requestJson(values) print(result) all_hostids = get_all_hostids() try: map_selements = result[0]['selements'] maped_selements_id = [i['elementid'] for i in map_selements] map_selements_id = list(set(all_hostids).difference(set(maped_selements_id))) max_host_y = max([int(i['y']) for i in map_selements]) print(max_host_y) host_y = int(max_host_y) + 100 except: host_y = 50 map_selements_id = all_hostids #创建selements列表 host_x = 50 width = 150 height = 100 print('%d, %d' % (host_x, host_y)) for elementid in map_selements_id: map_selements.append( { "elementid": elementid, "elementtype": 0, "iconid_off": "149", "x": host_x, "y": host_y, "width": width, "height": height, "label": "{HOST.CONN}\r\n{HOST.NAME}" } ) if host_y <= 1050: if host_x > 1000: host_y = host_y + height host_x = 50 else: host_x = host_x + width print('%s is added, x: %d, y: %d' % (elementid, host_x, host_y)) else: print('超出边界了') quit() values = { "jsonrpc": "2.0", "method": "map.update", "params": { "sysmapid": "2", "selements": map_selements }, "auth": auth, "id": 1 } print('values: %s' % values) result = requestJson(values) return result