Browse Source

Made a script that kills user programs

on the login node taking more than 10% of memory.
It has the '--dumy' flag enabled for now as i would like to test this
first.
pull/9/head
Egon Rijpkema 3 years ago
parent
commit
e60799f6a9
  1. 5
      kill_memory_hogs.yml
  2. 62
      roles/kill_memory_hogs/files/kill_hoggs.py
  3. 18
      roles/kill_memory_hogs/tasks/main.yml

5
kill_memory_hogs.yml

@ -0,0 +1,5 @@ @@ -0,0 +1,5 @@
---
- hosts: pg-login
become: true
roles:
- kill_memory_hogs

62
roles/kill_memory_hogs/files/kill_hoggs.py

@ -0,0 +1,62 @@ @@ -0,0 +1,62 @@
#!/usr/bin/env python3
import argparse
import psutil
import sys
def on_terminate(proc):
"""
Callback for terminate()
"""
print('process {} terminated with exit code {}'.format(
proc, proc.returncode))
def terminate(kill_list):
"""
Terminate processes. Kill if terminate is unsuccesful.
"""
for proc in kill_list:
proc.terminate()
gone, alive = psutil.wait_procs(
kill_list, timeout=3, callback=on_terminate)
for proc in alive:
print('Killing {} with signal 9'.format(proc))
proc.kill()
def kill_hogs(threshold, dummy=False):
"""
Kill processes that use more than <threshold> % of system memory.
"""
kill_list = []
for proc in psutil.process_iter():
if proc.uids().real == 0:
continue # do not kill root processes.
if proc.memory_percent() > threshold:
# Check username here. It is somewhat expensive.
username = proc.username()
if not (username[0] in ('s', 'p', 'f') and username[1:].isdigit()):
continue # we only kill processes of p, s and f accounts.
kill_list.append(proc)
print('{} pid {} {} {}%'.format(proc.username(), proc.pid,
proc.name(),
proc.memory_percent()))
if not dummy:
terminate(kill_list)
if __name__ == '__main__':
parser = argparse.ArgumentParser()
parser.add_argument(
"--threshold",
type=float,
default=10,
help="memory percentage above which processes are killed")
parser.add_argument(
"--dummy",
action='store_true',
help="Only display what would be killed")
args = parser.parse_args()
kill_hogs(threshold=args.threshold, dummy=args.dummy)

18
roles/kill_memory_hogs/tasks/main.yml

@ -0,0 +1,18 @@ @@ -0,0 +1,18 @@
---
- name: Install python script
copy:
src: 'files/kill_hoggs.py'
dest: /sbin/kill_hogs.py
mode: 0700
- cron:
name: Kill jobs that are using excessive memory.
minute: '/10'
user: root
job: '/usr/bin/python36 /sbin/kill_hogs.py --dummy'
cron_file: kill_memory_hogs
- cronvar:
name: MAILTO
value: e.m.a.rijpkema@rug.nl
cron_file: kill_memory_hogs
Loading…
Cancel
Save