Skip to content

Instantly share code, notes, and snippets.

@mreschke

mreschke/server-stats.php

Last active Aug 10, 2016
Embed
What would you like to do?
#!/usr/bin/env php
<?php
# Basic live zabbix stats specifically for Dynatron servers
# This is also a github gist, so be sure to keep that updated
# https://gist.github.com/mreschke/d86e073efc2a8757b30e413850f44439
# mReschke 2016-08-08
while (1) {
system('clear');
echo "CPU percent is average over 1 minute period, so not every second real-time\n";
echo "Memory shows percent USED. HD shows GB free\n";
echo "\n";
printf("%-10s %-54s %-4s %-11s %-11s\n", "Server", "CPU", "Mem", "C:", "D:");
echo "---------------------------------------------------------------------------------------------------------------------------\n";
(new Show('dyna-web'))->webServer();
(new Show('dyna-web2'))->webServer();
(new Show('dyna-web3'))->webServer();
(new Show('dyna-web4'))->webServer();
(new Show('dyna-web5'))->webServer();
(new Show('dyna-web6'))->webServer();
printf("\n%-10s %-54s %-4s %-11s %-11s %-12s %-11s\n", "Server", "CPU", "Mem", "C:", "D:", "E:", "F:");
echo "---------------------------------------------------------------------------------------------------------------------------\n";
(new Show('dyna-sql'))->sqlServer();
(new Show('dyna-sql2'))->sqlServer();
(new Show('dyna-sql3'))->sqlServer();
(new Show('dyna-sql4'))->sqlServer();
(new Show('dyna-sql5'))->sqlServer();
(new Show('dyna-sql6'))->sqlServer();
sleep(10);
}
class Show
{
private $server;
private $port;
private $green = "\033[0;32m";
private $red = "\033[0;31m";
private $orange = "\033[0;33m";
private $defaultColor = "\033[0m";
public function __construct($server, $port=10050)
{
$this->server = $server;
$this->port = $port;
}
public function webServer()
{
$cpu = $this->cpu();
$memory = $this->memory();
$c = $this->hd('C:');
$d = $this->hd('D:');
#$c = $d = 'x';
printf("%-10s %-65s %-15s %-22s %-22s\n", $this->server, $cpu, $memory, $c, $d);
}
public function sqlServer()
{
$cpu = $this->cpu();
$memory = $this->memory();
$c = $this->hd('C:');
$d = $this->hd('D:');
$e = $this->hd('E:');
$f = $this->hd('F:');
#$c = $d = $e = $f = 'x';
printf("%-10s %-65s %-15s %-22s %-22s %-23s %-22s\n", $this->server, $cpu, $memory, $c, $d, $e, $f);
}
private function cpu()
{
$cpu = round($this->zabbix('system.cpu.util[all,system,avg1]'), 0);
$text = str_pad($cpu, 2, ' ', STR_PAD_LEFT);
$color = $this->green;
if ($cpu >= 25) $color = $this->orange;
if ($cpu >= 50) $color = $this->red;
$text = $text.' '.$color.str_repeat("=", $cpu / 2).$this->defaultColor;
return $text;
}
private function memory()
{
$memory = round($this->zabbix('vm.memory.size[pused]'), 0);
$color = $this->green;
if ($memory >= 75) $color = $this->orange;
if ($memory >= 90) $color = $this->red;
$text = $color.$memory.'%'.$this->defaultColor;
return $text;
}
private function hd($path)
{
$total = round($this->zabbix("vfs.fs.size[$path,total]") / 1024 / 1024 / 1024, 0);
$free = round($this->zabbix("vfs.fs.size[$path,free]") / 1024 / 1024 / 1024, 1);
$color = $this->green;
if ($free <= 6) $color = $this->orange;
if ($free <= 4) $color = $this->red;
$text = $color.$free." ($total) ".$this->defaultColor;
return $text;
}
private function zabbix($key)
{
exec("zabbix_get -s $this->server -p $this->port -k $key", $value);
return $value[0];
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment