I've modified and made the code a little bit more robust. Allowing you to just configure things instead of having to comment out everything you don't want.
I added the xmms code as posted above by honkeykong. I added code to parse sensors output (for fan speed voltages temps etc). I also made it easy to display any info you want out of xmms-info and proc/cpuinfo by storing all the fields in a hash. Also supports displaying multiple cpus, though i only have 1. Fixed various other warnings etc. The code is a bit longer, but oh well.
DISCLAIMER: I'm not a perl genius. I've written enough scripts to know the basics. I'm sure there are probably better ways of doing than what i've done, but that's perl for you! Let me know if you run into any problems with it, it works on my system. I'm not responible for any damage to your computer due to this code. Always check code before running for the first time.
!/usr/bin/perl -w
# Credits : This script was created by and is currently maintained
# by Victor Igumnov [
[email protected]].
#
# Legal Info : Copyright (C) 2003, Victor Igumnov [
[email protected]]
#
# This program is free software. You may 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 of the License, or
# (at your option) any later version.
#
# This program 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.
#
# This version had been modified by Chris Case <
[email protected]>
$|=1;
use strict;
use CGI;
use URI::URL;
use LWP::UserAgent;
use Text::Wrap;
################ MODIFY THESE SETTINGS FOR YOUR SYSTEM ######################
# set username and password here
my $username = "foo";
my $password = "foo";
# set to 0 if you want to test the script w/o uploading. It will output to
# stdout instead
my $UPLOAD = "1";
# wrap text at 80 columns
$Text::Wrap::columns = 80;
# Set Distro Version
my $DISTRO = "Gentoo";
# Locations to various binaries for queries, use full paths for "better"
#security leave blank to not execute that query
my $UPTIMEBIN = "/usr/bin/uptime";
my $HOSTNAMEBIN = "/bin/hostname";
my $UNAMEBIN = "/bin/uname";
my $SENSORSBIN = "/usr/bin/sensors";
my @SENSORSTEMP = "temp2"; # array of temps for each cpu 0,1,2,3...etc
my @SENSORSFAN = "fan1";
my $FREEBIN = "/usr/bin/free";
#my $XDPYINFOBIN = "/usr/X11R6/bin/xdpyinfo";
my $XDPYINFOBIN;
#my $DFBIN = "/bin/df";
my $DFBIN;
# location to cpu info in proc
my $CPUINFOFILE = "/proc/cpuinfo";
my $XMMSINFOFILE="/tmp/xmms-info";
# other various binaries, you shouldn't need to change these unless yours are
# located somwhere else
my $GREP = "/bin/grep";
my $AWK = "/bin/awk";
my $HEAD = "/bin/head";
#############################################################################
# Get uptime
my $UPTIME;
if ($UPTIMEBIN) {
$UPTIME = `$UPTIMEBIN`;
chomp ($UPTIME);
}
# Get hostname
my $HOSTNAME;
if ($HOSTNAMEBIN) {
$HOSTNAME = `$HOSTNAMEBIN`;
chomp ($HOSTNAME);
}
# Get OS and Version
my $UNAME;
if ($UNAMEBIN) {
$UNAME = `$UNAMEBIN -sr`;
chomp ($UNAME);
}
#Get cpu info
my @CPUINFO; #cpuinfo is an array of hashes
if ( -r $CPUINFOFILE ) {
open(CPUINFO_IN, $CPUINFOFILE) || warn("Warning: $!");
my $proc_no = -1;
#process all the lines
while (<CPUINFO_IN>) {
chomp;
# index array by processor number
if (/^processor\s*:\s*(\d+)$/) {
$proc_no = $1;
}
# only parse lines with data!
elsif (/\S+/){
# split keys and values and clean them up then store into hash
my @fields = split(/:/);
my $key = shift(@fields);
$key =~ s/^\s*(.+?)\s*$/$1/;
my $value = join(":", @fields);
$value =~ s/^\s*(.+?)\s*$/$1/;
$CPUINFO[$proc_no]{$key}=$value;
#print "CPU" . $proc_no . ": " . $key ."=" . $value . "\n";
}
}
close CPUINFO_IN;
}
if ($SENSORSBIN) {
for (my $i = 0; $i < scalar(@CPUINFO); $i++){
$CPUINFO[$i]{'temp'} = `$SENSORSBIN| $GREP $SENSORSTEMP[$i]| $HEAD -n1|$AWK '{print \$2}'`;
$CPUINFO[$i]{'fan'} = `$SENSORSBIN| $GREP $SENSORSFAN[$i]| $HEAD -n1| $AWK '{print \$2}'`;
chomp $CPUINFO[$i]{'temp'};
chomp $CPUINFO[$i]{'fan'};
}
}
# Get Memory info
my %MEMINFO;
if ($FREEBIN) {
#--PERCENTAGE OF MEMORY FREE--#
$MEMINFO{'percent'} = `$FREEBIN|$GREP Mem|$AWK '{print (( \$3 -(\$6 + \$7) )/\$2)*100}'`;
chomp ($MEMINFO{'percent'});
$MEMINFO{'percent'} = sprintf('%.2f', $MEMINFO{'percent'});
#--MEMORY FREE--#
$MEMINFO{'free'} = `$FREEBIN|$GREP Mem |$AWK '{printf (\"%.0f\", ( \$3 -(\$6 + \$7) )/1000)}'`;
chomp ($MEMINFO{'free'});
#--TOTAL MEMORY--#
$MEMINFO{'total'} = `$FREEBIN |$GREP Mem |$AWK '{printf (\"%d\", \$2/1000 )}'`;
chomp ($MEMINFO{'total'});
#--MEMORY USED?--#
$MEMINFO{'used'}=$MEMINFO{'total'}-$MEMINFO{'free'};
}
#--SCREEN RESOLUTION--#
my $RES;
if ($XDPYINFOBIN) {
$RES = `$XDPYINFOBIN| $GREP dimensions| $AWK '{print \$2}'`;
chomp ($RES);
}
#--DISKSPACE--#
my %HDDINFO;
if ($DFBIN) {
$HDDINFO{'total'} = `$DFBIN| $AWK '{ sum+=\$2/1024^2 }; END { printf (\"%d\", sum )}'`;
chomp ($HDDINFO{'total'});
#--DISKSPACE FREE--#
$HDDINFO{'free'} = `$DFBIN| $AWK '{ sum+=\$4/1024^2 }; END { printf (\"%d\", sum )}'`;
chomp ($HDDINFO{'free'});
}
# let's get xmms info
my %XMMSINFO; #xmmsinfo is a hash
# Make sure we can read the file
if ( -r $XMMSINFOFILE ) {
open(XMMSINFO_IN, $XMMSINFOFILE) || warn("Warning: $!");
#process all the lines
while (<XMMSINFO_IN>) {
chomp;
# only parse lines with data!
if (/\S+/){
# split keys and values and clean them up then store into hash
my @fields = split(/:/);
my $key = shift(@fields);
$key =~ s/^\s*(.+?)\s*$/$1/;
my $value = join(":", @fields);
$value =~ s/^\s*(.+?)\s*$/$1/;
$XMMSINFO{$key}=$value;
}
}
close XMMSINFO_IN;
}
my $DATA_2_SEND ="";
$DATA_2_SEND .= "Host: $HOSTNAME is " if ($HOSTNAMEBIN);
$DATA_2_SEND .= "$DISTRO $UNAME" if ($UNAMEBIN);
$DATA_2_SEND .= ", $RES resolution" if ($XDPYINFOBIN);
$DATA_2_SEND .= "\nUptime:$UPTIME" if $UPTIMEBIN;
for (my $i = 0; $i < scalar(@CPUINFO); $i++){
$DATA_2_SEND .= "\nCPU$i: " . $CPUINFO[$i]{'model name'};
$DATA_2_SEND .= " @ " . $CPUINFO[$i]{'cpu MHz'} . "MHz";
if ($CPUINFO[$i]{'temp'}){
$DATA_2_SEND .=", temp: " . $CPUINFO[$i]{'temp'};
$DATA_2_SEND .=" @ " . $CPUINFO[$i]{'fan'} . "RPMs";
}
}
if ($FREEBIN) {
$DATA_2_SEND .= "\nMem: $MEMINFO{'total'}MB total";
$DATA_2_SEND .= ", $MEMINFO{'used'}MB used";
$DATA_2_SEND .= ", $MEMINFO{'free'}MB free";
}
if ($DFBIN) {
$DATA_2_SEND .= ", Diskspace: $HDDINFO{'total'}GB total";
$DATA_2_SEND .= ", $HDDINFO{'free'}GB free";
}
if (%XMMSINFO) {
$DATA_2_SEND .= "\nNow " . $XMMSINFO{'Status'} . ": ";
$DATA_2_SEND .= $XMMSINFO{'Title'};
}
else {
$DATA_2_SEND .= "\nIt's quiet in here.";
}
# this wraps anything that may have gone too far across (like xmms info)
my @lines = wrap("","",$DATA_2_SEND);
$DATA_2_SEND = join('', @lines);
print $DATA_2_SEND . "\n" unless $UPLOAD;
$DATA_2_SEND =~ s/\n/\*\<\*/g;
if ($UPLOAD) {
my $query = new CGI;
$query->import_names('webform');
my %form = ();
%form =('username' => $username,
'password' => $password,
'data' => $DATA_2_SEND,
'Submit' => '');
my $ua = new LWP::UserAgent;
my $curl = url("http:");
my $req = new HTTP::Request 'POST','http://sigx.yuriy.net/update.php';
$req->content_type('application/x-www-form-urlencoded');
$req->content($curl->equery);
$curl->query_form(%form);
$req->content($curl->equery);
my $response= $ua->request($req)->as_string;
}