From 5c6c3365d28144516f0ade183189b256d8de03bc Mon Sep 17 00:00:00 2001 From: pk910 Date: Thu, 28 Jul 2011 03:36:03 +0200 Subject: [PATCH] added RRD Stats Module --- Bots/Stats.class.php | 130 +++++++++++++++++++++++++++++++++++ Uplink/P10_Channel.class.php | 19 ++++- Uplink/P10_Server.class.php | 12 ++++ Uplink/P10_User.class.php | 17 +++++ Uplink/Uplink.class.php | 17 +++++ 5 files changed, 194 insertions(+), 1 deletion(-) create mode 100644 Bots/Stats.class.php diff --git a/Bots/Stats.class.php b/Bots/Stats.class.php new file mode 100644 index 0000000..6670485 --- /dev/null +++ b/Bots/Stats.class.php @@ -0,0 +1,130 @@ +uplink = $uplink; + if(!file_exists("db/network.rrd")) { + $fname = "db/network.rrd"; + $opts = array( + "--step", "300", "--start", "0", + "DS:users:GAUGE:600:0:U", + "DS:channels:GAUGE:600:0:U", + "DS:opers:GAUGE:600:0:U", + "DS:servers:GAUGE:600:0:U", + "RRA:AVERAGE:0.5:1:600", + "RRA:AVERAGE:0.5:6:700", + "RRA:AVERAGE:0.5:24:775", + "RRA:AVERAGE:0.5:288:797", + "RRA:MAX:0.5:1:600", + "RRA:MAX:0.5:6:700", + "RRA:MAX:0.5:24:775", + "RRA:MAX:0.5:288:797" + ); + $ret = rrd_create($fname, $opts, count($opts)); + if(!$ret) { + $err = rrd_error(); + echo "Create error: $err\n"; + die(); + } + } + if(!file_exists("db/network-away.rrd")) { + $fname = "db/network-away.rrd"; + $opts = array( + "--step", "300", "--start", "0", + "DS:useraway:GAUGE:600:0:U", + "DS:userhere:GAUGE:600:0:U", + "RRA:AVERAGE:0.5:1:600", + "RRA:AVERAGE:0.5:6:700", + "RRA:AVERAGE:0.5:24:775", + "RRA:AVERAGE:0.5:288:797", + "RRA:MAX:0.5:1:600", + "RRA:MAX:0.5:6:700", + "RRA:MAX:0.5:24:775", + "RRA:MAX:0.5:288:797" + ); + $ret = rrd_create($fname, $opts, count($opts)); + if(!$ret) { + $err = rrd_error(); + echo "Create error: $err\n"; + die(); + } + } + $this->timer = timer(5,array(&$this,"create_stats"),array()); + } + + public function unload($rehash = false) { + kill_timer($this->timer); + } + + function create_stats() { + $this->timer = timer(5*60,array(&$this,"create_stats"),array()); + $stats = array( + "opers" => 0, + "total" => 0, + "away" => 0, + "here" => 0 + ); + foreach(P10_User::getAllUsers() as $num => $user) { + $stats['total']++; + if($user->isAway()) $stats['away']++; + else $stats['here']++; + if($user->getModes()->hasMode('o')) $stats['opers']++; + } + $channels = P10_Channel::getChannelCount(); + $servers = P10_Server::getServerCount(); + rrd_update("db/network.rrd", time().":".$stats['total'].":".$channels.":".$stats['opers'].":".$servers); + rrd_update("db/network-away.rrd", time().":".$stats['away'].":".$stats['here']); + foreach(uplink::$uplink->servers as $snum => $server) { + $count = $server->getUserCount(); + $fname="db/".$server->getName().".rrd"; + if(!file_exists($fname)) { + $opts = array( + "--step", "300", "--start", "0", + "DS:users:GAUGE:600:0:U", + "RRA:AVERAGE:0.5:1:600", + "RRA:AVERAGE:0.5:6:700", + "RRA:AVERAGE:0.5:24:775", + "RRA:AVERAGE:0.5:288:797", + "RRA:MAX:0.5:1:600", + "RRA:MAX:0.5:6:700", + "RRA:MAX:0.5:24:775", + "RRA:MAX:0.5:288:797" + ); + $ret = rrd_create($fname, $opts, count($opts)); + } + rrd_update($fname, time().":".$count); + } + } + +} + +?> \ No newline at end of file diff --git a/Uplink/P10_Channel.class.php b/Uplink/P10_Channel.class.php index a155481..bc8c152 100644 --- a/Uplink/P10_Channel.class.php +++ b/Uplink/P10_Channel.class.php @@ -48,8 +48,17 @@ class P10_Channel { public static function getChannels() { return self::$static_channels; - } + } + public static function getChannelCount() { + return count(self::$static_channels); + } + + public static function recheckAllChannels() { + foreach(self::$static_channels as $channel) { + $channel->checkChannel(); + } + } private $name; private $topic; @@ -103,11 +112,18 @@ class P10_Channel { $user->addChannel($this); } + private function checkChannel() { + if(count($this->users) == 0 && !$this->modes->hasMode('z')) { + unset(self::$static_channels[strtolower($name)]); //drop empty channel + } + } + public function quitUser($user) { if(array_key_exists($user->getNumeric(), $this->users)) { unset($this->users[$user->getNumeric()]); unset($this->userPrivs[$user->getNumeric()]); //$user->delChannel($this) is not necessary because the user quits (the whole Object gets removed later) + $this->checkChannel(); } else { trigger_error("Tried to quit a User from a Channel it is not joined.", E_USER_WARNING); } @@ -118,6 +134,7 @@ class P10_Channel { unset($this->users[$user->getNumeric()]); unset($this->userPrivs[$user->getNumeric()]); $user->delChannel($this); + $this->checkChannel(); } else { trigger_error("Tried to part a User from a Channel it is not joined.", E_USER_WARNING); } diff --git a/Uplink/P10_Server.class.php b/Uplink/P10_Server.class.php index 465fa3c..ba9200e 100644 --- a/Uplink/P10_Server.class.php +++ b/Uplink/P10_Server.class.php @@ -87,6 +87,14 @@ class P10_Server { return NULL; } + public static function getServerCount() { + return count(self::$static_servers); + } + + public static function getServers() { + return self::$static_servers; + } + private $name; private $numeric; @@ -177,6 +185,10 @@ class P10_Server { public function getUsers() { return $this->users; } + + public function getUserCount() { + return count($this->users); + } } ?> \ No newline at end of file diff --git a/Uplink/P10_User.class.php b/Uplink/P10_User.class.php index cd3a485..8d4e5a3 100644 --- a/Uplink/P10_User.class.php +++ b/Uplink/P10_User.class.php @@ -55,6 +55,10 @@ class P10_User { return NULL; } + public static function getAllUsers() { + return self::$static_users; + } + private $numeric; private $server; @@ -66,6 +70,7 @@ class P10_User { private $modes; private $realname; private $channels; + private $away = null; public function __construct($nick, $numeric, $server, $connect_time, $ident, $host, $ip, $realname, $modes) { $this->nick = $nick; @@ -125,6 +130,18 @@ class P10_User { return $this->realname; } + public function setAway($away) { + $this->away = $away; + } + + public function getAway() { + return $this->away; + } + + public function isAway() { + return ($this->away != null); + } + public function quit($reason) { $this->server->delUser($this); foreach($this->channels as $channel) { diff --git a/Uplink/Uplink.class.php b/Uplink/Uplink.class.php index 14585f6..ba8b8ca 100644 --- a/Uplink/Uplink.class.php +++ b/Uplink/Uplink.class.php @@ -276,6 +276,9 @@ class Uplink { case "W": $this->recv_whois($from, $arguments); break; + case "A": + $this->recv_away($from, $arguments); + break; //default default: //unknown cmd @@ -356,6 +359,7 @@ class Uplink { private function recv_ping($from, $args) { $this->send("Z", $args[0]); //simply PONG + P10_Channel::recheckAllChannels(); } private function recv_nick($from, $args) { @@ -654,6 +658,19 @@ class Uplink { $this->send("318", $from, $args[1]); } + private function recv_away($from, $args) { + $user = P10_User::getUserByNum($from); + if($user == null) { + trigger_error("Server tries to send an away command from an user that does not exist or was not found on recv_away.", E_USER_ERROR); + return; + } + if(count($args) > 0) { + $user->setAway($args[0]); + } else { + $user->setAway(null); + } + } + /******************************************************************************************** * SERVER FUNCTIONS * ********************************************************************************************/ -- 2.20.1