added isLocalAddress method to IPAddr.class.php to check for local addresses (RFC1918...
authorpk910 <philipp@zoelle1.de>
Sun, 27 May 2012 15:13:07 +0000 (17:13 +0200)
committerpk910 <philipp@zoelle1.de>
Sun, 27 May 2012 15:13:07 +0000 (17:13 +0200)
Bots/Stats.class.php
Uplink/IPAddr.class.php

index ec4e2fb349c456d2beabc34e68796823a538749f..4fc06a2ad84d82fc11268257993b26b10335a393 100644 (file)
@@ -97,6 +97,29 @@ class {$_NAME} extends Bot {
                                        die();
                                }
                }
+               if(!file_exists("db/network-connections.rrd")) {
+                       $fname = "db/network-connections.rrd";
+                       $opts = array(
+                               "--step", "300", "--start", "0",
+                               "DS:localhost:GAUGE:600:0:U",
+                               "DS:ipv4:GAUGE:600:0:U",
+                               "DS:ipv6: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());
        }
 
@@ -112,7 +135,10 @@ class {$_NAME} extends Bot {
                        "away" => 0,
                        "here" => 0,
                        "chansperuser" => 0,
-                       "usersperchan" => 0
+                       "usersperchan" => 0,
+                       "local" => 0,
+                       "ipv4" => 0,
+                       "ipv6" => 0
                );
                foreach(P10_User::getAllUsers() as $user) {
                        $stats['total']++;
@@ -120,6 +146,9 @@ class {$_NAME} extends Bot {
                        if($user->isAway()) $stats['away']++;
                        else $stats['here']++;
                        if($user->getModes()->hasMode('o')) $stats['opers']++;
+                       if($user->getIP()->isLocalAddress()) $stats['local']++;
+                       if($user->getIP()->isIPv6()) $stats['ipv6']++;
+                       else $stats['ipv4']++;
                }
                $stats['chansperuser'] = $stats['chansperuser'] / $stats['total'];
                $channels = P10_Channel::getChannelCount();
@@ -131,6 +160,7 @@ class {$_NAME} extends Bot {
                rrd_update("db/network.rrd", time().":".$stats['total'].":".$channels.":".$stats['opers'].":".$servers);
                rrd_update("db/network-away.rrd", time().":".$stats['away'].":".$stats['here']);
                rrd_update("db/network-peruser.rrd", time().":".$stats['usersperchan'].":".$stats['chansperuser']);
+               rrd_update("db/network-connections.rrd", time().":".$stats['local'].":".$stats['ipv4'].":".$stats['ipv6']);
                foreach(P10_Server::getServers() as $snum => $server) {
                        $count = $server->getUserCount();
                        $fname="db/".$server->getName().".rrd";
index 1eddcbed0aa7f872ff688d122121426430ffd534..2370cf66e48fab506177595eec9a74ad9edd3611 100644 (file)
@@ -96,6 +96,34 @@ class IPAddr {
        public function isIPv6() {
                return $this->addr_is_ipv6;
        }
+       
+       public function isLocalAddress() {
+               /* checks if address is out of:
+               * 127.0.0.1/32
+               * 10.0.0.0/8
+               * 192.168.0.0/16
+               * 172.16.0.0/12
+               * ::1/128
+               * fc00::/7
+               */
+               if($this->addr_is_ipv6) {
+                       if(
+                               (($this->ip6[0] & 0xFE00) == 0xFC00) || /* fc00::/7 */
+                               ($this->ip6[0] == 0 && $this->ip6[1] == 0 && $this->ip6[2] == 0 && $this->ip6[3] == 0 && 
+                                $this->ip6[4] == 0 && $this->ip6[5] == 0 && $this->ip6[6] == 0 && $this->ip6[7] == 1)
+                         )
+                               return true;
+               } else {
+                       if(
+                               (($this->ip6[6] & 0xFFFF) == 0x7F00 && ($this->ip6[7] & 0xFFFF) == 0x0001) || /* 127.0.0.1/32 */
+                               (($this->ip6[6] & 0xFF00) == 0x0A00) || /* 10.0.0.0/8 */
+                               (($this->ip6[6] & 0xFFF0) == 0xAC10) || /* 172.16.0.0/12 */
+                               (($this->ip6[6] & 0xFFFF) == 0xC0A8) /* 192.168.0.0/16 */
+                         )
+                               return true;
+               }
+               return false;
+       }
 
        public function getAddress() {
                if($this->isIPv6()) {