Guest User

Untitled

a guest
Oct 4th, 2016
175
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.42 KB | None | 0 0
  1. #!/bin/bash
  2.  
  3. # Script to cycle nova-api workers periodically
  4. #
  5. # NB: TEST THIS BEFORE RUNNING IT IN PRODUCTION
  6. #
  7. # The expected usage of this is from cron, scheduled to run
  8. # as often as you want to kill off a single API worker. This
  9. # will generate a list of nova-api workers and kill off one
  10. # worker per run of the script. When it gets to the end, it
  11. # regenerates the list and starts over. So, if you run this
  12. # from cron every night at midnight, one nova-api worker will
  13. # be killed off each day. Adjust this to suit your need for
  14. # cycling them. Workers are chosen in order, not by any other
  15. # metric like CPU time or memory footprint.
  16.  
  17. SERVICE_REGEX='nova-api'
  18. STATE_FILE='/tmp/cycle-nova-api.state'
  19. LOCKFILE="${STATE_FILE}.lock"
  20.  
  21. function get_master_pid() {
  22. systemctl show openstack-nova-api | grep ^MainPID | cut -d = -f 2
  23. }
  24.  
  25. function refresh_state_file() {
  26. local master_pid=$(get_master_pid)
  27.  
  28. pgrep -P "$master_pid" "$SERVICE_REGEX" > $STATE_FILE
  29. }
  30.  
  31. function kill_next_victim() {
  32. local victim=$(head -n1 "$STATE_FILE")
  33. sed -i 1d "$STATE_FILE"
  34. pgrep "$SERVICE_REGEX" | grep -q $victim && kill $victim
  35. }
  36.  
  37. (
  38. flock -s 200
  39. if [ ! -f "$STATE_FILE" ]; then
  40. # First run
  41. refresh_state_file
  42. elif ! grep -q '[0-9]' "$STATE_FILE"; then
  43. # We have cycled all workers, start over
  44. refresh_state_file
  45. fi
  46.  
  47. kill_next_victim
  48. ) 200>$LOCKFILE
Advertisement
Add Comment
Please, Sign In to add comment