Linux - ServerThis forum is for the discussion of Linux Software used in a server related context.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
We running various versions of ubuntu and often experience Linux servers freezing & going into a hung state. After the hard reset is completed the server is back online. The issue we having is identifying what processes or applications has utilized high system resources. Our monitoring tool is limited in terms of identifying Process ID's, it however provides the overall CPU MEM Load usage via graphs.
I need to develop a bash script that records Process id's breaching a certain threshold and write the output to /home/monuser/perfstats.txt to identify the PIDS post server crash.
CPU is 95% +
MEM is 95% +
Load is 25 +
The below command is easy and provides the output we need.
The script below is continuous in running and gathering the information throughout the day however we dont require the entries below 95% for CPU and Mem and Load usage.
Code:
#!/bin/bash
# This script monitors CPU Memory and Load.
while :
do
# Check the current PID and usage of CPU and memory and Load.
cpuandmemUsage=$(ps -ax -o pid,pcpu,pmem,user --sort -pcpu --no-headers | head)
loadUsage=$(cat /proc/loadavg)
# Print the usage
echo "CPU and Mem Usage: $cpuandmemUsage%" >> /home/monuser/perfstats.txt
echo "SysLoad Usage: $loadUsage MB" >> /home/monuser/perfstats.txt
# Sleep for 1 second
sleep 1
done
1. Is there a mechanism OR script we can configure to write to a file when that 2 columns value exceeds 95% and activate the script to start recording the data ?
Is there a mechanism OR script we can configure to write to a file when that 2 columns value exceeds 95% and activate the script to start recording the data ?
Why do you care ?. The disk space used will be trivial.
Bash has arithmetic operators, but they are integer only IIRC. Too hard. Just record the lot and parse it later with something like awk. If you stick the date in the filename it will even create a new file for you each midnight. Any daylight saving issues are your own.
There are other (better) options, but if this is your chosen path, make it as easy as possible.
while :
do
# Check the current PID and usage of CPU and memory and Load.
cpuandmemUsage=$(ps -ax -o pid,pcpu,pmem,user --sort -pcpu --no-headers | head)
# Read the top line and split
read pid pcpu pmem user <<< "$cpuandmemUsage"
# Get the integer part and compare
if [ "${pcpu%.*}" -ge 95 ]
then
# Print the usage
read loadUsage < /proc/loadavg
echo "CPU and Mem Usage: $cpuandmemUsage%"
echo "SysLoad Usage: $loadUsage MB"
fi >> /home/monuser/perfstats.txt
# Sleep for 1 second
sleep 1
done
while :
do
# Check the current PID and usage of CPU and memory and Load.
cpuandmemUsage=$(ps -ax -o pid,pcpu,pmem,user --sort -pcpu --no-headers | head)
# Read the top line and split
read pid pcpu pmem user <<< "$cpuandmemUsage"
# Get the integer part and compare
if [ "${pcpu%.*}" -ge 95 ]
then
# Print the usage
read loadUsage < /proc/loadavg
echo "CPU and Mem Usage: $cpuandmemUsage%"
echo "SysLoad Usage: $loadUsage MB"
fi >> /home/monuser/perfstats.txt
# Sleep for 1 second
sleep 1
done
Thanks, will try this.
I have a secondary plan to use my monitoring tool to read the output of a command.
Problem is i cant get to display the output as i need it to display.
I wonder how long it will be before you regret throwing all the historical data away, so you now have no way of doing what-if queries around the time of any event.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.