Advertisement
Guest User

Untitled

a guest
Jun 24th, 2017
56
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Bash 2.43 KB | None | 0 0
  1. top -b -n1 | awk '
  2.        BEGIN {
  3.  
  4.                # this is the number of processes to dump
  5.                KILL_MAX = '${proc_count}'
  6.         THRESH = '${threshold}'
  7.  
  8.  
  9.                # this is the list of processes to dump. NOTE THAT YOU CAN USE REGULAR EXPRESSIONS! (e.g. hald$)
  10.                split("'${bincsv}'",names,",")
  11.                i = 0
  12.                j = 0
  13.                while (j < KILL_MAX)
  14.                {
  15.                        kills[j, 0] = 0
  16.                        kills[j, 1] = 0
  17.                        kills[j, 2] = 0
  18.  
  19.                        j++
  20.                }
  21.  
  22.        }
  23.  
  24.        {
  25.                i++
  26.        }
  27.    
  28.        # Grab cpu usage, compare to threshold
  29.     i == 3 {
  30.             cpu = $2
  31.             sub(/\..*%/,"", cpu)
  32.             print "Cpu: " cpu
  33.             if ( cpu > THRESH ) {
  34.                 OVER = ! OVER
  35.                 print "threshold reached: " cpu
  36.             }
  37.         }
  38.     # If cpu over threshold, and this is line 4+, process array results for the max utilizing procs
  39.     OVER == 1 && i > 4 {
  40.         # iterate over each process name
  41.         print "i: " i
  42.         for (name in names) {
  43.             # if this process matches it,
  44.             if (match($12, names[name]) ) {
  45.                 # begin at the end of our array
  46.                 j = KILL_MAX - 1
  47.                 # iterate until we reach the front of the array
  48.                 while (j >= 0) {
  49.                     # the element we are examining is greater than this one
  50.                     if (kills[j, 1] < $10) {
  51.                         # begin at the front of the array
  52.                         k = 0
  53.                         # until we reach the space where our new element goes
  54.                         while (k < j) {
  55.                             # shift our elements toward the front
  56.                             kills[k, 0] = kills[k + 1, 0]
  57.                             kills[k, 1] = kills[k + 1, 1]
  58.                             kills[k, 2] = kills[k + 1, 2]
  59.                             kills[k, 3] = kills[k + 1, 3]
  60.                             k++
  61.                         }
  62.  
  63.                         # now place our new element in its position
  64.                         kills[j, 0] = $2
  65.                         kills[j, 1] = $10
  66.                         kills[j, 2] = $12
  67.                         kills[j, 3] = $1
  68.  
  69.                         # break out of this iteration; go to next process
  70.                         break
  71.                     }
  72.                     j--
  73.                 }
  74.             }
  75.         }
  76.                    
  77.     }
  78.     END {
  79.         if ( OVER == 1 ) {
  80.             i = 0
  81.             # this is just an example that prints out the stored process information
  82.             # it can be changed to do whatever
  83.             while (i < KILL_MAX) {
  84.                 # remove "echo" in the next line and presto, it does what you want
  85.                 system("echo killss: " kills[i, 3])
  86.                 print "Dumping " kills[i, 2] " (pid " kills[i, 3] ", owner " kills[i, 0] ") for " kills[i, 1] "% CPU util"
  87.                 i++
  88.             }
  89.         }
  90.         else {
  91.             print "Threshold not hit: " cpu
  92.         }
  93.     }
  94. '
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement