#!/usr/bin/perl
use strict;
use Irssi 20011210.0250 ();
use vars qw($VERSION %IRSSI);
$VERSION = "1.3";
%IRSSI = (
authors => 'David Leadbeater',
contact => '[email protected]',
name => 'sysinfo-dg',
description => 'Adds a /sysinfo command which prints system information (linux only).',
license => 'GNU GPLv2 or later',
url => 'http://irssi.dgl.cx/',
);
#This script is mostly my own work but some ideas where taken from /sinfo by
#Laurens Buhler and Alain van Acker. Please leave this credit in the script and
#if you edit it and think the change is worthwhile tell me and i may add it into
#the script and credit you
use vars qw/$colour $graphs $graphs2 $colour2 $style/;
Irssi::command_bind("sysinfo","sysinfo");
sub sysinfo{
my @options = split(/ /,$_[0]);
my %info;
my($hostname,$uname,$procs) = basicinfo();
my($distro) = distro();
my($uptime,$users,$loadavg) = uptime();
my($memsize,$memfree) = meminfo();
my($swapsize,$swapfree) = swapinfo();
my($cpumodel,$cpumhz,$cpucache,$bogomips) = cpuinfo();
my %netinfo = netinfo();
my($disktotal,$diskused,$hddtype) = df();
my($videocard,$ethernet) = pciinfo();
my($screenres,$screendepth);
($screenres,$screendepth) = screenres() if $ENV{DISPLAY};
($colour,$graphs,$graphs2,$colour2,$style) = parseoptions(\%netinfo,@options);
%info = (
'os' => "$uname - $distro",
'up' => $uptime,
'cpu' => "$cpumodel, $cpumhz MHz ($bogomips bogomips)",
'cache' => $cpucache,
'mem' => ($memsize-$memfree) . "/$memsize MB (" . percent(($memsize-$memfree),$memsize) . ")",
'host' => $hostname,
'users' => $users,
'load' => $loadavg,
'procs' => $procs,
'swap' => ($swapsize-$swapfree) . "/$swapsize MB (" . percent(($swapsize-$swapfree),$swapsize) . ")",
'disk' => "$diskused/$disktotal MB (" . percent($diskused,$disktotal) . ") ($hddtype)",
'video' => "$videocard at $screenres ($screendepth bits)",
'ethernet' => $ethernet,
);
for(keys %netinfo){
$info{$_} = "in: $netinfo{$_}{in} MB, out: $netinfo{$_}{out} MB";
}
my $tmp;
for(split(/ /,$style)){
$tmp .= ircbit($_,$info{$_}) . " ";
}
$tmp =~ s/ $//;
Irssi::active_win()->command('say ' . $tmp);
($colour,$graphs,$graphs2,$colour2,$style) = undef;
}
sub parseoptions{
my($netinfo,@options) = @_;
my $tmp = shift(@options) if $options[0] =~ /^\-/;
$tmp =~ s/^\-//;
for(split //,$tmp){
if($_ eq "c"){
$tmp =~ /c(\d+)/;
$colour = $1;
if(!$colour){
$colour = 3;
}
}elsif($_ eq "g"){
$tmp =~ /g(\d+)/;
$graphs = $1;
if(!$graphs){
$graphs = 9;
}
}elsif($_ eq "G"){
$tmp =~ /G(\d+)/;
$graphs2 = $1;
}elsif($_ eq "C"){
$tmp =~ /C(\d+)/;
$colour2 = $1;
}
}
if(!defined $colour2 && $colour){
$colour2 = 15;
}
if(defined $graphs && !defined $graphs2){
$graphs2 = 3;
}
# We got the names on the command line
if($options[1]){
$style = join(" ",@options);
# style name
}elsif($options[0]){
if($options[0] eq "std"){
$style = "os up cpu mem video";
}elsif($options[0] eq "bigger"){
$style = "os up cpu cache mem load procs disk video";
}elsif($options[0] eq "full"){
$style = "host os up cpu cache mem users load procs swap disk video ethernet ".join(" ",keys %{$netinfo});
}elsif($options[0] eq "net"){
$style = join(" ",keys %{$netinfo});
}elsif($options[0] eq "uptime"){
$style = "os up";
}elsif($options[0] eq "use"){
$style = "mem swap disk";
}
}else{
# no input - default
$style = "os up cpu mem video";
}
return($colour,$graphs,$graphs2,$colour2,$style);
}
sub ircbit{
my($name,$text) = @_;
$name = " " . $name if $name =~ /^\d/;
$text = " " . $text if $text =~ /^\d/;
if($colour){
return "$colour$name$colour2\[$text$colour2\]";
}else{
return "$name\[$text\]";
}
}
sub percent{
my $percent = ($_[1] != 0) ? sprintf("%.1f",(($_[0]/$_[1])*100)) : 0;
if($graphs){
my $tmp = "[";
for(1..10){
if($_ > sprintf("%.0f",$percent / 10)){
$tmp .= "-" if !defined $colour;
$tmp .= "$graphs2-" if defined $colour;
}else{
$tmp .= "|" if !defined $colour;
$tmp .= "$graphs|" if defined $colour;
}
}
$tmp .= "]";
return $percent."% ".$tmp;
}
return $percent."%";
}
sub uptime{
my $uptimeinfo = `uptime`;
if ($uptimeinfo =~ /^\s+(\d+:\d+\w+|\d+:\d+:\d+)\s+up\s+(\d+)\s+day.?\W\s+(\d+):(\d+)\W\s+(\d+)\s+\w+\W\s+\w+\s+\w+\W\s+(\d+).(\d+)/igx) {
return("$2 days, $3 hours, $4 minutes", $5, "$6.$7");
}elsif ($uptimeinfo =~ /^\s+(\d+:\d+\w+|\d+:\d+:\d+)\s+up+\s+(\d+):(\d+)\W\s+(\d+)\s+\w+\W\s+\w+\s+\w+\W\s+(\d+).(\d+)/igx) {
return("$2 hours, $3 minutes", $4, "$5.$6");
}elsif ($uptimeinfo =~ /^\s+(\d+:\d+\w+|\d+:\d+:\d+)\s+up\s+(\d+)\s+day.?\W\s+(\d+)\s+min\W\s+(\d+)\s+\w+\W\s+\w+\s+\w+\W\s+(\d+).(\d+)/igx) {
return("$2 days, $3 minutes", $4, "$5.$6");
}elsif ($uptimeinfo =~ /^\s+(\d+:\d+\w+|\d+:\d+:\d+)\s+up+\s+(\d+)\s+min\W\s+(\d+)\s+\w+\W\s+\w+\s+\w+\W\s+(\d+).(\d+)/igx) {
return("$2 minutes", $3, "$4.$5");
}
return undef;
}
sub meminfo{
my($memsize,$memfree);
open(MEMINFO, "<", "/proc/meminfo") or return undef;
while(