Real-time monitoring dashboard for Proxmox VE, PBS, Docker, Kubernetes, TrueNAS and vSphere. Self-hosted, with alerts for failed backups, full disks, unhealthy containers and offline nodes.
Go aialertsdashboarddockerdocker-monitoringgohomelabinfrastructure-monitoringkubernetesmonitoringmspproxmoxproxmox-backup-serverproxmox-mail-gatewayproxmox-veself-hostedtruenastypescriptvspherewebhooks
import type { Resource } from '@/types/resource';
import type { StorageRecord, StorageAdapter, StorageAdapterContext } from './models';
import {
getCanonicalStoragePlatformKey,
getResourceStorageActionSummary,
getResourceStorageImpactSummary,
getResourceStorageIssueLabel,
getResourceStorageIssueSummary,
getResourceStoragePlatformLabel,
getResourceStorageProtectionLabel,
getResourceStorageProtectionSummary,
getResourceStorageTopologyLabel,
hasUnraidStorageAttentionIssue,
isUnraidStorageResource,
} from './resourceStoragePresentation';
import {
getStorageCapabilitiesForResource,
getStorageCategoryFromType,
isBackupRepositoryStorageResource,
isCanonicalDatastoreStorageResource,
readResourceStorageMeta,
resolveResourceStorageContent,
} from './resourceStorageMapping';
import {
asNumberOrNull,
buildStorageCapacity,
buildStorageSource,
canonicalStorageIdentityKey,
dedupe,
getStringArray,
metricsTargetForStorageResource,
normalizeStorageResourceHealth,
} from './storageAdapterCore';
const mapResourceStorageRecord = (resource: Resource, adapterId: string): StorageRecord => {
const platformData = (resource.platformData as Record<string, unknown> | undefined) || {};
const storageMeta = readResourceStorageMeta(resource, platformData);
const resourceType = (resource.type || '').toLowerCase();
const storageTypeHint =
storageMeta?.type || (platformData.type as string | undefined) || undefined;
const storagePlatform =
storageMeta?.platform ||
(platformData.platform as string | undefined) ||
resource.storage?.platform ||
resource.platformType;
const storageTopology =
storageMeta?.topology ||
(platformData.topology as string | undefined) ||
resource.storage?.topology;
const storageType = storageTypeHint || (resourceType === 'pbs' ? 'pbs' : resourceType);
const shared =
typeof storageMeta?.shared === 'boolean'
? storageMeta.shared
: typeof platformData.shared === 'boolean'
? platformData.shared
: undefined;
const classificationContext = {
resourceType,
platform: storagePlatform,
topology: storageTopology,
entityType: resource.vmware?.entityType,
shared,
};
const isBackupRepository = isBackupRepositoryStorageResource(
storageType,
storageMeta,
classificationContext,
);
const isDatastore = isCanonicalDatastoreStorageResource(
storageType,
storageMeta,
classificationContext,
);
const content = resolveResourceStorageContent(
storageMeta,
platformData,
isBackupRepository ? 'backup' : '',
);
const proxmoxPlatform = platformData.proxmox as Record<string, unknown> | undefined;
const proxmoxNativeNode =
resource.proxmox?.nodeName ||
resource.proxmox?.node ||
(proxmoxPlatform?.nodeName as string | undefined);
const proxmoxNodeDisplay =
resource.proxmox?.nodeDisplayName ||
(proxmoxPlatform?.nodeDisplayName as string | undefined) ||
resource.parentName ||
proxmoxNativeNode;
const proxmoxNodeAliases = [
...(resource.proxmox?.nodeAliases ?? []),
...getStringArray(proxmoxPlatform?.nodeAliases),
];
const proxmoxNodeIdentity =
resource.proxmox?.nodeIdentity || (proxmoxPlatform?.nodeIdentity as string | undefined);
const storageNodes = getStringArray(
(resource.storage as Record<string, unknown> | undefined)?.nodes,
);
const nodeHints = dedupe(
[
resource.parentName,
proxmoxNodeDisplay,
proxmoxNativeNode,
...proxmoxNodeAliases,
proxmoxNodeIdentity,
platformData.node as string | undefined,
...storageNodes,
resource.parentId,
resource.platformId,
].filter((value): value is string => typeof value === 'string' && value.trim().length > 0),
);
const locationLabel = isBackupRepository
? resource.parentName ||
(platformData.pbsInstanceName as string | undefined) ||
proxmoxNodeDisplay ||
(platformData.node as string | undefined) ||
storageNodes[0] ||
resource.parentId ||
resource.platformId ||
'Unknown'
: resource.parentName ||
proxmoxNodeDisplay ||
(platformData.node as string | undefined) ||
storageNodes[0] ||
resource.parentId ||
resource.platformId ||
'Unknown';
const usagePercent = asNumberOrNull(resource.disk?.current);
const totalBytes = asNumberOrNull(resource.disk?.total);
const usedBytes = asNumberOrNull(resource.disk?.used);
const freeBytes = asNumberOrNull(resource.disk?.free);
const canonicalPlatform = getCanonicalStoragePlatformKey(resource, resource.storage?.platform);
// The poller folds a shared storage into one entry whose node is the
// synthetic "cluster" marker (the mock uses "shared"); show that as a
// shared storage with its reporting node count rather than a node name.
const sharedLocationMarkers = new Set(['cluster', 'shared']);
const hostLabel =
shared && sharedLocationMarkers.has(locationLabel.trim().toLowerCase())
? storageNodes.length > 1
? `Shared · ${storageNodes.length} nodes`
: 'Shared'
: locationLabel;
const platformLabel = getResourceStoragePlatformLabel(canonicalPlatform);
const issueLabel = getResourceStorageIssueLabel(resource);
const issueSummary = getResourceStorageIssueSummary(resource);
const impactSummary = getResourceStorageImpactSummary(resource);
const actionSummary = getResourceStorageActionSummary(resource);
const protectionLabel = getResourceStorageProtectionLabel(resource);
const protectionSummary = getResourceStorageProtectionSummary(resource);
const isUnraid = isUnraidStorageResource(resource);
const normalizedHealth = normalizeStorageResourceHealth(
resource.status,
resource.tags,
resource.incidentSeverity,
);
const health =
normalizedHealth === 'warning' && isUnraid && !hasUnraidStorageAttentionIssue(resource)
? 'healthy'
: normalizedHealth;
const statusLabel =
isUnraid && storageMeta?.arrayState ? storageMeta.arrayState : resource.status;
const topologyLabel = getResourceStorageTopologyLabel(
resource,
storageType,
// Prefer the concrete vdev layout ("Mirror", "Raidz2") over the generic
// discriminator ("Pool") when the provider reports one.
resource.storage?.vdevLayout || resource.storage?.topology,
);
const metricsTarget = metricsTargetForStorageResource(resource);
const sourceStatuses = Object.values(resource.sourceStatus ?? {});
const degradedSource = sourceStatuses.find((source) =>
['stale', 'offline', 'error', 'failed', 'unauthorized'].includes(
(source.status || '').trim().toLowerCase(),
),
);
const freshness = degradedSource
? 'stale'
: sourceStatuses.some((source) => (source.status || '').trim().toLowerCase() === 'online')
? 'fresh'
: 'unknown';
return {
id: resource.id,
name: resource.name,
category:
storageMeta?.isCeph || storageMeta?.isZfs
? 'pool'
: getStorageCategoryFromType(storageType, classificationContext),
health,
statusLabel,
hostLabel,
platformLabel,
platformKey: canonicalPlatform,
topologyLabel,
protectionLabel,
protectionSummary,
protectionReduced: resource.storage?.protectionReduced,
rebuildInProgress: resource.storage?.rebuildInProgress,
incidentCategory: resource.incidentCategory,
incidentSeverity: resource.incidentSeverity,
incidentPriority: resource.incidentPriority || 0,
issueLabel,
issueSummary,
actionSummary,
impactSummary,
consumerCount: resource.storage?.consumerCount || 0,
protectedWorkloadCount: resource.pbs?.protectedWorkloadCount || 0,
affectedDatastoreCount: resource.pbs?.affectedDatastoreCount || 0,
location: {
label: locationLabel,
scope: isBackupRepository ? 'cluster' : isDatastore ? (shared ? 'cluster' : 'host') : 'node',
},
capacity: buildStorageCapacity(totalBytes, usedBytes, freeBytes, usagePercent),
capabilities: getStorageCapabilitiesForResource(
storageType,
storageMeta,
classificationContext,
),
source: buildStorageSource(canonicalPlatform, adapterId),
observedAt:
typeof resource.lastSeen === 'number' && Number.isFinite(resource.lastSeen)
? resource.lastSeen
: Date.now(),
freshness,
freshnessError: degradedSource?.error?.trim() || undefined,
metricsTarget,
refs: {
resourceId: metricsTarget?.resourceId || resource.id,
platformEntityId: resource.platformId,
},
details: {
type: storageType,
status: statusLabel,
parentId: resource.parentId,
parentName: resource.parentName,
node: proxmoxNativeNode || (platformData.node as string | undefined) || storageNodes[0],
instance:
resource.proxmox?.instance ||
(proxmoxPlatform?.instance as string | undefined) ||
(platformData.instance as string | undefined),
nodeHints,
hostLabel,
platformLabel,
topologyLabel,
issueLabel,
issueSummary,
impactSummary,
actionSummary,
incidentCategory: resource.incidentCategory,
incidentSeverity: resource.incidentSeverity,
incidentPriority: resource.incidentPriority,
protectionLabel,
protectionReduced: resource.storage?.protectionReduced,
rebuildInProgress: resource.storage?.rebuildInProgress,
platform: storagePlatform,
topology: storageTopology,
content,
contentTypes: storageMeta?.contentTypes,
shared,
path: storageMeta?.path,
protection: storageMeta?.protection,
isCeph: storageMeta?.isCeph,
isZfs: storageMeta?.isZfs,
zfsPool: storageMeta?.zfsPool ?? platformData.zfsPool,
arrayState: storageMeta?.arrayState,
syncAction: storageMeta?.syncAction,
syncProgress: storageMeta?.syncProgress,
numProtected: storageMeta?.numProtected,
numDisabled: storageMeta?.numDisabled,
numInvalid: storageMeta?.numInvalid,
numMissing: storageMeta?.numMissing,
},
};
};
const resourceStorageAdapter: StorageAdapter = {
id: 'resource-storage',
supports: (ctx) => Array.isArray(ctx.resources) && ctx.resources.length > 0,
build: (ctx) =>
(ctx.resources || [])
.filter((resource) => resource.type === 'storage' || resource.type === 'datastore')
.map((resource) => mapResourceStorageRecord(resource, 'resource-storage')),
};
export const DEFAULT_STORAGE_ADAPTERS: StorageAdapter[] = [resourceStorageAdapter];
const mergeStorageRecords = (current: StorageRecord, incoming: StorageRecord): StorageRecord => {
const preferred =
(current.incidentPriority || 0) >= (incoming.incidentPriority || 0) ? current : incoming;
const secondary = preferred === current ? incoming : current;
return {
...secondary,
...preferred,
capabilities: dedupe([...(current.capabilities || []), ...(incoming.capabilities || [])]),
health:
preferred.health === 'unknown' && secondary.health !== 'unknown'
? secondary.health
: preferred.health,
statusLabel: preferred.statusLabel || secondary.statusLabel,
hostLabel: preferred.hostLabel || secondary.hostLabel,
platformLabel: preferred.platformLabel || secondary.platformLabel,
platformKey: preferred.platformKey || secondary.platformKey,
topologyLabel: preferred.topologyLabel || secondary.topologyLabel,
protectionLabel:
preferred.protectionReduced || preferred.rebuildInProgress || preferred.incidentPriority
? preferred.protectionLabel || secondary.protectionLabel
: secondary.protectionLabel || preferred.protectionLabel,
protectionSummary:
preferred.protectionReduced || preferred.rebuildInProgress || preferred.incidentPriority
? preferred.protectionSummary || secondary.protectionSummary
: secondary.protectionSummary || preferred.protectionSummary,
protectionReduced: preferred.protectionReduced || secondary.protectionReduced,
rebuildInProgress: preferred.rebuildInProgress || secondary.rebuildInProgress,
issueLabel:
(preferred.incidentPriority || 0) > 0
? preferred.issueLabel || secondary.issueLabel
: secondary.issueLabel || preferred.issueLabel,
issueSummary:
(preferred.incidentPriority || 0) > 0
? preferred.issueSummary || secondary.issueSummary
: secondary.issueSummary || preferred.issueSummary,
actionSummary:
(preferred.incidentPriority || 0) > 0
? preferred.actionSummary || secondary.actionSummary
: secondary.actionSummary || preferred.actionSummary,
impactSummary:
secondary.impactSummary &&
secondary.impactSummary !== 'No dependent resources' &&
(preferred.impactSummary === 'No dependent resources' || !preferred.impactSummary)
? secondary.impactSummary
: preferred.impactSummary || secondary.impactSummary,
consumerCount: Math.max(current.consumerCount || 0, incoming.consumerCount || 0),
protectedWorkloadCount: Math.max(
current.protectedWorkloadCount || 0,
incoming.protectedWorkloadCount || 0,
),
affectedDatastoreCount: Math.max(
current.affectedDatastoreCount || 0,
incoming.affectedDatastoreCount || 0,
),
metricsTarget: preferred.metricsTarget || secondary.metricsTarget,
refs: {
resourceId:
preferred.metricsTarget?.resourceId ||
secondary.metricsTarget?.resourceId ||
preferred.refs?.resourceId ||
secondary.refs?.resourceId,
platformEntityId: preferred.refs?.platformEntityId || secondary.refs?.platformEntityId,
},
details: {
...(secondary.details || {}),
...(preferred.details || {}),
},
};
};
export const buildStorageRecords = (
ctx: StorageAdapterContext,
adapters: StorageAdapter[] = DEFAULT_STORAGE_ADAPTERS,
): StorageRecord[] => {
const map = new Map<string, StorageRecord>();
for (const adapter of adapters) {
if (!adapter.supports(ctx)) continue;
const records = adapter.build(ctx);
for (const record of records) {
const key = canonicalStorageIdentityKey(record);
const existing = map.get(key);
if (!existing) {
map.set(key, record);
continue;
}
map.set(key, mergeStorageRecords(existing, record));
}
}
return Array.from(map.values());
};