cPanel 11 Stats Script

Penguin129

New Member
Messages
68
Reaction score
0
Points
0
Ok, so I got really bored and wrote this class to get data from cPanel 11 and decided to share it. It only does bandwidth, disk space, and server status right now. If anyone wants something else, please post it here and I'll see what I can do. Erm the really big commented part should explain it, but I'll type it in a prettier form that isn't all orange.. Hope it helps anyone who needs it.. :happysad:

cPanel Stats by William Chu
Retrieves stuff from cPanel! :D
Note: For cPanel 11!

Declaring
The class needs to be declared with three strings, url, username, and password.
IE: a website is www.example.com and the login for cPanel 11 is example/elpmaxe so..
PHP:
$stats = new cPanelStats("www.example.com", "example", "elpmaxe");

Functions
Note: []s means optional
  • getData([filename])
    • retrieves a page from cPanel
    • .../frontend/x3/___
    • feel free to use this to read other pages
  • bandwidth([type])
    • gets bandwidth data
    • type gives a single string out of the normally returned array
  • disk([type])
    • gets disk data
    • specifying type gives a single string out of the normally returned array
  • server([type])
    • gets server data
    • specifying type gives a single string out of the normally returned array

Example
PHP:
<?php
include "cpanelstats.php";
$stats = new cPanelStats("www.example.com", "example", "elpmaxe");
echo "<pre>";
print_r($stats->bandwidth());
print_r($stats->disk());
print_r($stats->server());
echo "</pre>";
?>


PHP:
<?php
class cPanelStats {
	####
	# cPanel Stats by William Chu
	# Retrieves stuff from cPanel! :D
	# Note: For cPanel 11!
	####
	#	Declaring
	# The class needs to be declared with three
	# strings, url, username, and password. IE:
	# website is www.example.com and the login
	# for cPanel 11 is example/elpmaxe so..
	# $stats = new cPanelStats("www.example.com", "example", "elpmaxe");
	####
	# Functions
	# Note: []s means optional
	#	
	# getData([filename])
	#  - retrieves a page from cPanel
	#  - .../frontend/x3/___
	#  - feel free to use this to read other pages
	# bandwidth([type])
	#  - gets bandwidth data
	#  - specifying type gives a single string
	#    out of the normally returned array
	# disk([type])
	#  - gets disk data
	#  - specifying type gives a single string
	#    out of the normally returned array
	# server([type])
	#  - gets server data
	#  - specifying type gives a single string
	#    out of the normally returned array
	###
	# Example
	# 
	# include "cpanelstats.php";
	# $stats = new cPanelStats("www.example.com", "example", "elpmaxe");
	#	echo "<pre>";
	#	print_r($stats->bandwidth());
	#	print_r($stats->disk());
	#	print_r($stats->server());
	#	echo "</pre>";
	###
	function cPanelStats($url, $username, $password) {
		$this->url = $url;
		$this->username = $username;
		$this->password = $password;
	}
	function getData($file="index.html") {
		// Feel free to change the method it retrieves the page with.
		ob_start();
		readfile("http://{$this->username}:{$this->password}@{$this->url}:2082/frontend/x3/{$file}");
		$data = ob_get_contents();
		ob_end_clean();
		return $data;
	}
	function bandwidth($type=NULL) {
		$data = $this->getData();
		preg_match_all("/([0-9]+).([0-9]+)\/([0-9]+).([0-9]+) MB/", $data, $matches);
		$results["Used"] = "{$matches[1][1]}.{$matches[2][1]}";
		$results["Free"] = "{$matches[3][1]}.{$matches[4][1]}"-$results["Used"];
		$results["Total"] = "{$matches[3][1]}.{$matches[4][1]}";
		$results["PercentUsed"] = $results["Used"]/$results["Total"]*100;
		$results["PercentFree"] = $results["Free"]/$results["Total"]*100;
		if($type) {
			return $results[$type];
		} else {
			return $results;
		}
	}
	function disk($type=NULL) {
		$data = $this->getData();
		preg_match_all("/([0-9]+).([0-9]+)\/([0-9]+).([0-9]+) MB/", $data, $matches);
		$results["Used"] = "{$matches[1][0]}.{$matches[2][0]}";
		$results["Free"] = "{$matches[3][0]}.{$matches[4][0]}"-$results["Used"];
		$results["Total"] = "{$matches[3][0]}.{$matches[4][0]}";
		$results["PercentUsed"] = $results["Used"]/$results["Total"]*100;
		$results["PercentFree"] = $results["Free"]/$results["Total"]*100;
		if($type) {
			return $results[$type];
		} else {
			return $results;
		}
	}
	function server($type=NULL) {
		$data = $this->getData("status.html");
		preg_match_all("/<td class=\"statuscell\">(.+)<\/td>\n        <td class=\"statuscell\">(.+)<\/td>\n        <td class=\"statuscell\"><img src=\"\/(.+)-status/", $data, $matches);
		for($i=0;$i!=count($matches[0]);$i++) {
			$results[$matches[1][$i]] = array($matches[2][$i], $matches[3][$i]);
		}
		if($type) {
			return $results[$type];
		} else {
			return $results;
		}
	}
}
?>

.. that took a while to do..
 
Last edited:

nikaashvetia26

New Member
Messages
1
Reaction score
0
Points
0
x10k.png

can you pm this "last login from" code please
 
Top