Skip to content

Commit

Permalink
fix: add flag to skip external requests
Browse files Browse the repository at this point in the history
Signed-off-by: Daniel Kesselberg <[email protected]>
  • Loading branch information
kesselb committed Nov 2, 2023
1 parent 85f91ec commit f8d7102
Show file tree
Hide file tree
Showing 8 changed files with 81 additions and 16 deletions.
11 changes: 7 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,14 +15,17 @@ 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 `skipUpdate=true` to omit server updates.
- 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;
}
29 changes: 29 additions & 0 deletions js/script.js
Original file line number Diff line number Diff line change
Expand Up @@ -310,3 +310,32 @@
}

})(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('skipUpdate')
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_update' && $param.checked) {
url.searchParams.set('skipUpdate', 'true')
}
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 $skipUpdate = false, 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($skipUpdate, $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 @@ -47,7 +47,7 @@ public function __construct(string $appName,
*/
public function update(): JSONResponse {
$data = [
'system' => $this->systemStatistics->getSystemStatistics()
'system' => $this->systemStatistics->getSystemStatistics(true, 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 @@ -87,7 +87,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, true),
'thermalzones' => $this->os->getThermalZones(),
'phpinfo' => $this->config->getAppValue('serverinfo', 'phpinfo', 'no') === 'yes',
'phpinfoUrl' => $this->urlGenerator->linkToRoute('serverinfo.page.phpinfo')
Expand Down
17 changes: 13 additions & 4 deletions lib/SystemStatistics.php
Original file line number Diff line number Diff line change
Expand Up @@ -51,12 +51,12 @@ public function __construct(IConfig $config, IAppManager $appManager, Installer
*
* @throws \OCP\Files\InvalidPathException
*/
public function getSystemStatistics(): array {
public function getSystemStatistics(bool $skipUpdate = false, bool $skipApps = false): array {
$processorUsage = $this->getProcessorUsage();
$memoryUsage = $this->os->getMemory();
return [

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

if (!$skipUpdate) {
$data['update'] = $this->getServerUpdateInfo();
}

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

return $data;
}

/**
Expand Down
21 changes: 17 additions & 4 deletions templates/settings-admin.php
Original file line number Diff line number Diff line change
Expand Up @@ -412,15 +412,28 @@ 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_update" id="skip_update">
<label for="skip_update"><?php p($l->t('Skip server update')) ?></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

0 comments on commit f8d7102

Please sign in to comment.