Template for python data pusher
Init
Scripts
splunk_url = "https://<splunk-ip>:8088/services/collector/event"
splunk_token = "<generated-token-from-HEC>" # HEC HTTP tokenLast updated
splunk_url = "https://<splunk-ip>:8088/services/collector/event"
splunk_token = "<generated-token-from-HEC>" # HEC HTTP tokenLast updated
# only runs once
import json
import requests
# --- Splunk Config ---
splunk_url = "https://<splunk-ip>:8088/services/collector/event"
splunk_token = "<generated-token-from-HEC>" # HEC HTTP token
headers = {
"Authorization": f"Splunk {splunk_token}"
}
# --- Weather API Config (WeatherAPI.com) ---
weather_api_key = "<weather-api-key>" # replace with your key
zip_code = "<zip-code>" # Example: New York zip code
weather_url = f"http://api.weatherapi.com/v1/current.json?key={weather_api_key}&q={zip_code}&aqi=no"
# --- Get weather data ---
try:
weather_response = requests.get(weather_url)
weather_response.raise_for_status()
weather_data = weather_response.json()
# Build Splunk event
event = {
"event": weather_data, # the JSON from WeatherAPI
"sourcetype": "_json",
"index": "python_weather",
"host": "weather_app"
}
# Send to Splunk
response = requests.post(
splunk_url,
headers=headers,
data=json.dumps(event),
verify=False # disable SSL verification if self-signed cert
)
if response.status_code == 200:
print("✅ Weather data successfully sent to Splunk")
else:
print(f"❌ Failed: {response.text}")
except requests.exceptions.RequestException as e:
print(f"Error fetching weather data: {e}")
# runs every hour
import json
import requests
import time
from datetime import datetime
# --- Splunk Config ---
splunk_url = "https://<splunk-ip>:8088/services/collector/event"
splunk_token = "<generated-token-from-HEC>" # HEC HTTP token
headers = {
"Authorization": f"Splunk {splunk_token}"
}
# --- Weather API Config ---
weather_api_key = "<weather-api-key>" # replace with your key
zip_code = "<zip-code>" # Example: New York zip code
weather_url = f"http://api.weatherapi.com/v1/current.json?key={weather_api_key}&q={zip_code}&aqi=no"
def send_weather():
try:
weather_response = requests.get(weather_url)
weather_response.raise_for_status()
weather_data = weather_response.json()
weather_data["ran_at"] = datetime.utcnow().isoformat() + "Z"
event = {
"event": weather_data,
"sourcetype": "_json",
"index": "python_weather",
"host": "weather_app"
}
response = requests.post(
splunk_url,
headers=headers,
data=json.dumps(event),
verify=False
)
if response.status_code == 200:
print(f"✅ Weather data sent to Splunk at {weather_data['ran_at']}")
else:
print(f"❌ Failed: {response.text}")
except requests.exceptions.RequestException as e:
print(f"Error fetching weather data: {e}")
# --- Run every hour ---
while True:
send_weather()
time.sleep(3600) # 3600 sec = 1 hour