#! /usr/bin/perl # Copyright (C) 2002 Stéphane Levant # This file is free software; you can redistribute it and/or modify # it under the terms of the GNU General Public License as published by # the Free Software Foundation; either version 2, or (at your option) # any later version. # This file is distributed in the hope that it will be useful, # but WITHOUT ANY WARRANTY; without even the implied warranty of # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the # GNU General Public License for more details. # You should have received a copy of the GNU General Public License # along with GNU Emacs; see the file COPYING. If not, write to # the Free Software Foundation, Inc., 59 Temple Place - Suite 330, # Boston, MA 02111-1307, USA. # TODO : # xrandr # lshw ? # /proc... # Netstat, ftpwho... # option pour ppp0, eth0 # option pour irc # ping -c 1 www.joystick.com # /proc/acpi/thermal_zone/THRM/temperature # powertweak use strict; our $version = 1; our %DISTRIB = ("coas" => "", "environment.corel" => "", "debian_version" => "Debian ", "mandrake-release" => "", "SuSE-release" => "", "turbolinux-release" => "", "slackware-version" => "Slack ", "redhat-release" => ""); our $irc = 1; sub percent{ my ($used, $tot, $type) = @_; return sprintf("%i/%i %s (%i%%)",$used,$tot,$type,($used/$tot*100)); } sub firstline { my ($file) = @_; open (FILE, "$file"); chop (my $line = ); close (FILE); return $line; } sub fgrep { my ($regexp, $file) = @_; my $line; open (FILE, "$file"); while () { if (/$regexp/) { $line = $_; chop($line); } } close (FILE); return $line; } sub p { my $txt = @_[0]; if ($irc) { return sprintf ('7[%s7] ', $txt) } else { return sprintf ("%s\n", $txt); } } sub kernel { split " ", firstline ("/proc/version"); return "kernel @_[2]"; } sub distrib { foreach my $key (keys %DISTRIB) { my $file = "/etc/" . $key; if (-f $file) { return @DISTRIB{$key} . firstline($file); } } return ""; } sub free { my ($mem, $mem_used, $swap, $swap_used); for(`free`) { chomp; split; if (/^Mem/) { $mem = @_[1]/1024; } elsif(/-/) { $mem_used = @_[2]/1024; } elsif(/Swap/) { $swap = @_[1]/1024; $swap_used = @_[2]/1024; } } return sprintf ("RAM: %s Swap: %s", percent($mem_used, $mem, "MB"), percent($swap_used, $swap, "MB")); } sub df { my($tot, $used); for(`df`) { chomp; next if !/^\/dev\/\S+/; next if /(cd|cdrom|fd|floppy|grav)/; split; $tot += @_[1] /1024/1024; $used += @_[2] /1024/1024; } return "HD: " . percent ($used, $tot, "Go"); } sub cpu { my ($cpu, $smp, $mhz, $cache, $bmps); open(CPUINFO, "/proc/cpuinfo"); while() { if(/^model name\s+\:\s+(.+)$/) { if (defined $cpu) { if (defined $smp) { $smp++; }else{ $smp=2; } }else { $cpu = $1; $cpu =~ s/\([^\)]*\)//g; $cpu =~ s/ ?(processor|CPU)//; } } elsif (/^cpu MHz\s+:\s+([\d\.]+)/) { $mhz = $1; } elsif (/^cache size\s+:\s+(.+)/) { $cache = $1; $cache =~ s/ //g; } elsif (/^bogomips\s+:\s+([\d\.]+)/) { $bmps += $1; } } $cpu .= " SMP ($smp proc)" if defined $smp; return sprintf ("%s%s %s %ibmps", $cpu, $cpu =~ /Hz/ ? "" : sprintf (" %iMHz", $mhz), $cache, $bmps); } sub uptime { my ($day, $hour); open(UPTIME, "/proc/uptime"); while() { split; $hour = @_[0]/3600; $day = int ($hour/24); } return sprintf("up: %id %ih", $day, $hour-$day*24); } sub load { my $users = 0; for (`who`) { $users++; } my $load = firstline ("/proc/loadavg"); $load =~ /([^ ]+) ([^ ]+) ([^ ]+) \d+\/([^ ]+)/; my $ftp; for (`ps x`) { $ftp++ if (/ftp/); } return sprintf ("Load: %s %s %s process: %s ftp: %i users: %i", $1, $2, $3, $4, $ftp, $users); } sub netstat { my $i; for (`netstat`) { $i++ if (/^(udp|tcp)/ && ! /localhost/); } return sprintf ("Connexion: %i\n", $i); } sub hddtemp { my %hdd; chdir "/proc/ide"; for my $i () { for my $j () { open (HDD, "$i/$j/driver"); while () { @hdd{"/dev/$j"} = 1 if /ide-disk/; } close (HDD); } } my $result; for my $hd (keys %hdd) { for (`hddtemp $hd 2>/dev/null`) { if (!(/WARNING/ || /sensor/)) { if ($irc) { chop; if (/\/dev\/(\S+):.* (\d\d)°C/) { $result = $result . " $1:$2°C" ; } } else { $result = $result . $_; } } } } return $result; } sub nvidia { my ($mem, $core, $name); open (PCI, "/proc/pci"); while () { $name = $1 if /VGA.*nVidia.*NV\d+\s+\(([^\)]+)\)/; } for (`nvclock -s 2>/dev/null`) { $mem = $1 if (/^Current memory speed:\s+(\S+)\sMHz$/); $core = $1 if (/^Current core speed:\s+(\S+)\sMHz$/); } if (defined $mem) { return sprintf (" %s core: %iMHz mem: %iMHz", $name, $core, $mem); } return ""; } sub sensors { my ($cpu, $sys, $fan, $core); for (`sensors 2>/dev/null`) { if (/^CPU Temp:\s+\+(.*°[FC])\s+/) { $cpu = $1; } elsif (/^temp1:\s+\+(.*°[FC])\s+/) { $sys = $1; } elsif (/^temp2:\s+\+(.*°[FC])\s+/) { $cpu = $1; } elsif (/^SYS Temp:\s+\+(.*°[FC])\s+/) { $sys = $1; } elsif (/^fan2:\s+(\d+)\sRPM\s+/) { $fan = $1; } elsif (/^CPU Fan:\s+(\d+)\sRPM\s+/) { $fan = $1; } elsif (/^VCore 1:\s+\+(.*)\sV\s+/) { $core = $1; } elsif (/^CPU core:\s+\+(.*)\sV\s+/) { $core = $1; } } return defined $cpu ? " $cpu ${fan}RPM ${core}V sys: $sys" : ""; } sub screen { my ($dim, $depth, $freq); for (`xdpyinfo`) { if(/\s+dimensions:\s+(\S+)/) { $dim = $1; } elsif (/\s+depth:\s+(\S+)/) { $depth = $1; } } if (-f "/var/log/XFree86.0.log") { open (XFREE, "/var/log/XFree86.0.log"); # my $founded = 0; $freq = 0; while () { if (/Default mode/) { # if (/$dim/ && !$founded) { if (/$dim/) { split; if (@_[9] > $freq) { $freq = @_[9]; # $founded = 1; } } } } close (XFREE); } return sprintf("%s %ibpp%s", $dim, $depth, defined $freq ? sprintf (" %iHz", $freq) : ""); } sub readnet { my $line = fgrep ("ppp0", "/proc/net/dev"); $line =~ s/ppp0://; split (" ", $line); return (@_[0], @_[8]); } sub net { my ($dl1, $up1) = readnet (); sleep 1; my ($dl2, $up2) = readnet (); return sprintf ("dl/ul: %i %i (KB/s)", ($dl2-$dl1)/1024, ($up2-$up1)/1024); } print ((p distrib . " " . kernel) . (p load . " " . uptime) . (p free) . (p df . hddtemp) . (p cpu . sensors) . (p screen . nvidia) . (p net) . ($irc ? "\n" : ""));