[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: [cobalt-users] OT Shell/Perl Script



Here's a perl script I ripped from an X-Chat perl script... it gives
some pretty nice stats.  It's not set up to email anyone, however, this
can be quickly added to a shell script that pipes the output of this
script through 'mail' and sends it to whatever email addy you want... 
This script is pretty nice and I have to give credit to whoever wrote
it.  It even has some lines in there (I've commented them out for my
system) to get info from lm_sensors....  pretty slick.  Anyhow, maybe
this might help you.

#!/usr/bin/perl

$dev = eth0;   ## <-------------------------------CHANGE THAT

    #--UNAME--#
    $uname = `uname -sr`;
    chop($uname);

    #--MODEL--#
    $model = `cat /proc/cpuinfo | grep '^model name' | head -1 | sed -e
's/^.*: //'`;

    #--HOW MANY CPUS--#
    $num=`cat /proc/cpuinfo | grep "model name" | wc -l | sed 's/ *//'`;
    chop ($num);

    #--PROCESSOR SPEED--#
    $cpu = `cat /proc/cpuinfo | grep 'cpu MHz' | head -1 | sed -e
's/^.*: //'`;
    chop ($cpu);

    #--PROCS RUNNING--#
    $procs = `ps ax | wc -l | awk '{print \$1 - 5}'`;
    chop ($procs);

    #--UPTIME--#
    $uptime = `uptime | awk '{ print }'`;
#     @UPTIME = split " ", `up -n`;
#     $UPTIME = $UPTIME[1] ? "$UPTIME[1]y " : "";
#     $UPTIME .= $UPTIME[2] ? "$UPTIME[2]w " : "";
#     $UPTIME .= $UPTIME[3] ? "$UPTIME[3]d " : "";
#     $UPTIME .= $UPTIME[4] ? "$UPTIME[4]h " : "";
#     $UPTIME .= $UPTIME[5] ? "$UPTIME[5]m " : "";
     chop ($uptime);

    #--TOTAL MEMORY--#
    $memtotal = `free | grep Mem | awk '{printf (\"%dMb\", \$2/1000
)}'`;
    chop ($memtotal);

    #--PERCENTAGE OF MEMORY FREE--#
    $mempercent = `free | grep Mem | awk '{print (( \$3 -(\$6 + \$7)
)/\$2)*100}'`;
    chop ($mempercent);

    #--MEMORY FREE--#
    $memfree = `free | grep Mem | awk '{printf (\"%.0fg\", ( \$3 -(\$6 +
\$7) )/1000)}'`;
    chop ($memfree);

    #--BARGRAPH OF MEMORY FREE--#
    $freebar = int($mempercent/10);
    $membar = "[";
    for ( $x = 0; $x < 10; $x++ )
    {
        if ( $x eq $freebar )
                {
                $membar = "$membar*";
                }
        $membar = "$membar\|";
    }
    $membar = "$membar]";



    #--SCREEN RESOLUTION--#
    $res = `xdpyinfo | grep dimensions | awk '{print \$2}'`;
    chop ($res);

    #--DISKSPACE--#
    $hdd = `df | awk '{ sum+=\$2/1024^2 }; END { printf (\"%dGb\", sum
)}'`;
    chop ($hdd);

    #--DISKSPACE FREE--#
    $hddfree = `df | awk '{ sum+=\$4/1024^2 }; END { printf (\"%dGb\",
sum )}'`;
    chop ($hddfree);

    #--BOGOMIPS--#
    $mips = `cat /proc/cpuinfo | grep '^bogomips' | awk '{ sum+=\$3 };
END { print sum }'`;
    chop ($mips);

    #--LM_SENSORS #1--#
#    $SENSOR1 = `sensors -f | grep SYS | awk '{print \$1, \$2, \$3}'`;
#    chop ($SENSOR1);

    #--LM_SENSORS #2--#
#    $SENSOR2 = `sensors -f | grep 'CPU Temp' | awk '{print \$1, \$2,
\$3}'`;
#    chop ($SENSOR2);

    #--PACKETS IN--#
    $packin = `cat /proc/net/dev | grep $dev | awk -F: '/:/ {print \$2}'
| awk '{printf \$1}'`;
    if($packin < 1024**3) { $packin = sprintf("%.02f",$packin /
1024**2)."M"; } else { $packin = sprintf("%.02f", $packin /
1024**3)."G"; }

   #--PACKETS OUT--#
    $packout = `cat /proc/net/dev | grep $dev | awk -F: '/:/ {print
\$2}' | awk '{print \$9}'`;
    if($packout < 1024**3) { $packout = sprintf("%.02f",$packout /
1024**2)."M"; } else { $packout = sprintf("%.02f", $packout /
1024**3)."G"; }

    #--SUPPORT FOR DUAL PROCS--#
    unless($num eq 1 ) { $model="Dual $model"; }
    chop ($model);

    #--SPEW IT INTO THE CHANNEL SHALL WE--#
print "--------------------------------------------\n";
print "SysInfo: $uname | $model $cpu MHz\n";
print "Bogomips: $mips\n";
print "Mem: $memfree/$memtotal $membar\n";
print "Diskspace: $hdd Free: $hddfree\n";
print "Screen Res: $res | Procs: $procs\n";
print "Up: $uptime\n";
print "$dev: In: $packin Out: $packout\n";
print "--------------------------------------------\n";

Here's a sample of the output:

--------------------------------------------
SysInfo: Linux 2.4.19-4GB | Pentium III (Coppermine) 701.595 MHz
Bogomips: 1399.19
Mem: 199/515M [|||*|||||||]
Diskspace: 17G Free: 6G
Screen Res: 1024x768 | Procs: 109
Up:  12:17pm  up  1:52,  3 users,  load average: 0.09, 0.25, 0.38
eth0: In: 19.09M Out: 1.15M
--------------------------------------------

Here's a quick shell script that you can use to make this thing fire the
output to an email address (you'll have to set it up in cron if you want
it to mail regularly. :

#!/bin/sh

/path/to/sysinfo.pl |mail -s "System Stats" youremail@xxxxxxxxxxxxxx


That *should* work.  Hope this is helpful.


On Tue, 2003-02-25 at 20:50, Todd W wrote:
> I am trying to setup and shell/perl script that will email me the state my
> server is in and was wandering if some could help.  Basicly I have the perl
> script done but I am unsure how I can get the output mailed to me.  Here is
> the script
> 
> #!/usr/bin/perl
> 
> $cputemp=`cat /proc/cpuinfo | grep temperature | cut -b 15,16,17,18`;
> 
> $uptime = `/usr/bin/uptime`;
> 
> $disk = `df -h`;
> 
> $mem= `cat /proc/meminfo`;
> 
> $proc = `ps auxc | wc -l`;
> $proc =~ /(\d+)/;
> $procnum = $proc - 3;
> 
> $netstat = `netstat`;
> 
> $netstat3 = `netstat -l`;
> 
> $netstat2 = `netstat --statistics`;
> 
> print $uptime;
> print "\n";
> 
> print 'Number of Processes ';
> print $procnum;
> print "\n";
> print "\n";
> 
> print 'CPU Temp ';
> print $cputemp;
> print "\n";
> 
> print $mem;
> print "\n";
> print "\n";
> 
> print $disk;
> print "\n";
> print "\n";
> 
> print $netstat;
> print "\n";
> print "\n";
> 
> print $netstat2;
> print "\n";
> print "\n";
> 
> print $netstat3;
> print "\n";
> print "\n";
> 
> 
> _____________________________________
> cobalt-users mailing list
> cobalt-users@xxxxxxxxxxxxxxx
> To subscribe/unsubscribe, or to SEARCH THE ARCHIVES, go to:
> http://list.cobalt.com/mailman/listinfo/cobalt-users
-- 
Preston Kutzner | IT Manager


Unfair animal names:

-- tsetse fly			-- bullhead
-- booby			-- duck-billed platypus
-- sapsucker			-- Clarence
		-- Gary Larson