Δημοσιεύτηκε: 07 Φεβ 2010, 09:35
από sokoban4ever
ωραίος δεν το είχα προσέξει :thumbup:
αλλά σήμερα που το δοκίμασα
όταν το threshold το έβαζα στο 80.0 έκανε log όταν το cpuLoad ήταν 9 οπότε γέμιζε τα logs άσκοπα (το 9.0 είναι μικρότερο του 80.0 αλλά δεν τα βλέπει το ο τελεστής > στο bash )
βασικά φταίει ότι το bash δεν υποστηρίζει σύγκριση floating point numbers ( αληθινούς δηλαδή)
έτσι έβαλα την awk να το συγκρίνει
επίσης έβαλα και το $USER στο dFile ώστε να το τρέχει απαράλλαχτο όποιος θέλει
Κώδικας: Επιλογή όλων
#!/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

ευχαριστώ και εγώ :)