#!/opt/csw/bin/perl -w
#
# A System Information script for irssi on Solaris
# 
# v 0.0.4
#
# Pierre Bauduin
# February 2007
#
# Based on sysinfo277-irssi.pl by David Rudie
#
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions
# are met:
# 1. Redistributions of source code must retain the above copyright
#    notice, this list of conditions and the following disclaimer.
# 2. Redistributions in binary form must reproduce the above copyright
#    notice, this list of conditions and the following disclaimer in the
#    documentation and/or other materials provided with the distribution.
#
# THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
# ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
# ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
# FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
# DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
# OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
# HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
# LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
# OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
# SUCH DAMAGE.
#


$VERSION = '0.0.4';
%IRSSI = (
  authors	=> 'Pierre Bauduin',
  contact	=> 'pierre@baudu.in',
  name		=> 'sysinfo-solaris-irssi.pl',
  description	=> 'system information script for irssi on Solaris',
  based_on	=> 'Based on sysinfo277-irssi.pl by David Rudie',
  license	=> 'BSD',
  url		=> 'http://pierre.baudu.in/software/',
  changed	=> 'Wed Feb 21 23:10:44 CET 2007',
  bugs		=> 'Y en a pas, touchons du bois :) !'
);


use Irssi;
use POSIX qw(floor);
use strict;


# Set up the arrays and variables first.
use vars qw(
  $cpu
  @cpu
  @cpuinfo
  $data
  @data
  $diskusage
  $forks
  @forks
  @iostat
  @loadaverage
  $loadaverage
  @meminfo
  $mhz
  @mhz
  $model
  @netdev
  @netstat
  $output
  @physicalmemory
  $physicalmemory
  @prtconf
  @prtdiag
  $systemcalls
  @systemcalls
  @systemconfiguration
  $systemconfiguration
  $solaris
  $uptime
  $var
  @var
  @vmstat_s
);


my $os		= `uname -s`; chop($os);
my $osn		= `uname -n`; chop($osn);
my $osv		= `uname -r`; chop($osv);
my $osm		= `uname -m`; chop($osm);
my $uname	= "$os $osv/$osm";

my $solaris	= 1 if $os =~ /^SunOS$/;

my $sunsparc	= 1 if $osm =~ /^sun4u$/;
my $sunsparc	= 1 if $osm =~ /^sun4v$/;
my $sunx86      = 1 if $osm =~ /^i86pc$/;

die("This script will only run on Solaris.\n") unless $solaris;

sub cmd_sysinfo {

  # These are the default settings for which information gets displayed.
  # 0 = Off; 1 = On
  my $showHostname	= 1;
  my $showOS		= 1;
  my $showSystemConfiguration = 1;
  my $showPhysicalMemory = 1;
  my $showProcesses	= 1;
  my $showUptime	= 1;
  my $showLoadAverage	= 1;
  my $showMemoryUsage	= 1;
  my $showDiskUsage	= 1;
  my $showNetworkTraffic= 1;
  my $showForks		= 1;
  my $showSystemCalls   = 1;


  if($solaris) {
    @netstat		= `netstat -in`;
    @prtconf		= `/usr/sbin/prtconf`;
    @prtdiag		= `/usr/sbin/prtdiag`;
    @iostat		= `/usr/bin/iostat`;
    @vmstat_s		= `/usr/bin/vmstat -s`;
    #printf "DEBUG: Résultat de vmstat: @vmstat_s\n";
  }

  if($showHostname)		{ $output  = "Hostname: $osn - "; }

  if($showOS)			{ $output .= "OS: $uname - "; }

  if($showSystemConfiguration) {
        @systemconfiguration= grep(/^System Configuration/, @prtdiag);
	$systemconfiguration= join("\n", $systemconfiguration[0]);
	chomp($systemconfiguration);
        $output .= "$systemconfiguration - ";
    }

  if($showPhysicalMemory) {
        @physicalmemory= grep(/^Memory size/, @prtdiag);
	$physicalmemory= join("\n", $physicalmemory[0]);
	$physicalmemory =~ s/Memory size//;
	chomp($physicalmemory);
	$output .= "Physical Memory: $physicalmemory - ";
    }


  if($showUptime) {
    $uptime = `uptime`; chomp($uptime);
    if($uptime =~ /day/) {
      $uptime =~ s/^.* ([0-9]*) day.* ([0-9]*):([0-9]*), .*$/$1d $2h $3m/;
    } elsif($uptime =~/min/) {
      $uptime =~ s/^.* ([0-9]*) min.*$/0d 0h $1m/;
    } else {
      $uptime =~ s/^.* ([0-9]*):([0-9]*),.*$/0d $1h $2m/;
    }
    chomp($uptime);
    $output .= "Uptime: $uptime - ";
    }

  if($showLoadAverage) {
  $loadaverage = `uptime`; chomp($loadaverage);
  @loadaverage = split(/average: /, $loadaverage, 2);
  @loadaverage = split(/, /, $loadaverage[1], 2);
  $loadaverage = $loadaverage[0];
  chomp($loadaverage);
  $output .= "Load Average: $loadaverage - ";
    }


  if($showDiskUsage) {
    my $vara = `df -k | grep -v Filesystem | awk '{ sum+=\$2 / 1024 / 1024}; END { print sum }'`; chomp($vara);
    my $vard = `df -k | grep -v Filesystem | awk '{ sum+=\$3 / 1024 / 1024}; END { print sum }'`; chomp($vard);
    my $varp = sprintf("%.2f", $vard / $vara * 100);
    $vara = sprintf("%.2f", $vara);
    $vard = sprintf("%.2f", $vard);
    $diskusage=$vard."GB/".$vara."GB ($varp%)";
    chomp($diskusage);
    $output .= "Disk Usage: $diskusage - ";
    }

  if($showSystemCalls) {
        @systemcalls= grep(/system calls/, @vmstat_s);
	$systemcalls= join("\n", $systemcalls[0]);
	$systemcalls =~ s/system calls//;
	chomp($systemcalls);
	$output .= "System Calls: $systemcalls - ";
    }

  if($showForks) {
        @forks= grep(/forks/, @vmstat_s);
	$forks= join("\n", $forks[0]);
	$forks =~ s/forks//;
	chomp($forks);
	$output .= "Forks: $forks - ";
    }

  if($showMemoryUsage) {
        @var= grep(/user   cpu/, @vmstat_s);
	$var= join("\n", $var[0]);
	$var =~ s/var//;
	chomp($var);
	my $user_cpu=$var;

        @var= grep(/system cpu/, @vmstat_s);
	$var= join("\n", $var[0]);
	$var =~ s/var//;
	chomp($var);
	my $system_cpu=$var;

        @var= grep(/idle   cpu/, @vmstat_s);
	$var= join("\n", $var[0]);
	$var =~ s/var//;
	chomp($var);
	my $idle_cpu=$var;

        @var= grep(/wait   cpu/, @vmstat_s);
	$var= join("\n", $var[0]);
	$var =~ s/var//;
	chomp($var);
	my $wait_cpu=$var;

        my $total_cpu=$user_cpu+$system_cpu+$idle_cpu+$wait_cpu;
        $user_cpu=sprintf("%.2f", $user_cpu / $total_cpu * 100);
        $system_cpu=sprintf("%.2f", $system_cpu / $total_cpu * 100);
        $wait_cpu=sprintf("%.2f", $wait_cpu / $total_cpu * 100);
        $idle_cpu=sprintf("%.2f", $idle_cpu / $total_cpu * 100);

	$output .= "CPU Usage: User: $user_cpu% System: $system_cpu% Wait: $wait_cpu% Idle: $idle_cpu% ";

	# DEBUG: This will show what Perl binary is being used
	#        Useful when you got several versions of Perl (Sun,
	#        sunfreeware.com, blastwave.org, ...)
	#use Config;
	#my $perl_path = $Config{perlpath};
	#print $perl_path, "\n";
	#$output .= " - using $perl_path";
	# End DEBUG
    }

  # On va afficher les resultats
  # Les espaces multiples deviennent des espaces simples
  $output =~ s/\ \ */ /g;
  Irssi::active_win()->command("/ $output");
  return 1;
}

Irssi::command_bind("sysinfo", "cmd_sysinfo");

