|
| 1 | +#!/usr/bin/env python |
| 2 | +# |
| 3 | +# Copyright 2011-2015 Splunk, Inc. |
| 4 | +# |
| 5 | +# Licensed under the Apache License, Version 2.0 (the "License"): you may |
| 6 | +# not use this file except in compliance with the License. You may obtain |
| 7 | +# a copy of the License at |
| 8 | +# |
| 9 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | +# |
| 11 | +# Unless required by applicable law or agreed to in writing, software |
| 12 | +# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT |
| 13 | +# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the |
| 14 | +# License for the specific language governing permissions and limitations |
| 15 | +# under the License. |
| 16 | + |
| 17 | +"""A simple example showing to use the Service.job method to retrieve |
| 18 | +a search Job by its sid. |
| 19 | +""" |
| 20 | + |
| 21 | +import sys |
| 22 | +import os |
| 23 | +import time |
| 24 | +sys.path.insert(0, os.path.join(os.path.dirname(__file__), "..")) |
| 25 | +import splunklib.client as client |
| 26 | + |
| 27 | +try: |
| 28 | + from utils import * |
| 29 | +except ImportError: |
| 30 | + raise Exception("Add the SDK repository to your PYTHONPATH to run the examples " |
| 31 | + "(e.g., export PYTHONPATH=~/splunk-sdk-python.") |
| 32 | + |
| 33 | +def main(argv): |
| 34 | + opts = parse(argv, {}, ".splunkrc") |
| 35 | + service = client.connect(**opts.kwargs) |
| 36 | + |
| 37 | + # Execute a simple search, and store the sid |
| 38 | + sid = service.search("search index=_internal | head 5").sid |
| 39 | + |
| 40 | + # Now, we can get the `Job` |
| 41 | + job = service.job(sid) |
| 42 | + |
| 43 | + # Wait for the job to complete |
| 44 | + while not job.is_done(): |
| 45 | + time.sleep(1) |
| 46 | + |
| 47 | + print "Number of events found: %d" % int(job["eventCount"]) |
| 48 | + |
| 49 | +if __name__ == "__main__": |
| 50 | + main(sys.argv[1:]) |
| 51 | + |
0 commit comments