Operating System - Linux
1828227 Members
3099 Online
109975 Solutions
New Discussion

Scripting CPU and memory usage

 
SOLVED
Go to solution
TheJuiceman
Super Advisor

Scripting CPU and memory usage

Hey gang,

Does anyone have a nice universal script (meaning no special software needed like glance, etc) that will give a nice quick summary of percentage of memory and CPU usage that can be emailed? Thanks.
6 REPLIES 6
Mel Burslan
Honored Contributor

Re: Scripting CPU and memory usage

top is available with any new system installation and command

$ top -n 1 | grep -e ^Cpu -e ^Mem

provides this output:

Cpu(s): 0.2% us, 0.7% sy, 0.0% ni, 98.6% id, 0.2% wa, 0.0% hi, 0.3% si
Mem: 1024248k total, 990548k used, 33700k free, 130372k buffers

which can be piped into your mail agent to send via email or can be further massaged with combination of awk/sed/cut commands to make the output you wish to see.

Hope this helps
________________________________
UNIX because I majored in cryptology...
TheJuiceman
Super Advisor

Re: Scripting CPU and memory usage

I guess I'm looking for something a bit more than that. Something yielding a percentage used in a neat output like so...

CPU used: 23%
Memory used: 23%

Vitaly Karasik_1
Honored Contributor

Re: Scripting CPU and memory usage

Mel answered your question, I want just to suggest you to use some NMS (Zenoss, Ganglia, ...) for storing/analyzing historical data.

Getting email reports once per minute/hour/day seems me almost useless - you don't see any trends in this way.
Mel Burslan
Honored Contributor
Solution

Re: Scripting CPU and memory usage

allrighty then..

#!/bin/bash

cpuidle=`top -n 1 | grep ^Cpu | cut -d"," -f4| cut -d"%" -f1|cut -d" " -f2`
cpuused=`echo 100-$cpuidle | bc`

memtotal=`top -n 1 | grep ^Mem |awk {'print $3'}|cut -d"k" -f1`
memused=`top -n 1 | grep ^Mem |awk {'print $5'}|cut -d"k" -f1`
mempct=`echo "(${memused}*100)/${memtotal}"| bc`

echo "CPU Used: ${cpuused}%"
echo "Memory Used: ${mempct}%")



If you putt all of the above in a script, run it and pipe it to your mailer, you will receive the desired output
________________________________
UNIX because I majored in cryptology...
Mel Burslan
Honored Contributor

Re: Scripting CPU and memory usage

Oh, I fatfingered and put an extra right paranthesis at the end of the last line of the script, which should not be there. Please remove it.
________________________________
UNIX because I majored in cryptology...
TheJuiceman
Super Advisor

Re: Scripting CPU and memory usage

Mel...that is PERFECT!!!! Thank you so much for your help!!!