Files
rust-discovery/web/src/device-inventory.tsx
T

141 lines
6.7 KiB
TypeScript
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
import { useState, type ReactNode } from 'react';
import type { RecordValue } from './api';
const unreported = 'Nicht gemeldet';
const pageSize = 25;
function object(value: unknown): RecordValue {
return value && typeof value === 'object' && !Array.isArray(value) ? value as RecordValue : {};
}
function text(value: unknown): string | undefined {
return typeof value === 'string' && value.trim() ? value.trim() : undefined;
}
function values(value: unknown) {
return Array.isArray(value) ? value.flatMap(value => text(value) ?? []) : [];
}
function observation(device: RecordValue, name: string): RecordValue | undefined {
if (device.source_id === 'opnsense.base') {
const inv = object(device.inventory);
if (name === 'hardware' || name === 'os') return { data: inv.hardware };
const report = object(inv[name]);
if (report.status === 'ok' || report.status === 'partial')
return { data: report.entries, truncated: report.status === 'partial' };
return undefined;
}
const observations = object(device.inventory).observations;
return Array.isArray(observations) ? observations.map(object).find(item =>
item.quality === 'valid' && (item.scope === 'linux.' + name || item.scope === 'windows.' + name)
) : undefined;
}
export function systemDetails(device: RecordValue, formatTime: (value: unknown) => string) {
const os = observation(device, 'os');
const system = object(os?.data);
const hardware = object(observation(device, 'hardware')?.data);
const installed = formatTime(system.installed_at);
const source = system.install_date_source === 'root_filesystem' ? 'Root-Dateisystem'
: system.install_date_source === 'installer_directory' ? 'Installer-Verzeichnis' : null;
let booted = formatTime(system.booted_at);
if (booted === unreported && os?.scope === 'linux.os' && typeof system.uptime_seconds === 'number') {
const captured = Date.parse(text(os.observed_at) ?? '');
const estimated = captured - system.uptime_seconds * 1000;
if (system.uptime_seconds >= 0 && Number.isFinite(estimated) && estimated >= 0) {
booted = formatTime(new Date(estimated).toISOString());
}
}
return [
['OS installiert am', source && installed !== unreported ? installed + ' · geschätzt (' + source + ')' : installed],
['Letzter Neustart', booted],
['Hersteller', text(hardware.manufacturer) ?? text(object(hardware.readable_dmi).sys_vendor) ?? unreported],
['BIOS-Seriennummer', text(hardware.bios_serial_number) ?? unreported],
['Garantie bis', formatTime(hardware.warranty_expires_at)]
];
}
function entries(report: RecordValue | undefined): unknown[] | null {
const rows = Array.isArray(report?.data) ? report.data : object(report?.data).entries;
return Array.isArray(rows) ? rows : null;
}
function note(report: RecordValue | undefined) {
const readErrors = object(report?.data).read_errors;
return report?.truncated === true || (typeof readErrors === 'number' && readErrors > 0);
}
function InventoryTable({ headings, children }: { headings: string[]; children: ReactNode }) {
return <div className="table"><table>
<thead><tr>{headings.map(label => <th scope="col" key={label}>{label}</th>)}</tr></thead>
<tbody>{children}</tbody>
</table></div>;
}
function Software({ report }: { report: RecordValue | undefined }) {
const [query, setQuery] = useState('');
const [page, setPage] = useState(0);
const rows = entries(report)?.flatMap(entry => {
const row = object(entry);
const name = text(row.name) ?? text(row.display_name);
if (!name) return [];
return [{ name, version: text(row.version), publisher: text(row.publisher), architecture: text(row.architecture) }];
}).sort((left, right) => left.name.localeCompare(right.name, 'de'));
const matching = (rows ?? []).filter(row => Object.values(row).some(value =>
value?.toLocaleLowerCase('de').includes(query.trim().toLocaleLowerCase('de'))
));
const lastPage = Math.max(0, Math.ceil(matching.length / pageSize) - 1);
const current = Math.min(page, lastPage);
return <details>
<summary>Installierte Software{rows ? ' (' + rows.length + ')' : ''}</summary>
{note(report) && <p className="muted">Die Meldung enthält einen Teil der gefundenen Einträge.</p>}
{rows === undefined ? <p>{unreported}</p> : rows.length === 0 ? <p>Keine Softwareeinträge gemeldet.</p> : <>
<label>Software suchen
<input type="search" value={query} maxLength={120} onChange={e => { setQuery(e.target.value); setPage(0); }} />
</label>
{matching.length ? <InventoryTable headings={['Name', 'Version', 'Herausgeber', 'Architektur']}>
{matching.slice(current * pageSize, (current + 1) * pageSize).map((row, index) => <tr key={index}>
<td>{row.name}</td>
<td>{row.version ?? '–'}</td>
<td>{row.publisher ?? '–'}</td>
<td>{row.architecture ?? '–'}</td>
</tr>)}
</InventoryTable> : <p>Keine Treffer.</p>}
{lastPage > 0 && <div className="list-footer">
<span>Seite {current + 1} von {lastPage + 1}</span>
<div>
<button className="quiet" disabled={current === 0} onClick={() => setPage(current - 1)}>Zurück</button>
<button className="quiet" disabled={current === lastPage} onClick={() => setPage(current + 1)}>Weiter</button>
</div>
</div>}
</>}
</details>;
}
export function DeviceInventoryPanels({ device }: { device: RecordValue }) {
const network = observation(device, 'interfaces');
const interfaces = entries(network)?.map(object).filter(row => text(row.name));
const sessions = observation(device, 'users');
const users = values(entries(sessions)).sort((a, b) => a.localeCompare(b, 'de'));
return <>
<section>
<h2>Netzwerkkarten und IP-Adressen</h2>
{note(network) && <p className="muted">Die Meldung enthält einen Teil der Netzwerkkarten oder Adressen.</p>}
{interfaces === undefined ? <p>{unreported}</p> : interfaces.length === 0 ? <p>Keine Netzwerkkarten gemeldet.</p> :
<InventoryTable headings={['Netzwerkkarte', 'MAC-Adresse', 'IP-Adressen']}>
{interfaces.map((row, index) => <tr key={index}>
<td>{text(row.name)}</td>
<td>{text(row.mac) ?? '–'}</td>
<td>{values(row.addresses).map(address => address.replace(/%0$/, '')).join(', ') || 'Keine Adresse gemeldet'}</td>
</tr>)}
</InventoryTable>}
</section>
<section>
<h2>Angemeldete Benutzer</h2>
{note(sessions) && <p className="muted">Die Meldung enthält einen Teil der Sitzungen.</p>}
{users.length ? <ul>{users.map(name => <li key={name}>{name}</li>)}</ul>
: <p>{entries(sessions) && !note(sessions) ? 'Keine Benutzer angemeldet.' : unreported}</p>}
</section>
<section><Software report={observation(device, 'software')} /></section>
</>;
}