All pastes #66420 Raw Edit

Someone

public text v1 · immutable
#66420 ·published 2006-06-17 00:28 UTC
rendered paste body
#!/usr/bin/perl
# System Information Script originally for X-Chat, by s0ulslack (Tony Annen)
# Made compatible with the use strict; pragma by deitarion (Stephan Sokolow)
# Distributed under the terms of the GNU General Public License, v2 or later
# $Header: ~/.xchat2/yasis.pl,v 2.1.2 2005/09/05 23:41:44 $

# If you run into any trouble or need help, you can use http://soul.f2o.org/feedback.php
# Automatically install this script for xchat by typing "./yasis-2.1.pl xchat_install"
# at the bash prompt.

# X-Chat commands:
#
#  Name			Command
#  /yasis		Show your stats to the channel
#  /qyasis		Print the stats to your screen but don't show everybody else

# Known Bugs:
# - Aliased sensor names in lm_sensors breaks YASIS's support for displaying the values.

# changes in v2.1.2 (by Stephan Sokolow)
## YASIS now calculates disk usage (the percentage of free space) correctly.
## YASIS now ignores space in devices which don't start with a slash.
##      This prevents Gentoo Linux's /lib/splash/cache parition from doubling calculated value.
##      It also prevents RAM drives like /dev/shm and udev's /dev from being included.
## The processor activity at the time of the script's calling is now displayed.

# changes in v2.1.1 (by Stephan Sokolow)
## YASIS can now be run as a normal X-Chat plugin/script
## Doesn't matter to normal users: YASIS now works with "use strict;"

# changes in v2.1
## added best uptime display to -u (uptime) switch
## output tweaked
## refreshed the comments above!
## added an install function (./yasis-2.1.pl xchat_install)
##  \- moves yasis script from current directory to ~/.xchat2/yasis.pl
##   \- adds /yasis and /yasishelp commands automatically to X-Chat (in ~/.xchat2/commands.conf)
## /yasishelp command echos commands and doesn't print them in the channel/msg
## includes (tries to anyway) linux distro/version in [OS] sections
## should detect multiple connections automagically (ppp0-2, lan)
##  \- should display the network card/modem name of the default route 
##   \- pools packets for both devices for total In/Out Bytes

use strict;
#use warnings;
#use diagnostics;

my $xchat_plugin = 0;
eval { 
Xchat::register( 'YASIS', '2.1', 'Yet Another System Info Script') 
};
if ($@) { 
	$xchat_plugin = 0; 
} else { 
	$xchat_plugin = 1; 
}

# As of v2.1, $do_display and $do_temp are obsolete.
# The display is automatically detected and added to the
# full stats (-a) by default (use short (-s) to exclude video info)
# The cpu tempurate is enabled if "sensors" is found
#$do_display = 0;
#$do_temp = 0;

sub get_info
{

# Kernel Version
  my $uname = `uname -r`;
  chop ($uname);

# CPU
  my $cpu_cnt = `cat /proc/cpuinfo | grep 'processor' | wc -l | sed 's/ *//'`; chomp($cpu_cnt);
  my $cache = `cat /proc/cpuinfo | grep 'cache size' | head -n 1 | sed -e 's/^.*: //'`; chomp($cache);
  my $bogomips = `cat /proc/cpuinfo | grep 'bogomips' | head -n 1  | sed -e 's/^.*: //'`; chomp($bogomips);
# CPU Temp
# TODO: Fix this. It breaks if the values are aliased
  my $do_temp;
  if (-x "sensors") { $do_temp=1; } else { $do_temp=0;}
    if ($do_temp == 1) {
      my $cpu_temp = `sensors | grep Temp3 | awk '{print \$2}' | sed 's/.*+//'`; chop($cpu_temp);
      my $cpu_temp1 = `sensors -f | grep Temp3 | awk '{print \$2}' | sed 's/.*+//'`; chop($cpu_temp1);
      my $cpu_rpm = `sensors | grep fan1 | awk '{print \$2,\$3}' | sed 's/.*+//'`; chop($cpu_rpm);
  }

# CPU Usage
  my $avg_cpu_usage = +(split( /\s+/, (`vmstat`)[2] ))[13]; chop($avg_cpu_usage); # TODO: Actually use this value
  my $cpu_usage = `top -bn1 -p 1 | egrep -o "[0-9.]+% us," | egrep -o "[0-9.]+"`; chop($cpu_usage);
  # my $avg_cpu_usage =  ((split( /\s+/, (`vmstat`)[2] ))[12]);

# Memory
  my $mem_total = `free | grep Mem | awk '{printf (\"%dMb\", \$2/1024 )}'`; chop($mem_total);
  my $mem_free = `free | grep Mem | awk '{printf (\"%.0fMb\", ( \$3 -(\$6 + \$7) )/1024)}'`; chop($mem_free);
  my $mem_per = `free -m | grep Mem | awk '{printf (\"%.0f\", (\$2 - \$3 + \$6 + \$7) * (100/ \$2))}'`; 

# Video 
  my ($do_display,$x,$vid_card,$screen_res,$screen_depth);
  $x = "";
  if (-x "/usr/X11R6/bin/xdpyinfo") { $do_display=1; } else { $do_display=0;}
    if ($do_display == 1) {
      $vid_card = `cat /proc/pci | grep VGA | sed -e 's/.*: //' | sed 's/ (.*//' | sed 's/ATI//' | sed 's/Technologies//' | sed 's/Inc//' | sed 's/   //'`; chop($vid_card);
      $screen_res = `xdpyinfo | grep dimensions | awk '{print \$2}'`; chop($screen_res);
      $screen_depth = `xdpyinfo | grep "depth of root window" | awk '{print \$5}'`; chop($screen_depth);
# Get Xorg/Xfree Version
    if ($x eq "") {$x = `xdpyinfo | grep XFree86"."version | awk '{print \$3}'`; if ($x ne "") {$x = "XFree86 $x";}} chomp($x);
    if ($x eq "") {$x = `xdpyinfo | grep X.Org"."version | awk '{print \$3}'`; if ($x ne "") {$x = "X.org $x";}} chomp($x);
    if ($x eq "") {$x = "Unknown";}
}

# Hard Drive
  my $hdspacet = `df | awk '{ if (substr(\$1,1,1) == "/") sum+=\$2/1024^2 }; END { printf (\"%.02fG\", sum )}'`;
  my $hdspaceu = `df | awk '{ if (substr(\$1,1,1) == "/") sum+=\$3/1024^2 }; END { printf (\"%.02fG\", sum )}'`;
  my $hdspacef = `df | awk '{ if (substr(\$1,1,1) == "/") sum+=\$4/1024^2 }; END { printf (\"%.02fG\", sum )}'`;
  my $hdpercent = sprintf("%.0f",(100/${hdspacet}*${hdspacef}));

# Network (default and others)
  system(`/sbin/route -n > /tmp/rinfo`);
  open(ROUTE, "/tmp/rinfo") or die;
  my @rinfo = <ROUTE>;
  close(ROUTE) or die;
    my $line;
    my ($dest, $gate, $mask, $flags, $metric, $ref, $use, $face);
    my ($dest1, $gate1, $mask1, $flags1, $metric1, $ref1, $use1, $face1);
    foreach $line (@rinfo) {
      if($line=~/0.0.0.0/) {
        ($dest, $gate, $mask, $flags, $metric, $ref, $use, $face) = split(/\s+/, $line, 8);
        chomp($dest, $gate, $mask, $flags, $metric, $ref, $use, $face);
      } 
      if ($line=~/192.168/) {
        ($dest1, $gate1, $mask1, $flags1, $metric1, $ref1, $use1, $face1) = split(/\s+/, $line, 8);
        chomp($dest1, $gate1, $mask1, $flags1, $metric1, $ref1, $use1, $face1);
      };

  }

  if ($face1 eq $face)  {$face1=""}
  my $ppp = `cat /proc/pci | grep Communication | sed 's/.*: //' | sed 's/ (.*//'`; chop($ppp);
  my $eth = `cat /proc/pci | grep Ethernet | sed 's/.*: //' | sed 's/ (.*//'`; chop($eth);
  my ($eth_name, $inet);
  my ($eth_name1, $inet1);
  if ($face eq "ppp0") {$eth_name=$ppp; $inet='ppp0';} elsif ($face eq "ppp1") {$eth_name=$ppp; $inet='ppp1';} elsif ($face eq "ppp2") {$eth_name=$ppp; $inet='ppp2';}
  if ($face eq "lan") {$eth_name=$eth; $inet='lan';} elsif ($face eq "lan") {$eth_name=$eth; $inet='lan';} elsif ($face eq "lan") {$eth_name=$eth; $inet='lan';}
  if ($face1 eq "lan") {$eth_name1=$eth; $inet1='lan';} elsif ($face1 eq "eth1") {$eth_name1=$eth; $inet1='eth1';} elsif ($face1 eq "eth2") {$eth_name1=$eth; $inet1='eth3';}
  if ($face && $face1) {$face="(${face}*, $face1)"} else {$face="($face)";}
  if ($face eq "") { $face="Unknown Device";}

#$echk = `/sbin/ifconfig | grep eth | wc -l`;
#$pchk = `/sbin/ifconfig | grep ppp | wc -l`;
#if ($echk == 1) {$eth_name=$eth; $inet_dev=lan; $sc=1; }
#if ($pchk == 1) {$eth_name=$ppp; $inet_dev=ppp0; $sc=2; }
#if ($echk == 1 && $pchk == 1) {undef $sc; $eth_name ="Multiple Connections"; $inet_dev="2"; } 
#if ($echk == 0 && $pchk == 0) {$eth_name ="Unknown Connection"; } 

# Network Traffic
my ($packet_in, $packet_out);
if ($inet && $inet1) {
  my $packet_inp = `cat /proc/net/dev | grep $inet | awk -F: '/:/ {print \$2}' | awk '{printf \$1}'`;
    if($packet_inp < 1024**3) { $packet_inp = sprintf("%.02f",$packet_inp / 1024**2)."M"; } else { $packet_inp = sprintf("%.02f", $packet_inp / 1024**3)."G"; }
     my  $packet_outp = `cat /proc/net/dev | grep $inet | awk -F: '/:/ {print \$2}' | awk '{print \$9}'`;
        if($packet_outp < 1024**3) { $packet_outp = sprintf("%.02f",$packet_outp / 1024**2)."M"; } else { $packet_outp = sprintf("%.02f", $packet_outp / 1024**3)."G"; }

  my $packet_ine = `cat /proc/net/dev | grep $inet1 | awk -F: '/:/ {print \$2}' | awk '{printf \$1}'`;
    if($packet_ine < 1024**3) { $packet_ine = sprintf("%.02f",$packet_ine / 1024**2)."M"; } else { $packet_ine = sprintf("%.02f", $packet_ine / 1024**3)."G"; }
      my $packet_oute = `cat /proc/net/dev | grep $inet1 | awk -F: '/:/ {print \$2}' | awk '{print \$9}'`;
        if($packet_oute < 1024**3) { $packet_oute = sprintf("%.02f",$packet_oute / 1024**2)."M"; } else { $packet_oute = sprintf("%.02f", $packet_oute / 1024**3)."G"; }

	$packet_in = $packet_inp+$packet_ine."M";
	$packet_out = $packet_outp+$packet_oute."M";

  } elsif ($inet) {
  $packet_in = `cat /proc/net/dev | grep $inet | awk -F: '/:/ {print \$2}' | awk '{printf \$1}'`;
    if($packet_in < 1024**3) { $packet_in = sprintf("%.02f",$packet_in / 1024**2)."M"; } else { $packet_in = sprintf("%.02f", $packet_in / 1024**3)."G"; }
      $packet_out = `cat /proc/net/dev | grep $inet | awk -F: '/:/ {print \$2}' | awk '{print \$9}'`;
        if($packet_out < 1024**3) { $packet_out = sprintf("%.02f",$packet_out / 1024**2)."M"; } else { $packet_out = sprintf("%.02f", $packet_out / 1024**3)."G"; }
  } else {
  $packet_in = "0.00M";
  $packet_out = "0.00M";
}

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

# best uptime related
  my $buchk = `ls -asl ~/ | grep .buptime | wc -l`;
  if ($buchk eq "0") { system("cat /proc/uptime | awk '{printf \$1}' > ~/.buptime_uptime"); system("echo \"$uptime\" > ~/.buptime"); }
  my $cu = `cat /proc/uptime | awk '{printf \$1}'`; chomp($cu); 
  my $bu = `cat ~/.buptime_uptime`; chomp($bu);
  my $buptime = `cat ~/.buptime | sed 's/ //'`; chomp($buptime);

# Distro related
my $distro = "";
  if (-e "/etc/gentoo-release") {$distro = `cat /etc/gentoo-release | sed 's/version //'`;}
  if (-e "/etc/slackware-version") {$distro = `cat /etc/slackware-version | sed 's/version //'`;}
  if (-e "/etc/redhat-release") {$distro = `cat /etc/redhat-release | sed 's/version //'`;}
  if (-e "/etc/mandrake-release") {$distro = `cat /etc/mandrake-release | sed 's/version //'`;}
  if (-e "/etc/debian_version") {$distro = "Debian ".`cat /etc/debian_version | sed 's/version //'`."";}
  if (-e "/etc/SuSE-release") {$distro = `cat /SuSE-release | sed 's/version //'`;}
  if (-e "/etc/turbolinux-release") {$distro = `cat /etc/turbolinux-release | sed 's/version //'`;}
  if (-e "/etc/libranet_version") {$distro = 'Libranet-'.`cat /etc/libranet_version | sed 's/version //'`;}
  if (-e "/etc/arch-release") {$distro = `cat /etc/arch-release | sed 's/version //'`;}
  if ($distro eq "") {$distro = "Unknown";} 
  chomp ($distro);
  my $ver="2.1";

my $switch;
if ($ARGV[0]){
    $switch = $ARGV[0];
  } else {
  $switch = "-";
}
		

# Handle the xchat_install switch
if ($switch eq "xchat_install") {
   system("clear");
   system("echo \"Moving yasis-\"$ver\".pl from `pwd`/ to ~/.xchat2/yasis.pl...\"");
   system("mv ./yasis-\"$ver\".pl ~/.xchat2/yasis.pl");
   system("echo \"Done installing YASIS. Please either restart X-Chat or load it in the plugins dialog.\"");
   system("chmod 700 ~/.xchat2/yasis.pl");
 exit;
}

my ($final,$cpu,$cpumodel,@cpumodel,$cpumhz,@cpumhz,$cpuinf,$cpu_temp,$cpu_temp1,$cpu_rpm,$z);
if ($switch =~/b/ or $switch =~/[e-g]/ or $switch =~/[i-l]/ or $switch =~/[p-r]/ or $switch =~/[w-z]/){
  $final = "";

} elsif ($switch =~/d/){
  $final = "[\cBDisk\cB]  $hdspacef Free, $hdspacet Total ($hdpercent% Free)";

} elsif ($switch =~/m/){
  $final = "[\cBMemory\cB]  $mem_free Used, $mem_total Total ($mem_per% Free)"; 

} elsif ($switch =~/s/){
  if ($cu > $bu) {system("echo \"$uptime\" > ~/.buptime"); system("echo \"$cu\" > ~/.buptime_uptime"); }
  $cpu = cat("/proc/cpuinfo");
  
  if ($cpu =~/sparc/){
    $cpu =~ /(.*sparc.*)/;
    @cpumodel = split/:/,$1;
    $cpumodel = $cpumodel[1];
    $cpumodel =~ s/ //;
	
    $cpu =~ /(.*bogomips.*)/;
    @cpumhz = split/:/,$1;
    $cpumhz = sprintf ("%.f",$cpumhz[1]);
	
    $cpuinf = "$cpumodel @ ${cpumhz}MHz"; 
  
  } elsif ($cpu =~/ 486/){
    $cpu =~ /(.*model name.*)/;
    @cpumodel = split/:/,$1;
    $cpumodel = $cpumodel[1];
    $cpumodel =~ s/ //;
		
    $cpu =~ /(.*bogomips.*)/;
    @cpumhz = split/:/,$1;
    $cpumhz = sprintf ("%.f",$cpumhz[1]);
	
    $cpuinf = "$cpumodel @ ${cpumhz}MHz";
  
  } else {
    $cpu =~ /(.*model name.*)/;
    @cpumodel = split/:/,$1;
    $cpumodel = $cpumodel[1];
    $cpumodel =~ s/ //;

    $cpu =~ /(.*cpu MHz.*)/;
    @cpumhz = split/:/,$1;
    $cpumhz = sprintf ("%.f",$cpumhz[1]);
    $cpuinf = "$cpumodel($cpu_temp) @ $cpumhz MHz";
    $cpuinf = "$cpumodel @ ${cpumhz}MHz";
    if ($cache) {$cache="($cache)";}
    if ($cpu_usage) {$cpu_usage="(Activity: $cpu_usage%) ";}
    $final = "[\cBOS\cB]  $distro; Kernel $uname :: [\cBUptime\cB] $uptime :: [\cBCPU\cB] $cpu_cnt-$cpuinf ${cache}${cpu_usage}:: [\cBMemory\cB] $mem_free Used, $mem_total Total ($mem_per% Free) :: [\cBDisk\cB] $hdspacef Free, $hdspacet Total ($hdpercent% Free) :: [\cBNetwork\cB] $eth_name $face ($packet_in In, $packet_out Out)";
  }
} elsif ($switch =~/t/){
  if ($cpu_temp eq "" and $cpu_temp1 eq "" and $cpu_rpm eq "") {
    $final ="[\cBCPU  Temp\cB] No Sensors Found";
} else {
  $final = "[\cBCPU  Temp\cB] $cpu_temp/$cpu_temp1 (fan: $cpu_rpm)";
}

} elsif ($switch =~/v/){
  if ($vid_card eq "" and $screen_res eq "" and $screen_depth eq "") {
    $final = "[\cBVideo\cB]  No Display Found";
  } else {
  $final = "[\cBVideo\cB]  $x running on $vid_card @ $screen_res ($screen_depth bit)";
}

} elsif ($switch =~/n/){
  $final = "[\cBNetwork\cB]  $eth_name $face ($packet_in In, $packet_out Out)";

} elsif ($switch =~/h/){
  $z="/yasis";
  $final = "[\cBcommands\cB]
   $z -a (all)
  $z -c (cpu, also bogomips)
  $z -d (hdd's)
  $z -m (ram)
  $z -n (network)
  $z -o (operating system)
  $z -s (same as full stats (-a) minus video info)
  $z -t (cpu temperature)
  $z -u (uptime, also best uptime)
  $z -v (video/resolution, plus Xorg/Xfree version)";

} elsif ($switch =~/o/){
  $final = "[\cBOS\cB]  $distro; Kernel $uname";

} elsif ($switch =~/c/){
  $cpu = cat("/proc/cpuinfo");
  if ($cpu =~/sparc/){
    $cpu =~ /(.*sparc.*)/;
    @cpumodel = split/:/,$1;
    $cpumodel = $cpumodel[1];
    $cpumodel =~ s/ //;
	
    $cpu =~ /(.*bogomips.*)/;
    @cpumhz = split/:/,$1;
    $cpumhz = sprintf ("%.f",$cpumhz[1]);
	
    $cpuinf = "$cpumodel @ ${cpumhz}MHz"; 

} elsif ($cpu =~/ 486/){
  $cpu =~ /(.*model name.*)/;
  @cpumodel = split/:/,$1;
  $cpumodel = $cpumodel[1];
  $cpumodel =~ s/ //;
	
  $cpu =~ /(.*bogomips.*)/;
  @cpumhz = split/:/,$1;
  $cpumhz = sprintf ("%.f",$cpumhz[1]);
	
  $cpuinf = "$cpumodel @ ${cpumhz}MHz";
} else {
  $cpu =~ /(.*model name.*)/;
  @cpumodel = split/:/,$1;
  $cpumodel = $cpumodel[1];
  $cpumodel =~ s/ //;
	
  $cpu =~ /(.*cpu MHz.*)/;
  @cpumhz = split/:/,$1;
  $cpumhz = sprintf ("%.f",$cpumhz[1]);
  $cpuinf = "$cpumodel @ ${cpumhz}MHz";
  $cpuinf = "$cpumodel @ ${cpumhz}MHz";
  if ($cache && $bogomips) {$cache="($cache/$bogomips bogomips)";} elsif ($cache) {$cache="($cache)";}
    $final="[\cBCPU\cB]  $cpu_cnt-$cpuinf $cache ($cpu_usage% Activity)";
}

} elsif ($switch =~/u/){
  if ($cu > $bu) { system("echo \"$uptime\" > ~/.buptime"); system("echo \"$cu\" > ~/.buptime_uptime"); }
  $buptime = `cat ~/.buptime`; chomp($buptime);
  $final = "[\cBUptime\cB]  $uptime (Best: $buptime)";
} else {
  if ($cu > $bu) { system("echo \"$uptime\" > ~/.buptime"); system("echo \"$cu\" > ~/.buptime_uptime"); }
  $cpu = cat("/proc/cpuinfo");

if ($cpu =~/sparc/){
  $cpu =~ /(.*sparc.*)/;
  @cpumodel = split/:/,$1;
  $cpumodel = $cpumodel[1];
  $cpumodel =~ s/ //;
	
  $cpu =~ /(.*bogomips.*)/;
  @cpumhz = split/:/,$1;
  $cpumhz = sprintf ("%.f",$cpumhz[1]);
	
  $cpuinf = "$cpumodel @ ${cpumhz}MHz"; 

} elsif ($cpu =~/ 486/){
  $cpu =~ /(.*model name.*)/;
  @cpumodel = split/:/,$1;
  $cpumodel = $cpumodel[1];
  $cpumodel =~ s/ //;
		
  $cpu =~ /(.*bogomips.*)/;
  @cpumhz = split/:/,$1;
  $cpumhz = sprintf ("%.f",$cpumhz[1]);
	
  $cpuinf = "$cpumodel @ ${cpumhz}MHz";

} else {
  $cpu =~ /(.*model name.*)/;
  @cpumodel = split/:/,$1;
  $cpumodel = $cpumodel[1];
  $cpumodel =~ s/ //;
	
  $cpu =~ /(.*cpu MHz.*)/;
  @cpumhz = split/:/,$1;
  $cpumhz = sprintf ("%.f",$cpumhz[1]);
  $cpuinf = "$cpumodel @ ${cpumhz}MHz";
}
if ($cache) {$cache="($cache)";}
if ($cpu_usage) {$cpu_usage="(Activity: $cpu_usage%) ";}
   if ($do_display == 1) {
     $final = "[\cBOS\cB]  $distro; Kernel $uname :: [\cBUptime\cB] $uptime :: [\cBCPU\cB] $cpu_cnt-$cpuinf ${cache}${cpu_usage}:: [\cBMemory\cB] $mem_free Used, $mem_total Total ($mem_per% Free) :: [\cBVideo\cB] $vid_card @ $screen_res ($screen_depth bit) :: [\cBDisk\cB] $hdspacef Free, $hdspacet Total ($hdpercent% Free) :: [\cBNetwork\cB] $eth_name $face ($packet_in In, $packet_out Out)";
   } else {
     $final = "[\cBOS\cB]  $distro; Kernel $uname :: [\cBUptime\cB] $uptime :: [\cBCPU\cB] $cpu_cnt-$cpuinf ${cache}${cpu_usage}:: [\cBMemory\cB] $mem_free Used, $mem_total Total ($mem_per% Free) :: [\cBDisk\cB] $hdspaceu Used, $hdspacet Total ($hdpercent% Free) :: [\cBNetwork\cB] $eth_name $face ($packet_in In, $packet_out Out)";
   }	
}


$final =~ s/ //;
return "$final\n";

}

sub cat {
  my @file; 
  open MYFILE, $_[0] or die "$!"; 
  @file = <MYFILE>; 
  close MYFILE; 
  return (wantarray) ? @file : join("",  @file);
}

sub qget_info_cb {
	Xchat::print(get_info())
}

sub get_info_cb {
	my $currChan = Xchat::get_info( 'channel' );
	Xchat::command( "msg $currChan ".get_info() );
}

if ($xchat_plugin) { 
	Xchat::hook_command( "yasis", "get_info_cb"); 
	Xchat::hook_command( "qyasis", "qget_info_cb");
} else { 
	print get_info().'\n'; 
}