doti3/i3status.php

443 lines
10 KiB
PHP
Executable File
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

<?php
class Attribute
{
protected $full_text;
protected $color;
protected $separator = true;
const NORMAL = '#B3FF6C';
const WARNING = '#FF6836';
const CRITICAL = '#FF474A';
const INFO = '#E9F1FF';
const DATE = '#98A6C7';
const MUSIC = '#C3D4FF';
public function __construct($full_text, $color = null, $separator = true)
{
$this->full_text = $full_text;
$this->separator = $separator;
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;
}
public function setSeparator($separator)
{
$this->separator = $separator;
return $this;
}
public function getSeparator()
{
return $this->separator;
}
}
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 addAttribute(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 addElementIfNotEmtpy($e)
{
if (empty($e)) {
return $this;
}
return $this->addElement($e);
}
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 = '{';
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 .= '}';
$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->addAttribute(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->addAttribute(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->addAttribute(new Attribute('['.$point.'] '.$usedP, $color));
}
return $element;
}
/**
* DATE.
*/
function get_the_date()
{
$element = new Element();
$element->addAttribute(new Attribute(date('H:i:s'), Attribute::DATE));
$element->addAttribute(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->addAttribute(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->addAttribute(new Attribute(implode(' ', array($charging, $times)), $color));
return $element;
}
/**
* MUSIC.
*/
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->addAttribute(new Attribute($mach[1].' ♪', Attribute::MUSIC));
}
}
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 = trim(shell_exec('wget -O - -q '.($ipv6 ? '-6' : '').' ip.deblan.org | head -n 1 | cut -c1-50'));
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)) {
$ip = '?';
}
$memcache->set($key, '[wan] '.$ip, $cache);
}
$memcache->quit();
$element->addAttribute(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);
}
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;
}
$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_mount_space_used('/secured'))
->addElementIfNotEmtpy(get_spotify_metadata())
->addElement(get_the_master_volume())
->addElement(get_the_date())
->addElement(get_the_acpi())
;
echo $bar;