33e8e5fc619acdc51100d5bc247eeac8ea2067a5
[PHP-P10.git] / Bots / Stats.class.php
1 <?php
2 /********************************* PHP-P10 ******************************
3  *    P10 uplink class by pk910   (c)2011 pk910                         *
4  ************************************************************************
5  *                          Version 2 (OOP)                             *
6  *                                                                      *
7  * PHP-P10 is free software; you can redistribute it and/or modify      *
8  * it under the terms of the GNU General Public License as published by *
9  * the Free Software Foundation; either version 2 of the License, or    *
10  * (at your option) any later version.                                  *
11  *                                                                      *
12  * This program is distributed in the hope that it will be useful,      *
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of       *
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the        *
15  * GNU General Public License for more details.                         *
16  *                                                                      *
17  * You should have received a copy of the GNU General Public License    *
18  * along with PHP-P10; if not, write to the Free Software Foundation,   *
19  * Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA.       *
20  *                                                                      *
21  ************************************************************************
22  * 
23  *  Bots/Stats.class.php
24  *
25  * RRD Stats module...
26  *
27  */
28
29 class {$_NAME} extends Bot {
30         private $uplink;
31         private $timer;
32         
33         public function load($uplink, $old = false) {
34                 $this->uplink = $uplink;
35                 if(!file_exists("db/network.rrd")) {
36                         $fname = "db/network.rrd";
37                         $opts = array(
38                                 "--step", "300", "--start", "0",
39                                 "DS:users:GAUGE:600:0:U",
40                                 "DS:channels:GAUGE:600:0:U",
41                                 "DS:opers:GAUGE:600:0:U",
42                                 "DS:servers:GAUGE:600:0:U",
43                                 "RRA:AVERAGE:0.5:1:600",
44                                 "RRA:AVERAGE:0.5:6:700",
45                                 "RRA:AVERAGE:0.5:24:775",
46                                 "RRA:AVERAGE:0.5:288:797",
47                                 "RRA:MAX:0.5:1:600",
48                                 "RRA:MAX:0.5:6:700",
49                                 "RRA:MAX:0.5:24:775",
50                                 "RRA:MAX:0.5:288:797"
51                         );
52                         $ret = rrd_create($fname, $opts, count($opts));
53                         if(!$ret) {
54                                 $err = rrd_error();
55                                 echo "Create error: $err\n";
56                                 die();
57                         }
58                 }
59                 if(!file_exists("db/network-away.rrd")) {
60                         $fname = "db/network-away.rrd";
61                         $opts = array(
62                                 "--step", "300", "--start", "0",
63                                 "DS:useraway:GAUGE:600:0:U",
64                                 "DS:userhere:GAUGE:600:0:U",
65                                 "RRA:AVERAGE:0.5:1:600",
66                                 "RRA:AVERAGE:0.5:6:700",
67                                 "RRA:AVERAGE:0.5:24:775",
68                                 "RRA:AVERAGE:0.5:288:797",
69                                 "RRA:MAX:0.5:1:600",
70                                 "RRA:MAX:0.5:6:700",
71                                 "RRA:MAX:0.5:24:775",
72                                 "RRA:MAX:0.5:288:797"
73                         );
74                         $ret = rrd_create($fname, $opts, count($opts));
75                         if(!$ret) {
76                                 $err = rrd_error();
77                                 echo "Create error: $err\n";
78                                 die();
79                         }
80                 }
81                 if(!file_exists("db/network-peruser.rrd")) {
82                         $fname = "db/network-peruser.rrd";
83                         $opts = array(
84                                 "--step", "300", "--start", "0",
85                                 "DS:userperchan:GAUGE:600:0:U",
86                                 "DS:chanperuser:GAUGE:600:0:U",
87                                 "RRA:AVERAGE:0.5:1:600",
88                                 "RRA:AVERAGE:0.5:6:700",
89                                 "RRA:AVERAGE:0.5:24:775",
90                                 "RRA:AVERAGE:0.5:288:797",
91                                 "RRA:MAX:0.5:1:600",
92                                 "RRA:MAX:0.5:6:700",
93                                 "RRA:MAX:0.5:24:775",
94                                 "RRA:MAX:0.5:288:797"
95                         );
96                         $ret = rrd_create($fname, $opts, count($opts));
97                         if(!$ret) {
98                                 $err = rrd_error();
99                                 echo "Create error: $err\n";
100                                 die();
101                         }
102                 }
103                 $this->timer = timer(5,array(&$this,"create_stats"),array());
104         }
105         
106         public function unload($rehash = false) {
107                 kill_timer($this->timer);
108         }
109         
110         function create_stats() {
111                 $this->timer = timer(5*60,array(&$this,"create_stats"),array());
112                 $stats = array(
113                         "opers" => 0,
114                         "total" => 0,
115                         "away" => 0,
116                         "here" => 0,
117                         "chansperuser" => 0,
118                         "usersperchan" => 0
119                 );
120                 foreach(P10_User::getAllUsers() as $user) {
121                         $stats['total']++;
122                         $stats['chansperuser'] += $user->getChannelCount();
123                         if($user->isAway()) $stats['away']++;
124                         else $stats['here']++;
125                         if($user->getModes()->hasMode('o')) $stats['opers']++;
126                 }
127                 $stats['chansperuser'] = $stats['chansperuser'] / $stats['total'];
128                 $channels = P10_Channel::getChannelCount();
129                 foreach(P10_Channel::getChannels() as $channel) {
130                         $stats['usersperchan'] += $channel->getUserCount();
131                 }
132                 $stats['usersperchan'] = $stats['usersperchan'] / $channels;
133                 $servers = P10_Server::getServerCount();
134                 rrd_update("db/network.rrd", time().":".$stats['total'].":".$channels.":".$stats['opers'].":".$servers);
135                 rrd_update("db/network-away.rrd", time().":".$stats['away'].":".$stats['here']);
136                 rrd_update("db/network-peruser.rrd", time().":".$stats['usersperchan'].":".$stats['chansperuser']);
137                 foreach(P10_Server::getServers() as $snum => $server) {
138                         $count = $server->getUserCount();
139                         $fname="db/".$server->getName().".rrd";
140                         if(!file_exists($fname)) {
141                                 $opts = array(
142                                         "--step", "300", "--start", "0",
143                                         "DS:users:GAUGE:600:0:U",
144                                         "RRA:AVERAGE:0.5:1:600",
145                                         "RRA:AVERAGE:0.5:6:700",
146                                         "RRA:AVERAGE:0.5:24:775",
147                                         "RRA:AVERAGE:0.5:288:797",
148                                         "RRA:MAX:0.5:1:600",
149                                         "RRA:MAX:0.5:6:700",
150                                         "RRA:MAX:0.5:24:775",
151                                         "RRA:MAX:0.5:288:797"
152                                 );
153                                 $ret = rrd_create($fname, $opts, count($opts));
154                         }
155                         rrd_update($fname, time().":".$count);
156                 }
157         }
158         
159 }
160
161 ?>