This script fetches performance statistics from the Imperva API, processes the data into a flattened JSON format, and sends it to Splunk for monitoring and analysis. The script runs continuously, fetching new data every minute and sending it to Splunk.
- Python 3.6+
requestsmodule (install usingpip install requests)
Before running the script, replace all placeholder values (<...>) with your actual values:
<api-id>: Your Imperva API ID<api-key>: Your Imperva API Key<splunk-address-collector>: Your Splunk HTTP Event Collector (HEC) address<splunk-token>: Your Splunk authentication tokenSITE_IDS: Replace with the relevant site IDs for your organization
Example:
API_ID = "your-api-id"
API_KEY = "your-api-key"
SPLUNK_ADDRESS = "https://your-splunk-server:8088/services/collector"
SPLUNK_TOKEN = "your-splunk-token"
SITE_IDS = ["111111", "2222222"]- The script fetches performance statistics from Imperva’s API using the provided API credentials.
- It processes the response and extracts three key metrics:
errorResponseTypesoriginResponseTimepopLatency
- The extracted metrics are converted into a flat JSON format.
- Each flattened JSON object is sent directly to Splunk.
- The script repeats this process every 60 seconds.
To run the script, use the following command:
python3 upload_splunk_script.pyThe script will continue running and sending data to Splunk. To stop execution, use CTRL+C.
- If you encounter an SSL error, try disabling SSL verification by modifying the
requests.postcall:response = requests.post(splunk_address, headers=headers, data=json.dumps(splunk_event), verify=False)
- If the script does not send data to Splunk, ensure that your Splunk HEC is configured to accept events.
- If you see an authentication error, verify that your Splunk token and API credentials are correct.
- The script runs every minute at exactly MM:00 UTC to ensure consistent execution.
- Ensure that your Splunk HEC endpoint is reachable from the system running this script.
- If running in production, consider using a logging mechanism instead of printing errors to stdout.
- The retry parameters (
RETRY_COUNTandRETRY_DELAY) can be adjusted to fine-tune error handling and API request retries.