4fc06a2ad84d82fc11268257993b26b10335a393
[PHP-P10.git] / Bots / Stats.class.php
1 <?php
2 /******************************* PHP-P10 v2 *****************************
3  * Copyright (C) 2011-2012  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                 if(!file_exists("db/network-connections.rrd")) {
101                         $fname = "db/network-connections.rrd";
102                         $opts = array(
103                                 "--step", "300", "--start", "0",
104                                 "DS:localhost:GAUGE:600:0:U",
105                                 "DS:ipv4:GAUGE:600:0:U",
106                                 "DS:ipv6:GAUGE:600:0:U",
107                                 "RRA:AVERAGE:0.5:1:600",
108                                 "RRA:AVERAGE:0.5:6:700",
109                                 "RRA:AVERAGE:0.5:24:775",
110                                 "RRA:AVERAGE:0.5:288:797",
111                                 "RRA:MAX:0.5:1:600",
112                                 "RRA:MAX:0.5:6:700",
113                                 "RRA:MAX:0.5:24:775",
114                                 "RRA:MAX:0.5:288:797"
115                                 );
116                                 $ret = rrd_create($fname, $opts, count($opts));
117                                 if(!$ret) {
118                                         $err = rrd_error();
119                                         echo "Create error: $err\n";
120                                         die();
121                                 }
122                 }
123                 $this->timer = timer(5,array(&$this,"create_stats"),array());
124         }
125
126         public function unload($rehash = false) {
127                 kill_timer($this->timer);
128         }
129
130         function create_stats() {
131                 $this->timer = timer(5*60,array(&$this,"create_stats"),array());
132                 $stats = array(
133                         "opers" => 0,
134                         "total" => 0,
135                         "away" => 0,
136                         "here" => 0,
137                         "chansperuser" => 0,
138                         "usersperchan" => 0,
139                         "local" => 0,
140                         "ipv4" => 0,
141                         "ipv6" => 0
142                 );
143                 foreach(P10_User::getAllUsers() as $user) {
144                         $stats['total']++;
145                         $stats['chansperuser'] += $user->getChannelCount();
146                         if($user->isAway()) $stats['away']++;
147                         else $stats['here']++;
148                         if($user->getModes()->hasMode('o')) $stats['opers']++;
149                         if($user->getIP()->isLocalAddress()) $stats['local']++;
150                         if($user->getIP()->isIPv6()) $stats['ipv6']++;
151                         else $stats['ipv4']++;
152                 }
153                 $stats['chansperuser'] = $stats['chansperuser'] / $stats['total'];
154                 $channels = P10_Channel::getChannelCount();
155                 foreach(P10_Channel::getChannels() as $channel) {
156                         $stats['usersperchan'] += $channel->getUserCount();
157                 }
158                 $stats['usersperchan'] = $stats['usersperchan'] / $channels;
159                 $servers = P10_Server::getServerCount();
160                 rrd_update("db/network.rrd", time().":".$stats['total'].":".$channels.":".$stats['opers'].":".$servers);
161                 rrd_update("db/network-away.rrd", time().":".$stats['away'].":".$stats['here']);
162                 rrd_update("db/network-peruser.rrd", time().":".$stats['usersperchan'].":".$stats['chansperuser']);
163                 rrd_update("db/network-connections.rrd", time().":".$stats['local'].":".$stats['ipv4'].":".$stats['ipv6']);
164                 foreach(P10_Server::getServers() as $snum => $server) {
165                         $count = $server->getUserCount();
166                         $fname="db/".$server->getName().".rrd";
167                         if(!file_exists($fname)) {
168                                 $opts = array(
169                                         "--step", "300", "--start", "0",
170                                         "DS:users:GAUGE:600:0:U",
171                                         "RRA:AVERAGE:0.5:1:600",
172                                         "RRA:AVERAGE:0.5:6:700",
173                                         "RRA:AVERAGE:0.5:24:775",
174                                         "RRA:AVERAGE:0.5:288:797",
175                                         "RRA:MAX:0.5:1:600",
176                                         "RRA:MAX:0.5:6:700",
177                                         "RRA:MAX:0.5:24:775",
178                                         "RRA:MAX:0.5:288:797"
179                                         );
180                                         $ret = rrd_create($fname, $opts, count($opts));
181                         }
182                         rrd_update($fname, time().":".$count);
183                 }
184         }
185
186 }
187
188 ?>