cvs: embed /php-irssi/examples sysinfo.php

From: Date: Thu, 03 Apr 2003 20:31:43 +0000
Subject: cvs: embed /php-irssi/examples sysinfo.php
Groups: php.embed.cvs 
Request: Send a blank email to [email protected] to get a copy of this message
bs		Thu Apr  3 15:31:43 2003 EDT

  Added files:                 
    /embed/php-irssi/examples	sysinfo.php 
  Log:
  add sysinfo.php which displays an item for network activity and load average
  

Index: embed/php-irssi/examples/sysinfo.php
+++ embed/php-irssi/examples/sysinfo.php
<?php
/* $Id: sysinfo.php,v 1.1 2003/04/03 20:31:43 bs Exp $
 *
 * loadavg
 *   puts an item containing the systems loadaverage in the statusbar 
 *   needs /proc/loadavg
 *
 *   activate it using
 *   /statusbar window add loadavg
 *   or if you want to have it in an own statusbar
 *   /statusbar sysinfo add (-alignment left|right) loadavg
 *
 *   set the refresh interval (in seconds)
 *   /set loadavg_refresh (default 10)
 *
 * netact
 *   puts an item containing the systems network activity in the statusbar 
 *   needs /proc/net/dev
 *
 *   activate it using
 *   /statusbar window add netact
 *
 *   set the refresh interval (in seconds):
 *   /set netact_refresh (default 60)
 *
 *   define the interfaces to monitor
 *   /set netact_devices eth0(,eth1,..)
 *
 * Author: Benjamin Schulz <[email protected]> */
 
namespace samples {

    class sysinfo {

		var $_loadavg_refresh = 10;
		var $_loadavg_refresh_tag = false;
		var $_netact_refresh = 60;
		var $_netact_devices = array();
		var $_netact_refresh_tag = false;
		
		function __construct()
		{
			irssi_settings_add_str('loadavg', 'loadavg_refresh',
$this->_loadavg_refresh);
			irssi_settings_add_str('netact', 'netact_refresh',
$this->_netact_refresh);
			irssi_settings_add_str('netact', 'netact_devices', 'eth0');

			$this->init_loadavg();
			$this->init_netact();

			irssi_signal_add('setup changed', array($this, 'init_loadavg'),
IRSSI_SIGNAL_PRIORITY_LOW);
			irssi_signal_add('setup changed', array($this, 'init_netact'),
IRSSI_SIGNAL_PRIORITY_LOW);
		}
		
		function init_loadavg()
		{
            $refresh = irssi_settings_get_str('loadavg_refresh');
            
            if (false !== $this->_loadavg_refresh_tag)
            	irssi_timeout_remove($this->_loadavg_refresh_tag);
            
        	$this->_loadavg_refresh = $refresh;
        	$this->_loadavg_refresh_tag = irssi_timeout_add($refresh * 1000, array($this,
'refresh_loadavg') );

			irssi_statusbar_item_register('loadavg', '$0', array($this,
'draw_loadavg'));
		}
		
		function _get_loadavg()
		{
			$str = @file_get_contents('/proc/loadavg');
			if ($str === false)
				return 'unable to read /proc/loadavg';
			return trim($str);
		}
		
		function refresh_loadavg()
		{
			irssi_statusbar_items_redraw('loadavg');	
		}
		
		function draw_loadavg($item, $get_size_only)
		{			
			$item->default_handler($get_size_only, '{sb Load:
'.$this->_get_loadavg().'}', '', true);
			return;
		}


		function init_netact()
		{
            $refresh = irssi_settings_get_str('netact_refresh');
            $devices = irssi_settings_get_str('netact_devices');
            
            if (false !== $this->_netact_refresh_tag)
            	irssi_timeout_remove($this->_netact_refresh_tag);
            
           	$this->_netact_refresh = $refresh;
           	$this->_netact_devices = explode(',',$devices);

			$this->_netact_refresh_tag = irssi_timeout_add($refresh * 1000, array($this,
'refresh_netact') );           

			irssi_statusbar_item_register('netact', '$0', array($this,
'draw_netact'));
		}
		
        function _get_netact()
        {
        	$return = '';
            $results = array();

            if (!$fd = @fopen('/proc/net/dev', 'r'))
            	return 'unable to read /proc/net/dev';

            while ($buf = fgets($fd, 4096)) {
                if (!preg_match('/:/', $buf))
                	continue;

                list($dev, $stats_list) = preg_split('/:/', $buf, 2);
                $dev = trim($dev);
                if (!in_array($dev, $this->_netact_devices))
                	continue;
                $stats = preg_split('/\s+/', trim($stats_list));
                $results[] .= sprintf('%s: %s %s %s',
                	$dev,
                	$this->_format_bytes($stats[0] / 1024), // received
                	$this->_format_bytes($stats[8] / 1024), // sent
                	$this->_format_bytes($stats[2] + $stats[10])  // errors
                
                );
            }
            
			return implode(' ', $results);
        }
        
        function _format_bytes ($kbytes) {
            $spacer = ' ';
            if ($kbytes > 1048576)
                $result  = sprintf('%.2fG', $kbytes / 1048576);
            elseif ($kbytes > 1024)
                $result  = sprintf('%.2fM', $kbytes / 1024);
            else
                $result  = sprintf('%.2fK', $kbytes);

            return $result;
        }
		
		function refresh_netact()
		{
			irssi_statusbar_items_redraw('netact');	
		}
		
		function draw_netact($item, $get_size_only)
		{			
			$item->default_handler($get_size_only, '{sb '.$this->_get_netact().'}',
'', true);
			return;
		}

    }
    
}

new samples::sysinfo;

?>



Thread (1 message)

  • Benjamin Schulz
« previous php.embed.cvs (#82) next »