Δημοσιεύτηκε: 07 Φεβ 2010, 09:50
από sokoban4ever
cpu Monitor
από padavrexei + me
viewtopic.php?f=4&t=8829&p=103822

άμα κολλάει ξαφνικά το pc και δεν βρίσκουμε ίχνη ποια διεργασία μας "την 'έκανε "
μπορούμε να τρέξουμε το cpuMon.sh σε ένα άλλο tty
αυτό κρατάει logs για της πρώτες 10 διεργασίες που καταναλώνουν την CPU μας
στο αρχείο /home/user/cpuPeak.txt
οπου user to username μας
για να το τρέξουμε
κάνουμε ctrl + alt + f1
και κάνουμε login στο tty1
to κάνουμε εκτελέσιμο
Κώδικας: Επιλογή όλων
chmod +x cpuMon.sh

έπειτα τρέχουμε
Κώδικας: Επιλογή όλων
watch -n.6 ./cpuMon.sh

με .6 σημαίνει log κάθε 600 milliseconds
μπορούμε να δώσουμε και
Κώδικας: Επιλογή όλων
watch -n 1 ./cpuMon.sh

για ολόκληρα δευτερόλεπτα
και πάει λέγωντας ..

και το bash script το οποίό σώζουμε αν θέλουμε σε cpuMon.sh
Κώδικας: Επιλογή όλων
#!/bin/bash
#
# cpuMon.sh
# Monitors CPU load and logs it if it exceeds the threshold
# Usage : watch ./cpuMon.sh
# also with parameter -n in watch
# Example: watch -n.6 ./cpuMon.sh
# every 600 miliseconds

threshold="90.0"

dFile="/home/$USER/cpuPeak.txt"


# Get current total-CPU-load (by adding all processes's %CPU)
cpuLoad=`ps aux --no-headers --sort=-%cpu | awk '{cpuload += $3} END {print cpuload}'`

#Change : Added support for Comparison of Floating point numbers ( because 9.1 is smaller than 80.0 ) but bash does not "see" that :-)
#If cpuLoad is greater than the threshold , ReturnStatus will be 1

echo $cpuLoad |awk '{ if ( $1 > '$threshold') {exit 1}};'
ReturnStatus=$?

#if awk returns 1 means that cpuLoad has become greater than our thresold so we can log
# If the sum is greater than the threshold, log top-10 processes
if [ "$ReturnStatus" -eq 1 ]; then
echo =====[ CPU load = "$cpuLoad" ]===== >> "$dFile"
date >> "$dFile"
ps aux --no-headers --sort=-%cpu | head -n10 >> "$dFile"
echo >> "$dFile"
fi

exit

αν το threshold είναι πολυ ψηλό αλλάξτε κατα βούληση εγώ το βάζω στο 80