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