From 017fb7924f3551004607aa74dbc0b3431d6c01a1 Mon Sep 17 00:00:00 2001 From: Simon Rettberg Date: Thu, 26 Oct 2017 16:58:14 +0200 Subject: [dnbd3] Improve server sorting, main-warning, errormsg, Taskmanager intergration --- modules-available/dnbd3/inc/dnbd3.inc.php | 10 +++++++ modules-available/dnbd3/inc/dnbd3rpc.inc.php | 17 ++++++++--- modules-available/dnbd3/inc/dnbd3util.inc.php | 41 +++++++++++++++++++++++---- 3 files changed, 58 insertions(+), 10 deletions(-) (limited to 'modules-available/dnbd3/inc') diff --git a/modules-available/dnbd3/inc/dnbd3.inc.php b/modules-available/dnbd3/inc/dnbd3.inc.php index 905bdc68..eb41c000 100644 --- a/modules-available/dnbd3/inc/dnbd3.inc.php +++ b/modules-available/dnbd3/inc/dnbd3.inc.php @@ -12,6 +12,16 @@ class Dnbd3 { public static function setEnabled($bool) { Property::set(self::PROP_ENABLED, $bool ? 1 : 0); + $task = Taskmanager::submit('Systemctl', array( + 'operation' => ($bool ? 'start' : 'stop'), + 'service' => 'dnbd3-server' + )); + return $task; + } + + public static function getLocalStatus() + { + } } \ No newline at end of file diff --git a/modules-available/dnbd3/inc/dnbd3rpc.inc.php b/modules-available/dnbd3/inc/dnbd3rpc.inc.php index 27713bfb..35d79a31 100644 --- a/modules-available/dnbd3/inc/dnbd3rpc.inc.php +++ b/modules-available/dnbd3/inc/dnbd3rpc.inc.php @@ -2,6 +2,10 @@ class Dnbd3Rpc { + const QUERY_UNREACHABLE = 1; + const QUERY_NOT_200 = 2; + const QUERY_NOT_JSON = 3; + /** * Query given DNBD3 server for status information. * @@ -11,7 +15,7 @@ class Dnbd3Rpc { * @param bool $clients include client list * @param bool $images include image list * @param bool $diskSpace include disk space stats - * @return false|array the queried data as an array, or false on error + * @return int|array the queried data as an array, or false on error */ public static function query($server, $port, $stats, $clients, $images, $diskSpace) { @@ -33,9 +37,14 @@ class Dnbd3Rpc { $url .= 'q=space&'; } $str = Download::asString($url, 3, $code); - if ($str === false || $code !== 200) - return false; - return json_decode($str, true); + if ($str === false) + return self::QUERY_UNREACHABLE; + if ($code !== 200) + return self::QUERY_NOT_200; + $ret = json_decode($str, true); + if (!is_array($ret)) + return self::QUERY_NOT_JSON; + return $ret; } } diff --git a/modules-available/dnbd3/inc/dnbd3util.inc.php b/modules-available/dnbd3/inc/dnbd3util.inc.php index 03499413..a9b9241e 100644 --- a/modules-available/dnbd3/inc/dnbd3util.inc.php +++ b/modules-available/dnbd3/inc/dnbd3util.inc.php @@ -49,10 +49,26 @@ class Dnbd3Util { // Now query them all $NOW = time(); foreach ($servers as $server) { - $data = Dnbd3Rpc::query($server['addr'], 5003, true, false, false, true); - if (!is_array($data) || !isset($data['runId'])) { - Database::exec('UPDATE dnbd3_server SET uptime = 0, clientcount = 0 WHERE serverid = :serverid', - array('serverid' => $server['serverid'])); + $port = 5003; + $data = Dnbd3Rpc::query($server['addr'], $port, true, false, false, true); + if ($data === Dnbd3Rpc::QUERY_UNREACHABLE) { + $error = 'No (HTTP) reply on port ' . $port; + } elseif ($data === Dnbd3Rpc::QUERY_NOT_200) { + $error = 'No HTTP 200 OK on port ' . $port; + } elseif ($data === Dnbd3Rpc::QUERY_NOT_JSON) { + $error = 'Reply to status query is not JSON'; + } elseif (!is_array($data) || !isset($data['runId'])) { + if (is_array($data) && isset($data['errorMsg'])) { + $error = 'DNBD3: ' . $data['errorMsg']; + } else { + $error = 'Reply to status query has unexpected format'; + } + } else { + $error = false; + } + if ($error !== false) { + Database::exec('UPDATE dnbd3_server SET uptime = 0, clientcount = 0, errormsg = :errormsg WHERE serverid = :serverid', + array('serverid' => $server['serverid'], 'errormsg' => $error)); continue; } // Seems up - since we only get absolute rx/tx values from the server, we have to prevent update race conditions @@ -60,7 +76,7 @@ class Dnbd3Util { Database::exec('UPDATE dnbd3_server SET runid = :runid, lastseen = :now, uptime = :uptime, totalup = totalup + If(runid = :runid AND uptime <= :uptime, If(lastup < :up, :up - lastup, 0), If(:uptime < 1800, :up, 0)), totaldown = totaldown + If(runid = :runid AND uptime <= :uptime, If(lastdown < :down, :down - lastdown, 0), If(:uptime < 1800, :up, 0)), - lastup = :up, lastdown = :down, clientcount = :clientcount, disktotal = :disktotal, diskfree = :diskfree + lastup = :up, lastdown = :down, clientcount = :clientcount, disktotal = :disktotal, diskfree = :diskfree, errormsg = NULL WHERE serverid = :serverid', array( 'runid' => $data['runId'], 'now' => $NOW, @@ -93,7 +109,7 @@ class Dnbd3Util { while ($row = $res->fetch(PDO::FETCH_ASSOC)) { $assignedLocs[] = $row['locationid']; } - $modeData = (array)json_decode($modeData, true); + $modeData = (array)json_decode($modeData, true) + self::defaultRunmodeConfig(); if (!empty($assignedLocs) && isset($modeData['firewall']) && $modeData['firewall']) { // Get all sub-locations too $recursiveLocs = $assignedLocs; @@ -122,6 +138,8 @@ class Dnbd3Util { WHERE s.machineuuid <> :uuid OR s.machineuuid IS NULL', array('uuid' => $machineUuid)); $public = array(); $private = array(); + $self = Property::getServerIp(); + $public[$self] = $self; while ($row = $res->fetch(PDO::FETCH_ASSOC)) { $ip = $row['clientip'] ? $row['clientip'] : $row['fixedip']; if ($ip === '') { @@ -132,13 +150,16 @@ class Dnbd3Util { $public[$ip] = $ip; } } else { + unset($public[$ip]); $private[$ip] = $ip; } } if (!empty($public)) { + shuffle($public); ConfigHolder::add('SLX_DNBD3_PUBLIC', implode(' ', $public)); } if (!empty($private)) { + shuffle($private); ConfigHolder::add('SLX_DNBD3_PRIVATE', implode(' ', $private)); } if (isset($modeData['bgr']) && $modeData['bgr']) { @@ -170,4 +191,12 @@ class Dnbd3Util { return long2ip($start) . '/' . $mask; } + public static function defaultRunmodeConfig() + { + return array( + 'bgr' => true, + 'firewall' => false + ); + } + } -- cgit v1.2.3-55-g7522