added RRD Stats Module
[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                 $this->timer = timer(5,array(&$this,"create_stats"),array());
82         }
83         
84         public function unload($rehash = false) {
85                 kill_timer($this->timer);
86         }
87         
88         function create_stats() {
89                 $this->timer = timer(5*60,array(&$this,"create_stats"),array());
90                 $stats = array(
91                         "opers" => 0,
92                         "total" => 0,
93                         "away" => 0,
94                         "here" => 0
95                 );
96                 foreach(P10_User::getAllUsers() as $num => $user) {
97                         $stats['total']++;
98                         if($user->isAway()) $stats['away']++;
99                         else $stats['here']++;
100                         if($user->getModes()->hasMode('o')) $stats['opers']++;
101                 }
102                 $channels = P10_Channel::getChannelCount();
103                 $servers = P10_Server::getServerCount();
104                 rrd_update("db/network.rrd", time().":".$stats['total'].":".$channels.":".$stats['opers'].":".$servers);
105                 rrd_update("db/network-away.rrd", time().":".$stats['away'].":".$stats['here']);
106                 foreach(uplink::$uplink->servers as $snum => $server) {
107                         $count = $server->getUserCount();
108                         $fname="db/".$server->getName().".rrd";
109                         if(!file_exists($fname)) {
110                                 $opts = array(
111                                         "--step", "300", "--start", "0",
112                                         "DS:users:GAUGE:600:0:U",
113                                         "RRA:AVERAGE:0.5:1:600",
114                                         "RRA:AVERAGE:0.5:6:700",
115                                         "RRA:AVERAGE:0.5:24:775",
116                                         "RRA:AVERAGE:0.5:288:797",
117                                         "RRA:MAX:0.5:1:600",
118                                         "RRA:MAX:0.5:6:700",
119                                         "RRA:MAX:0.5:24:775",
120                                         "RRA:MAX:0.5:288:797"
121                                 );
122                                 $ret = rrd_create($fname, $opts, count($opts));
123                         }
124                         rrd_update($fname, time().":".$count);
125                 }
126         }
127         
128 }
129
130 ?>