doti3/i3status.php

443 lines
10 KiB
PHP
Raw Normal View History

2015-03-02 19:19:34 +01:00
<?php
class Attribute
{
protected $full_text;
protected $color;
2017-12-15 17:43:30 +01:00
protected $separator = true;
2015-03-02 19:19:34 +01:00
2017-12-15 17:43:30 +01:00
const NORMAL = '#B3FF6C';
const WARNING = '#FF6836';
2015-03-02 19:19:34 +01:00
const CRITICAL = '#FF474A';
2017-12-15 17:43:30 +01:00
const INFO = '#E9F1FF';
const DATE = '#98A6C7';
const MUSIC = '#C3D4FF';
2015-03-02 19:19:34 +01:00
2017-12-15 17:43:30 +01:00
public function __construct($full_text, $color = null, $separator = true)
2015-03-02 19:19:34 +01:00
{
$this->full_text = $full_text;
2017-12-15 17:43:30 +01:00
$this->separator = $separator;
2015-03-02 19:19:34 +01:00
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;
}
2017-12-15 17:43:30 +01:00
public function setSeparator($separator)
{
$this->separator = $separator;
return $this;
}
public function getSeparator()
{
return $this->separator;
}
2015-03-02 19:19:34 +01:00
}
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;
}
2017-12-15 17:43:30 +01:00
public function addAttribute(Attribute $a)
2015-03-02 19:19:34 +01:00
{
$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;
}
2017-12-15 17:43:30 +01:00
public function addElementIfNotEmtpy($e)
{
if (empty($e)) {
return $this;
}
return $this->addElement($e);
}
2015-03-02 19:19:34 +01:00
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 = '{';
2017-12-15 17:43:30 +01:00
if (!$attr->getSeparator()) {
$attr_render .= sprintf('"full_text": "%s",', $attr->getFullText());
$attr_render .= '"separator": false,';
$attr_render .= '"separator_block_width": 0,';
} else {
$attr_render .= sprintf('"full_text": " %s ",', $attr->getFullText());
$attr_render .= '"separator": true,';
$attr_render .= '"separator_block_width": 15,';
}
$attr_render .= sprintf('"color": "%s"', $attr->getColor());
$attr_render .= '}';
2015-03-02 19:19:34 +01:00
$attrs[] = $attr_render;
}
2017-12-15 17:43:30 +01:00
$render .= count($element) > 1 ? '[' : '';
$render .= implode(',', $attrs).',';
$render .= count($element) > 1 ? ']' : '';
2015-03-02 19:19:34 +01:00
}
2017-12-15 17:43:30 +01:00
$render .= ']';
2015-03-02 19:19:34 +01:00
$render = str_replace(',]', ']', $render)."\n";
return $render;
}
}
/**
2017-12-15 17:43:30 +01:00
* IPS.
2015-03-02 19:19:34 +01:00
*/
2017-12-15 17:43:30 +01:00
function get_the_ips($ifaces = array())
2015-03-02 19:19:34 +01:00
{
$element = new Element();
foreach ($ifaces as $iface) {
preg_match('`inet ([^\s]+)`', shell_exec('ip addr show '.escapeshellcmd($iface)), $inet);
if ($inet) {
2017-12-15 17:43:30 +01:00
$element->addAttribute(new Attribute('['.$iface.'] '.$inet[1], Attribute::INFO));
2015-03-02 19:19:34 +01:00
}
}
return $element;
}
/**
2017-12-15 17:43:30 +01:00
* BANDWITH.
2015-03-02 19:19:34 +01:00
*/
2017-12-15 17:43:30 +01:00
function get_the_bandwith($ifaces = array())
2015-03-02 19:19:34 +01:00
{
$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.')';
2017-12-15 17:43:30 +01:00
$element->addAttribute(new Attribute('['.$iface.'] '.$bwidth[0].'Ko/s↓ '.$bwidth[1].'Ko/s↑', Attribute::INFO));
2015-03-02 19:19:34 +01:00
}
}
return $element;
}
/**
2017-12-15 17:43:30 +01:00
* SENSORS.
2015-03-02 19:19:34 +01:00
*/
2016-05-23 22:51:38 +02:00
function get_sensors()
{
$sensors = array();
2015-03-02 19:19:34 +01:00
2016-05-23 22:51:38 +02:00
preg_match_all('/^([^:]+): +\+([0-9.]+)°C +\(high = \+([0-9.]+)°C, crit = \+([0-9.]+)°C\)/iU', shell_exec('sensors | grep "°C"'), $datas, PREG_SET_ORDER);
2015-03-02 19:19:34 +01:00
2017-12-15 17:43:30 +01:00
var_dump($datas);
2016-05-23 22:51:38 +02:00
foreach ($datas as $data) {
}
2015-03-02 19:19:34 +01:00
2016-05-23 22:51:38 +02:00
return $sensors;
2015-03-02 19:19:34 +01:00
}
2016-05-23 22:51:38 +02:00
function get_sensor($item)
{
get_sensors();
2015-03-02 19:19:34 +01:00
}
/**
2017-12-15 17:43:30 +01:00
* MOUNT.
2015-03-02 19:19:34 +01:00
*/
function get_the_mount_space_used($mount_point, $limits = array(70, 90))
{
2017-12-15 17:43:30 +01:00
$element = new Element();
2015-03-02 19:19:34 +01:00
$mount_point = ' '.$mount_point.'$';
2017-12-15 17:43:30 +01:00
$mount = shell_exec('df -h | egrep '.escapeshellcmd($mount_point).' | head -n 1');
2015-03-02 19:19:34 +01:00
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;
}
2017-12-15 17:43:30 +01:00
$element->addAttribute(new Attribute('['.$point.'] '.$usedP, $color));
2015-03-02 19:19:34 +01:00
}
return $element;
}
/**
2017-12-15 17:43:30 +01:00
* DATE.
2015-03-02 19:19:34 +01:00
*/
function get_the_date()
{
$element = new Element();
2017-12-15 17:43:30 +01:00
$element->addAttribute(new Attribute(date('H:i:s'), Attribute::DATE));
$element->addAttribute(new Attribute(date('d/m/Y'), Attribute::DATE));
2015-03-02 19:19:34 +01:00
return $element;
}
/**
2017-12-15 17:43:30 +01:00
* ACPI.
2015-03-02 19:19:34 +01:00
*/
function get_the_acpi()
{
$element = new Element();
2017-12-15 17:43:30 +01:00
$acpi = shell_exec('acpi');
2015-03-02 19:19:34 +01:00
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;
}
2017-12-15 17:43:30 +01:00
$charging .= '↑';
2015-03-02 19:19:34 +01:00
2017-12-15 17:43:30 +01:00
$element->addAttribute(new Attribute($charging, $color));
2015-03-02 19:19:34 +01:00
}
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].'↓';
2017-12-15 17:43:30 +01:00
$times = str_replace(':', 'h', $values[2]);
2015-03-02 19:19:34 +01:00
2017-12-15 17:43:30 +01:00
$element->addAttribute(new Attribute(implode(' ', array($charging, $times)), $color));
2015-03-02 19:19:34 +01:00
return $element;
}
/**
2017-12-15 17:43:30 +01:00
* MUSIC.
2015-03-02 19:19:34 +01:00
*/
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])) {
2017-12-15 17:43:30 +01:00
$element->addAttribute(new Attribute($mach[1].' ♪', Attribute::MUSIC));
2015-03-02 19:19:34 +01:00
}
}
return $element;
}
function get_the_public_ip($ipv6 = false)
{
2017-12-15 17:43:30 +01:00
$element = new Element();
2015-03-02 19:19:34 +01:00
$memcache = new Memcached();
2017-12-15 17:43:30 +01:00
$key = $ipv6 ? 'ipv6' : 'ipv4';
$cache = 60 * 5;
2015-03-02 19:19:34 +01:00
$memcache->addServer('localhost', 11211);
$ip = $memcache->get($key);
if (empty($ip)) {
2017-12-15 17:43:30 +01:00
$ip = trim(shell_exec('wget -O - -q '.($ipv6 ? '-6' : '').' ip.deblan.org | head -n 1 | cut -c1-50'));
2015-03-02 19:19:34 +01:00
2017-12-15 17:43:30 +01:00
if ($ip === '192.168.0.254') {
$ip = trim(shell_exec('wget -O - -q '.($ipv6 ? '-6' : '').' "https://api.ipify.org/?format=text" | head -n 1 | cut -c1-50'));
} elseif (empty($ip)) {
2015-03-02 19:19:34 +01:00
$ip = '?';
}
$memcache->set($key, '[wan] '.$ip, $cache);
}
$memcache->quit();
2017-12-15 17:43:30 +01:00
$element->addAttribute(new Attribute($ip, Attribute::INFO));
2015-03-02 19:19:34 +01:00
return $element;
}
function get_the_public_ipv4()
{
return get_the_public_ip();
}
function get_the_public_ipv6()
{
return get_the_public_ip(true);
}
2017-12-15 17:43:30 +01:00
function get_spotify_metadata()
{
$status = trim(shell_exec('playerctl -p spotify status'));
//if (!in_array($status, ['Playing', 'Paused'])) {
if (!in_array($status, ['Playing'])) {
return null;
}
// $status = str_replace(
// ['Playing', 'Paused'],
// ['▮▮', '▶'],
// $status
// );
$cleanup = function($d) {
$string = trim(str_replace(['"', "'"], ' ', $d));
if (mb_strlen($string) > 15) {
$string = substr($string, 0, 15).'…';
}
return $string;
};
$title = $cleanup(shell_exec('playerctl -p spotify metadata xesam:title'));
$artist = $cleanup(shell_exec('playerctl -p spotify metadata xesam:artist'));
$label = sprintf('%s %s', $title, $artist);
$element = new Element();
$element->addAttribute(new Attribute($label, Attribute::MUSIC));
return $element;
}
2015-03-02 19:19:34 +01:00
$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'))
2015-05-12 15:59:39 +02:00
->addElement(get_the_mount_space_used('/secured'))
2017-12-15 17:43:30 +01:00
->addElementIfNotEmtpy(get_spotify_metadata())
2015-03-02 19:19:34 +01:00
->addElement(get_the_master_volume())
->addElement(get_the_date())
->addElement(get_the_acpi())
;
echo $bar;