Skip to content

Commit c6a6dbc

Browse files
authored
Merge pull request #406 from splunk/fix-mod-inputs-examples
Fix mod inputs examples
2 parents b583f8c + f93129f commit c6a6dbc

File tree

6 files changed

+57
-12
lines changed

6 files changed

+57
-12
lines changed

docker-compose.yml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,9 @@ services:
1111
- SPLUNK_APPS_URL=https://github.com/splunk/sdk-app-collection/releases/download/v1.1.0/sdkappcollection.tgz
1212
volumes:
1313
- ./examples/github_forks:/opt/splunk/etc/apps/github_forks
14+
- ./splunklib:/opt/splunk/etc/apps/github_forks/lib/splunklib
1415
- ./examples/random_numbers:/opt/splunk/etc/apps/random_numbers
16+
- ./splunklib:/opt/splunk/etc/apps/random_numbers/lib/splunklib
1517
- ./examples/searchcommands_app/package:/opt/splunk/etc/apps/searchcommands_app
1618
- ./splunklib:/opt/splunk/etc/apps/searchcommands_app/lib/splunklib
1719
- ./examples/twitted/twitted:/opt/splunk/etc/apps/twitted

examples/github_forks/README.md

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
splunk-sdk-python github_forks example
2+
========================================
3+
4+
This app provides an example of a modular input that generates the number of repository forks according to the Github API based on the owner and repo_name provided by the user during setup of the input.
5+
6+
To run this example locally run `SPLUNK_VERSION=latest docker compose up -d` from the root of this repository which will mount this example alongside the latest version of splunklib within `/opt/splunk/etc/apps/github_forks` and `/opt/splunk/etc/apps/github_forks/lib/splunklib` within the `splunk` container.
7+
8+
Once the docker container is up and healthy log into the Splunk UI and setup a new `Github Repository Forks` input by visiting this page: http://localhost:8000/en-US/manager/github_forks/datainputstats and selecting the "Add new..." button next to the Local Inputs > Random Inputs. Enter values for a Github Repository owner and repo_name, for example owner = `splunk` repo_name = `splunk-sdk-python`.
9+
10+
NOTE: If no Github Repository Forks input appears then the script is likely not running properly, see https://docs.splunk.com/Documentation/SplunkCloud/latest/AdvancedDev/ModInputsDevTools for more details on debugging the modular input using the command line and relevant logs.
11+
12+
Once the input is created you should be able to see an event when running the following search: `source="github_forks://*"` the event should contain fields for `owner` and `repository` matching the values you input during setup and then a `fork_count` field corresponding to the number of forks the repo has according to the Github API.

examples/github_forks/github_forks.py renamed to examples/github_forks/bin/github_forks.py

Lines changed: 27 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -15,10 +15,18 @@
1515
# under the License.
1616

1717
from __future__ import absolute_import
18-
import sys, urllib2, json
18+
import os
19+
import sys
20+
import json
21+
# NOTE: splunklib must exist within github_forks/lib/splunklib for this
22+
# example to run! To run this locally use `SPLUNK_VERSION=latest docker compose up -d`
23+
# from the root of this repo which mounts this example and the latest splunklib
24+
# code together at /opt/splunk/etc/apps/github_forks
25+
sys.path.insert(0, os.path.join(os.path.dirname(__file__), "..", "lib"))
1926

2027
from splunklib.modularinput import *
2128
from splunklib import six
29+
from six.moves import http_client
2230

2331
class MyScript(Script):
2432
"""All modular inputs should inherit from the abstract base class Script
@@ -87,11 +95,9 @@ def validate_input(self, validation_definition):
8795
# Get the values of the parameters, and construct a URL for the Github API
8896
owner = validation_definition.parameters["owner"]
8997
repo_name = validation_definition.parameters["repo_name"]
90-
repo_url = "https://api.github.com/repos/%s/%s" % (owner, repo_name)
9198

92-
# Read the response from the Github API, then parse the JSON data into an object
93-
response = urllib2.urlopen(repo_url).read()
94-
jsondata = json.loads(response)
99+
# Call Github to retrieve repo information
100+
jsondata = _get_github_repos(owner, repo_name)
95101

96102
# If there is only 1 field in the jsondata object,some kind or error occurred
97103
# with the Github API.
@@ -125,9 +131,7 @@ def stream_events(self, inputs, ew):
125131
repo_name = input_item["repo_name"]
126132

127133
# Get the fork count from the Github API
128-
repo_url = "https://api.github.com/repos/%s/%s" % (owner, repo_name)
129-
response = urllib2.urlopen(repo_url).read()
130-
jsondata = json.loads(response)
134+
jsondata = _get_github_repos(owner, repo_name)
131135
fork_count = jsondata["forks_count"]
132136

133137
# Create an Event object, and set its fields
@@ -139,5 +143,20 @@ def stream_events(self, inputs, ew):
139143
# Tell the EventWriter to write this event
140144
ew.write_event(event)
141145

146+
147+
def _get_github_repos(owner, repo_name):
148+
# Read the response from the Github API, then parse the JSON data into an object
149+
repo_path = "/repos/%s/%s" % (owner, repo_name)
150+
connection = http_client.HTTPSConnection('api.github.com')
151+
headers = {
152+
'Content-type': 'application/json',
153+
'User-Agent': 'splunk-sdk-python',
154+
}
155+
connection.request('GET', repo_path, headers=headers)
156+
response = connection.getresponse()
157+
body = response.read().decode()
158+
return json.loads(body)
159+
160+
142161
if __name__ == "__main__":
143162
sys.exit(MyScript().run(sys.argv))

examples/random_numbers/README.md

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
splunk-sdk-python random_numbers example
2+
========================================
3+
4+
This app provides an example of a modular input that generates a random number between the min and max values provided by the user during setup of the input.
5+
6+
To run this example locally run `SPLUNK_VERSION=latest docker compose up -d` from the root of this repository which will mount this example alongside the latest version of splunklib within `/opt/splunk/etc/apps/random_numbers` and `/opt/splunk/etc/apps/random_numbers/lib/splunklib` within the `splunk` container.
7+
8+
Once the docker container is up and healthy log into the Splunk UI and setup a new `Random Numbers` input by visiting this page: http://localhost:8000/en-US/manager/random_numbers/datainputstats and selecting the "Add new..." button next to the Local Inputs > Random Inputs. Enter values for the `min` and `max` values which the random number should be generated between.
9+
10+
NOTE: If no Random Numbers input appears then the script is likely not running properly, see https://docs.splunk.com/Documentation/SplunkCloud/latest/AdvancedDev/ModInputsDevTools for more details on debugging the modular input using the command line and relevant logs.
11+
12+
Once the input is created you should be able to see an event when running the following search: `source="random_numbers://*"` the event should contain a `number` field with a float between the min and max specified when the input was created.

examples/random_numbers/random_numbers.py renamed to examples/random_numbers/bin/random_numbers.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,10 @@
1717
from __future__ import absolute_import
1818
import random, sys
1919
import os
20+
# NOTE: splunklib must exist within random_numbers/lib/splunklib for this
21+
# example to run! To run this locally use `SPLUNK_VERSION=latest docker compose up -d`
22+
# from the root of this repo which mounts this example and the latest splunklib
23+
# code together at /opt/splunk/etc/apps/random_numbers
2024
sys.path.insert(0, os.path.join(os.path.dirname(__file__), "..", "lib"))
2125

2226
from splunklib.modularinput import *

setup.py

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,13 +15,9 @@
1515
# under the License.
1616

1717
from setuptools import setup, Command
18-
from contextlib import closing
19-
from subprocess import check_call, STDOUT
2018

2119
import os
2220
import sys
23-
import shutil
24-
import tarfile
2521

2622
import splunklib
2723

0 commit comments

Comments
 (0)