From dcc51dde70fe88c5fe37a240238da61f7edbed6e Mon Sep 17 00:00:00 2001 From: Simon Vieille Date: Mon, 2 Mar 2015 19:19:34 +0100 Subject: [PATCH] status scripts --- i3status.php | 380 +++++++++++++++++++++++++++++++++++++++++++++++++++ i3status.sh | 9 ++ 2 files changed, 389 insertions(+) create mode 100755 i3status.php create mode 100755 i3status.sh diff --git a/i3status.php b/i3status.php new file mode 100755 index 0000000..c06bc9a --- /dev/null +++ b/i3status.php @@ -0,0 +1,380 @@ +full_text = $full_text; + + if (null === $color) { + $color = self::NORMAL; + } + + $this->color = $color; + } + + public function setFullText($full_text) + { + $this->full_text = $full_text; + + return $this; + } + + public function getFullText() + { + return $this->full_text; + } + + public function setColor($color) + { + $this->color = $color; + + return $this; + } + + public function getColor() + { + return $this->color; + } +} + +class Element +{ + public $attributes = null; + + public function __construct() + { + $this->attributes = array(); + } + + public function setAttributes(array $attributes) + { + $this->attributes = $attributes; + + return $this; + } + + public function getAttributes() + { + return $this->attributes; + } + + public function addAttribut(Attribute $a) + { + $this->attributes[] = $a; + } +} + +class i3Bar +{ + protected $elements = null; + + public function __construct() + { + $this->protected = array(); + } + + public function setElements(array $elements) + { + $this->elements = $elements; + + return $this; + } + + public function getElements() + { + return $this->elements; + } + + public function addElement($e) + { + if (!empty($e)) { + $this->elements[] = $e; + } + + return $this; + } + + public function __toString() + { + $render = ',['; + + foreach ($this->getElements() as $element) { + $attributes = $element->getAttributes(); + + if (!is_array($attributes)) { + $attributes = array($attributes); + } + + if (empty($attributes)) { + continue; + } + + $attrs = array(); + + foreach ($attributes as $attr) { + $attr_render = '{'; + $attr_render.= sprintf('"full_text": " %s ",', $attr->getFullText()); + $attr_render.= sprintf('"color": "%s"', $attr->getColor()); + $attr_render.= '}'; + + $attrs[] = $attr_render; + } + + $render.= count($element) > 1 ? '[' : ''; + $render.= implode(',', $attrs).','; + $render.= count($element) > 1 ? ']' : ''; + } + + $render.= ']'; + + $render = str_replace(',]', ']', $render)."\n"; + + return $render; + } +} + +/** + * + * IPS + * + */ +function get_the_ips($ifaces=array()) +{ + $element = new Element(); + + foreach ($ifaces as $iface) { + preg_match('`inet ([^\s]+)`', shell_exec('ip addr show '.escapeshellcmd($iface)), $inet); + + if ($inet) { + $element->addAttribut(new Attribute('['.$iface.'] '.$inet[1], Attribute::INFO)); + } + } + + return $element; +} + +/** + * + * BANDWITH + * + */ +function get_the_bandwith($ifaces=array()) +{ + $element = new Element(); + + foreach ($ifaces as $iface) { + preg_match('`inet ([^\s]+)`', shell_exec('ip addr show '.escapeshellcmd($iface)), $inet); + + if ($inet) { + $bwidth = explode(' ', preg_replace('/ +/', ' ', trim(shell_exec('ifstat -i '.escapeshellcmd($iface).' 1 1 | tail -n 1')))); + + //$dstats = trim(shell_exec('/sbin/ifconfig '.escapeshellcmd($iface).' | grep bytes | sed \'s/[()]//g\' | awk \'{print $3"Mo↓ "$7"Mo↑"}\'')); + //$ips[] = '['.$iface.'] '.$bwidth[0].'Ko/s↓ '.$bwidth[1].'Ko/s↑ ('.$dstats.')'; + + $element->addAttribut(new Attribute('['.$iface.'] '.$bwidth[0].'Ko/s↓ '.$bwidth[1].'Ko/s↑', Attribute::INFO)); + } + } + + return $element; +} + +/** + * + * SENSORS + * + */ +function get_sensors() { + $sensors = array(); + + preg_match_all('/^([^:]+): +\+([0-9.]+)°C +\(high = \+([0-9.]+)°C, crit = \+([0-9.]+)°C\)/iU', shell_exec('sensors | grep "°C"'), $datas, PREG_SET_ORDER); + + var_dump($datas); + foreach($datas as $data) { + } + + return $sensors; +} + +function get_sensor($item) { + get_sensors(); +} + +/** + * + * MOUNT + * + */ +function get_the_mount_space_used($mount_point, $limits = array(70, 90)) +{ + $element = new Element(); + $mount_point = ' '.$mount_point.'$'; + $mount = shell_exec('df -h | egrep '.escapeshellcmd($mount_point).' | head -n 1'); + + if ($mount) { + list($dev, $size, $used, $free, $usedP, $point) = explode(' ', preg_replace('/\s+/', ' ', $mount)); + + $pourcent = intval($usedP); + + if ($pourcent < $limits[0]) { + $color = Attribute::NORMAL; + } elseif ($pourcent < $limits[1]) { + $color = Attribute::WARNING; + } else { + $color = Attribute::CRITICAL; + } + + $element->addAttribut(new Attribute('['.$point.'] '.$usedP, $color)); + } + + return $element; +} + +/** + * + * DATE + * + */ +function get_the_date() +{ + $element = new Element(); + + $element->addAttribut(new Attribute(date('H:i:s'), Attribute::DATE)); + $element->addAttribut(new Attribute(date('d/m/Y'), Attribute::DATE)); + + return $element; +} + +/** + * + * ACPI + * + */ +function get_the_acpi() +{ + $element = new Element(); + $acpi = shell_exec('acpi'); + + if (!preg_match('`Discharging`', $acpi)) { + preg_match('`([0-9]+%)`', $acpi, $values); + + if (!empty($values)) { + $charging = $values[1]; + + if ($charging < 20) { + $color = Attribute::CRITICAL; + } elseif ($charging < 50) { + $color = Attribute::WARNING; + } else { + $color = Attribute::NORMAL; + } + + $charging.= '↑'; + + $element->addAttribut(new Attribute($charging, $color)); + } + + return $element; + } + + preg_match('`([0-9]+%), ([0-9]+:[0-9]+)`', $acpi, $values); + + if ($values[1] < 20) { + $color = Attribute::CRITICAL; + } elseif ($values[1] < 50) { + $color = Attribute::WARNING; + } else { + $color = Attribute::INFO; + } + + $charging = $values[1].'↓'; + $times = str_replace(':', 'h', $values[2]); + + $element->addAttribut(new Attribute(implode(' ', array($charging, $times)), $color)); + + return $element; +} + +/** + * + * VOLUME + * + */ +function get_the_master_volume() +{ + $element = new Element(); + $volume = shell_exec('amixer get Master | grep Mono:'); + + if ($volume) { + preg_match('/\[([0-9%]+)\]/', $volume, $mach); + + if (isset($mach[1])) { + $element->addAttribut(new Attribute($mach[1].' ♪', Attribute::VOLUME)); + } + } + + return $element; +} + +function get_the_public_ip($ipv6 = false) +{ + $element = new Element(); + $memcache = new Memcached(); + $key = $ipv6 ? 'ipv6' : 'ipv4'; + $cache = 60 * 5; + $memcache->addServer('localhost', 11211); + + $ip = $memcache->get($key); + + if (empty($ip)) { + $ip = shell_exec('wget -O - -q '.($ipv6 ? '-6' : '').' ip.deblan.org'); + + if (!$ip) { + $ip = '?'; + } + + $memcache->set($key, '[wan] '.$ip, $cache); + } + + $memcache->quit(); + + $element->addAttribut(new Attribute($ip, Attribute::INFO)); + + return $element; +} + +function get_the_public_ipv4() +{ + return get_the_public_ip(); +} + +function get_the_public_ipv6() +{ + return get_the_public_ip(true); +} + +$bar = new i3Bar(); + +$bar + ->addElement(get_the_bandwith(array('eth0', 'eth1', 'wlan0', 'easytether0'))) + ->addElement(get_the_ips(array('eth0', 'eth1', 'wlan0', 'easytether0'))) + ->addElement(get_the_public_ipv4()) + ->addElement(get_the_mount_space_used('/')) + ->addElement(get_the_mount_space_used('/home', array(80, 90))) + ->addElement(get_the_mount_space_used('/share')) + ->addElement(get_the_master_volume()) + ->addElement(get_the_date()) + ->addElement(get_the_acpi()) +; + +echo $bar; diff --git a/i3status.sh b/i3status.sh new file mode 100755 index 0000000..7cfad1e --- /dev/null +++ b/i3status.sh @@ -0,0 +1,9 @@ +#!/bin/sh + +echo '{"version":1}' + +echo '[[]' + +while true; do + php ~/.i3/i3status.php 2>/dev/null +done