#$Id: sysinfo.pl,v 1.6 2001/09/29 02:42:13 kc8apf Exp $

#!/usr/bin/perl -w

# -----------------------------------------------------------------
# kc8apf's sysinfo v0.7 for x-chat
# Avaliable at http://www.kc8apf.net/sysinfo/sysinfo.pl
# Author: <kc8apf@kc8apf.net>
# usage : /sys
#------------------------------------------------------------------
# CHANGELOG
# v0.5 - Initial release.  Built from whoo's hacked up sysinfo.pl
# v0.6 - Anthony Burhite (aburhite@charter.net)
#        provided a slight modification to retrieve the modem
#        controller for ppp connections. This replaces my quick 
#        hack to display "PPP Connection"
# v0.7 - Small fix for X-Chat v1.9.x.  Make sure you compile
#        the old perl hooks into X-Chat or the script will refuse
#        to even load
# -----------------------------------------------------------------
# YOU MUST ASSIGN THE VARIABLE BELOW TO SET YOUR INTERNET
# CONNECTION DEVICE

$DEV = dc0;   ## <-------------------------------CHANGE THAT


IRC::register("kc8apf's sysinfo", "0.6", "", "");
IRC::print "Loading kc8apf's sysinfo script";
IRC::add_command_handler("sys", "display_info");


sub display_info
{  
    
    #--UNAME--#
    $UNAME = `uname -sr`;
    chop ($UNAME);

  #--GET INFO ON FREEBSD--#
  if ($UNAME =~ /^freebsd/i) {
    #--MODEL--#
    $MODEL = `sysctl hw.model | cut -d":" -f2`;
    chop($MODEL);
    $MODEL =~ s/^ +//;

    #--HOW MANY CPUS--#
    $NUM=`sysctl hw.ncpu | cut -d":" -f2`;
    chop($NUM);
    $NUM =~ s/^ +//;

    #--PROCESSOR SPEED--#
    $CPU = `dmesg | grep CPU: | cut -d"C" -d"P" -d"U" -d":" -f2`;
    chop($CPU);

    #--TOTAL MEMORY--#
    $MEMTOTAL = `sysctl hw.physmem | cut -d":" -f2`;
    chop($MEMTOTAL);
    $MEMTOTAL = sprintf("%d",$MEMTOTAL/1024**2);

    #--MEMORY USED--#
    $MEMUSED = `top -b | grep Mem | cut -d":" -f2`;
    chop ($MEMUSED);
    $MEMUSED =~ /.+, ([0-9]+)([KM]) Cache, ([0-9]+)([KM]) Buf, ([0-9]+)([KM]) Free$/;
    $MEMCACHE = $1;
    $MEMCACHEKM = $2;
    $MEMBUF = $3;
    $MEMBUFKM = $4;
    $MEMUSED = $5;
    $MEMUSEDKM = $6;
    if ($MEMCACHEKM =~ /^K$/) {
       $MEMCACHE = $MEMCACHE / 1024;
    };
    if ($MEMBUFKM =~ /^K$/) {
       $MEMBUF = $MEMBUF / 1024;
    };
    if ($MEMUSEDKM =~ /^K$/) {
       $MEMUSED = $MEMUSED / 1024;
    };
    $MEMUSED = int($MEMUSED + $MEMBUF + $MEMCACHE);
    $MEMUSED = int($MEMTOTAL - $MEMUSED);

    #--LM_SENSORS #1--#
    $SENSOR1 = `/usr/local/bin/lmmon -i -p -s | head -n2 | tail -n1 | cut -d"/" -f2`;
    chop ($SENSOR1);
    $SENSOR1 =~ s/ +//;
    
    #--LM_SENSORS #2--#
    $SENSOR2 = `/usr/local/bin/lmmon -i -p -s | grep -A1 Fans: | tail -n1 | cut -d":" -f2`;
    chop ($SENSOR2);
    $SENSOR2 =~ s/ +//;

    #--CONNECTION DEVICE--# 
    $DEVTYPE=`dmesg | grep $DEV | head -n1 | cut -d"<" -f2 | cut -d">" -f1`;
    chop($DEVTYPE);
    if ($DEVTYPE =~ /^$/) {
        $DEVTYPE="Unknown";
    };

    #--PACKETS IN--# 
    $PACKIN = `netstat -i -n -b | grep $DEV | head -n1 | awk '{print \$7}'`;
    if($PACKIN < 1024**3) { $PACKIN = sprintf("%.02f",$PACKIN / 1024**2)."M"; } else { $PACKIN = sprintf("%.02f", $PACKIN / 1024**3)."G"; }

    #--PACKETS OUT--# 
    $PACKOUT = `netstat -i -n -b | grep $DEV | head -n1 | awk '{print \$10}'`; 
    if($PACKOUT < 1024**3) { $PACKOUT = sprintf("%.02f",$PACKOUT / 1024**2)."M"; } else { $PACKOUT = sprintf("%.02f", $PACKOUT / 1024**3)."G"; }

  } else {
  #--GET INFO ON LINUX--#

    #--MODEL--#
    $MODEL = `cat /proc/cpuinfo | grep '^model name' | head -1 | cut -d":" -f2`;
    $MODEL =~ s/^ +//;
    
    #--HOW MANY CPUS--#
    $NUM=`cat /proc/cpuinfo | grep 'model name' | wc -l | cut -d" " -f7`;
    chop ($NUM);
    
    #--PROCESSOR SPEED--#
    $CPU = `cat /proc/cpuinfo | grep 'cpu MHz' | cut -d":" -f2`;
    chop ($CPU);
    $CPU =~ s/^ +//;

    #--TOTAL MEMORY--#
    $MEMTOTAL = `free | grep Mem | awk '{printf (\"%d\", \$2/1000 )}'`;
    
    #--MEMORY USED--#
    $MEMUSED = `free | grep Mem | awk '{printf (\"%.0fg\", ( \$3 -(\$6 + \$7) )/1000)}'`;
    chop ($MEMUSED);

    #--BOGOMIPS--#
    $MIPS = `cat /proc/cpuinfo | grep '^bogomips' | cut -d":" -f2`;
    chop ($MIPS);
    $MIPS =~ s/ +//;
    
    #--LM_SENSORS #1--#
    $SENSOR1 = `sensors -f | grep 'temp:'`;
    chop ($SENSOR1);
    $SENSOR1 =~ /[\w\:]+[\s]+([^\s]+)/;
    $SENSOR1 = $1;
    
    #--LM_SENSORS #2--#
    $SENSOR2 = `sensors -f | grep 'fan1:'`;
    chop ($SENSOR2);
    $SENSOR2 =~ /[\w\:]+[\s]+([^\s]+)/;
    $SENSOR2 = $1;

    #--CONNECTION DEVICE--#
    if($DEV =~ /^ppp/i) {
       $DEVTYPE=`cat /proc/pci | grep 'Communication controller:' | cut -d" " -f7-`;
    } else {	    
       $DEVTYPE=`cat /proc/pci | grep "IRQ \`/sbin/ifconfig $DEV | grep Interrupt | cut -d":" -f2 | cut -d" " -f1\`" -B1 | grep Ethernet | cut -d":" -f2- | cut -d" " -f2-`;
    };
    chop($DEVTYPE);
    if ($DEVTYPE =~ /^$/) {
        $DEVTYPE="Unknown";
    };

    #--PACKETS IN--# 
    $PACKIN = `netstat -i -n -b | grep fxp0 | head -n1 | awk '{print \$7}'`;
	if($PACKIN < 1024**3) { $PACKIN = sprintf("%.02f",$PACKIN / 1024**2)."M"; } else { $PACKIN = sprintf("%.02f", $PACKIN / 1024**3)."G"; }

    #--PACKETS OUT--# 
    $PACKOUT = `netstat -i -n -b | grep fxp0 | head -n1 | awk '{print \$10}'`; 
	if($PACKOUT < 1024**3) { $PACKOUT = sprintf("%.02f",$PACKOUT / 1024**2)."M"; } else { $PACKOUT = sprintf("%.02f", $PACKOUT / 1024**3)."G"; }
  };

  #--GET COMMON INFO--#

    #--PROCS RUNNING--#
    $PROCS = `ps ax | wc -l`;
    $PROCS =~ s/^\s+//;
    chop ($PROCS);

    #--PERCENTAGE OF MEMORY USED--#
    $MEMPERCENT = $MEMUSED/$MEMTOTAL*100;
    if (int($MEMPERCENT) == int($MEMPERCENT + .5)) {
	    $MEMPERCENT = int($MEMPERCENT);
    } else {
	    $MEMPERCENT = int($MEMPERCENT + .5);
    };
    
    #--SCREEN RESOLUTION--#
    $RES = `xdpyinfo | grep dimensions | cut -d" " -f7`;
    chop ($RES);

    #--DISKSPACE--#
    $HDD = `df -l | awk '{ sum+=\$2/1024^2 }; END { printf (\"%dGb\", sum )}'`;
    chop ($HDD);
    
    #--DISKSPACE USED--#
    $HDDUSED = `df -l | awk '{ sum+=\$3/1024^2 }; END { printf (\"%dGb\", sum )}'`;
    chop ($HDDUSED);

    #--SUPPORT FOR DUAL PROCS--#
    unless($NUM eq 1 ) { $MODEL="Dual $MODEL"; }
    chop ($MODEL);

    #--UPTIME--#
    $UPTIME = `uptime`;
    chop ($UPTIME);
    $UPTIME =~ /.+ up (.+),.+[0-9]+ user/;
    $UPTIME = $1;

    #--BUILD OUTPUT--#
    $out = "SysInfo";
    if ($UNAME !~ /^$/) {
	$out = $out . " | System: $UNAME";
    };
    if ($MODEL !~ /^$/) {
        $out = $out . " | CPU: $CPU";
    };
    if ($MEMUSED !~ /^$/ && $MEMTOTAL !~ /^$/) {
        $out = $out . " | Mem: $MEMUSED/" . $MEMTOTAL . "Mb ($MEMPERCENT%)";
    };
    if ($HDD !~ /^$/ && $HDDUSED !~ /^$/) {
	$out = $out . " | Diskspace: " . $HDD . "b Used: " . $HDDUSED . "b";
    };
    if ($MIPS !~ /^$/) {
        $out = $out . " | Bogomips: $MIPS";
    };
    if ($RES !~ /^$/) {
        $out = $out . " | Screen Res: $RES";
    };
    if ($PROCS !~ /^$/) {
        $out = $out . " | Procs: $PROCS";
    };
    if ($SENSOR1 !~ /^$/ && $SENSOR2 !~ /^$/) {
	$out = $out . " | CPU: $SENSOR1 Fan 1: $SENSOR2";
    }
    if ($UPTIME !~ /^$/) {
	$out = $out . " | Uptime: $UPTIME";
    };
    if ($DEVTYPE !~ /^$/ && $PACKIN !~ /^$/ && $PACKOUT !~ /^$/) {
        $out = $out . " | Connection Device: $DEVTYPE In: " . $PACKIN . "b Out: " . $PACKOUT . "b";
    };

    #--SPEW IT INTO THE CHANNEL SHALL WE--#
    IRC::command("/say " . $out);
    return 1;}
