Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

@Programie
Created August 15, 2017 20:38
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save Programie/26c7014f4b16a2fd8ea2fe71f9a25e2b to your computer and use it in GitHub Desktop.
Save Programie/26c7014f4b16a2fd8ea2fe71f9a25e2b to your computer and use it in GitHub Desktop.
Nextcloud update check for Check_MK
#! /usr/bin/env php
<?php
/**
* This is a simple local check for Check_MK which triggers a warning in case an update for your Nextcloud installation is available.
*
* The check does not request the update information from the Nextcloud update server, the cached data stored in the database is used instead.
*
* The check was tested with Nextcloud 12. As this script is not using some official API of Nextcloud, a future update of Nextcloud might break it.
*
* Installation:
* 1. Specify the location to your Nextcloud installation in the $baseDir$ variable
* 2. Save the script in the local checks folder of the Check_MK Agent (e.g. /usr/lib/check_mk_agent/local/check_nextcloud_update.php)
* 3. Make it executable (chmod +x /usr/lib/check_mk_agent/local/check_nextcloud_update.php)
*/
$baseDir = "/var/www/nextcloud";// Specify the path to your Nextcloud installation
function getNewVersion($baseDir)
{
require $baseDir . "/config/config.php";
$pdo = new PDO(sprintf("mysql:host=%s;dbname=%s", $CONFIG["dbhost"], $CONFIG["dbname"]), $CONFIG["dbuser"], $CONFIG["dbpassword"]);
$query = $pdo->query("
SELECT `configvalue`
FROM `oc_appconfig`
WHERE `appid` = 'core' AND `configkey` = 'lastupdateResult'
");
if (!$query->rowCount()) {
return null;
}
$json = json_decode($query->fetchObject()->configvalue, true);
if ($json === null) {
return null;
}
if (!is_array($json)) {
return null;
}
if (empty($json)) {
return true;
}
if (!isset($json["version"])) {
return null;
}
if (!is_string($json["version"])) {
return null;
}
return $json["version"];
}
function getCurrentVersion($baseDir)
{
require $baseDir . "/version.php";
return implode(".", $OC_Version);
}
function output($state, $message)
{
printf("%d Nextcloud_Version - %s\n", $state, $message);
}
$newVersion = getNewVersion($baseDir);
$currentVersion = getCurrentVersion($baseDir);
if ($newVersion === null) {
output(1, "Unable to read new version");
} elseif (!is_string($currentVersion)) {
output(1, "Unable to read current version");
} elseif ($newVersion === true or $newVersion === $currentVersion) {
output(0, sprintf("No update available (installed version: %s)", $currentVersion));
} else {
output(1, sprintf("An update to version %s is available (installed version: %s)", $newVersion, $currentVersion));
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment