If you’re experiencing problems with your Linux server’s load, there’s a good chance that you can diagnose and fix the issue with a simple script. To start, you’ll need to collect some information about your server. This includes its IP address, the amount of CPU and memory it’s using, and the type of operating system it is running. Once you have this information, you can start the script by entering the following command:
./load_info.sh
This script will run and will output all of the information that we collected about our server. The first column will show us the IP address of our server, while the other columns will give us detailed information about our server’s CPU and memory usage. If we look at our CPU usage, we can see that our server is using around 20% of its total CPU power. This means that there may be some issues with our server’s load that we can fix by adjusting its settings or by upgrading its hardware. If we look at our memory usage, we can see that our server is using around 2GB of RAM. This means that there may be some issues with our application or database servers that we can fix by upgrading their software or by adding more RAM to their servers. ..
The following script might be able to help. It was written for a web server, so has some parts of it that are specifically looking for httpd processes and some parts that deal with MySQL. Depending on your server deployment, simply comment/delete those sections and add others. It should be used for a starting point.
Prerequisites for this version of the script is some freeware released under the GNU General Public License called mytop (available at http://jeremy.zawodny.com/mysql/mytop/) which is a fantastic tool for checking how MySQL is performing. It is getting old, but still works great for our purposes here. Additionally, I use mutt as the mailer – you may want to change the script to simply use the linux built in mail
utility. I run it via cron every hour; adjust as you see fit. Oh – and this script needs to run as root since it does read from some protected areas of the server.
So let’s get started, shall we?
First, set your script variables:
Next, check your load level to see if the script should continue:
And continue through the checks, writing the results to the temporary file. Add or delete items from here where applicable to your situation:
if the load level is greater than you want, start the script process. Otherwise, exit 0
if [ $loadLevel -gt $levelToCheck ]; then echo "" > $tmpfile echo “” »$tmpfile echo “Date: $dt " »$tmpfile echo “Check System Load & Processes " »$tmpfile echo “” »$tmpfile
Notice with the top command, we are writing to two temp files. One is for the much smaller message to cell phone. If you don’t want the urgency of cell phone alerts at three in the morning, you can take this out (and take out the second mailing routine later in the script).
Show current load level: echo “Load Level Is: $loadLevel” »$tmpfile echo “*************************************************” »$tmpfile
Show number of httpd processes now running (not including children): echo “Number of httpd processes now: $httpdProcesses” »$tmpfile echo “*************************************************” »$tmpfile echo "” »$tmpfile
Show process list: echo “Processes now running:” »$tmpfile ps f -ef »$tmpfile echo “*************************************************” »$tmpfile echo "” »$tmpfile
Show current MySQL info: echo “Results from mytop:” »$tmpfile /usr/bin/mytop -u $dbusr -p $dbpw -b -d $db »$tmpfile echo “*************************************************” »$tmpfile echo "" »$tmpfile
More checks:
Then write the temporary file contents to a more permanent log file and email the results to the appropriate parties. The second mailing is the pared down results consisting simply of the standard out of top
:
Check disk space echo “disk space:” »$tmpfile /bin/df -k »$tmpfile echo “*************************************************” »$tmpfile echo "" »$tmpfile
And then some housekeeping and exit:
And email results to sysadmin: /usr/bin/mutt -s “$machine has a high load level! - $dt” -a $mysqlLog -a $msgLog $mailstop <$tmpfile /usr/bin/mutt -s “$machine has a high load level! - $dt” $mailstop1 <$topfile echo “**************************************” »$logfile
Hopefully this helps someone out there. Fully assembled script is:
exit 0
if the load level is greater than you want, start the script process. Otherwise, exit 0
if [ $loadLevel -gt $levelToCheck ]; then echo "" > $tmpfile echo “” »$tmpfile echo “Date: $dt " »$tmpfile echo “Check System Load & Processes " »$tmpfile echo “” »$tmpfile