Skip to content

armgit / Pulse

Public Imported from GitHub
0

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.

16.4 KB
Raw Blame
import type { Resource, ResourceMetric, ResourceType } from '@/types/resource';
import { buildProxmoxBackupsPath } from '@/routing/resourceLinks';
import { formatProxmoxVersion } from '@/utils/proxmoxVersion';
import { matchesSearchTermSplit, splitSearchExclusions } from '@/utils/searchQuery';

export type ProxmoxPageTabId = 'overview' | 'storage' | 'replication' | 'backups' | 'ceph' | 'mail';

export type ProxmoxPlatformScope = 'proxmox-pve' | 'proxmox-pbs' | 'proxmox-pmg';

export type ProxmoxTabSpec = {
  id: ProxmoxPageTabId;
  label: string;
  path: string;
};

export type ProxmoxResourceTypeCounts = Partial<Record<ResourceType, number>>;

export type ProxmoxClusterGroup = {
  id: string;
  label: string;
  nodes: Resource[];
  guests: Resource[];
  storage: Resource[];
};

export type ProxmoxPageSummary = {
  clusterCount: number;
  nodeCount: number;
  guestCount: number;
  runningGuestCount: number;
  degradedGuestCount: number;
  stoppedGuestCount: number;
  storageCount: number;
  pbsCount: number;
  pmgCount: number;
  cephCount: number;
};

export type ProxmoxPageModel = {
  resources: Resource[];
  pveNodes: Resource[];
  guests: Resource[];
  storage: Resource[];
  pbs: Resource[];
  pmg: Resource[];
  ceph: Resource[];
  physicalDisks: Resource[];
  clusterGroups: ProxmoxClusterGroup[];
  summary: ProxmoxPageSummary;
};

const PROXMOX_RESOURCE_TYPES = new Set<ResourceType>([
  'agent',
  'vm',
  'system-container',
  'oci-container',
  'storage',
  'datastore',
  'pool',
  'dataset',
  'physical_disk',
  'ceph',
  'pbs',
  'pmg',
]);

const PROXMOX_WORKLOAD_TYPES = new Set<ResourceType>(['vm', 'system-container', 'oci-container']);

const PROXMOX_STORAGE_TYPES = new Set<ResourceType>([
  'storage',
  'datastore',
  'pool',
  'dataset',
  'physical_disk',
]);

export const PROXMOX_TAB_SPECS: ProxmoxTabSpec[] = [
  { id: 'overview', label: 'Overview', path: '/proxmox/overview' },
  { id: 'storage', label: 'Storage', path: '/proxmox/storage' },
  { id: 'replication', label: 'Replication', path: '/proxmox/replication' },
  { id: 'backups', label: 'Backups', path: buildProxmoxBackupsPath() },
  { id: 'ceph', label: 'Ceph', path: '/proxmox/ceph' },
  { id: 'mail', label: 'Mail Gateway', path: '/proxmox/mail' },
];

const isRecord = (value: unknown): value is Record<string, unknown> =>
  typeof value === 'object' && value !== null;

const getPlatformData = (resource: Resource): Record<string, unknown> => {
  if (!isRecord(resource.platformData)) return {};
  return resource.platformData;
};

const getPlatformSources = (resource: Resource): string[] => {
  const sources = getPlatformData(resource).sources;
  if (!Array.isArray(sources)) return resource.sources ?? [];
  return sources.filter((source): source is string => typeof source === 'string');
};

export function resolveProxmoxPlatformScope(resource: Resource): ProxmoxPlatformScope | null {
  if (resource.type === 'pbs') return 'proxmox-pbs';
  if (resource.type === 'pmg') return 'proxmox-pmg';
  if (resource.platformType === 'proxmox-pve') return 'proxmox-pve';
  if (resource.platformType === 'proxmox-pbs') return 'proxmox-pbs';
  if (resource.platformType === 'proxmox-pmg') return 'proxmox-pmg';
  if (resource.proxmox || isRecord(getPlatformData(resource).proxmox)) return 'proxmox-pve';

  const sources = getPlatformSources(resource).map((source) => source.toLowerCase());
  if (sources.some((source) => source === 'pbs' || source === 'proxmox-pbs')) {
    return 'proxmox-pbs';
  }
  if (sources.some((source) => source === 'pmg' || source === 'proxmox-pmg')) {
    return 'proxmox-pmg';
  }
  if (
    sources.some((source) => source === 'pve' || source === 'proxmox' || source === 'proxmox-pve')
  ) {
    return 'proxmox-pve';
  }

  return null;
}

export function isProxmoxResource(resource: Resource): boolean {
  return (
    PROXMOX_RESOURCE_TYPES.has(resource.type) && resolveProxmoxPlatformScope(resource) !== null
  );
}

export function isProxmoxWorkload(resource: Resource): boolean {
  return (
    PROXMOX_WORKLOAD_TYPES.has(resource.type) &&
    resolveProxmoxPlatformScope(resource) === 'proxmox-pve'
  );
}

export function isProxmoxStorageResource(resource: Resource): boolean {
  const scope = resolveProxmoxPlatformScope(resource);
  if (resource.type === 'ceph' || resource.storage?.isCeph === true) return false;
  return (
    PROXMOX_STORAGE_TYPES.has(resource.type) && (scope === 'proxmox-pve' || scope === 'proxmox-pbs')
  );
}

export function getMetricPercent(metric?: ResourceMetric): number {
  if (!metric) return 0;
  if (typeof metric.current === 'number' && Number.isFinite(metric.current)) {
    return Math.max(0, Math.min(100, metric.current));
  }
  if (metric.total && metric.used) {
    return Math.max(0, Math.min(100, (metric.used / metric.total) * 100));
  }
  return 0;
}

export function getResourceClusterLabel(resource: Resource): string {
  return (
    resource.proxmox?.clusterName ||
    resource.identity?.clusterName ||
    resource.clusterId ||
    'Standalone'
  );
}

export function getResourceNodeName(resource: Resource): string {
  return (
    resource.proxmox?.nodeName ||
    resource.proxmox?.node ||
    resource.parentName ||
    resource.identity?.hostname ||
    resource.name
  );
}

export function getProxmoxProviderScope(resource: Resource): string {
  return (
    resource.proxmox?.instance ||
    resource.platformId ||
    resource.clusterId ||
    resource.parentId ||
    resource.id
  );
}

const getProxmoxProviderScopes = (resource: Resource): string[] =>
  [resource.proxmox?.instance, resource.platformId, resource.clusterId]
    .filter((scope): scope is string => typeof scope === 'string' && scope.trim().length > 0)
    .map((scope) => scope.trim());

export function isProxmoxChildOfNode(child: Resource, node: Resource): boolean {
  if (child.parentId && child.parentId === node.id) return true;
  if (getResourceNodeName(child) !== getResourceNodeName(node)) return false;

  const childScopes = getProxmoxProviderScopes(child);
  const nodeScopes = new Set(getProxmoxProviderScopes(node));
  return (
    childScopes.length === 0 ||
    nodeScopes.size === 0 ||
    childScopes.some((scope) => nodeScopes.has(scope))
  );
}

const buildProxmoxNodeResolver = (nodes: readonly Resource[]) => {
  const nodesById = new Map(nodes.map((node) => [node.id, node] as const));
  const nodesByName = new Map<string, Resource[]>();
  for (const node of nodes) {
    const name = getResourceNodeName(node);
    const matching = nodesByName.get(name);
    if (matching) matching.push(node);
    else nodesByName.set(name, [node]);
  }

  return (child: Resource): Resource | undefined => {
    if (child.parentId) {
      const explicitParent = nodesById.get(child.parentId);
      if (explicitParent) return explicitParent;
    }
    const candidates = nodesByName.get(getResourceNodeName(child)) ?? [];
    if (candidates.length === 0) return undefined;
    const childScopes = getProxmoxProviderScopes(child);
    return candidates.find((node) => {
      const nodeScopes = new Set(getProxmoxProviderScopes(node));
      return (
        childScopes.length === 0 ||
        nodeScopes.size === 0 ||
        childScopes.some((scope) => nodeScopes.has(scope))
      );
    });
  };
};

export function getResourceVmid(resource: Resource): string {
  const vmid = resource.proxmox?.vmid;
  if (typeof vmid === 'number' && Number.isFinite(vmid)) {
    return String(vmid);
  }
  const platformProxmox = getPlatformData(resource).proxmox;
  if (isRecord(platformProxmox) && typeof platformProxmox.vmid === 'number') {
    return String(platformProxmox.vmid);
  }
  return '';
}

// Search here mirrors the operator-visible Proxmox workload vocabulary. Raw
// unified-resource ids are intentionally excluded: VM/LXC rows expose their
// canonical node-scoped id/VMID instead, and matching an opaque hash would
// retain a parent node for a result the guest table cannot show.
const getProxmoxSearchValues = (resource: Resource): Array<string | number | undefined> => [
  resource.name,
  resource.displayName,
  resource.parentName,
  resource.status,
  resource.proxmox?.node,
  resource.proxmox?.nodeName,
  resource.proxmox?.nodeIdentity,
  resource.proxmox?.nodeDisplayName,
  ...(resource.proxmox?.nodeAliases ?? []),
  resource.proxmox?.clusterName,
  resource.proxmox?.instance,
  getResourceVmid(resource),
];

/**
 * Keeps the overview's host inventory aligned with its workload search.
 * A matching guest makes its parent node searchable too, so searching for a
 * VM/LXC does not leave the operator looking at an unrelated host preview.
 */
export const filterProxmoxNodes = (
  nodes: Resource[],
  guests: Resource[],
  search: string,
): Resource[] => {
  const split = splitSearchExclusions(search);
  if (!split.needle && split.excludes.length === 0) return nodes;

  const resolveNode = buildProxmoxNodeResolver(nodes);
  const searchValuesByNodeId = new Map(
    nodes.map((node) => [node.id, getProxmoxSearchValues(node)] as const),
  );
  for (const guest of guests) {
    const owningNode = resolveNode(guest);
    if (!owningNode) continue;
    searchValuesByNodeId.get(owningNode.id)?.push(...getProxmoxSearchValues(guest));
  }

  return nodes.filter((node) => {
    const haystack = (searchValuesByNodeId.get(node.id) ?? [])
      .filter(
        (value): value is string | number => typeof value === 'string' || typeof value === 'number',
      )
      .map(String)
      .join(' ')
      .toLowerCase();
    return matchesSearchTermSplit(haystack, split);
  });
};

export function getResourceLastBackup(resource: Resource): string | number | null {
  const platformProxmox = getPlatformData(resource).proxmox;
  if (!isRecord(platformProxmox)) return null;
  const value = platformProxmox.lastBackup;
  return typeof value === 'string' || typeof value === 'number' ? value : null;
}

const hasBackupSignal = (resource: Resource): boolean => {
  if (getResourceLastBackup(resource) !== null) return true;
  const haystack = [
    resource.id,
    resource.name,
    resource.displayName,
    ...(resource.tags ?? []),
    ...(resource.recentChanges ?? []).flatMap((change) => [
      change.id,
      change.kind,
      change.sourceAdapter,
      change.reason,
      ...(change.relatedResources ?? []),
    ]),
  ]
    .filter((value): value is string => typeof value === 'string')
    .join(' ')
    .toLowerCase();

  return (
    haystack.includes('backup') || haystack.includes('snapshot') || haystack.includes('vzdump')
  );
};

export function getResourceVersion(resource: Resource): string {
  const pveVersion = formatProxmoxVersion(resource.proxmox?.pveVersion);
  if (pveVersion) return pveVersion;
  const platformProxmox = getPlatformData(resource).proxmox;
  if (isRecord(platformProxmox) && typeof platformProxmox.pveVersion === 'string') {
    const version = formatProxmoxVersion(platformProxmox.pveVersion);
    if (version) return version;
  }
  if (resource.pbs?.version) return resource.pbs.version;
  const platformPbs = getPlatformData(resource).pbs;
  if (isRecord(platformPbs) && typeof platformPbs.version === 'string') return platformPbs.version;
  const platformPmg = getPlatformData(resource).pmg;
  if (isRecord(platformPmg) && typeof platformPmg.version === 'string') return platformPmg.version;
  if (resource.agent?.osName?.toLowerCase().includes('proxmox') && resource.agent.osVersion) {
    return formatProxmoxVersion(resource.agent.osVersion) || resource.agent.osVersion;
  }
  return '';
}

export function buildProxmoxPageModel(resources: Resource[]): ProxmoxPageModel {
  const proxmoxResources = resources.filter(isProxmoxResource);
  const pveNodes = proxmoxResources.filter(
    (resource) =>
      resource.type === 'agent' && resolveProxmoxPlatformScope(resource) === 'proxmox-pve',
  );
  const guests = proxmoxResources.filter(isProxmoxWorkload);
  const storage = proxmoxResources.filter(isProxmoxStorageResource);
  const pbs = proxmoxResources.filter(
    (resource) => resolveProxmoxPlatformScope(resource) === 'proxmox-pbs',
  );
  const pmg = proxmoxResources.filter(
    (resource) => resolveProxmoxPlatformScope(resource) === 'proxmox-pmg',
  );
  const ceph = proxmoxResources.filter(
    (resource) =>
      resource.type === 'ceph' ||
      resource.storage?.isCeph === true ||
      isRecord(getPlatformData(resource).ceph),
  );
  const physicalDisks = proxmoxResources.filter((resource) => resource.type === 'physical_disk');
  const resolveOwningNode = buildProxmoxNodeResolver(pveNodes);

  const groupsById = new Map<string, ProxmoxClusterGroup>();
  const ensureGroup = (label: string, providerScope: string): ProxmoxClusterGroup => {
    const labelKey = label.toLowerCase() === 'standalone' ? '__standalone__' : label;
    const id = `${providerScope}::${labelKey}`;
    const existing = groupsById.get(id);
    if (existing) return existing;
    const created: ProxmoxClusterGroup = {
      id,
      label,
      nodes: [],
      guests: [],
      storage: [],
    };
    groupsById.set(id, created);
    return created;
  };

  pveNodes.forEach((resource) => {
    ensureGroup(getResourceClusterLabel(resource), getProxmoxProviderScope(resource)).nodes.push(
      resource,
    );
  });

  guests.forEach((resource) => {
    const owningNode = resolveOwningNode(resource);
    ensureGroup(
      owningNode ? getResourceClusterLabel(owningNode) : getResourceClusterLabel(resource),
      owningNode ? getProxmoxProviderScope(owningNode) : getProxmoxProviderScope(resource),
    ).guests.push(resource);
  });

  storage.forEach((resource) => {
    const owningNode = resolveOwningNode(resource);
    ensureGroup(
      owningNode ? getResourceClusterLabel(owningNode) : getResourceClusterLabel(resource),
      owningNode ? getProxmoxProviderScope(owningNode) : getProxmoxProviderScope(resource),
    ).storage.push(resource);
  });

  const clusterGroups = Array.from(groupsById.values()).sort((a, b) => {
    const aStandalone = a.label.toLowerCase() === 'standalone';
    const bStandalone = b.label.toLowerCase() === 'standalone';
    if (aStandalone !== bStandalone) return aStandalone ? 1 : -1;
    return a.label.localeCompare(b.label) || a.id.localeCompare(b.id);
  });

  return {
    resources: proxmoxResources,
    pveNodes,
    guests,
    storage,
    pbs,
    pmg,
    ceph,
    physicalDisks,
    clusterGroups,
    summary: {
      clusterCount: clusterGroups.filter((group) => group.label.toLowerCase() !== 'standalone')
        .length,
      nodeCount: pveNodes.length,
      guestCount: guests.length,
      runningGuestCount: guests.filter(
        (resource) => resource.status === 'online' || resource.status === 'running',
      ).length,
      degradedGuestCount: guests.filter(
        (resource) => resource.status === 'degraded' || resource.status === 'warning',
      ).length,
      stoppedGuestCount: guests.filter(
        (resource) => resource.status === 'offline' || resource.status === 'stopped',
      ).length,
      storageCount: storage.length,
      pbsCount: pbs.length,
      pmgCount: pmg.length,
      cephCount: ceph.length,
    },
  };
}

// Replication deliberately bypasses the unified-resource pipeline (it is
// projected straight from Monitor.ReplicationJobsSnapshot via
// /api/replication/jobs), so the tab is gated on the fetched job count
// rather than on anything derivable from `model`.
export function buildVisibleProxmoxTabSpecs(
  model: ProxmoxPageModel,
  replicationJobCount: number,
): ProxmoxTabSpec[] {
  const visible = new Set<ProxmoxPageTabId>(['overview']);

  if (model.storage.length > 0 || model.physicalDisks.length > 0) {
    visible.add('storage');
  }
  if (replicationJobCount > 0) {
    visible.add('replication');
  }
  if (model.resources.some(hasBackupSignal) || model.pbs.length > 0) {
    visible.add('backups');
  }
  if (model.ceph.length > 0) {
    visible.add('ceph');
  }
  if (model.pmg.length > 0) {
    visible.add('mail');
  }

  return PROXMOX_TAB_SPECS.filter((tab) => visible.has(tab.id));
}

export function buildVisibleProxmoxTabSpecsFromCounts(
  counts: ProxmoxResourceTypeCounts,
  replicationJobCount: number,
): ProxmoxTabSpec[] {
  const visible = new Set<ProxmoxPageTabId>(['overview']);
  const count = (type: ResourceType): number => Math.max(0, counts[type] ?? 0);
  if (count('storage') + count('physical_disk') > 0) visible.add('storage');
  if (replicationJobCount > 0) visible.add('replication');
  if (count('pbs') + count('vm') + count('system-container') > 0) visible.add('backups');
  if (count('ceph') > 0) visible.add('ceph');
  if (count('pmg') > 0) visible.add('mail');
  return PROXMOX_TAB_SPECS.filter((tab) => visible.has(tab.id));
}