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 )
0 commit comments