#!/usr/bin/perl -w
#
# Copyright (c) 2002, 2003 David Rudie
# All rights reserved.
#
# 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.
#
# $ident: sysinfo277-irssi.pl,v 2.77 2003/09/01 04:20:02 drudie Exp $
#
use strict;
use vars qw($VERSION %IRSSI);
$VERSION = '2.79';
%IRSSI = (
authors => 'David Rudie',
contact => '[email protected]',
name => 'SysInfo',
description => 'Cross-platform/architecture system information script.',
license => 'BSD',
url => 'http://www.inexistent.com/',
changed => '2019-11-08',
bugs => 'Probably some if it cannot read /proc.'
);
use Irssi;
use POSIX qw(floor);
# Set up the arrays and variables first.
use vars qw(
@arr
@arr1
@arr2
$cpu
@cpu
@cpuinfo
$data
@data
$df
@dmesgboot
@hinv
@meminfo
$mhz
@mhz
$model
@netdev
@netstat
@nic
@nicname
$smp
@smp
$stream
$sysctl
@uptime
$var
$vara
$varb
$varc
$vard
$varh
$varm
$varp
$varx
$vary
$varz
);
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 $darwin = 1 if $os =~ /^Darwin$/;
my $freebsd = 1 if $os =~ /^FreeBSD$/;
my $linux = 1 if $os =~ /^Linux$/;
my $netbsd = 1 if $os =~ /^NetBSD$/;
my $openbsd = 1 if $os =~ /^OpenBSD$/;
my $irix = 1 if $os =~ /^IRIX$/;
my $irix64 = 1 if $os =~ /^IRIX64$/;
my $alpha = 1 if $osm =~ /^alpha$/;
my $armv4l = 1 if $osm =~ /^armv4l$/;
my $armv5l = 1 if $osm =~ /^armv5l$/;
my $armv7l = 1 if $osm =~ /^armv7l$/;
my $i586 = 1 if $osm =~ /^i586$/;
my $i686 = 1 if $osm =~ /^i686$/;
my $ia64 = 1 if $osm =~ /^ia64$/;
my $mips = 1 if $osm =~ /^mips$/;
my $parisc64 = 1 if $osm =~ /^parisc64$/;
my $ppc = 1 if $osm =~ /^ppc$/;
my $x86_64 = 1 if $osm =~ /^x86_64$/;
# linux => 2.6
$osv =~ m/^(\d+\.\d+)/;
my $l26 = 1 if 2.6 <=$1;
sub cmd_sysinfo {
# Specify your NIC interface name (eth0, rl0, fxp0, etc) and a name for it.
#
# Example: @nic = ('eth0', 'eth1');
# @nicname = ('External', 'Internal');
#
# NOTE: If you set one then you HAVE to set the other.
@nic = split(/;/, Irssi::settings_get_str('sysinfo_nics'));
@nicname = split(/;/, Irssi::settings_get_str('sysinfo_nicnames'));
# These are the default settings for which information gets displayed.
# 0 = Off; 1 = On
my $showHostname = 1;
my $showOS = 1;
my $showCPU = 1;
my $showProcesses = 1;
my $showUptime = 1;
my $showLoadAverage = 1;
my $showBattery = 0;
my $showMemoryUsage = 1;
my $showDiskUsage = 1;
my $showNetworkTraffic= 1;
###############################################
### Nothing below here should need changed. ###
###############################################
if($linux) {
@cpuinfo = &openfile("/proc/cpuinfo");
@meminfo = &openfile("/proc/meminfo");
@netdev = &openfile("/proc/net/dev");
@uptime = &openfile("/proc/uptime");
} elsif($irix || $irix64) {
@hinv = `hinv`;
} else {
@dmesgboot = &openfile("/var/run/dmesg.boot");
@netstat = `netstat -ibn`;
if($darwin) {
$sysctl = '/usr/sbin/sysctl';
} else {
$sysctl = '/sbin/sysctl';
}
}
if($armv4l || $armv5l || $armv7l) {
$df = 'df -k';
} else {
$df = 'df -lk';
}
if($showCPU) {
if($freebsd) {
if($alpha) {
@cpu = grep(/^COMPAQ/, @dmesgboot);
$cpu = join("\n", $cpu[0]);
} else {
@cpu = grep(/CPU: /, @dmesgboot);
$cpu = join("\n", @cpu);
@cpu = split(/: /, $cpu);
$cpu = $cpu[1];
@smp = grep(/ cpu/, @dmesgboot);
$smp = scalar @smp;
}
}
if($netbsd) {
if($alpha) {
@cpu = grep(/^COMPAQ/, @dmesgboot);
$cpu = join("\n", $cpu[0]);
@cpu = split(/, /, $cpu);
$cpu = $cpu[0];
} else {
@cpu = grep(/cpu0: /, @dmesgboot);
@cpu = grep(!/apic/, @cpu);
$cpu = join("\n", $cpu[0]);
@cpu = split(/: /, $cpu);
$cpu = $cpu[1];
@smp = grep(/cpu\d+:/, @dmesgboot);
@smp = grep(/MHz/, @smp);
$smp = scalar @smp;
}
}
if($openbsd) {
@cpu = grep(/cpu0: /, @dmesgboot);
@cpu = grep(/[M|G]Hz/, @cpu);
$cpu = join("\n", @cpu);
@cpu = split(/: /, $cpu);
$cpu = $cpu[1];
}
if($irix || $irix64) {
@cpu = grep(/CPU:/, @hinv);
$cpu = join("\n", @cpu);
$cpu =~ s/^.*(R[0-9]*) .*$/$1/;
@mhz = grep(/MHZ/, @hinv);
$mhz = join("\n", @mhz);
$mhz = $mhz[0];
$mhz =~ s/^.* ([0-9]*) MHZ.*$/$1/;
@smp = grep(/ IP/, @hinv);
$smp = scalar @smp;
chop($cpu);
chop($mhz);
$cpu = "MIPS $cpu ($mhz MHz)";
}
if($linux) {
if($alpha) {
$cpu = &cpuinfo("cpu\\s+: ");
$model = &cpuinfo("cpu model\\s+: ");
$cpu = "$cpu $model";
$smp = &cpuinfo("cpus detected\\s+: ");
}
if($armv4l || $armv5l) {
$cpu = &cpuinfo("Processor\\s+: ");
}
if($armv7l) {
$cpu = &cpuinfo("model name\\s+: ");
}
if($i686 || $i586 || $x86_64) {
$cpu = &cpuinfo("model name\\s+: ");
$cpu =~ s/(.+) CPU family\t+\d+MHz/$1/g;
$cpu =~ s/(.+) CPU .+GHz/$1/g;
$mhz = &cpuinfo("cpu MHz\\s+: ");
$cpu = "$cpu ($mhz MHz)";
@smp = grep(/processor\s+: /, @cpuinfo);
$smp = scalar @smp;
}
if($ia64) {
$cpu = &cpuinfo("vendor\\s+: ");
$model = &cpuinfo("family\\s+: ");
$mhz = &cpuinfo("cpu MHz\\s+: ");
$mhz = sprintf("%.2f", $mhz);
$cpu = "$cpu $model ($mhz MHz)";
@smp = grep(/processor\s+: /, @cpuinfo);
$smp = scalar @smp;
}
if($mips) {
$cpu = &cpuinfo("cpu\\s+: ");
$model = &cpuinfo("cpu model\\s+: ");
$cpu = "$cpu $model";
}
if($parisc64) {
$cpu = &cpuinfo("cpu\\s+: ");
$model = &cpuinfo("model name\\s+: ");
$mhz = &cpuinfo("cpu MHz\\s+: ");
$mhz = sprintf("%.2f", $mhz);
$cpu = "$model $cpu ($mhz MHz)";
}
if($ppc) {
$cpu = &cpuinfo("cpu\\s+: ");
$mhz = &cpuinfo("clock\\s+: ");
if($cpu =~ /^9.+/) {
$model = "IBM PowerPC G5";
} elsif($cpu =~ /^74.+/) {
$model = "Motorola PowerPC G4";
} else {
$model = "IBM PowerPC G3";
}
$cpu = "$model $cpu ($mhz)";
}
} elsif($darwin) {
$cpu = `hostinfo | grep 'Processor type' | cut -f2 -d':'`; chomp($cpu);
$cpu =~ s/^\s*(.+)\s*$/$1/g;
if($cpu =~ /^ppc7.+/) {
$cpu = "Motorola PowerPC G4";
}
$mhz = `$sysctl -n hw.cpufrequency`; chomp($mhz);
$mhz = sprintf("%.2f", $mhz / 1000000);
$cpu = "$cpu ($mhz MHz)";
$smp = `hostinfo | grep "physically available" | cut -f1 -d' '`; chomp($smp);
}
if($smp && $smp gt 1) {
$cpu = "$smp x $cpu";
}
}
my $output;
if($showHostname) { $output = "Hostname: $osn - "; }
if($showOS) { $output .= "OS: $uname - "; }
if($showCPU) { $output .= "CPU: $cpu - "; }
if($showProcesses) { $output .= "Processes: ".&processes." - "; }
if($showUptime) { $output .= "Uptime: ".&uptime." - "; }
if($showLoadAverage) { $output .= "Load Average: ".&loadaverage." - "; }
if($showBattery) { $output .= "Battery: ".&battery." - "; }
if($showMemoryUsage) { $output .= "Memory Usage: ".&memoryusage." - "; }
if($showDiskUsage) { $output .= "Disk Usage: ".&diskusage." - "; }
if($showNetworkTraffic) { $output .= &networktraffic; }
$output =~ s/ - $//g;
Irssi::active_win()->command("/ $output");
return 1;
}
sub battery {
$data = "";
if(open(FD, "<", '/proc/apm')) {
while($stream =