-
Notifications
You must be signed in to change notification settings - Fork 0
/
splunk_weather-gov_observations.py
78 lines (66 loc) · 2.33 KB
/
splunk_weather-gov_observations.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
''' SPLUNK_WEATHER-GOV_OBSERVATIONS '''
import boto3
import datetime
import json
import os
import re
import requests
import time
def lambda_handler(event, context):
start = time.perf_counter()
# UNWRAP EVENT BODY FROM SQS
for record in event['Records']:
body = json.loads(record['body'])
# INITIALIZE PARAMETERS
source_endpoint = "/observations/latest"
sqs_client = boto3.client("sqs",
region_name = os.environ['SQS_REGION'])
body['forwarders'].append(context.invoked_function_arn)
# GETS LATEST OBSERVATIONS FOR STATION
try:
response = requests.get(body['station'] + source_endpoint,
timeout = 60)
except requests.exceptions.RequestException as e:
raise(e)
# PRINTS STATUS
log = {
'time': datetime.datetime.now().isoformat(),
'description': 'api.weather.gov get latest observations',
'station': body['station'],
'status': response.status_code,
'status_detail': response.reason,
'duration': round(time.perf_counter() - start, 3)
}
print(json.dumps(log))
# ENDS IF STATUS IS NOT 200
if response.status_code != 200:
return {
'statusCode': response.status_code
}
start = time.perf_counter()
# CONSTRUCT PAYLOAD WITH REQUIRED ELEMENTS FOR HTTP EVENT COLLECTOR
payload = {
'index': os.environ['SPLUNK_INDEX'],
'host': (re.search("(?<=//)[^/]*", body['station'])).group(0),
'source': source_endpoint,
'sourcetype': os.environ['SPLUNK_SOURCETYPE'],
'forwarders': body['forwarders'],
'endpoint': os.environ['SPLUNK_ENDPOINT'],
'event': response.json()
}
# SEND PAYLOAD TO SQS TO BE PROCESSED VIA splunk_http-inputs-illinois
sqs = sqs_client.send_message(QueueUrl = os.environ['SQS_URL'],
MessageBody = json.dumps(payload))
# PRINT STATUS
log = {
'time': datetime.datetime.now().isoformat(),
'description': 'sqs conditions to HEC',
'status': sqs['ResponseMetadata']['HTTPStatusCode'],
'retry_attempts': sqs['ResponseMetadata']['RetryAttempts'],
'duration': round(time.perf_counter() - start, 3)
}
print(json.dumps(log))
# END
return {
'statusCode': 200
}