-
Notifications
You must be signed in to change notification settings - Fork 120
/
say_my_name.py
127 lines (99 loc) · 3.47 KB
/
say_my_name.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
import json
import requests
import subprocess
import time
requests.packages.urllib3.disable_warnings()
SPLUNK_URL = 'https://localhost:8089'
AUTH = requests.auth.HTTPBasicAuth('username', 'password')
# Hipchat creds and my user id
hipchat_url = "https://hipchat.company.com"
hipchat_auth_token = ""
hipchat_user_id = 1
name = "josh"
# Check for mentions within the last minute
name_search = ("""search index=say_what sourcetype=_json """
"""minutes=*{}* earliest=-1m@s""".format(name))
# Get everything that was said in the last minute
minutes_search = ("""search index=say_what sourcetype=_json """
"""minutes=* earliest=-1m@s| sort _time| fields minutes""")
def start_splunk_search(search_string):
try:
r = requests.post(
'%s/services/search/jobs' % SPLUNK_URL,
data={"search": search_string},
auth=AUTH,
verify=False,
params={'output_mode': 'json'})
except Exception as e:
print(e)
sid = r.json()['sid']
return sid
def search_results(sid):
done = False
print "Searching Splunk"
while not done:
status = requests.get(
'%s/services/search/jobs/%s' % (SPLUNK_URL, sid),
auth=AUTH,
verify=False,
params={'output_mode': 'json'})
done = status.json()['entry'][0]['content']['isDone']
time.sleep(1)
result_count = int(status.json()['entry'][0]['content']['resultCount'])
return result_count
def get_results(sid, result_count):
events = []
offset = 0
while result_count > len(events):
offset = len(events)
results = requests.get(
'%s/services/search/jobs/%s/results/' % (SPLUNK_URL, sid),
auth=AUTH,
verify=False,
params={'output_mode': 'json', 'count': 0, 'f': 'minutes', 'offset': offset})
more_events = [row['minutes'].encode('utf-8') for row in results.json()['results']]
events += more_events
return events
def splunk_search(search_string):
search_id = start_splunk_search(search_string)
search_result_count = search_results(search_id)
events = get_results(search_id, search_result_count)
# print "Splunk returned {} events".format(search_result_count)
return events
def notify(user_id, auth_token, message):
# Send the conversation context to yourself on hipchat
data = {
'message': message,
'notify': True,
'message_format': 'text'
}
r = requests.post('{}/v2/user/{}/message'.format(hipchat_url, user_id),
data=json.dumps(data),
headers = {
'content-type': 'application/json',
"Authorization": "Bearer {}".format(auth_token)
}
)
def muted():
# Play the "Sorry, I was on mute" audio file.
audio_file = "./Muted.m4a"
# This works on my macbook,
# change the command line tool to another one if on a different OS
audio_output = subprocess.call(["afplay", audio_file])
while True:
mentioned = splunk_search(name_search)
if not mentioned:
time.sleep(1)
continue
mention_time = time.time()
print("You were mentioned!")
minutes = "\n".join(splunk_search(minutes_search))
try:
notify(hipchat_user_id, hipchat_auth_token, minutes)
except Exception as e:
print(e)
while int(time.time() - mention_time) < 15:
time.sleep(1)
muted()
# Wait another minute before checking for name mentions
time.sleep(60)