Refactoring: Changed all check parameters starting with an 'o' to the new rulespec...
[check_mk.git] / checks / memory.include
bloba510d806131d86f213cef982f6ac0c57a3ace301
1 #!/usr/bin/python
2 # -*- encoding: utf-8; py-indent-offset: 4 -*-
3 # +------------------------------------------------------------------+
4 # | ____ _ _ __ __ _ __ |
5 # | / ___| |__ ___ ___| | __ | \/ | |/ / |
6 # | | | | '_ \ / _ \/ __| |/ / | |\/| | ' / |
7 # | | |___| | | | __/ (__| < | | | | . \ |
8 # | \____|_| |_|\___|\___|_|\_\___|_| |_|_|\_\ |
9 # | |
10 # | Copyright Mathias Kettner 2015 mk@mathias-kettner.de |
11 # +------------------------------------------------------------------+
13 # This file is part of Check_MK.
14 # The official homepage is at http://mathias-kettner.de/check_mk.
16 # check_mk is free software; you can redistribute it and/or modify it
17 # under the terms of the GNU General Public License as published by
18 # the Free Software Foundation in version 2. check_mk is distributed
19 # in the hope that it will be useful, but WITHOUT ANY WARRANTY; with-
20 # out even the implied warranty of MERCHANTABILITY or FITNESS FOR A
21 # PARTICULAR PURPOSE. See the GNU General Public License for more de-
22 # tails. You should have received a copy of the GNU General Public
23 # License along with GNU Make; see the file COPYING. If not, write
24 # to the Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
25 # Boston, MA 02110-1301 USA.
28 def check_memory_simple(used, total, params):
29 # Convert old-style tuple params to dict
30 if params:
31 if isinstance(params, tuple):
32 params = {"levels": ("perc_used", params)}
33 else:
34 params = {"levels": ("ignore")}
36 perc_used = (float(used) / total) * 100
37 infotext = "Usage: %s (Used: %s, Total: %s)" % (get_percent_human_readable(perc_used),
38 get_bytes_human_readable(used),
39 get_bytes_human_readable(total))
41 status = 0
42 if params["levels"][0] == "perc_used":
43 warn_perc, crit_perc = params["levels"][1]
44 warn_abs = (warn_perc / 100.0) * total
45 crit_abs = (crit_perc / 100.0) * total
46 levelstext = " (warn/crit at %s/%s used)" % (get_percent_human_readable(warn_perc),
47 get_percent_human_readable(crit_perc))
49 elif params["levels"][0] == "abs_free":
50 warn_abs_free, crit_abs_free = params["levels"][1]
51 warn_abs = total - warn_abs_free
52 crit_abs = total - crit_abs_free
53 levelstext = " (warn/crit below %s/%s free)" % (get_bytes_human_readable(warn_abs_free),
54 get_bytes_human_readable(crit_abs_free))
56 else:
57 # No levels imposed, ie. params = {'levels': 'ignore'}
58 crit_abs = None
59 warn_abs = None
60 levelstext = ""
62 if crit_abs is not None and used >= crit_abs:
63 status = 2
64 elif warn_abs is not None and used >= warn_abs:
65 status = 1
66 if status:
67 infotext += levelstext
69 perfdata = [("memory_used", used, warn_abs, crit_abs, 0, total)]
70 return status, infotext, perfdata
73 def check_memory_multiitem(params, data, base=1024):
74 if 'mem_total' not in data:
75 return 3, 'Invalid data: missing mem_total'
76 mem_total = data['mem_total']
78 if 'mem_used' in data:
79 mem_used = data["mem_used"]
80 mem_avail = mem_total - mem_used
81 elif 'mem_avail' in data:
82 mem_avail = data['mem_avail']
83 mem_used = mem_total - mem_avail
84 else:
85 return 3, 'Invalid data: missing mem_used or mem_avail sizes'
87 infotext = '%s used (%s of %s)' % (get_percent_human_readable(
88 float(mem_used) / float(mem_total) * 100), get_bytes_human_readable(mem_used, base=base),
89 get_bytes_human_readable(mem_total, base=base))
91 state = 0
92 if 'levels' in params:
93 warn, crit = params['levels']
94 if isinstance(warn, int):
95 warn_absolute = warn
96 else:
97 warn_absolute = int(mem_total * warn / 100)
99 if isinstance(crit, int):
100 crit_absolute = crit
101 else:
102 crit_absolute = int(mem_total * crit / 100)
104 if mem_used > crit_absolute:
105 state = 2
106 elif mem_used > warn_absolute:
107 state = 1
108 if state:
109 infotext += ' (warn/crit at %s/%s)' % (get_bytes_human_readable(warn_absolute),
110 get_bytes_human_readable(crit_absolute))
111 else:
112 warn_absolute = None
113 crit_absolute = None
115 return state, infotext, [('memused', mem_used, warn_absolute, crit_absolute, 0, mem_total)]