|
| 1 | +import { logger } from "@homarr/log"; |
| 2 | + |
| 3 | +import { HandleIntegrationErrors } from "../base/errors/decorator"; |
| 4 | +import type { IntegrationTestingInput } from "../base/integration"; |
| 5 | +import { Integration } from "../base/integration"; |
| 6 | +import type { TestingResult } from "../base/test-connection/test-connection-service"; |
| 7 | +import type { ISystemHealthMonitoringIntegration } from "../interfaces/health-monitoring/health-monitoring-integration"; |
| 8 | +import type { SystemHealthMonitoring } from "../interfaces/health-monitoring/health-monitoring-types"; |
| 9 | +import type { UnraidArrayInfo, UnraidSystemInfo } from "./unraid-types"; |
| 10 | +import { unraidArrayInfoSchema, unraidSystemInfoSchema } from "./unraid-types"; |
| 11 | +import type { fetch as undiciFetch } from "undici/types/fetch"; |
| 12 | +import { fetchWithTrustedCertificatesAsync } from "@homarr/certificates/server"; |
| 13 | + |
| 14 | +const localLogger = logger.child({ module: "UnraidIntegration" }); |
| 15 | + |
| 16 | +@HandleIntegrationErrors([]) |
| 17 | +export class UnraidIntegration extends Integration implements ISystemHealthMonitoringIntegration { |
| 18 | + protected async testingAsync(input: IntegrationTestingInput): Promise<TestingResult> { |
| 19 | + await this.queryGraphQLAsync<{ info: UnraidSystemInfo }>( |
| 20 | + ` |
| 21 | + query { |
| 22 | + info { |
| 23 | + os { platform } |
| 24 | + } |
| 25 | + } |
| 26 | + `, |
| 27 | + input.fetchAsync, |
| 28 | + ); |
| 29 | + |
| 30 | + return { success: true }; |
| 31 | + } |
| 32 | + |
| 33 | + public async getSystemInfoAsync(): Promise<SystemHealthMonitoring> { |
| 34 | + const [systemInfo, arrayInfo] = await Promise.all([ |
| 35 | + this.getSystemInformationAsync(), |
| 36 | + this.getArrayInfoAsync(), |
| 37 | + ]); |
| 38 | + |
| 39 | + const cpuUtilization = systemInfo.cpu.usage?.reduce((acc, val) => acc + val, 0) ?? 0; |
| 40 | + const cpuCount = systemInfo.cpu.cores ?? 1; |
| 41 | + |
| 42 | + return { |
| 43 | + version: systemInfo.os.release ?? "unknown", |
| 44 | + cpuModelName: systemInfo.cpu.brand ?? "unknown", |
| 45 | + cpuUtilization: cpuUtilization / cpuCount, |
| 46 | + memUsedInBytes: (systemInfo.memory.total ?? 0) - (systemInfo.memory.free ?? 0), |
| 47 | + memAvailableInBytes: systemInfo.memory.total ?? 0, |
| 48 | + uptime: systemInfo.os.uptime ?? 0, |
| 49 | + network: null, |
| 50 | + loadAverage: systemInfo.cpu.loadAverage |
| 51 | + ? { |
| 52 | + "1min": systemInfo.cpu.loadAverage[0] ?? 0, |
| 53 | + "5min": systemInfo.cpu.loadAverage[1] ?? 0, |
| 54 | + "15min": systemInfo.cpu.loadAverage[2] ?? 0, |
| 55 | + } |
| 56 | + : null, |
| 57 | + rebootRequired: false, |
| 58 | + availablePkgUpdates: 0, |
| 59 | + cpuTemp: systemInfo.cpu.temperature ?? undefined, |
| 60 | + fileSystem: arrayInfo.disks.map((disk) => ({ |
| 61 | + deviceName: disk.name ?? "unknown", |
| 62 | + used: this.formatBytes(disk.size - disk.free), |
| 63 | + available: this.formatBytes(disk.free), |
| 64 | + percentage: disk.size > 0 ? ((disk.size - disk.free) / disk.size) * 100 : 0, |
| 65 | + })), |
| 66 | + smart: arrayInfo.disks.map((disk) => ({ |
| 67 | + deviceName: disk.name ?? "unknown", |
| 68 | + temperature: disk.temp ?? null, |
| 69 | + overallStatus: disk.status ?? "unknown", |
| 70 | + })), |
| 71 | + }; |
| 72 | + } |
| 73 | + |
| 74 | + private async getSystemInformationAsync(): Promise<UnraidSystemInfo> { |
| 75 | + localLogger.debug("Retrieving system information", { |
| 76 | + url: this.url("/graphql"), |
| 77 | + }); |
| 78 | + |
| 79 | + const query = ` |
| 80 | + query { |
| 81 | + info { |
| 82 | + os { |
| 83 | + platform |
| 84 | + distro |
| 85 | + release |
| 86 | + uptime |
| 87 | + } |
| 88 | + cpu { |
| 89 | + manufacturer |
| 90 | + brand |
| 91 | + cores |
| 92 | + threads |
| 93 | + loadAverage |
| 94 | + usage |
| 95 | + temperature |
| 96 | + } |
| 97 | + memory { |
| 98 | + total |
| 99 | + free |
| 100 | + used |
| 101 | + } |
| 102 | + } |
| 103 | + } |
| 104 | + `; |
| 105 | + |
| 106 | + const response = await this.queryGraphQLAsync<{ info: UnraidSystemInfo }>(query); |
| 107 | + const result = await unraidSystemInfoSchema.parseAsync(response.info); |
| 108 | + |
| 109 | + localLogger.debug("Retrieved system information", { |
| 110 | + url: this.url("/graphql"), |
| 111 | + }); |
| 112 | + |
| 113 | + return result; |
| 114 | + } |
| 115 | + |
| 116 | + private async getArrayInfoAsync(): Promise<UnraidArrayInfo> { |
| 117 | + localLogger.debug("Retrieving array information", { |
| 118 | + url: this.url("/graphql"), |
| 119 | + }); |
| 120 | + |
| 121 | + const query = ` |
| 122 | + query { |
| 123 | + array { |
| 124 | + state |
| 125 | + capacity { |
| 126 | + disks { |
| 127 | + free |
| 128 | + used |
| 129 | + total |
| 130 | + } |
| 131 | + } |
| 132 | + disks { |
| 133 | + name |
| 134 | + size |
| 135 | + free |
| 136 | + status |
| 137 | + temp |
| 138 | + } |
| 139 | + } |
| 140 | + } |
| 141 | + `; |
| 142 | + |
| 143 | + const response = await this.queryGraphQLAsync<{ array: UnraidArrayInfo }>(query); |
| 144 | + const result = await unraidArrayInfoSchema.parseAsync(response.array); |
| 145 | + |
| 146 | + localLogger.debug("Retrieved array information", { |
| 147 | + url: this.url("/graphql"), |
| 148 | + diskCount: result.disks.length, |
| 149 | + }); |
| 150 | + |
| 151 | + return result; |
| 152 | + } |
| 153 | + |
| 154 | + private async queryGraphQLAsync<T>( |
| 155 | + query: string, |
| 156 | + fetchAsync: typeof undiciFetch = fetchWithTrustedCertificatesAsync, |
| 157 | + ): Promise<T> { |
| 158 | + const url = this.url("/graphql"); |
| 159 | + const apiKey = this.getSecretValue("apiKey"); |
| 160 | + |
| 161 | + localLogger.debug("Sending GraphQL query", { |
| 162 | + url: url.toString(), |
| 163 | + }); |
| 164 | + |
| 165 | + const response = await fetchAsync(url, { |
| 166 | + method: "POST", |
| 167 | + headers: { |
| 168 | + "Content-Type": "application/json", |
| 169 | + "x-api-key": apiKey, |
| 170 | + }, |
| 171 | + body: JSON.stringify({ query }), |
| 172 | + }); |
| 173 | + |
| 174 | + if (!response.ok) { |
| 175 | + throw new Error(`GraphQL request failed: ${response.status} ${response.statusText}`); |
| 176 | + } |
| 177 | + |
| 178 | + const json = (await response.json()) as { data: T; errors?: { message: string }[] }; |
| 179 | + |
| 180 | + if (json.errors) { |
| 181 | + throw new Error(`GraphQL errors: ${json.errors.map((error) => error.message).join(", ")}`); |
| 182 | + } |
| 183 | + |
| 184 | + return json.data; |
| 185 | + } |
| 186 | + |
| 187 | + private formatBytes(bytes: number): string { |
| 188 | + if (bytes === 0) return "0 B"; |
| 189 | + const k = 1024; |
| 190 | + const sizes = ["B", "KB", "MB", "GB", "TB"]; |
| 191 | + const i = Math.floor(Math.log(bytes) / Math.log(k)); |
| 192 | + return `${parseFloat((bytes / Math.pow(k, i)).toFixed(2))} ${sizes[i]}`; |
| 193 | + } |
| 194 | +} |
0 commit comments