Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 32 additions & 1 deletion plugins/default/lib/memory_usage.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import sys
import traceback
from collections import Counter
import re

import openshift
import nagios
Expand All @@ -28,10 +29,37 @@ def generate_parser():
return parser


check_memory_usage_cmd = ("cat", "/sys/fs/cgroup/memory/memory.usage_in_bytes")
check_memory_usage_cmd = ("cat", "/proc/meminfo")
check_jvm_max_heap_cmd = ("java", "-XX:+PrintFlagsFinal", "-version")
check_memory_limit_cmd = ("cat", "/sys/fs/cgroup/memory/memory.limit_in_bytes")


def jvm_data(data, key):
return re.findall(r'\s+[a-zA-Z]+\s%s\s+\:?\=\s([0-9]+)\s+\{[a-zA-Z\s?]+\}\n' % key, data)[0]


def get_meminfo_data(field, output):
data = None
try:
data = int(re.findall(r"%s\:\s+([0-9]+)\skB\n" % data, output)[0])
except IndexError:
data = None
finally:
return data


def heap_thread_size(pid=1, thread_size=1024):
folder = '/proc/%s/task' % pid
total_tasks = len([task for task in os.listdir(folder) if task.isdigit()])
return total_tasks * thread_size


def get_memused(data, jvm_data):
initial = int(jvm_data(jvm_data, 'InitialHeapSize'))
single_thread_size = int(jvm_data(info, 'ThreadStackSize'))
return initial + heap_thread_size(2727, thread_size=single_thread_size)


def analize(pod, container, memory_limit, memory_used, warning_threshold, critical_threshold):
results = []
usage = (float(memory_used) / float(memory_limit)) * 100
Expand Down Expand Up @@ -103,6 +131,9 @@ def check(warn, crit, project):
try:
memory_usage = openshift.exec_in_pod_container(
project, pod_name, container_name, check_memory_usage_cmd)
jvm_heap = openshift.exec_in_pod_container(
project, pod_name, container_name, check_jvm_max_heap_cmd)
memory_usage = get_memused(memory_usage, jvm_heap)
if memory_limit:
memory_limit = openshift.exec_in_pod_container(
project, pod_name, container_name, check_memory_limit_cmd)
Expand Down