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