|
| 1 | +/** |
| 2 | + * Test script for Splunk HEC connection |
| 3 | + * Run this in Node.js to test your Splunk HEC setup |
| 4 | + */ |
| 5 | + |
| 6 | +const https = require('https'); |
| 7 | + |
| 8 | +// Configuration - UPDATE THESE VALUES |
| 9 | +const SPLUNK_ENDPOINT = 'https://splunk-instance.nhs.uk:8088/services/collector'; |
| 10 | +const SPLUNK_TOKEN = 'hec-token-placeholder'; |
| 11 | + |
| 12 | +// Test event data |
| 13 | +const testEvent = { |
| 14 | + event: { |
| 15 | + timestamp: new Date().toISOString(), |
| 16 | + session_id: 'test_session_123', |
| 17 | + environment: 'test', |
| 18 | + user_agent: 'Test Script', |
| 19 | + page_url: 'http://localhost:4000/test', |
| 20 | + page_title: 'Test Page', |
| 21 | + page_path: '/test', |
| 22 | + referrer: 'http://localhost:4000/', |
| 23 | + event_type: 'test_event', |
| 24 | + event_category: 'test', |
| 25 | + event_action: 'test', |
| 26 | + event_label: 'HEC Test', |
| 27 | + event_value: 1, |
| 28 | + test_message: 'This is a test event from NHS Notify Web CMS' |
| 29 | + }, |
| 30 | + sourcetype: 'nhs_notify_frontend_events', |
| 31 | + index: 'events_nhsnotify_nonprod', |
| 32 | + host: 'test-host' |
| 33 | +}; |
| 34 | + |
| 35 | +// Function to send test event |
| 36 | +function sendTestEvent() { |
| 37 | + const postData = JSON.stringify(testEvent); |
| 38 | + |
| 39 | + const options = { |
| 40 | + hostname: new URL(SPLUNK_ENDPOINT).hostname, |
| 41 | + port: new URL(SPLUNK_ENDPOINT).port || 443, |
| 42 | + path: new URL(SPLUNK_ENDPOINT).pathname, |
| 43 | + method: 'POST', |
| 44 | + headers: { |
| 45 | + 'Content-Type': 'application/json', |
| 46 | + 'Authorization': `Splunk ${SPLUNK_TOKEN}`, |
| 47 | + 'Content-Length': Buffer.byteLength(postData) |
| 48 | + } |
| 49 | + }; |
| 50 | + |
| 51 | + const req = https.request(options, (res) => { |
| 52 | + console.log(`Status: ${res.statusCode}`); |
| 53 | + console.log(`Headers: ${JSON.stringify(res.headers)}`); |
| 54 | + |
| 55 | + let data = ''; |
| 56 | + res.on('data', (chunk) => { |
| 57 | + data += chunk; |
| 58 | + }); |
| 59 | + |
| 60 | + res.on('end', () => { |
| 61 | + console.log('Response:', data); |
| 62 | + if (res.statusCode === 200) { |
| 63 | + console.log('✅ SUCCESS: Test event sent to Splunk successfully!'); |
| 64 | + console.log('Check your Splunk index "events_nhsnotify_nonprod" for the test event.'); |
| 65 | + } else { |
| 66 | + console.log('❌ FAILED: Test event failed to send to Splunk.'); |
| 67 | + } |
| 68 | + }); |
| 69 | + }); |
| 70 | + |
| 71 | + req.on('error', (err) => { |
| 72 | + console.error('❌ ERROR:', err.message); |
| 73 | + console.log('Check your Splunk endpoint and token configuration.'); |
| 74 | + }); |
| 75 | + |
| 76 | + req.write(postData); |
| 77 | + req.end(); |
| 78 | +} |
| 79 | + |
| 80 | +// Run the test |
| 81 | +console.log('🚀 Testing Splunk HEC connection...'); |
| 82 | +console.log('Endpoint:', SPLUNK_ENDPOINT); |
| 83 | +console.log('Token:', SPLUNK_TOKEN.substring(0, 10) + '...'); |
| 84 | +console.log(''); |
| 85 | + |
| 86 | +sendTestEvent(); |
0 commit comments