-
-
Notifications
You must be signed in to change notification settings - Fork 10
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
29 changed files
with
1,068 additions
and
22 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
import { observable } from "@trpc/server/observable"; | ||
|
||
import type { CpuLoad, MemoryLoad, NetworkLoad, ServerInfo } from "@homarr/integrations"; | ||
import { createItemAndIntegrationChannel } from "@homarr/redis"; | ||
|
||
import { createOneIntegrationMiddleware } from "../../middlewares/integration"; | ||
import { createTRPCRouter, publicProcedure } from "../../trpc"; | ||
|
||
export const hardwareUsageRouter = createTRPCRouter({ | ||
getServerInfo: publicProcedure | ||
.unstable_concat(createOneIntegrationMiddleware("query", "getDashDot")) | ||
.query(async ({ ctx }) => { | ||
const channel = createItemAndIntegrationChannel<{ | ||
info: ServerInfo; | ||
}>("hardwareUsage", ctx.integration.id); | ||
const data = await channel.getAsync(); | ||
return { | ||
info: data?.data.info ?? ({} as ServerInfo), | ||
}; | ||
}), | ||
getHardwareInformationHistory: publicProcedure | ||
.unstable_concat(createOneIntegrationMiddleware("query", "getDashDot")) | ||
.query(async ({ ctx }) => { | ||
const channel = createItemAndIntegrationChannel<{ | ||
cpuLoad: CpuLoad; | ||
memoryLoad: MemoryLoad; | ||
networkLoad: NetworkLoad; | ||
}>("hardwareUsage", ctx.integration.id); | ||
const data = await channel.getAsync(); | ||
return { | ||
cpuLoad: data?.data.cpuLoad ?? ({} as CpuLoad), | ||
memoryLoad: data?.data.memoryLoad ?? ({} as MemoryLoad), | ||
networkLoad: data?.data.networkLoad ?? ({} as NetworkLoad), | ||
}; | ||
}), | ||
subscribeCpu: publicProcedure | ||
.unstable_concat(createOneIntegrationMiddleware("query", "getDashDot")) | ||
.subscription(({ ctx }) => { | ||
return observable<{ cpuLoad: CpuLoad; memoryLoad: MemoryLoad; networkLoad: NetworkLoad }>((emit) => { | ||
const channel = createItemAndIntegrationChannel<{ | ||
cpuLoad: CpuLoad; | ||
memoryLoad: MemoryLoad; | ||
networkLoad: NetworkLoad; | ||
}>("hardwareUsage", ctx.integration.id); | ||
const unsubscribe = channel.subscribe((data) => { | ||
emit.next(data); | ||
}); | ||
return () => { | ||
unsubscribe(); | ||
}; | ||
}); | ||
}), | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
67 changes: 67 additions & 0 deletions
67
packages/cron-jobs/src/jobs/integrations/hardware-usage.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
import { decryptSecret } from "@homarr/common/server"; | ||
import { EVERY_SECOND } from "@homarr/cron-jobs-core/expressions"; | ||
import { db, eq } from "@homarr/db"; | ||
import { items } from "@homarr/db/schema/sqlite"; | ||
import type { CpuLoad, MemoryLoad, NetworkLoad, ServerInfo } from "@homarr/integrations"; | ||
import { DashDotIntegration } from "@homarr/integrations"; | ||
import { createItemAndIntegrationChannel } from "@homarr/redis"; | ||
|
||
import { createCronJob } from "../../lib"; | ||
|
||
export const hardwareUsageJob = createCronJob("hardwareUsage", EVERY_SECOND).withCallback(async () => { | ||
const itemsForIntegration = await db.query.items.findMany({ | ||
where: eq(items.kind, "hardwareUsage"), | ||
with: { | ||
integrations: { | ||
with: { | ||
integration: { | ||
with: { | ||
secrets: { | ||
columns: { | ||
kind: true, | ||
value: true, | ||
}, | ||
}, | ||
}, | ||
}, | ||
}, | ||
}, | ||
}, | ||
}); | ||
|
||
for (const itemForIntegration of itemsForIntegration) { | ||
for (const integration of itemForIntegration.integrations) { | ||
const dashDotIntegration = new DashDotIntegration({ | ||
...integration.integration, | ||
decryptedSecrets: integration.integration.secrets.map((secret) => ({ | ||
...secret, | ||
value: decryptSecret(secret.value), | ||
})), | ||
}); | ||
|
||
const info = await dashDotIntegration.getInfoAsync(); | ||
const cpuLoad = await dashDotIntegration.getCurrentCpuLoadAsync(); | ||
const memoryLoad = await dashDotIntegration.getCurrentMemoryLoadAsync(); | ||
const networkLoad = await dashDotIntegration.getCurrentNetworkLoadAsync(); | ||
|
||
const cache = createItemAndIntegrationChannel<{ | ||
info: ServerInfo; | ||
cpuLoad: CpuLoad; | ||
memoryLoad: MemoryLoad; | ||
networkLoad: NetworkLoad; | ||
}>("hardwareUsage", integration.integrationId); | ||
await cache.setAsync({ | ||
memoryLoad, | ||
networkLoad, | ||
cpuLoad, | ||
info, | ||
}); | ||
await cache.publishAndUpdateLastStateAsync({ | ||
cpuLoad, | ||
networkLoad, | ||
memoryLoad, | ||
info, | ||
}); | ||
} | ||
} | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,93 @@ | ||
import { Integration } from "../base/integration"; | ||
import type { CpuLoad } from "../interfaces/hardware-usage/cpu-load"; | ||
import type { MemoryLoad } from "../interfaces/hardware-usage/memory-load"; | ||
import type { NetworkLoad } from "../interfaces/hardware-usage/network-load"; | ||
import type { ServerInfo } from "../interfaces/hardware-usage/server-info"; | ||
|
||
export class DashDotIntegration extends Integration { | ||
public async testConnectionAsync(): Promise<void> { | ||
const response = await fetch(this.appendPathToUrlWithEndingSlash(this.integration.url, "info")); | ||
await response.json(); | ||
} | ||
|
||
public async getInfoAsync(): Promise<ServerInfo> { | ||
const infoResponse = await fetch(this.appendPathToUrlWithEndingSlash(this.integration.url, "info")); | ||
const serverInfo = (await infoResponse.json()) as InternalServerInfo; | ||
return { | ||
maxAvailableMemoryBytes: serverInfo.ram.size, | ||
}; | ||
} | ||
|
||
public async getCurrentCpuLoadAsync(): Promise<CpuLoad> { | ||
const cpu = await fetch(this.appendPathToUrlWithEndingSlash(this.integration.url, "load/cpu")); | ||
const data = (await cpu.json()) as CpuLoadApi[]; | ||
return { | ||
sumLoad: data.reduce((acc, current) => acc + current.load, 0) / data.length, | ||
}; | ||
} | ||
|
||
public async getCurrentMemoryLoadAsync(): Promise<MemoryLoad> { | ||
const memoryLoad = await fetch(this.appendPathToUrlWithEndingSlash(this.integration.url, "load/ram")); | ||
const data = (await memoryLoad.json()) as MemoryLoadApi; | ||
return { | ||
loadInBytes: data.load, | ||
}; | ||
} | ||
|
||
public async getCurrentNetworkLoadAsync(): Promise<NetworkLoad> { | ||
const memoryLoad = await fetch(this.appendPathToUrlWithEndingSlash(this.integration.url, "load/network")); | ||
const data = (await memoryLoad.json()) as NetworkLoadApi; | ||
return { | ||
down: data.down, | ||
up: data.up, | ||
}; | ||
} | ||
} | ||
|
||
/** | ||
* CPU load per core | ||
*/ | ||
interface CpuLoadApi { | ||
load: number; | ||
} | ||
|
||
interface MemoryLoadApi { | ||
load: number; | ||
} | ||
|
||
interface NetworkLoadApi { | ||
up: number; | ||
down: number; | ||
} | ||
|
||
interface InternalServerInfo { | ||
ram: { | ||
/** | ||
* Available memory in bytes | ||
*/ | ||
size: number; | ||
}; | ||
storage: { | ||
/** | ||
* Size of storage in bytes | ||
*/ | ||
size: number; | ||
disks: { | ||
/** | ||
* Name of the device, e.g. sda | ||
*/ | ||
device: string; | ||
|
||
/** | ||
* Brand name of the device | ||
*/ | ||
brand: string; | ||
|
||
/** | ||
* Type of the device. | ||
* See option "physical" of https://systeminformation.io/filesystem.html | ||
*/ | ||
type: string; | ||
}[]; | ||
}[]; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
3 changes: 3 additions & 0 deletions
3
packages/integrations/src/interfaces/hardware-usage/cpu-load.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
export interface CpuLoad { | ||
sumLoad: number; | ||
} |
3 changes: 3 additions & 0 deletions
3
packages/integrations/src/interfaces/hardware-usage/memory-load.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
export interface MemoryLoad { | ||
loadInBytes: number; | ||
} |
4 changes: 4 additions & 0 deletions
4
packages/integrations/src/interfaces/hardware-usage/network-load.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
export interface NetworkLoad { | ||
up: number; | ||
down: number; | ||
} |
3 changes: 3 additions & 0 deletions
3
packages/integrations/src/interfaces/hardware-usage/server-info.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
export interface ServerInfo { | ||
maxAvailableMemoryBytes: number; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.