Coder Profile - Show off your skills, get a coder profile.
 
 
 
The Author
Tommey
Tamás Tóth
Send A Message
Screenshot
View Full Size
checkmemoryusage.p... ( 0.01Mb )
Rating
Not
Rated
Please login to rate source codes.

Click here to register a free account with us.
General Details
Bash
Posted 3.15 Years Ago
516 Views
Received 0 Ratings
More Codes By This Author
Check memory usage

Check memory usage


Description
This is a bash script that i made when i was bored on a saturday night.

There were memory issues with one of our systems and we couldn't debug it, because nobody really monitored the server, we just know that in 24hours the memory fills and the server dies. So i created this script to warn me when the memory usage goes up and list me the processes with the highest memory usage.

This wasn't a solution for the problem. :-) Later i manually checked the server in every hour, listed the processes with 'ps', etc. Then i found the problem. We had a cronjob that opened a link with lynx in every 5 minutes, but lynx never exited, so in every 5 minutes the processes were groving with one more lynx and the first version of this script couldn't detect it, because we were waiting for a huge memory usage of one process, but the cronjob made a many processes with low memory usage, so the server died again. :-)

Then i developed this script to the version as you can see now, this monitors the whole memory usage too, counts with the cached memory size and else. So now it can really warn you if the free memory is running out.

In the first section of the script you can find the Config, where you should adjust the values to your system, especially for the limit_vsz, limit_rss, i just set them based on what i saw in 'top'.
Also if you disable the mail_cmd then email won't be send, but you will get a nice output in the console, but you may check the warn email too. ;-)

PS.: i have some trouble with the mail send part, it will be better to set an email address and subject in the config and then use it in the end of the script for the mail send, but on our server it made a lot of errors i couldn't make out, althought i copied solutions from tutorials.. so that's why i made a mail_cmd config variable instead.
Technical
OS: Linux (tested on Debian 3)
Requirements: probably nothing more than a Linux server has already.
Installation: it should be runned as a cronjob, although it has a nice console output.
Setup: in the first (config) section, the variables should be changed to suit your needs, especially the mail_cmd that will be used to send the warning email (if needed).

Config variables and meanings:
- mail_cmd - command to send email
- limit_vsz - virtual memory usage size limit per process (in kilobyte)
- limit_rss - normal memory usage size limit per process (in kilobyte)
- limit_warning - percentage of the used memory limit -> warn email send
- limit_critical - percentage of the used memory limit -> critical warn email send
- limit_report_lines - number of top memory using processes to list in email
- report_file - here will be the contents of the email generated
- memget_file - a temporary file to store data returned be 'free'

Cron line for running the script every 5 minute (ignoring console output):
*/5 * * * * bash /path/to/file/check_memory_usage.sh &> /dev/null
Source Code
Download Download Source Code
check_memory_usage.zip ( 0.00Mb )
NEVER open a .exe, .bat, .cmd, or other executable file since that is where viruses are likely to live. Please virus scan all files you download.
Comments
Please login to post comments.
Page 1 of 1
More "Bash" Source Codes By This Author
Recently Posted "Bash" Source Codes
Recently Rated "Bash" Source Codes
 
 
Latest News About Coder Profile
Coder Profile Poll
If you made money from keeping your profile up to date, say $30 and up, per month. What extra time would you spend on your profile?

No extra time
A few hours at weekends
A whole day each week
Every minute i can get free


please login to cast your vote
and see the results of this poll
Latest Coder Profile Changes
Coder Profile was last updated
3.49 Years Ago
Official Blog :: Make A Donation :: Credits :: Contact Me
Terms & Conditions :: Privacy Policy :: Documents :: Wallpapers
Version 1.46.00
Copyright © 2007 - 2012, Scott Thompson, All Rights Reserved