Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
84 changes: 84 additions & 0 deletions lib/ActiveConnections.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
<?php

declare(strict_types=1);

/**
* SPDX-FileCopyrightText: 2026 Nextcloud GmbH and Nextcloud contributors
* SPDX-License-Identifier: AGPL-3.0-or-later
*/

namespace OCA\ServerInfo;

use OCP\IDBConnection;

class ActiveConnections {
public function __construct(
private IDBConnection $db,
) {
}

/**
* Approximate active sessions/connections by counting auth tokens
* with recent activity. Each token corresponds to one client (browser
* tab, mobile app, desktop client, etc.).
*
* @return array{
* last5min: int,
* last1h: int,
* totalTokens: int,
* byType: array<string, int>
* }
*/
public function getActiveConnections(): array {
try {
return [
'last5min' => $this->countSince(time() - 300),
'last1h' => $this->countSince(time() - 3600),
'totalTokens' => $this->countTotal(),
'byType' => $this->byType(),
];
} catch (\Throwable) {
return ['last5min' => 0, 'last1h' => 0, 'totalTokens' => 0, 'byType' => []];
}
}

private function countSince(int $ts): int {
$qb = $this->db->getQueryBuilder();
$qb->select($qb->func()->count('id'))
->from('authtoken')
->where($qb->expr()->gte('last_activity', $qb->createNamedParameter($ts)));
$result = $qb->executeQuery();
$count = (int)$result->fetchOne();
$result->closeCursor();
return $count;
}

private function countTotal(): int {
$qb = $this->db->getQueryBuilder();
$qb->select($qb->func()->count('id'))->from('authtoken');
$result = $qb->executeQuery();
$count = (int)$result->fetchOne();
$result->closeCursor();
return $count;
}

/**
* @return array<string, int>
*/
private function byType(): array {
$qb = $this->db->getQueryBuilder();
$qb->select('type')
->selectAlias($qb->func()->count('id'), 'count')
->from('authtoken')
->where($qb->expr()->gte('last_activity', $qb->createNamedParameter(time() - 3600)))
->groupBy('type');
$result = $qb->executeQuery();
$out = ['session' => 0, 'permanent' => 0];
while (($row = $result->fetch()) !== false) {
$type = (int)($row['type'] ?? 0) === 0 ? 'session' : 'permanent';
$out[$type] = (int)($row['count'] ?? 0);
}
$result->closeCursor();
return $out;
}
}
87 changes: 87 additions & 0 deletions lib/ActivityRate.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
<?php

declare(strict_types=1);

/**
* SPDX-FileCopyrightText: 2026 Nextcloud GmbH and Nextcloud contributors
* SPDX-License-Identifier: AGPL-3.0-or-later
*/

namespace OCA\ServerInfo;

use OCP\App\IAppManager;
use OCP\IDBConnection;

class ActivityRate {
public function __construct(
private IAppManager $appManager,
private IDBConnection $db,
) {
}

/**
* Counts user-facing activity events from the activity app over
* recent time windows. Useful as a "is anything happening on the
* server right now" signal.
*
* @return array{
* installed: bool,
* last1h: int,
* last24h: int,
* last7d: int,
* topActions: list<array{action: string, count: int}>
* }
*/
public function getActivityRate(): array {
if (!$this->appManager->isInstalled('activity')) {
return ['installed' => false, 'last1h' => 0, 'last24h' => 0, 'last7d' => 0, 'topActions' => []];
}

try {
return [
'installed' => true,
'last1h' => $this->countSince(time() - 3600),
'last24h' => $this->countSince(time() - 86400),
'last7d' => $this->countSince(time() - 7 * 86400),
'topActions' => $this->topActions(),
];
} catch (\Throwable) {
return ['installed' => true, 'last1h' => 0, 'last24h' => 0, 'last7d' => 0, 'topActions' => []];
}
}

private function countSince(int $ts): int {
$qb = $this->db->getQueryBuilder();
$qb->select($qb->func()->count('activity_id'))
->from('activity')
->where($qb->expr()->gte('timestamp', $qb->createNamedParameter($ts)));
$result = $qb->executeQuery();
$count = (int)$result->fetchOne();
$result->closeCursor();
return $count;
}

/**
* @return list<array{action: string, count: int}>
*/
private function topActions(int $limit = 5): array {
$qb = $this->db->getQueryBuilder();
$qb->select('subjectparams', 'type')
->selectAlias($qb->func()->count('activity_id'), 'count')
->from('activity')
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

->where($qb->expr()->gte('timestamp', $qb->createNamedParameter(time() - 86400)))
->groupBy('type', 'subjectparams')
->orderBy('count', 'DESC')
->setMaxResults($limit);
$result = $qb->executeQuery();
$out = [];
while (($row = $result->fetch()) !== false) {
$out[] = [
'action' => (string)($row['type'] ?? 'unknown'),
'count' => (int)($row['count'] ?? 0),
];
}
$result->closeCursor();
return $out;
}
}
69 changes: 69 additions & 0 deletions lib/LogTailReader.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
<?php

declare(strict_types=1);

/**
* SPDX-FileCopyrightText: 2026 Nextcloud GmbH and Nextcloud contributors
* SPDX-License-Identifier: AGPL-3.0-or-later
*/

namespace OCA\ServerInfo;

use OCP\IConfig;
use OCP\Log\IFileBased;
use OCP\Log\ILogFactory;

class LogTailReader {
public function __construct(
private IConfig $config,
private ILogFactory $logFactory,
) {
}

/**
* @return array{
* entries: list<array{time: string, level: int, app: string, message: string}>,
* available: bool,
* reason?: string
* }
*/
public function recentErrors(int $limit = 8, int $minLevel = 2): array {
$logType = $this->config->getSystemValue('log_type', 'file');
Comment thread
ChristophWurst marked this conversation as resolved.
if ($logType !== 'file') {
return ['entries' => [], 'available' => false, 'reason' => 'log_type_not_file'];
}

$log = $this->logFactory->get('file');
if (!($log instanceof IFileBased)) {
return ['entries' => [], 'available' => false, 'reason' => 'log_not_readable'];
}

$raw = $log->getEntries($limit * 10);
$collected = [];
foreach ($raw as $entry) {
if (count($collected) >= $limit) {
break;
}
$level = (int)($entry['level'] ?? 0);
if ($level < $minLevel) {
continue;
}
$collected[] = [
'time' => (string)($entry['time'] ?? ''),
'level' => $level,
'app' => (string)($entry['app'] ?? ''),
'message' => $this->snippet((string)($entry['message'] ?? '')),
];
}

return ['entries' => $collected, 'available' => true];
}

private function snippet(string $msg, int $max = 200): string {
$msg = trim($msg);
if (mb_strlen($msg) > $max) {
return mb_substr($msg, 0, $max - 1) . '…';
}
return $msg;
}
}
112 changes: 112 additions & 0 deletions lib/LoginStats.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,112 @@
<?php

declare(strict_types=1);

/**
* SPDX-FileCopyrightText: 2026 Nextcloud GmbH and Nextcloud contributors
* SPDX-License-Identifier: AGPL-3.0-or-later
*/

namespace OCA\ServerInfo;

use OCP\IConfig;
use OCP\IDBConnection;

class LoginStats {
public function __construct(
private IConfig $config,
private IDBConnection $db,
) {
}

/**
* @return array{
* bruteforceAttempts24h: int,
* bruteforceAttempts1h: int,
* bruteforceTotal: int,
* topIps: list<array{ip: string, count: int}>,
* available: bool,
* reason?: string
* }
*/
public function getStats(): array {
if ($this->usesRedisBruteforceBackend()) {
return [
'bruteforceAttempts24h' => 0,
'bruteforceAttempts1h' => 0,
'bruteforceTotal' => 0,
'topIps' => [],
'available' => false,
'reason' => 'redis_backend',
];
}

try {
$total = $this->countAttempts();
} catch (\Throwable) {
return [
'bruteforceAttempts24h' => 0,
'bruteforceAttempts1h' => 0,
'bruteforceTotal' => 0,
'topIps' => [],
'available' => false,
];
}

return [
'bruteforceAttempts24h' => $this->countAttempts(time() - 86400),
'bruteforceAttempts1h' => $this->countAttempts(time() - 3600),
'bruteforceTotal' => $total,
'topIps' => $this->topIps(),
'available' => true,
];
}

private function usesRedisBruteforceBackend(): bool {
if ($this->config->getSystemValueBool('auth.bruteforce.protection.force.database', false)) {
return false;
}
$distributed = ltrim($this->config->getSystemValueString('memcache.distributed', ''), '\\');
return $distributed === 'OC\Memcache\Redis';
}

private function countAttempts(?int $sinceTimestamp = null): int {
$qb = $this->db->getQueryBuilder();
$qb->select($qb->func()->count('id'))->from('bruteforce_attempts');
if ($sinceTimestamp !== null) {
$qb->where($qb->expr()->gte('occurred', $qb->createNamedParameter($sinceTimestamp)));
}
$result = $qb->executeQuery();
$count = (int)$result->fetchOne();
$result->closeCursor();
return $count;
}

/**
* @return list<array{ip: string, count: int}>
*/
private function topIps(int $limit = 5): array {
$qb = $this->db->getQueryBuilder();
$qb->select('ip')
->selectAlias($qb->func()->count('id'), 'count')
->from('bruteforce_attempts')
Comment thread
ChristophWurst marked this conversation as resolved.
->where($qb->expr()->gte('occurred', $qb->createNamedParameter(time() - 86400)))
->groupBy('ip')
->orderBy('count', 'DESC')
->setMaxResults($limit);
try {
$result = $qb->executeQuery();
} catch (\Throwable) {
return [];
}
$out = [];
while (($row = $result->fetch()) !== false) {
$out[] = [
'ip' => (string)($row['ip'] ?? ''),
'count' => (int)($row['count'] ?? 0),
];
}
$result->closeCursor();
return $out;
}
}
12 changes: 12 additions & 0 deletions lib/Settings/AdminSettings.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,14 @@

namespace OCA\ServerInfo\Settings;

use OCA\ServerInfo\ActiveConnections;
use OCA\ServerInfo\ActivityRate;
use OCA\ServerInfo\CronInfo;
use OCA\ServerInfo\DatabaseStatistics;
use OCA\ServerInfo\FpmStatistics;
use OCA\ServerInfo\JobQueueInfo;
use OCA\ServerInfo\LoginStats;
use OCA\ServerInfo\LogTailReader;
use OCA\ServerInfo\Os;
use OCA\ServerInfo\PhpStatistics;
use OCA\ServerInfo\SessionStatistics;
Expand Down Expand Up @@ -42,6 +46,10 @@ public function __construct(
private CronInfo $cronInfo,
private JobQueueInfo $jobQueueInfo,
private SlowestJobs $slowestJobs,
private LogTailReader $logTailReader,
private LoginStats $loginStats,
private ActivityRate $activityRate,
private ActiveConnections $activeConnections,
private IConfig $config,
) {
}
Expand Down Expand Up @@ -69,6 +77,10 @@ public function getForm(): TemplateResponse {
'cron' => $this->cronInfo->getCronInfo(),
'jobQueue' => $this->jobQueueInfo->getJobQueueInfo(),
'slowestJobs' => $this->slowestJobs->getSlowestJobs(),
'logTail' => $this->logTailReader->recentErrors(),
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@ChristophWurst

Could we not bind them in getFrom but introduce a new controller (maybe ocs)?

This has the advantage to not block the main operation with slow operations (like expensive database queries, opening log files, etc) and also give us the ability to let the dashboard refresh without a page load in the long run.

Also okay as follow up, I would just mention it.

'loginStats' => $this->loginStats->getStats(),
'activityRate' => $this->activityRate->getActivityRate(),
'activeConnections' => $this->activeConnections->getActiveConnections(),
'phpinfo' => $this->config->getAppValue('serverinfo', 'phpinfo', 'no') === 'yes',
'phpinfoUrl' => $this->urlGenerator->linkToRoute('serverinfo.page.phpinfo')
];
Expand Down
Loading
Loading