Add Mumble/mumble_stats_collector.php
This commit is contained in:
		
							parent
							
								
									6faaeec92f
								
							
						
					
					
						commit
						e7e857fc0a
					
				
					 1 changed files with 100 additions and 0 deletions
				
			
		
							
								
								
									
										100
									
								
								Mumble/mumble_stats_collector.php
									
										
									
									
									
										Normal file
									
								
							
							
						
						
									
										100
									
								
								Mumble/mumble_stats_collector.php
									
										
									
									
									
										Normal file
									
								
							| 
						 | 
					@ -0,0 +1,100 @@
 | 
				
			||||||
 | 
					<?php
 | 
				
			||||||
 | 
					// Minimal, bulletproof Mumble stats collector
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					require_once "/usr/share/php/Ice.php";
 | 
				
			||||||
 | 
					if (!class_exists("Ice\\Value")) {
 | 
				
			||||||
 | 
					    eval("namespace Ice; class Value {}");
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					if (!class_exists("Ice\\UserException")) {
 | 
				
			||||||
 | 
					    eval("namespace Ice; class UserException extends \\Exception {}");
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					require_once __DIR__ . "/MumbleServer.php";
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					$iceConnectionString = "Meta:tcp -h 127.0.0.1 -p 6502";
 | 
				
			||||||
 | 
					$serverId = 1;
 | 
				
			||||||
 | 
					$iceSecret = "PASSWORD1234";
 | 
				
			||||||
 | 
					$userStatsFile = __DIR__ . "/mumble_users.json";
 | 
				
			||||||
 | 
					$now = time();
 | 
				
			||||||
 | 
					$today = date("Y-m-d");
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					// Load stats
 | 
				
			||||||
 | 
					$userStats = [];
 | 
				
			||||||
 | 
					if (file_exists($userStatsFile)) {
 | 
				
			||||||
 | 
					    $userStats = json_decode(file_get_contents($userStatsFile), true) ?: [];
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					// Remove legacy fields
 | 
				
			||||||
 | 
					foreach ($userStats as &$userData) {
 | 
				
			||||||
 | 
					    unset($userData["sessions"], $userData["channels"]);
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					unset($userData);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					try {
 | 
				
			||||||
 | 
					    $ICE = Ice\initialize();
 | 
				
			||||||
 | 
					    $proxy = $ICE->stringToProxy($iceConnectionString);
 | 
				
			||||||
 | 
					    $meta = class_exists("\Murmur\MetaPrxHelper")
 | 
				
			||||||
 | 
					        ? \Murmur\MetaPrxHelper::checkedCast($proxy)
 | 
				
			||||||
 | 
					        : \MumbleServer\MetaPrxHelper::checkedCast($proxy);
 | 
				
			||||||
 | 
					    $context = ["secret" => $iceSecret];
 | 
				
			||||||
 | 
					    $server = $meta->getServer($serverId, $context);
 | 
				
			||||||
 | 
					    $rawUsers = $server->getUsers($context);
 | 
				
			||||||
 | 
					} catch (Exception $e) {
 | 
				
			||||||
 | 
					    exit(1);
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					// Build online user hashes
 | 
				
			||||||
 | 
					$onlineHashes = [];
 | 
				
			||||||
 | 
					foreach ($rawUsers as $id => $user) {
 | 
				
			||||||
 | 
					    $userName = is_object($user)
 | 
				
			||||||
 | 
					        ? $user->name ?? "Unknown User"
 | 
				
			||||||
 | 
					        : $user["name"] ?? "Unknown User";
 | 
				
			||||||
 | 
					    $userHash = is_object($user)
 | 
				
			||||||
 | 
					        ? $user->hash ?? md5($userName)
 | 
				
			||||||
 | 
					        : $user["hash"] ?? md5($userName);
 | 
				
			||||||
 | 
					    $onlineHashes[$userHash] = $userName;
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					// Process online users
 | 
				
			||||||
 | 
					foreach ($onlineHashes as $userHash => $userName) {
 | 
				
			||||||
 | 
					    if (!isset($userStats[$userHash])) {
 | 
				
			||||||
 | 
					        $userStats[$userHash] = [
 | 
				
			||||||
 | 
					            "name" => $userName,
 | 
				
			||||||
 | 
					            "first_seen" => $now,
 | 
				
			||||||
 | 
					            "last_seen" => $now,
 | 
				
			||||||
 | 
					            "connect_count" => 1,
 | 
				
			||||||
 | 
					            "days" => [$today => 1],
 | 
				
			||||||
 | 
					            "total_time" => 0,
 | 
				
			||||||
 | 
					            "current_session" => ["start" => $now],
 | 
				
			||||||
 | 
					            "is_connected" => true,
 | 
				
			||||||
 | 
					        ];
 | 
				
			||||||
 | 
					    } else {
 | 
				
			||||||
 | 
					        $userStats[$userHash]["name"] = $userName;
 | 
				
			||||||
 | 
					        $userStats[$userHash]["last_seen"] = $now;
 | 
				
			||||||
 | 
					        if (empty($userStats[$userHash]["is_connected"])) {
 | 
				
			||||||
 | 
					            $userStats[$userHash]["connect_count"]++;
 | 
				
			||||||
 | 
					            $userStats[$userHash]["current_session"] = ["start" => $now];
 | 
				
			||||||
 | 
					        }
 | 
				
			||||||
 | 
					        $userStats[$userHash]["is_connected"] = true;
 | 
				
			||||||
 | 
					        if (!isset($userStats[$userHash]["days"][$today])) {
 | 
				
			||||||
 | 
					            $userStats[$userHash]["days"][$today] = 1;
 | 
				
			||||||
 | 
					        }
 | 
				
			||||||
 | 
					    }
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					// Process disconnects
 | 
				
			||||||
 | 
					foreach ($userStats as $userHash => &$userData) {
 | 
				
			||||||
 | 
					    if (!isset($onlineHashes[$userHash])) {
 | 
				
			||||||
 | 
					        if (!empty($userData["is_connected"])) {
 | 
				
			||||||
 | 
					            $userData["is_connected"] = false;
 | 
				
			||||||
 | 
					            if (isset($userData["current_session"]["start"])) {
 | 
				
			||||||
 | 
					                $duration = $now - $userData["current_session"]["start"];
 | 
				
			||||||
 | 
					                $userData["total_time"] += $duration;
 | 
				
			||||||
 | 
					                unset($userData["current_session"]);
 | 
				
			||||||
 | 
					            }
 | 
				
			||||||
 | 
					        }
 | 
				
			||||||
 | 
					    }
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					unset($userData);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					file_put_contents($userStatsFile, json_encode($userStats, JSON_PRETTY_PRINT));
 | 
				
			||||||
 | 
					chmod($userStatsFile, 0666);
 | 
				
			||||||
		Loading…
	
	Add table
		
		Reference in a new issue