Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- top -b -n1 | awk '
- BEGIN {
- # this is the number of processes to dump
- KILL_MAX = '${proc_count}'
- THRESH = '${threshold}'
- # this is the list of processes to dump. NOTE THAT YOU CAN USE REGULAR EXPRESSIONS! (e.g. hald$)
- split("'${bincsv}'",names,",")
- i = 0
- j = 0
- while (j < KILL_MAX)
- {
- kills[j, 0] = 0
- kills[j, 1] = 0
- kills[j, 2] = 0
- j++
- }
- }
- {
- i++
- }
- # Grab cpu usage, compare to threshold
- i == 3 {
- cpu = $2
- sub(/\..*%/,"", cpu)
- print "Cpu: " cpu
- if ( cpu > THRESH ) {
- OVER = ! OVER
- print "threshold reached: " cpu
- }
- }
- # If cpu over threshold, and this is line 4+, process array results for the max utilizing procs
- OVER == 1 && i > 4 {
- # iterate over each process name
- print "i: " i
- for (name in names) {
- # if this process matches it,
- if (match($12, names[name]) ) {
- # begin at the end of our array
- j = KILL_MAX - 1
- # iterate until we reach the front of the array
- while (j >= 0) {
- # the element we are examining is greater than this one
- if (kills[j, 1] < $10) {
- # begin at the front of the array
- k = 0
- # until we reach the space where our new element goes
- while (k < j) {
- # shift our elements toward the front
- kills[k, 0] = kills[k + 1, 0]
- kills[k, 1] = kills[k + 1, 1]
- kills[k, 2] = kills[k + 1, 2]
- kills[k, 3] = kills[k + 1, 3]
- k++
- }
- # now place our new element in its position
- kills[j, 0] = $2
- kills[j, 1] = $10
- kills[j, 2] = $12
- kills[j, 3] = $1
- # break out of this iteration; go to next process
- break
- }
- j--
- }
- }
- }
- }
- END {
- if ( OVER == 1 ) {
- i = 0
- # this is just an example that prints out the stored process information
- # it can be changed to do whatever
- while (i < KILL_MAX) {
- # remove "echo" in the next line and presto, it does what you want
- system("echo killss: " kills[i, 3])
- print "Dumping " kills[i, 2] " (pid " kills[i, 3] ", owner " kills[i, 0] ") for " kills[i, 1] "% CPU util"
- i++
- }
- }
- else {
- print "Threshold not hit: " cpu
- }
- }
- '
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement