My i3 configurations and scripts.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

i3status2.php 7.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376
  1. <?php
  2. class Attribute
  3. {
  4. protected $full_text;
  5. protected $color;
  6. const NORMAL = '#B3FF6C';
  7. const WARNING = '#FF6836';
  8. const CRITICAL = '#FF474A';
  9. const INFO = '#E9F1FF';
  10. const DATE = '#98A6C7';
  11. const VOLUME = '#C3D4FF';
  12. public function __construct($full_text, $color = null)
  13. {
  14. $this->full_text = $full_text;
  15. if (null === $color) {
  16. $color = self::NORMAL;
  17. }
  18. $this->color = $color;
  19. }
  20. public function setFullText($full_text)
  21. {
  22. $this->full_text = $full_text;
  23. return $this;
  24. }
  25. public function getFullText()
  26. {
  27. return $this->full_text;
  28. }
  29. public function setColor($color)
  30. {
  31. $this->color = $color;
  32. return $this;
  33. }
  34. public function getColor()
  35. {
  36. return $this->color;
  37. }
  38. }
  39. class Element
  40. {
  41. public $attributes = null;
  42. public function __construct()
  43. {
  44. $this->attributes = array();
  45. }
  46. public function setAttributes(array $attributes)
  47. {
  48. $this->attributes = $attributes;
  49. return $this;
  50. }
  51. public function getAttributes()
  52. {
  53. return $this->attributes;
  54. }
  55. public function addAttribut(Attribute $a)
  56. {
  57. $this->attributes[] = $a;
  58. }
  59. }
  60. class i3Bar
  61. {
  62. protected $elements = null;
  63. public function __construct()
  64. {
  65. $this->protected = array();
  66. }
  67. public function setElements(array $elements)
  68. {
  69. $this->elements = $elements;
  70. return $this;
  71. }
  72. public function getElements()
  73. {
  74. return $this->elements;
  75. }
  76. public function addElement($e)
  77. {
  78. if (!empty($e)) {
  79. $this->elements[] = $e;
  80. }
  81. return $this;
  82. }
  83. public function __toString()
  84. {
  85. $render = ',[';
  86. foreach ($this->getElements() as $element) {
  87. $attributes = $element->getAttributes();
  88. if (!is_array($attributes)) {
  89. $attributes = array($attributes);
  90. }
  91. if (empty($attributes)) {
  92. continue;
  93. }
  94. $attrs = array();
  95. foreach ($attributes as $attr) {
  96. $attr_render = '{';
  97. $attr_render.= sprintf('"full_text": " %s ",', $attr->getFullText());
  98. $attr_render.= sprintf('"color": "%s"', $attr->getColor());
  99. $attr_render.= '}';
  100. $attrs[] = $attr_render;
  101. }
  102. $render.= count($element) > 1 ? '[' : '';
  103. $render.= implode(',', $attrs).',';
  104. $render.= count($element) > 1 ? ']' : '';
  105. }
  106. $render.= ']';
  107. $render = str_replace(',]', ']', $render)."\n";
  108. return $render;
  109. }
  110. }
  111. /**
  112. *
  113. * IPS
  114. *
  115. */
  116. function get_the_ips($ifaces=array())
  117. {
  118. $element = new Element();
  119. foreach ($ifaces as $iface) {
  120. preg_match('`inet ([^\s]+)`', shell_exec('ip addr show '.escapeshellcmd($iface)), $inet);
  121. if ($inet) {
  122. $element->addAttribut(new Attribute('['.$iface.'] '.$inet[1], Attribute::INFO));
  123. }
  124. }
  125. return $element;
  126. }
  127. /**
  128. *
  129. * BANDWITH
  130. *
  131. */
  132. function get_the_bandwith($ifaces=array())
  133. {
  134. $element = new Element();
  135. foreach ($ifaces as $iface) {
  136. preg_match('`inet ([^\s]+)`', shell_exec('ip addr show '.escapeshellcmd($iface)), $inet);
  137. if ($inet) {
  138. $bwidth = explode(' ', preg_replace('/ +/', ' ', trim(shell_exec('ifstat -i '.escapeshellcmd($iface).' 1 1 | tail -n 1'))));
  139. //$dstats = trim(shell_exec('/sbin/ifconfig '.escapeshellcmd($iface).' | grep bytes | sed \'s/[()]//g\' | awk \'{print $3"Mo↓ "$7"Mo↑"}\''));
  140. //$ips[] = '['.$iface.'] '.$bwidth[0].'Ko/s↓ '.$bwidth[1].'Ko/s↑ ('.$dstats.')';
  141. $element->addAttribut(new Attribute('['.$iface.'] '.$bwidth[0].'Ko/s↓ '.$bwidth[1].'Ko/s↑', Attribute::INFO));
  142. }
  143. }
  144. return $element;
  145. }
  146. /**
  147. *
  148. * SENSORS
  149. *
  150. */
  151. function get_sensors() {
  152. $sensors = array();
  153. preg_match_all('/^([^:]+): +\+([0-9.]+)°C +\(high = \+([0-9.]+)°C, crit = \+([0-9.]+)°C\)/iU', shell_exec('sensors | grep "°C"'), $datas, PREG_SET_ORDER);
  154. var_dump($datas);
  155. foreach($datas as $data) {
  156. }
  157. return $sensors;
  158. }
  159. function get_sensor($item) {
  160. get_sensors();
  161. }
  162. /**
  163. *
  164. * MOUNT
  165. *
  166. */
  167. function get_the_mount_space_used($mount_point)
  168. {
  169. $element = new Element();
  170. $mount_point = ' '.$mount_point.'$';
  171. $mount = shell_exec('df -h | egrep '.escapeshellcmd($mount_point).' | head -n 1');
  172. if ($mount) {
  173. list($dev, $size, $used, $free, $usedP, $point) = explode(' ', preg_replace('/\s+/', ' ', $mount));
  174. $pourcent = intval($usedP);
  175. if ($pourcent < 70) {
  176. $color = Attribute::NORMAL;
  177. } elseif ($pourcent < 90) {
  178. $color = Attribute::WARNING;
  179. } else {
  180. $color = Attribute::CRITICAL;
  181. }
  182. $element->addAttribut(new Attribute('['.$point.'] '.$usedP, $color));
  183. }
  184. return $element;
  185. }
  186. /**
  187. *
  188. * DATE
  189. *
  190. */
  191. function get_the_date()
  192. {
  193. $element = new Element();
  194. $element->addAttribut(new Attribute(date('H:i'), Attribute::DATE));
  195. $element->addAttribut(new Attribute(date('d/m/Y'), Attribute::DATE));
  196. return $element;
  197. }
  198. /**
  199. *
  200. * ACPI
  201. *
  202. */
  203. function get_the_acpi()
  204. {
  205. $element = new Element();
  206. $acpi = shell_exec('acpi');
  207. if (!preg_match('`Discharging`', $acpi)) {
  208. preg_match('`([0-9]+%)`', $acpi, $values);
  209. if (!empty($values)) {
  210. $charging = $values[1];
  211. if ($charging < 20) {
  212. $color = Attribute::CRITICAL;
  213. } elseif ($charging < 50) {
  214. $color = Attribute::WARNING;
  215. } else {
  216. $color = Attribute::NORMAL;
  217. }
  218. $charging.= '↑';
  219. $element->addAttribut(new Attribute($charging, $color));
  220. }
  221. return $element;
  222. }
  223. preg_match('`([0-9]+%), ([0-9]+:[0-9]+)`', $acpi, $values);
  224. if ($values[1] < 20) {
  225. $color = Attribute::CRITICAL;
  226. } elseif ($values[1] < 50) {
  227. $color = Attribute::WARNING;
  228. } else {
  229. $color = Attribute::INFO;
  230. }
  231. $charging = $values[1].'↓';
  232. $times = str_replace(':', 'h', $values[2]);
  233. $element->addAttribut(new Attribute(implode(' ', array($charging, $times)), $color));
  234. return $element;
  235. }
  236. /**
  237. *
  238. * VOLUME
  239. *
  240. */
  241. function get_the_master_volume()
  242. {
  243. $element = new Element();
  244. $volume = shell_exec('amixer get Master | grep Mono:');
  245. if ($volume) {
  246. preg_match('/\[([0-9%]+)\]/', $volume, $mach);
  247. if (isset($mach[1])) {
  248. $element->addAttribut(new Attribute($mach[1].' ♪', Attribute::VOLUME));
  249. }
  250. }
  251. return $element;
  252. }
  253. function get_the_public_ip($ipv6 = false)
  254. {
  255. $element = new Element();
  256. $memcache = new Memcached();
  257. $key = $ipv6 ? 'ipv6' : 'ipv4';
  258. $cache = 60 * 5;
  259. $memcache->addServer('localhost', 11211);
  260. $ip = $memcache->get($key);
  261. if (empty($ip)) {
  262. $ip = shell_exec('wget -O - -q '.($ipv6 ? '-6' : '').' ip.deblan.org');
  263. if (!$ip) {
  264. $ip = '?';
  265. }
  266. $memcache->set($key, '[wan] '.$ip, $cache);
  267. }
  268. $memcache->quit();
  269. $element->addAttribut(new Attribute($ip, Attribute::INFO));
  270. return $element;
  271. }
  272. function get_the_public_ipv4()
  273. {
  274. return get_the_public_ip();
  275. }
  276. function get_the_public_ipv6()
  277. {
  278. return get_the_public_ip(true);
  279. }
  280. $bar = new i3Bar();
  281. $bar
  282. ->addElement(get_the_ips(array('eth0', 'eth1', 'wlan0', 'easytether0')))
  283. #->addElement(get_the_public_ipv4())
  284. ->addElement(get_the_master_volume())
  285. ->addElement(get_the_date())
  286. ->addElement(get_the_acpi())
  287. ;
  288. echo $bar;