|
| 1 | +# |
| 2 | +# Copyright 2021 Splunk Inc. |
| 3 | +# |
| 4 | +# Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | +# you may not use this file except in compliance with the License. |
| 6 | +# You may obtain a copy of the License at |
| 7 | +# |
| 8 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | +# |
| 10 | +# Unless required by applicable law or agreed to in writing, software |
| 11 | +# distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | +# See the License for the specific language governing permissions and |
| 14 | +# limitations under the License. |
| 15 | +# |
| 16 | +import os.path as op |
| 17 | +import sys |
| 18 | +import time |
| 19 | + |
| 20 | +sys.path.insert(0, op.dirname(op.dirname(op.abspath(__file__)))) |
| 21 | +import context |
| 22 | +from splunklib import client |
| 23 | +from splunklib import results as splunklib_results |
| 24 | + |
| 25 | + |
| 26 | +def search(session_key, query): |
| 27 | + service = client.connect(host=context.host, token=session_key) |
| 28 | + job = service.jobs.create(query) |
| 29 | + while True: |
| 30 | + while not job.is_ready(): |
| 31 | + pass |
| 32 | + stats = { |
| 33 | + "isDone": job["isDone"], |
| 34 | + "doneProgress": job["doneProgress"], |
| 35 | + "scanCount": job["scanCount"], |
| 36 | + "eventCount": job["eventCount"], |
| 37 | + "resultCount": job["resultCount"], |
| 38 | + } |
| 39 | + if stats["isDone"] == "1": |
| 40 | + break |
| 41 | + time.sleep(0.5) |
| 42 | + json_results_reader = splunklib_results.JSONResultsReader( |
| 43 | + job.results(output_mode="json") |
| 44 | + ) |
| 45 | + results = [] |
| 46 | + for result in json_results_reader: |
| 47 | + if isinstance(result, dict): |
| 48 | + results.append(result) |
| 49 | + return results |
0 commit comments