diff options
author | Simon Rettberg | 2017-10-06 18:00:59 +0200 |
---|---|---|
committer | Simon Rettberg | 2017-10-06 18:00:59 +0200 |
commit | 2933768b0dc7d58f0b0ba7f2efcbaf1806eb275b (patch) | |
tree | 487583c2145bd86cd98ced3705f46af455323267 /modules-available/dnbd3/inc/dnbd3util.inc.php | |
parent | Allow page modules with digits in name (diff) | |
download | slx-admin-2933768b0dc7d58f0b0ba7f2efcbaf1806eb275b.tar.gz slx-admin-2933768b0dc7d58f0b0ba7f2efcbaf1806eb275b.tar.xz slx-admin-2933768b0dc7d58f0b0ba7f2efcbaf1806eb275b.zip |
[dnbd3] New module for managing dnbd3 servers - WIP
Diffstat (limited to 'modules-available/dnbd3/inc/dnbd3util.inc.php')
-rw-r--r-- | modules-available/dnbd3/inc/dnbd3util.inc.php | 62 |
1 files changed, 62 insertions, 0 deletions
diff --git a/modules-available/dnbd3/inc/dnbd3util.inc.php b/modules-available/dnbd3/inc/dnbd3util.inc.php new file mode 100644 index 00000000..b04583b8 --- /dev/null +++ b/modules-available/dnbd3/inc/dnbd3util.inc.php @@ -0,0 +1,62 @@ +<?php + +class Dnbd3Util { + + public static function updateServerStatus() + { + $dynClients = RunMode::getForMode('dnbd3', 'proxy', false, true); + $servers = array(); + $res = Database::simpleQuery('SELECT s.serverid, s.machineuuid, s.fixedip, s.lastup, s.lastdown, m.clientip + FROM dnbd3_server s + LEFT JOIN machine m USING (machineuuid)'); + while ($row = $res->fetch(PDO::FETCH_ASSOC)) { + if (!is_null($row['clientip'])) { + $ip = $row['clientip']; + } elseif (!is_null($row['fixedip'])) { + $ip = $row['fixedip']; + } else { + continue; // Huh? + } + if (!is_null($row['machineuuid'])) { + unset($dynClients[$row['machineuuid']]); + } + $server = array( + 'serverid' => $row['serverid'], + 'addr' => $ip, + ); + $servers[] = $server; + } + // See if any clients are in dnbd3 proxy mode but don't have a matching row in the dnbd3_server table + foreach ($dynClients as $client) { + Database::exec('INSERT IGNORE INTO dnbd3_server (machineuuid) VALUES (:machineuuid)', + array('machineuuid' => $client['machineuuid'])); + // Missing from $servers now but we'll handle them in the next run, so don't bother + } + // Now query them all + $NOW = time(); + foreach ($servers as $server) { + $data = Dnbd3Rpc::query(true, false, false, $server['addr']); + if (!is_array($data) || !isset($data['runId'])) { + Database::exec('UPDATE dnbd3_server SET uptime = 0, clientcount = 0 WHERE serverid = :serverid', + array('serverid' => $server['serverid'])); + continue; + } + // Seems up - since we only get absolute rx/tx values from the server, we have to prevent update race conditions + // and make sure the server was not restarted in the meantime (use runid and uptime for this) + 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 + WHERE serverid = :serverid', array( + 'runid' => $data['runId'], + 'now' => $NOW, + 'uptime' => $data['uptime'], + 'up' => $data['bytesSent'], + 'down' => $data['bytesReceived'], + 'clientcount' => $data['clientCount'], + 'serverid' => $server['serverid'] + )); + } + } + +}
\ No newline at end of file |