How to use serverinfo from command line (curl and PowerShell)

German below…

if you are wondering how exactly the external monitoring tool works to monitor the Nextcloud, then see the following step by step guide:

Generate a random password:

user@server:~$ openssl rand -hex 32
f65578a77feb8ba2ce584f15f50718fa2a4a94465748fb433a4f1b475a11acc8

Set token for serverinfo:

user@server:/var/www/html$ ./occ config:app:set serverinfo token --value f65578a77feb8ba2ce584f15f50718fa2a4a94465748fb433a4f1b475a11acc8
Config value token for app serverinfo set to f65578a77feb8ba2ce584f15f50718fa2a4a94465748fb433a4f1b475a11acc8

Query with curl:

user@client:~$ curl -H 'NC-Token: f65578a77feb8ba2ce584f15f50718fa2a4a94465748fb433a4f1b475a11acc8' -X GET https://your.nextcloud.com/ocs/v2.php/apps/serverinfo/api/v1/info
Output
<?xml version="1.0"?>
<ocs>
 <meta>
  <status>ok</status>
  <statuscode>200</statuscode>
  <message>OK</message>
 </meta>
 <data>
  <nextcloud>
   <system>
[...]

If you want to delete the token:

user@server:/var/www/html$ ./occ config:app:delete serverinfo token
Config value token of app serverinfo deleted


DEUTSCH:

Wenn du dich wunderst, wie genau man das mit dem NC-Token im Externen Überwachungsprogramm macht um die Nextcloud überwachen zu können, dann folgt hier eine kurze Anleitung:

Zufälliges Passwort generieren:

user@server:~$ openssl rand -hex 32
f65578a77feb8ba2ce584f15f50718fa2a4a94465748fb433a4f1b475a11acc8

Token für Serverinfo festlegen:

user@server:/var/www/html$ ./occ config:app:set serverinfo token --value f65578a77feb8ba2ce584f15f50718fa2a4a94465748fb433a4f1b475a11acc8
Config value token for app serverinfo set to f65578a77feb8ba2ce584f15f50718fa2a4a94465748fb433a4f1b475a11acc8

Abfrage mit curl:

user@client:~$ curl -H 'NC-Token: f65578a77feb8ba2ce584f15f50718fa2a4a94465748fb433a4f1b475a11acc8' -X GET https://your.nextcloud.com/ocs/v2.php/apps/serverinfo/api/v1/info
Output
<?xml version="1.0"?>
<ocs>
 <meta>
  <status>ok</status>
  <statuscode>200</statuscode>
  <message>OK</message>
 </meta>
 <data>
  <nextcloud>
   <system>
[...]

Falls du den Token wieder zu löschen möchtest:

user@server:/var/www/html$ ./occ config:app:delete serverinfo token
Config value token of app serverinfo deleted


To let you search for any of those lines of the serverinfo app:

Summary

External monitoring tool

You can connect an external monitoring tool by using this end point:
https://your.nextcloud.com/ocs/v2.php/apps/serverinfo/api/v1/info

Appending “?format=json” at the end of the URL gives you the result in JSON.

To use an access token, please generate one then set it using the following command:

occ config:app:set serverinfo token --value yourtoken

Then pass the token with the “NC-Token” header when querying the above URL.

Damit dieser Beitrag gefunden werden kann:

Zusammenfassung

Externes Überwachungsprogramm

Durch Verwendung des folgenden Zugangspunktes kann ein externes Überwachungsprogramm verwendet werden:
https://your.nextcloud.com/ocs/v2.php/apps/serverinfo/api/v1/info

Wenn du “?format=json” am Ende der URL anhängst, erhältst du das Ergebnis in JSON.

Um ein Zugriffstoken zu verwenden, generiere bitte ein Token und lege es mit dem folgenden Befehl fest:

occ config:app:set serverinfo token --value yourtoken

Übergib dann das Token mit dem “NC-Token”-Header bei der Abfrage der obigen URL.

3 Likes

I have made this little PowerShell script for a quick and dirty initial status and version control of my Nextcloud setup. All it requires is an App password on a user which can access the System settings hence have rights to access the serverinfo API.

$Response = Invoke-WebRequest -Uri “https://your.domain.tld/ocs/v2.php/apps/serverinfo/api/v1/info?format=json” -Headers @{Authorization = (“Basic " + [System.Convert]::ToBase64String([System.Text.Encoding]::ASCII.GetBytes(”$(‘USERNAME’):$(‘APP PASSWORD’)")))} | ConvertFrom-Json; Write-Host “==== NextCloud Production Status ====”; Write-Host “Status: “$Response.ocs.meta.statuscode “(”$Response.ocs.meta.status”)”; Write-Host "Version: "$Response.ocs.data.nextcloud.system.version; Write-Host "PHP: "$Response.ocs.data.server.php.version

Use it as is from a powershell coonsole, or modify it however you like to output the data for convenient use by better graphical tools.

2 Likes

I came to this topic I want to improve it the PowerShell code a little:

  • use serverinfo token occ config:system:get serverinfo token
    (create the token if not set till now: occ config:app:set serverinfo token --value y0urToken)

access with token is easier to consume and feels more secure as you don’t need to pass user credentials using basic auth…

$Response = Invoke-WebRequest -Uri "https://cloud.mydomain.tld/ocs/v2.php/apps/serverinfo/api/v1/info?format=json" -Headers @{'NC-Token'='y0urToken'}| ConvertFrom-Json;

$REsponse will hold an object with many interesting artefacts - see full list with

$response.ocs.meta|fl *
$response.ocs.data.nextcloud.system|fl *
$response.ocs.data.nextcloud.storage|fl *
$response.ocs.data.nextcloud.shares|fl *
$response.ocs.data.activeusers|fl *
$response.ocs.data.server.webserver|fl *
$response.ocs.data.server.php|fl *
$response.ocs.data.server.php.apcu.cache|fl *
$response.ocs.data.server.php.extensions -join ','|fl *

(and yes you need the fl * everywhere if run the commands in one step). Later you can craft the interesting output like kerasit explained:

Write-Host "==== NextCloud Production Status ====";
Write-Host "Status: "$Response.ocs.meta.statuscode "("$Response.ocs.meta.status")";
Write-Host "Version: "$Response.ocs.data.nextcloud.system.version;
Write-Host "PHP: "$Response.ocs.data.server.php.version

or maybe more PowerShell way with an object:

[PSCustomObject]@{
    Status = $Response.ocs.meta.status
    Version = $Response.ocs.data.nextcloud.system.version
    PHP = $Response.ocs.data.server.php.version
    WebServer = $response.ocs.data.server.webserver
    FilesCOunt = $response.ocs.data.nextcloud.storage.num_files
    db = $response.ocs.data.server.database.type
}
2 Likes

Whatever works for you. :slight_smile:

Anything I add to this Community, you are more than welcome to enhance, change and tweak. Use anything I contribute with as inspiration at best.

I really no longer uses that powershell script anyway. I stumbled upon the NextStats mobile app to monitor the health of both of the instances I operates. If I had a bigger setup - and for some who operates at a larger scale - this might still be relevan for myself.

High praise for the NC developers for making this possible in the first place.

2 Likes