store network traffic statistics in database

This commit is contained in:
Max-Wilhelm Bruker 2011-12-04 14:24:36 +01:00
parent 1455c093cc
commit eb6520a737
4 changed files with 37 additions and 4 deletions

View file

@ -119,9 +119,11 @@ CREATE TABLE IF NOT EXISTS `cockatrice_users` (
CREATE TABLE `cockatrice_uptime` ( CREATE TABLE `cockatrice_uptime` (
`id_server` tinyint(3) NOT NULL, `id_server` tinyint(3) NOT NULL,
`timest` datetime NOT NULL DEFAULT '0000-00-00 00:00:00', `timest` datetime NOT NULL DEFAULT '0000-00-00 00:00:00',
`uptime` int(11) DEFAULT NULL, `uptime` int(11) NOT NULL,
`users_count` int(11) DEFAULT NULL, `users_count` int(11) NOT NULL,
`games_count` int(11) DEFAULT NULL, `games_count` int(11) NOT NULL,
`rx_bytes` int(11) NOT NULL,
`tx_bytes` int(11) NOT NULL,
PRIMARY KEY (`timest`) PRIMARY KEY (`timest`)
) ENGINE=MyISAM DEFAULT CHARSET=utf8; ) ENGINE=MyISAM DEFAULT CHARSET=utf8;

View file

@ -398,15 +398,26 @@ void Servatrice::statusUpdate()
uptime += statusUpdateClock->interval() / 1000; uptime += statusUpdateClock->interval() / 1000;
txBytesMutex.lock();
quint64 tx = txBytes;
txBytes = 0;
txBytesMutex.unlock();
rxBytesMutex.lock();
quint64 rx = rxBytes;
rxBytes = 0;
rxBytesMutex.unlock();
QMutexLocker locker(&dbMutex); QMutexLocker locker(&dbMutex);
checkSql(); checkSql();
QSqlQuery query; QSqlQuery query;
query.prepare("insert into " + dbPrefix + "_uptime (id_server, timest, uptime, users_count, games_count) values(:id, NOW(), :uptime, :users_count, :games_count)"); query.prepare("insert into " + dbPrefix + "_uptime (id_server, timest, uptime, users_count, games_count, tx_bytes, rx_bytes) values(:id, NOW(), :uptime, :users_count, :games_count, :tx, :rx)");
query.bindValue(":id", serverId); query.bindValue(":id", serverId);
query.bindValue(":uptime", uptime); query.bindValue(":uptime", uptime);
query.bindValue(":users_count", uc); query.bindValue(":users_count", uc);
query.bindValue(":games_count", gc); query.bindValue(":games_count", gc);
query.bindValue(":tx", tx);
query.bindValue(":rx", rx);
execSqlQuery(query); execSqlQuery(query);
} }
@ -424,6 +435,20 @@ void Servatrice::scheduleShutdown(const QString &reason, int minutes)
shutdownTimeout(); shutdownTimeout();
} }
void Servatrice::incTxBytes(quint64 num)
{
txBytesMutex.lock();
txBytes += num;
txBytesMutex.unlock();
}
void Servatrice::incRxBytes(quint64 num)
{
rxBytesMutex.lock();
rxBytes += num;
rxBytesMutex.unlock();
}
void Servatrice::shutdownTimeout() void Servatrice::shutdownTimeout()
{ {
QMutexLocker locker(&serverMutex); QMutexLocker locker(&serverMutex);

View file

@ -75,6 +75,8 @@ public:
QMap<QString, ServerInfo_User *> getBuddyList(const QString &name); QMap<QString, ServerInfo_User *> getBuddyList(const QString &name);
QMap<QString, ServerInfo_User *> getIgnoreList(const QString &name); QMap<QString, ServerInfo_User *> getIgnoreList(const QString &name);
void scheduleShutdown(const QString &reason, int minutes); void scheduleShutdown(const QString &reason, int minutes);
void incTxBytes(quint64 num);
void incRxBytes(quint64 num);
protected: protected:
int startSession(const QString &userName, const QString &address); int startSession(const QString &userName, const QString &address);
void endSession(int sessionId); void endSession(int sessionId);
@ -89,6 +91,8 @@ private:
int serverId; int serverId;
bool threaded; bool threaded;
int uptime; int uptime;
QMutex txBytesMutex, rxBytesMutex;
quint64 txBytes, rxBytes;
int maxGameInactivityTime, maxPlayerInactivityTime; int maxGameInactivityTime, maxPlayerInactivityTime;
int maxUsersPerAddress, messageCountingInterval, maxMessageCountPerInterval, maxMessageSizePerInterval, maxGamesPerUser; int maxUsersPerAddress, messageCountingInterval, maxMessageCountPerInterval, maxMessageSizePerInterval, maxGamesPerUser;
ServerInfo_User *evalUserQueryResult(const QSqlQuery &query, bool complete); ServerInfo_User *evalUserQueryResult(const QSqlQuery &query, bool complete);

View file

@ -86,6 +86,7 @@ void ServerSocketInterface::flushXmlBuffer()
QMutexLocker locker(&xmlBufferMutex); QMutexLocker locker(&xmlBufferMutex);
if (xmlBuffer.isEmpty()) if (xmlBuffer.isEmpty())
return; return;
servatrice->incTxBytes(xmlBuffer.size());
socket->write(xmlBuffer.toUtf8()); socket->write(xmlBuffer.toUtf8());
socket->flush(); socket->flush();
xmlBuffer.clear(); xmlBuffer.clear();
@ -94,6 +95,7 @@ void ServerSocketInterface::flushXmlBuffer()
void ServerSocketInterface::readClient() void ServerSocketInterface::readClient()
{ {
QByteArray data = socket->readAll(); QByteArray data = socket->readAll();
servatrice->incRxBytes(data.size());
if (!data.contains("<cmd type=\"ping\"")) if (!data.contains("<cmd type=\"ping\""))
logger->logMessage(QString(data), this); logger->logMessage(QString(data), this);
xmlReader->addData(data); xmlReader->addData(data);