Refactored snapin server_time to new snapin API
[check_mk.git] / checks / mysql_capacity
blob3d517666f7186bc251065e09e51d25bc25d3c523
1 #!/usr/bin/python
2 # -*- encoding: utf-8; py-indent-offset: 4 -*-
3 # +------------------------------------------------------------------+
4 # | ____ _ _ __ __ _ __ |
5 # | / ___| |__ ___ ___| | __ | \/ | |/ / |
6 # | | | | '_ \ / _ \/ __| |/ / | |\/| | ' / |
7 # | | |___| | | | __/ (__| < | | | | . \ |
8 # | \____|_| |_|\___|\___|_|\_\___|_| |_|_|\_\ |
9 # | |
10 # | Copyright Mathias Kettner 2014 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.
27 # <<<mysql_capacity>>>
28 # greendb 163840 1428160512
29 # hirn 16384 238026752
30 # information_schema 9216 0
31 # mysql 650067 0
32 # performance_schema 0 0
33 # wa-confluence 15499264 13805551616
35 # new: can have instance headers (can be empty), e.g.:
36 # <<<mysql_capacity>>>
37 # [[]]
38 # information_schema 147456 0
39 # mysql 665902 292
40 # performance_schema 0 0
41 # test 409255936 54525952
43 # FIXME: Crapy copy n paste! Consolidate with other mysql_* parse functions
44 def parse_mysql_capacity(info):
45 def cut_spaces(line):
46 name = " ".join(line[:-2])
47 size = line[-2]
48 avail = line[-1]
49 return name, size, avail
51 parsed = {}
52 instance = False
53 for line in info:
54 if line[0].startswith("[["):
55 instance = line[0][2:-2]
56 if instance == "":
57 instance = "mysql"
58 parsed[instance] = []
59 elif instance:
60 parsed[instance].append(cut_spaces(line))
62 if not instance:
63 parsed['mysql'] = []
64 for line in info:
65 parsed['mysql'].append(cut_spaces(line))
67 return parsed
70 @discover
71 def inventory_mysql_size(instance, values):
72 for dbname, used, avail in values:
73 if dbname not in ["information_schema", "mysql", "performance_schema"] \
74 and used != 'NULL' and avail != 'NULL':
75 yield "%s:%s" % (instance, dbname)
77 def check_mysql_size(item, params, parsed):
78 if ":" not in item:
79 # support items discovered before 1.2.7
80 instance = "mysql"
81 dbname = item
82 else:
83 instance, dbname = item.split(':')
85 dbs = parsed[instance]
87 # size and avail are given as bytes
88 for db, size, _avail in dbs:
89 if db == dbname:
90 if size == 'NULL':
91 return 3, "Missing information - Size is reported as 'NULL'"
92 size = int(size)
93 infotext = "Size is %s" % get_bytes_human_readable(size)
94 if params:
95 state = 0
96 warn, crit = params
97 warn_b = warn * 1024
98 crit_b = crit * 1024
99 perfdata = [("database_size", size, warn_b, crit_b)]
100 if size > crit_b:
101 state = 2
102 infotext += " (critical at %s)" % get_bytes_human_readable(crit_b)
103 elif size > warn_b:
104 state = 1
105 infotext += " (warning at %s)" % get_bytes_human_readable(crit_b)
106 else:
107 state = 0
108 perfdata = [("database_size", size)]
109 return state, infotext, perfdata
112 check_info['mysql_capacity'] = {
113 "parse_function" : parse_mysql_capacity,
114 "inventory_function" : inventory_mysql_size,
115 "check_function" : check_mysql_size,
116 "service_description" : "MySQL DB Size %s",
117 "has_perfdata" : True,
118 "group" : "mysql_db_size",