|
| 1 | +import splunk from "../../splunk.app.mjs"; |
| 2 | +import { exec } from "child_process"; |
| 3 | +import util from "util"; |
| 4 | +import sampleEmit from "./test-event.mjs"; |
| 5 | + |
| 6 | +export default { |
| 7 | + key: "splunk-new-alert-fired", |
| 8 | + name: "New Alert Fired (Instant)", |
| 9 | + description: "Emit new event when a new alert is triggered in Splunk. [See the documentation](https://docs.splunk.com/Documentation/Splunk/9.4.1/RESTREF/RESTsearch#alerts.2Ffired_alerts)", |
| 10 | + version: "0.0.1", |
| 11 | + type: "source", |
| 12 | + dedupe: "unique", |
| 13 | + props: { |
| 14 | + splunk, |
| 15 | + http: "$.interface.http", |
| 16 | + db: "$.service.db", |
| 17 | + savedSearchName: { |
| 18 | + propDefinition: [ |
| 19 | + splunk, |
| 20 | + "savedSearchName", |
| 21 | + ], |
| 22 | + }, |
| 23 | + }, |
| 24 | + hooks: { |
| 25 | + async activate() { |
| 26 | + const response = await this.updateSavedSearch(`-d action.webhook=1 -d action.webhook.param.url="${this.http.endpoint}"`); |
| 27 | + if (!response) { |
| 28 | + throw new Error("Error creating webhook"); |
| 29 | + } |
| 30 | + }, |
| 31 | + async deactivate() { |
| 32 | + const response = await this.updateSavedSearch("-d action.webhook=0"); |
| 33 | + if (!response) { |
| 34 | + throw new Error("Error disabling webhook"); |
| 35 | + } |
| 36 | + }, |
| 37 | + }, |
| 38 | + methods: { |
| 39 | + async updateSavedSearch(data) { |
| 40 | + const cmd = `curl -X POST ${this.splunk._baseUrl()}/saved/searches/${encodeURIComponent(this.savedSearchName)}?output_mode=json -k -H "Authorization: Bearer ${this.splunk.$auth.api_token}" ${data}`; |
| 41 | + const execPromise = util.promisify(exec); |
| 42 | + try { |
| 43 | + const { stdout } = await execPromise(cmd); |
| 44 | + return stdout; |
| 45 | + } catch (error) { |
| 46 | + console.error("Error:", error.message); |
| 47 | + } |
| 48 | + }, |
| 49 | + generateMeta(alert) { |
| 50 | + const ts = +alert.result._time; |
| 51 | + return { |
| 52 | + id: ts, |
| 53 | + summary: `New Alert Fired for Source: ${alert.result.source}`, |
| 54 | + ts, |
| 55 | + }; |
| 56 | + }, |
| 57 | + }, |
| 58 | + async run(event) { |
| 59 | + const { body } = event; |
| 60 | + if (!body) { |
| 61 | + return; |
| 62 | + } |
| 63 | + |
| 64 | + const meta = this.generateMeta(body); |
| 65 | + this.$emit(body, meta); |
| 66 | + }, |
| 67 | + sampleEmit, |
| 68 | +}; |
0 commit comments