Skip to content

Commit 8ded0c6

Browse files
author
Shakeel Mohamed
committed
Added 2 modular input examples.
*pythonic_github_forks - takes a github user/org name and repository name, creates an even with the number of forks on that repository *pythonic_random_number - takes a min and max number, creates an event with a random value between the min and max
1 parent 30b1234 commit 8ded0c6

File tree

12 files changed

+229
-0
lines changed

12 files changed

+229
-0
lines changed
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
[pythonic_github_forks://<name>]
2+
*Generates events containing the number of forks on a Github repo, uses the Python SDK.
3+
4+
owner = <value>
5+
repo_name = <value>
Lines changed: 99 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,99 @@
1+
#!/usr/bin/env python
2+
#
3+
# Copyright 2013 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+
from random import random
18+
import sys, logging, urllib2, json
19+
20+
from splunklib.modularinput.argument import Argument
21+
from splunklib.modularinput.event import Event
22+
from splunklib.modularinput.scheme import Scheme
23+
from splunklib.modularinput.script import Script
24+
25+
try:
26+
import xml.etree.cElementTree as ET
27+
except ImportError:
28+
import xml.etree.ElementTree as ET
29+
30+
# set up logging suitable for splunkd consumption
31+
logging.root
32+
logging.root.setLevel(logging.DEBUG)
33+
formatter = logging.Formatter('%(levelname)s %(message)s')
34+
handler = logging.StreamHandler()
35+
handler.setFormatter(formatter)
36+
logging.root.addHandler(handler)
37+
38+
def count_forks(owner=None, repo_name=None):
39+
if owner is None or repo_name is None:
40+
return None
41+
else:
42+
repo_url = "https://api.github.com/repos/%s/%s" % (owner, repo_name)
43+
response = urllib2.urlopen(repo_url).read()
44+
jsondata = json.loads(response)
45+
46+
return jsondata["forks_count"]
47+
48+
class MyScript(Script):
49+
def get_scheme(self):
50+
51+
scheme = Scheme("Pythonic Github Forks Counter")
52+
scheme.description = "Generates events containing the number of forks of a Github repository."
53+
scheme.use_external_validation = True
54+
scheme.use_single_instance = True
55+
56+
min_argument = Argument("owner")
57+
min_argument.data_type = Argument.data_type_number
58+
min_argument.description = "Github user or organization that created the repository."
59+
min_argument.required_on_create = True
60+
scheme.add_argument(min_argument)
61+
62+
max_argument = Argument("repo_name")
63+
max_argument.data_type = Argument.data_type_number
64+
max_argument.description = "Name of the Github repository."
65+
max_argument.required_on_create = True
66+
scheme.add_argument(max_argument)
67+
68+
return scheme
69+
70+
def validate_input(self, validation_definition):
71+
owner = validation_definition.parameters["owner"]
72+
repo_name = validation_definition.parameters["repo_name"]
73+
74+
def stream_events(self, inputs, ew):
75+
76+
for input_name in inputs.inputs:
77+
owner = inputs.inputs[input_name]["owner"]
78+
repo_name = inputs.inputs[input_name]["repo_name"]
79+
80+
fork_count = count_forks(owner, repo_name)
81+
82+
event = Event()
83+
event.stanza = input_name
84+
event.data = "repo_url=http://github.com/%s/%s; fork_count=%s" \
85+
% (owner, repo_name, fork_count)
86+
ew.write_event(event)
87+
88+
def do_run(args):
89+
"""
90+
:param args: list of command line arguments
91+
"""
92+
#skip first argument "./bin/[this file name].py"
93+
args = args[1:]
94+
95+
script = MyScript()
96+
script.run(args)
97+
98+
if __name__ == "__main__":
99+
do_run(sys.argv)
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
[install]
2+
is_configured = 0
3+
4+
[ui]
5+
is_visible = 1
6+
label = Pythonic Github repository fork counter
7+
8+
[launcher]
9+
author=Shakeel Mohamed
10+
description=Pythonic Github (Modular Input example)
11+
version = 1.0
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
[install]
2+
state = enabled

examples/pythonic_github_forks/local/inputs.conf

Whitespace-only changes.
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
[]
2+
export = system
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
[pythonic_random_numbers://<name>]
2+
*Generates events containing a random floating point number.
3+
4+
min = <value>
5+
max = <value>
Lines changed: 90 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,90 @@
1+
#!/usr/bin/env python
2+
#
3+
# Copyright 2013 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+
from random import random
18+
import sys, logging
19+
20+
from splunklib.modularinput.argument import Argument
21+
from splunklib.modularinput.event import Event
22+
from splunklib.modularinput.scheme import Scheme
23+
from splunklib.modularinput.script import Script
24+
25+
try:
26+
import xml.etree.cElementTree as ET
27+
except ImportError:
28+
import xml.etree.ElementTree as ET
29+
30+
# set up logging suitable for splunkd consumption
31+
logging.root
32+
logging.root.setLevel(logging.DEBUG)
33+
formatter = logging.Formatter('%(levelname)s %(message)s')
34+
handler = logging.StreamHandler()
35+
handler.setFormatter(formatter)
36+
logging.root.addHandler(handler)
37+
38+
class MyScript(Script):
39+
def get_scheme(self):
40+
41+
scheme = Scheme("Pythonic Random Numbers")
42+
scheme.description = "Generates events containing a random number, uses the Python SDK."
43+
scheme.use_external_validation = True
44+
scheme.use_single_instance = True
45+
46+
min_argument = Argument("min")
47+
min_argument.data_type = Argument.data_type_number
48+
min_argument.description = "Minimum random number to be produced by this input."
49+
min_argument.required_on_create = True
50+
scheme.add_argument(min_argument)
51+
52+
max_argument = Argument("max")
53+
max_argument.data_type = Argument.data_type_number
54+
max_argument.description = "Minimum random number to be produced by this input."
55+
max_argument.required_on_create = True
56+
scheme.add_argument(max_argument)
57+
58+
return scheme
59+
60+
def validate_input(self, validation_definition):
61+
minimum = float(validation_definition.parameters["min"])
62+
maximum = float(validation_definition.parameters["max"])
63+
64+
if minimum >= maximum:
65+
raise ValueError("min must be less than max; found min=%d, max=%d" % minimum, maximum)
66+
67+
def stream_events(self, inputs, ew):
68+
69+
for input_name in inputs.inputs:
70+
minimum = float(inputs.inputs[input_name]["min"])
71+
maximum = float(inputs.inputs[input_name]["max"])
72+
73+
event = Event()
74+
event.stanza = input_name
75+
event.data = "number=" + str(random() * (maximum - minimum) + minimum)
76+
77+
ew.write_event(event)
78+
79+
def do_run(args):
80+
"""
81+
:param args: list of command line arguments
82+
"""
83+
#skip first argument "./bin/[this file name].py"
84+
args = args[1:]
85+
86+
script = MyScript()
87+
script.run(args)
88+
89+
if __name__ == "__main__":
90+
do_run(sys.argv)
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
[install]
2+
is_configured = 0
3+
4+
[ui]
5+
is_visible = 1
6+
label = Pythonic random-numbers
7+
8+
[launcher]
9+
author=Shakeel Mohamed
10+
description=Pythonic Random Numbers (Modular Input Example)
11+
version = 1.0
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
[install]
2+
state = enabled

0 commit comments

Comments
 (0)