Created
July 11, 2015 15:29
-
-
Save gcphost/f5fb4087de067e8df23e to your computer and use it in GitHub Desktop.
PHP Simple Stats v2
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <?php | |
| class SimpleStats { | |
| private $url = 'http://localhost/'; | |
| private $lock = '.stats.lock'; | |
| private $network = 'lo'; | |
| private $response = '/Unable to locate domain/'; | |
| private $procs = array( | |
| '/usr/sbin/mysqld', | |
| '/usr/sbin/lighttpd', | |
| '/usr/sbin/php5-fpm' | |
| ); | |
| public function __construct(){ | |
| if(is_file($this->lock)) die('Stats are locked with ' . $this->lock); | |
| } | |
| private function Set($i, $v=false){ | |
| $this->$i=$v; | |
| } | |
| private function Net($t, $s=1){ | |
| $a = trim(file_get_contents('/sys/class/net/'.$this->network.'/statistics/'.$t.'_bytes')); | |
| sleep($s); | |
| $b = trim(file_get_contents('/sys/class/net/'.$this->network.'/statistics/'.$t.'_bytes')); | |
| return round((($b - $a) / $s) / 1024, 2) ; | |
| } | |
| private function Pidof($p){ | |
| return trim(exec('pidof '.$p)); | |
| } | |
| private function Procs(){ | |
| foreach($this->procs as $proc){ | |
| if(!$this->Pidof($proc)) return false; | |
| } | |
| return true; | |
| } | |
| private function Load(){ | |
| return sys_getloadavg(); | |
| } | |
| public function run(){ | |
| return array( | |
| 'memory' => round(exec("printf \$(free -m| grep Mem | awk '{ print \$3/\$2*100 }')"),2), | |
| 'disc' => (100-round(disk_free_space(getcwd())/disk_total_space (getcwd()) * 100,2)), | |
| 'cpu' => round(exec("grep 'cpu ' /proc/stat | awk '{c=($2+$4)*100/($2+$4+$5)} END {print c}'"),2), | |
| 'load' => $this->Load()[0], | |
| 'net_out' => $this->Net('tx'), | |
| 'net_in' => $this->Net('rx'), | |
| 'connections' => exec("netstat -nt | awk '{ print $5}' | cut -d: -f1 | sed -e '/^$/d' | sort -n | uniq | wc -l"), | |
| 'running' => $this->Procs(), | |
| 'available' => (preg_match($this->response, file_get_contents($this->url)) ? true : false) | |
| ); | |
| } | |
| public function __destroy(){ | |
| if(is_file($this->lock)) unlink($this->lock); | |
| } | |
| } | |
| $SimpleStats=new SimpleStats(); | |
| die(json_encode($SimpleStats->run())); | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment