| 1 | import type { InfluxDBAlert } from "@/types/healthchecks.d" |
| 2 | import _toNumber from "lodash/toNumber" |
| 3 | import { acceptHMRUpdate, defineStore } from "pinia" |
| 4 | import Api from "@/api" |
| 5 | import { InfluxDBAlertSeverity } from "@/types/healthchecks.d" |
| 6 | import { IndexHealth } from "@/types/indices.d" |
| 7 | import { useAuthStore } from "./auth" |
| 8 | |
| 9 | export const useHealthcheckStore = defineStore("healthcheck", { |
| 10 | state: () => ({ |
| 11 | uncommittedJournalEntriesThreshold: _toNumber(import.meta.env.VITE_UNCOMMITTED_JOURNAL_ENTRIES_THRESHOLD), |
| 12 | healthchecksInterval: _toNumber(import.meta.env.VITE_HEALTHCHECKS_INTERVAL) * 1000, |
| 13 | getDataTimer: null as NodeJS.Timeout | null, |
| 14 | uncommittedJournalEntries: 0 as number | null, |
| 15 | clusterName: "" as string | null, |
| 16 | clusterStatus: IndexHealth.GREEN as IndexHealth | null, |
| 17 | alerts: null as InfluxDBAlert[] | null |
| 18 | }), |
| 19 | actions: { |
| 20 | getGraylogCheck() { |
| 21 | Api.graylog |
| 22 | .getMetrics() |
| 23 | .then(res => { |
| 24 | if (res.data.success) { |
| 25 | this.uncommittedJournalEntries = res.data.uncommitted_journal_entries || 0 |
| 26 | } else { |
| 27 | this.uncommittedJournalEntries = null |
| 28 | } |
| 29 | }) |
| 30 | .catch(() => { |
| 31 | this.uncommittedJournalEntries = null |
| 32 | }) |
| 33 | }, |
| 34 | getClusterHealth() { |
| 35 | Api.wazuh.indices |
| 36 | .getClusterHealth() |
| 37 | .then(res => { |
| 38 | if (res.data.success) { |
| 39 | this.clusterName = res.data.cluster_health.cluster_name |
| 40 | this.clusterStatus = res.data.cluster_health.status |
| 41 | } else { |
| 42 | this.clusterName = null |
| 43 | this.clusterStatus = null |
| 44 | } |
| 45 | }) |
| 46 | .catch(() => { |
| 47 | this.clusterName = null |
| 48 | this.clusterStatus = null |
| 49 | }) |
| 50 | }, |
| 51 | getHealthchecks() { |
| 52 | Api.healthchecks |
| 53 | .getHealthchecks({ |
| 54 | days: 1, |
| 55 | status: "active", |
| 56 | exclude_ok: true |
| 57 | }) |
| 58 | .then(res => { |
| 59 | if (res.data.success) { |
| 60 | // Filter to only show critical alerts |
| 61 | this.alerts = res.data.alerts.filter(o => o.severity === InfluxDBAlertSeverity.Critical) |
| 62 | } else { |
| 63 | this.alerts = null |
| 64 | } |
| 65 | }) |
| 66 | .catch(() => { |
| 67 | this.alerts = null |
| 68 | }) |
| 69 | }, |
| 70 | |
| 71 | getData() { |
| 72 | const authStore = useAuthStore() |
| 73 | |
| 74 | if (authStore.isLogged) { |
| 75 | if (this.uncommittedJournalEntriesThreshold) { |
| 76 | this.getGraylogCheck() |
| 77 | // this.getClusterHealth() |
| 78 | this.getHealthchecks() |
| 79 | } |
| 80 | } |
| 81 | }, |
| 82 | |
| 83 | stop() { |
| 84 | if (this.getDataTimer !== null) { |
| 85 | clearInterval(this.getDataTimer) |
| 86 | this.getDataTimer = null |
| 87 | } |
| 88 | }, |
| 89 | |
| 90 | start() { |
| 91 | this.getData() |
| 92 | if (this.healthchecksInterval) { |
| 93 | this.getDataTimer = setInterval(this.getData, this.healthchecksInterval) |
| 94 | } |
| 95 | } |
| 96 | } |
| 97 | }) |
| 98 | |
| 99 | if (import.meta.hot) { |
| 100 | import.meta.hot.accept(acceptHMRUpdate(useHealthcheckStore, import.meta.hot)) |
| 101 | } |