X-Chat: System Information Script (Linux)

Platform:  Perl
Published  May 13, 2011
Updated  Jul 01, 2012
Usage:
/system <displays all info about the pc> #Sysinfo 1.0 Beta for Linux Xchat by Alex Sorlie
#You may not re-distribute the script without this label.
# Revised by Arcademan!

#--------------------------------------------------------------------------

IRC::register("System Info", "1.0", "System Script", "System Script","");
IRC::print ("Loading System-Info Script!");
IRC::add_command_handler("system", "display_sys_info_");

#--------------------------------------------------------------------------

my $color = 1;
my $maxtext = 500;
my $SMARTDRIVE = "/dev/hda";
my $UNDERL_S = "\037";
my $UNDERL_E = "\037";
my $TITLE_S = "\002";
my $TITLE_E = "\002";
my $ALERT_S = "\002";
my $NORMAL_S = "";
my $ALERT_E = "\002";
my $NORMAL_E = "";

#--------------------------------------------------------------------------

use strict;

BEGIN {
use vars '$xchat';
eval q {
IRC::get_info(1);
};
$xchat = !$@;
}

#--------------------------------------------------------------------------

my $procloc = "/proc";

if ( $color ) {
$ALERT_E = "\003";
$NORMAL_E = "\003";
$ALERT_S = "\0034";
$NORMAL_S = "\0033";
}

sub file_executable
{
my $command = shift;
my @directories=split(/:/, $ENV{'PATH'} );
for (@directories) {
if ( -x "$_/$command" ) { return 1; }
}
return 0;
}

# All available modules.
#--------------------------------------------------------------------------

my @modules = qw /
distro
cpu
mem
hddtemp
diskspace
uptime
load
procs
graphic
screen
nvidia
netinfo
sensors
/;

#--------------------------------------------------------------------------

sub distro {
my $distro = `cat /etc/issue`;
chomp($distro);
chop($distro);
chomp($distro);
chop($distro);
chomp($distro);
chop($distro);
chomp($distro);
chop($distro);
chomp($distro);
chop($distro);
chomp($distro);
chop($distro);
return "${TITLE_S}Distro: \00304 ${TITLE_E} \00304 ${distro} - ";
}

#--------------------------------------------------------------------------

sub cpu
{
my $atsign = chr(64);
my $NUM = 0;
my $MIPS = 0;
my ($MODEL,$SYSTEM);
my $CPU = 0;

open (X, "${procloc}/cpuinfo");
while (<X>) {
if (/^(cpu model|model name).*: (.*)\n$/) {
$MODEL = $2;
$NUM += 1;
} elsif (/^system type.*: (.*)\n$/) {
$SYSTEM = $1;
} elsif (!$CPU && /^cpu MHz.*: (.*)\n$/) {
$CPU = $1;
} elsif (/^bogomips.*: (.*)\n$/i) {
$MIPS += $1;
}
}
close (X);

if ($NUM == 2 ) { $MODEL="Dual $MODEL"; }
if ($NUM == 4 ) { $MODEL="Quad $MODEL"; }

$MODEL = $SYSTEM . " " . $MODEL;
$MIPS = $MIPS / 1024;
$MIPS = sprintf("%.2f", $MIPS);
return "${TITLE_S}CPU: \00306 ${TITLE_E}$MODEL - \003";
}

#--------------------------------------------------------------------------

sub mem
{
my($MEMTOTAL,$MEMFREE);
open(X, "${procloc}/meminfo") or $MEMTOTAL = 1;
while(<X>){
chomp;
if(/^MemTotal:\s+(\d+)/){
$MEMTOTAL = sprintf("%.0f",$1/1024);
} elsif(/^MemFree:\s+(\d+)/){
$MEMFREE = sprintf("%.0f",$1/1024);
} elsif(/^Buffers:\s+(\d+)/){
$MEMFREE += sprintf("%.0f",$1/1024);
} elsif(/^Cached:\s+(\d+)/){
$MEMFREE += sprintf("%.0f",$1/1024);
}
}

close(X);

#--PERCENTAGE OF MEMORY FREE--#
my $MEMPERCENT = sprintf("%.1f",(100/${MEMTOTAL}*${MEMFREE}));

#--BARGRAPH OF MEMORY FREE--#
my $FREEBAR = int(${MEMPERCENT}/10);
my $MEMBAR;
my $x;

$MEMBAR = "${TITLE_S}\[${NORMAL_S}";

for ($x = 0;$x < 10; $x++) {
if ( $x == $FREEBAR ) { $MEMBAR .= "${ALERT_S}"; }
$MEMBAR .= "\|";
}
$MEMBAR .= "${ALERT_E}]${TITLE_E}";
my $MEMUSED = $MEMTOTAL - $MEMFREE;
return "${TITLE_S}Memory: \00307 ${TITLE_E} ${MEMUSED}/${MEMTOTAL}M $MEMBAR - \003";
}

#--------------------------------------------------------------------------

sub diskspace
{
my $HDD = 0;
my $HDDFREE = 0;
my $SCSI = 0;
my $SCSIFREE = 0;

for (`df 2>/dev/null`) {
if (/^\/dev\/(ida\/c[0-9]d[0-9]p[0-9]|[sh]d[a-z][0-9]+)\s+(\d+)\s+\d+\s+(\d+)\s+\d+%/) {
$HDD += $2;
$HDDFREE += $3;
}
if (/^\/dev\/(ida\/c[0-9]d[0-9]p[0-9]|sd[a-z][0-9]+)\s+(\d+)\s+\d+\s+(\d+)\s+\d+%/) {
$SCSI += $2;
$SCSIFREE += $3;
}
}

my $ALL = $HDD;
$HDDFREE = sprintf("%.02f", $HDDFREE / 1048576)."G";
$HDD = sprintf("%.02f", $HDD / 1048576)."G";
return "${TITLE_S}Storage: \00308 ${TITLE_E} $HDD ${TITLE_S}Free:${TITLE_E} $HDDFREE - \003";
}

#--------------------------------------------------------------------------

sub procs
{
opendir(PROC, "${procloc}");
my $PROCS = scalar grep(/^\d/,readdir PROC);
return "${TITLE_S}Processes: \00309 ${TITLE_E} $PROCS - \003";
}

#--------------------------------------------------------------------------

sub uptime
{
#--UPTIME--#
open (X, "${procloc}/uptime");
my $up2 = "";
my $uptime = <X>;
close (X);
$uptime =~ s/(\d+\.\d+)\s\d+\.\d+/$1/;

my $years = sprintf ("%.d",$uptime/31536000) || 0;
my $yearbase = sprintf ($years * 31536000);
my $weeks = sprintf ("%.d",($uptime - $yearbase)/6048000) || 0;
my $weekbase = sprintf ($weeks * 6048000);
my $days = sprintf ("%.d",($uptime - $yearbase - $weekbase)/86400) || 0;
my $daybase = sprintf ($days * 86400);
my $hours = sprintf ("%.d",($uptime - $yearbase - $weekbase - $daybase)/3600) || 0;
my $hourbase = sprintf ($hours * 3600);
my $mins = sprintf ("%.d",($uptime - $yearbase - $weekbase - $daybase - $hourbase)/60) || 0;
my $minbase = sprintf ($mins * 60);
my $secs = sprintf ("%.d",($uptime - $yearbase - $weekbase - $daybase - $hourbase - $minbase)) ||0;

if ($years){$up2 .= $years =~ /^1$/ ? "$years yr " : "$years yrs ";}
if ($weeks){$up2 .= $weeks =~ /^1$/ ? "$weeks wk " : "$weeks wks ";}
if ($days){$up2 .= $days =~ /^1$/ ? "$days day " : "$days days ";}
if ($hours){$up2 .= $hours =~ /^1$/ ? "$hours hr " : "$hours hrs ";}
if ($mins){$up2 .= $mins =~/^[01]$/ ? "$mins min " : "$mins mins ";}

$up2 .= $secs =~ /^[01]$/ ? "$secs sec" : "$secs secs";
return "${TITLE_S}Uptime: \00310 ${TITLE_E} $up2 - \003";
}

#--------------------------------------------------------------------------

sub load()
{
if (open (X, "${procloc}/loadavg")) {
my $LOADAVG = <X>;
close (X);
$LOADAVG =~ s/^((\d+\.\d+\s){3}).*\n$/$1/;
return "${TITLE_S}Load Average: \00311 ${TITLE_E} $LOADAVG - \003";
}
}

#--------------------------------------------------------------------------

sub graphic()
{
#--GRAPHICSCARD--#

my $VGA = "unknown";
if (file_executable("lspci") == 1) {
for (`lspci 2>/dev/null`) {
if (/VGA compatible controller:\s(.*)$/) { $VGA = $1; }
}
}
elsif ( -e "${procloc}/pci" ) {
open(X, "${procloc}/pci") ;
while(<X>){
chomp;
if (/VGA compatible controller:\s(.*)\.$/){
$VGA = $1; }
}
close(X);
}

return "${TITLE_S}Graphic: \00312 ${TITLE_E} ${VGA} \003";
}

#--------------------------------------------------------------------------

sub sensors
{
my $SENSOR1 = "NA";
my $SENSOR2 = "NA";
my $SENSOR3 = "NA";
my $SENSOR4 = "NA";
if ( -e "${procloc}/sys/dev/sensors" && file_executable("sensors") == 1) {
for (`sensors 2>/dev/null`){
if (/^[tT]emp2.*:\s+(.*(\s|.|)[FC])\s+\(.*\)(\s+ALARM)?/) {
if (!$2) { $SENSOR1 = "${NORMAL_S} $1${NORMAL_E}"; }
else { $SENSOR1 = "${ALERT_S} $1${ALERT_E}"; }
} elsif (/^[tT]emp1.*:\s+(.*(\s|.|)[FC])\s+\(.*\)(\s+ALARM)?/) {
if (!$2) { $SENSOR2 = "${NORMAL_S} $1${NORMAL_E}"; }
else { $SENSOR2 = "${ALERT_S} $1${ALERT_E}"; }
} elsif (/^fan1:\s+(\d+\sRPM)\s+\(.*\)(\s+ALARM)?/) {
if (!$2) { $SENSOR3 = "${NORMAL_S} $1${NORMAL_E}"; }
else { $SENSOR3 = "${ALERT_S} $1${ALERT_E}"; }
} elsif (/^fan2:\s+(\d+\sRPM)\s+\(.*\)(\s+ALARM)?/) {
if (!$2) { $SENSOR4 = "${NORMAL_S} $1${NORMAL_E}"; }
else { $SENSOR4 = "${ALERT_S} $1${ALERT_E}"; }
}
}

return "${TITLE_S}CPU:${TITLE_E}$SENSOR1 ${TITLE_S}Fan:${TITLE_E}$SENSOR3 ${TITLE_S}Case:${TITLE_E}$SENSOR2 ${TITLE_S}Fan:${TITLE_E}$SENSOR4 ";
}

#--------------------------------------------------------------------------

sub hddtemp
{
my $HDDTEMP;
if ( file_executable("hddtemp") == 1) {
for (`hddtemp $SMARTDRIVE 2>/dev/null`) {
if (/^\/dev\/[sh]da:\s+(.*):\s+(.*)$/) {
$HDDTEMP = $2;
}
}
return "${TITLE_S}HDD:${TITLE_E} $HDDTEMP ";
}
}

#--------------------------------------------------------------------------

sub display_sys_info_ {
my $args = shift;
my $a = 0;
my $temp;
my @lines;
my $count = 0;
my $title = "\00304\002System Info\002 \003";
my $output;

if ($args =~ m/-h/) {
return 1;
}

$lines[$count] = $title;

foreach my $arg (@modules) {
if ($args eq "" || $args =~ m/^-p$/ || $args =~ m/$arg/) {
$a = 1;
$temp = eval $arg;
$count++ if length ($lines[$count]) > $maxtext;
$lines[$count] .= $temp;
}
}

if ($a == 0) {
return 1;
}

$output = 1 if ($args !~ m/-p/);
if ($xchat && $lines[0] ne $title) {
if ($output) {
IRC::command("$_") for @lines;
} else {
IRC::command ("$_") for @lines;
}
}

return 1;

}
}

#--------------------------------------------------------------------------

Comments

Sign in to comment.
cmrocks   -  Jul 01, 2012
SpotsBox, Yes you can modify to show free/used memory. I made the change to show used memory since so many of you requested it that way. I may soon split the code to some subroutines.
 Respond  
SpotsBox   -  Feb 07, 2012
i really like the script! Thanks! if this script can be modified can i get a list of commands to add to the script like temp and also why does it show all memory in use when its not?
 Respond  
cmrocks   -  Sep 26, 2011
I will create a separate script for Windows since it does not do bash very well :)

Script is only for Linux! :)
 Respond  
GeekShedboy123   -  Jul 28, 2011
lol well im on linux now.. windows went bye bye for a wile ._. so...
 Respond  
cmrocks   -  Jul 24, 2011
Well, It be under your C:\Documents and Settings\username\Application Data\X-Chat 2 folder.
I am not sure if it would take off right away since you would be missing Perl, I only use Linux someone might know how to get Perl on Windows via xchat :).
http://www.perl.org/get.html -
 Respond  
GeekShedboy123   -  Jul 23, 2011
Im on Windows.
 Respond  
cmrocks   -  Jul 19, 2011
GeekShedboy123, Assuming your on linux place it in this directory:

/home/<username>/.xchat2/

The folder is hidden by default. Reload x-chat :)
 Respond  
GeekShedboy123   -  Jul 17, 2011
I am a bet of a noob when it comes to scrips in XCHAT. Where and how do i put this?
 Respond  
napa182   -  May 14, 2011
Well to be fair cmrocks RJosh, does have a point. Your description is lacking, you should be a lil more in def about what ur snippets do rather then just stating "Usage: /system <displays all info about the pc>"
 Respond  
cmrocks   -  May 13, 2011
RJosh its does not do a thing except repeat, its in the original as well. Should look at things more carefully. Updated Success!

:)
 Respond  
RJosh   -  May 13, 2011
Pretty sure that's what the for loop is for, to loop and collect the storage space and the used space of each HDD attached to the system. But if it's really needed an explanation would be nice. I just find it awkward that there are two of the same. Thanks.
 Respond  
SunnyD   -  May 13, 2011
Looks like you have been busy, cmrocks.
 Respond  
Jethro   -  May 13, 2011
If you post a comment under this thread, it'll be made "up-to-date" as being the latest. :p

P.S. I've just made it happen.
 Respond  
napa182   -  May 13, 2011
Why do you flood the comment section by making comments saying "Feel free to leave comments :) " on all ur snippets you submit? I just find that a bit odd thats all.
 Respond  
cmrocks   -  May 13, 2011
Feel free to leave comments :)
 Respond  
Are you sure you want to unfollow this person?
Are you sure you want to delete this?
Click "Unsubscribe" to stop receiving notices pertaining to this post.
Click "Subscribe" to resume notices pertaining to this post.