# change the data from unicode to utf8 # This is needed for csm api "set_ARRAY" function # nodes_j1 = [x.encode('utf8') for x in nodes_j] # Once you understand the above you can combine as in below. # The above code in comments has been combined into one line. nodes = [x.encode('utf8') for x in response.json()] # Once the nodes have been gathered. You can call the CSM API. # PART 2 ==== CSM API # CSM API csm.init_lib() input = inv.node_attributes_query_input_t() # set nodes from gathered xCAT data above. input.set_node_names(nodes) # set other input data input.limit=-1 input.offset=-1 # Call the CSM API rc,handler,output = inv.node_attributes_query(input) # Access csm api output how ever you want.
def query(input_details): ''' Queries CSM and Elastic to get a listing of records with a matching key. ''' # Query CSM for allocation details csm.init_lib() alloc_input = wm.allocation_query_input_t() alloc_input.allocation_id = input_details["allocation_id"] alloc_input.primary_job_id = input_details["primary_job_id"] alloc_input.secondary_job_id = input_details["secondary_job_id"] rc, handler, alloc_output = wm.allocation_query(alloc_input) if rc is 0: allocation = alloc_output.allocation # Build some of the searches. end_time = "*" if allocation.history is not None: end_time = allocation.history.end_time.replace(' ', 'T') + "Z" timerange = '''@timestamp:[{0}Z TO {1}]'''.format( allocation.begin_time.replace(' ', 'T'), end_time) # The hostname query. hostnames = 'syslogHostname:(' host_count = 0 if input_details["hostnames"] is not None: for i in range(0, allocation.num_nodes): host = allocation.get_compute_nodes(i) if host in input_details["hostnames"]: host_count += 1 hostnames += "{0} OR ".format(host) else: for i in range(0, allocation.num_nodes): host_count += 1 hostnames += "{0} OR ".format(allocation.get_compute_nodes(i)) hostnames = hostnames[:-4] if host_count is 0: print("No hosts found matching query.") return 1 # The message portion of the query, splat query needs special TLC. message = "message:" keys = input_details["key"].split(',') if input_details["key"] is not "*": for key in keys: message += '"{0}",'.format(key) message = message[:-1] else: message += "*" aggregation = 'aggs:{ keys: { filter key_count : { value_count: { "field" : " "} }' # Open a connection to the elastic cluster. es = Elasticsearch(cluster, sniff_on_start=True, sniff_on_connection_fail=True, sniffer_timeout=60) query = '{0} AND {1} AND {2})'.format(message, timerange, hostnames) #query='message:"{0}" AND {1}'.format(input_details["key"], timerange) print(query) res = es.search(index="_all", q=query) print("Got %d Hits:" % res['hits']['total']) #if res['hits']['total'] > 0: # print(res['hits']['hits'][0]) csm.api_object_destroy(handler) csm.term_lib()