Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[stable26] [stable27] fix: add flag to skip external requests #521

Merged
merged 3 commits into from
Nov 7, 2023
Merged
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
10 changes: 6 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,14 +15,16 @@ directory **nextcloud/apps/serverinfo**

## API

The API provides a lot of information information about a running Nextcloud
instance in XML or JSON format, by using the following URL. If you want to
get the information returned in JSON format, you have to append **`?format=json`**
to the URL.
The API provides a lot of information about a running Nextcloud
instance in XML or JSON format by using the following URL.

```
https://<nextcloud-fqdn>/ocs/v2.php/apps/serverinfo/api/v1/info
```

- To request the information in JSON append the url parameter `format=json`
- Use the url parameter `skipApps=true` to omit app updates (including available app updates will send an external request to the app store).

### Example XML output:
```
<ocs>
Expand Down
11 changes: 11 additions & 0 deletions css/style.css
Original file line number Diff line number Diff line change
Expand Up @@ -244,3 +244,14 @@
}
}
*/

.monitoring-url-params {
margin-top: 3px;
margin-bottom: 3px;
}

.monitoring-url-param {
display: flex;
align-items: center;
height: 24px;
}
27 changes: 26 additions & 1 deletion js/script.js
Original file line number Diff line number Diff line change
Expand Up @@ -225,7 +225,7 @@
function initMonitoringLinkToClipboard() {
var clipboard = new Clipboard('.clipboardButton');
clipboard.on('success', function (e) {
OC.Notification.show('Copied!', { type: 'success' })
OC.Notification.show(t('serverinfo', 'Copied!'), { type: 'success' })
});
clipboard.on('error', function () {
var actionMsg = '';
Expand Down Expand Up @@ -310,3 +310,28 @@
}

})(jQuery, OC);

function updateMonitoringUrl(event) {
const $endpointUrl = document.getElementById('monitoring-endpoint-url');
const $params = document.querySelectorAll('.update-monitoring-endpoint-url');

const url = new URL($endpointUrl.value)
url.searchParams.delete('format')
url.searchParams.delete('skipApps')

for (const $param of $params) {
if ($param.name === 'format_json' && $param.checked) {
url.searchParams.set('format', 'json')
}
if ($param.name === 'skip_apps' && $param.checked) {
url.searchParams.set('skipApps', 'true')
}
}

$endpointUrl.value = url.toString()
}

document.addEventListener('DOMContentLoaded', function (event) {
const $params = document.querySelectorAll('.update-monitoring-endpoint-url');
$params.forEach($param => $param.addEventListener('change', updateMonitoringUrl));
});
4 changes: 2 additions & 2 deletions lib/Controller/ApiController.php
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,7 @@ private function checkAuthorized(): bool {
* @PublicPage
* @BruteForceProtection(action=serverinfo)
*/
public function info(): DataResponse {
public function info(bool $skipApps = false): DataResponse {
if (!$this->checkAuthorized()) {
$response = new DataResponse(['message' => 'Unauthorized']);
$response->throttle();
Expand All @@ -122,7 +122,7 @@ public function info(): DataResponse {
}
return new DataResponse([
'nextcloud' => [
'system' => $this->systemStatistics->getSystemStatistics(),
'system' => $this->systemStatistics->getSystemStatistics($skipApps),
'storage' => $this->storageStatistics->getStorageStatistics(),
'shares' => $this->shareStatistics->getShareStatistics()
],
Expand Down
2 changes: 1 addition & 1 deletion lib/Controller/PageController.php
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ public function __construct(string $appName,
*/
public function update(): JSONResponse {
$data = [
'system' => $this->systemStatistics->getSystemStatistics()
'system' => $this->systemStatistics->getSystemStatistics(true)
];

return new JSONResponse($data);
Expand Down
2 changes: 1 addition & 1 deletion lib/Settings/AdminSettings.php
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ public function getForm(): TemplateResponse {
'php' => $this->phpStatistics->getPhpStatistics(),
'database' => $this->databaseStatistics->getDatabaseStatistics(),
'activeUsers' => $this->sessionStatistics->getSessionStatistics(),
'system' => $this->systemStatistics->getSystemStatistics(),
'system' => $this->systemStatistics->getSystemStatistics(true),
'thermalzones' => $this->os->getThermalZones()
];

Expand Down
12 changes: 9 additions & 3 deletions lib/SystemStatistics.php
Original file line number Diff line number Diff line change
Expand Up @@ -51,10 +51,11 @@ public function __construct(IConfig $config, IAppManager $appManager, Installer
*
* @throws \OCP\Files\InvalidPathException
*/
public function getSystemStatistics(): array {
public function getSystemStatistics(bool $skipApps = false): array {
$processorUsage = $this->getProcessorUsage();
$memoryUsage = $this->os->getMemory();
return [

$data = [
'version' => $this->config->getSystemValue('version'),
'theme' => $this->config->getSystemValue('theme', 'none'),
'enable_avatars' => $this->config->getSystemValue('enable_avatars', true) ? 'yes' : 'no',
Expand All @@ -70,8 +71,13 @@ public function getSystemStatistics(): array {
'mem_free' => $memoryUsage->getMemAvailable() * 1024,
'swap_total' => $memoryUsage->getSwapTotal() * 1024,
'swap_free' => $memoryUsage->getSwapFree() * 1024,
'apps' => $this->getAppsInfo()
];

if (!$skipApps) {
$data['apps'] = $this->getAppsInfo();
}

return $data;
}

/**
Expand Down
21 changes: 13 additions & 8 deletions templates/settings-admin.php
Original file line number Diff line number Diff line change
Expand Up @@ -165,10 +165,6 @@ function FormatMegabytes(int $byte): string {
<?php endforeach; ?>
</div>

<div class="smallinfo">
<?php p($l->t('You will get a notification once one of your disks is nearly full.')); ?>
</div>

<p><?php p($l->t('Files:')); ?> <strong id="numFilesStorage"><?php p($_['storage']['num_files']); ?></strong></p>
<p><?php p($l->t('Storages:')); ?> <strong id="numFilesStorages"><?php p($_['storage']['num_storages']); ?></strong></p>
<?php if ($_['system']['freespace'] !== null): ?>
Expand Down Expand Up @@ -398,15 +394,24 @@ function FormatMegabytes(int $byte): string {
<!-- OCS ENDPOINT -->
<h2><?php p($l->t('External monitoring tool')); ?></h2>
<p>
<?php p($l->t('You can connect an external monitoring tool by using this end point:')); ?>
<?php p($l->t('Use this end point to connect an external monitoring tool:')); ?>
</p>
<div class="monitoring-wrapper">
<input type="text" readonly="readonly" id="monitoring-endpoint-url" value="<?php echo p($_['ocs']); ?>"/>
<a class="clipboardButton icon icon-clippy" title="<?php p($l->t('Copy')); ?>" aria-label="<?php p($l->t('Copy')); ?>" data-clipboard-target="#monitoring-endpoint-url"></a>
</div>
<p class="settings-hint">
<?php p($l->t('Appending "?format=json" at the end of the URL gives you the result in JSON.')); ?>
</p>

<div class="monitoring-url-params">
<div class="monitoring-url-param">
<input type="checkbox" class="update-monitoring-endpoint-url" name="format_json" id="format_json">
<label for="format_json"><?php p($l->t('Output in JSON')) ?></label>
</div>
<div class="monitoring-url-param">
<input type="checkbox" class="update-monitoring-endpoint-url" name="skip_apps" id="skip_apps">
<label for="skip_apps"><?php p($l->t('Skip app updates (including app updates will send an external request to the app store)')) ?></label>
</div>
</div>

<p>
<?php p($l->t('To use an access token, please generate one then set it using the following command:')); ?>
<div><i>occ config:app:set serverinfo token --value yourtoken</i></div>
Expand Down
Loading