Skip to content

Instantly share code, notes, and snippets.

@luanntvn
Forked from samuel/check_redis.py
Last active September 5, 2022 17:50
Show Gist options
  • Save luanntvn/3800a9fa007a9c49c87b6da31df1d8de to your computer and use it in GitHub Desktop.
Save luanntvn/3800a9fa007a9c49c87b6da31df1d8de to your computer and use it in GitHub Desktop.
Redis health and memory check for Nagios
#!/usr/bin/python
#
# LICENSE: MIT
#
# Copyright (C) 2014 Samuel Stauffer
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to
# deal in the Software without restriction, including without limitation
# the rights to use, copy, modify, merge, publish, distribute, sublicense,
# and/or sell copies of the Software, and to permit persons to whom the
# Software is furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in
# all copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
#
import socket
import sys
from optparse import OptionParser
EXIT_OK = 0
EXIT_WARN = 1
EXIT_CRITICAL = 2
EXIT_INVALID_AUTH = 3
def get_info(host, port, timeout, auth):
socket.setdefaulttimeout(timeout or None)
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.connect((host, port))
if auth is not None:
s.send("*2\r\n$4\r\nAUTH\r\n$%d\r\n%s\r\n" % (len(auth),auth))
result = s.recv(100)
if not 'OK' in result:
print "ERROR: Invalid authentication"
sys.exit(EXIT_INVALID_AUTH)
s.send("*1\r\n$4\r\ninfo\r\n")
buf = ""
while '\r\n\r\n' not in buf:
buf += s.recv(1024)
s.close()
return dict(x.split(':', 1) for x in buf.split('\r\n') if ':' in x)
def build_parser():
parser = OptionParser()
parser.add_option("-s", "--server", dest="server", help="Redis server to connect to.", default="127.0.0.1")
parser.add_option("-p", "--port", dest="port", help="Redis port to connect to.", type="int", default=6379)
parser.add_option("-w", "--warn", dest="warn_memory", help="Memory utilization (in MB) that triggers a warning status.", type="int")
parser.add_option("-c", "--critical", dest="crit_memory", help="Memory utilization (in MB) that triggers a critical status.", type="int")
parser.add_option("-t", "--timeout", dest="timeout", help="Number of milliesconds to wait before timing out and considering redis down", type="int", default=2000)
parser.add_option("-a", "--auth", dest="auth", help="Authentication string", default=None)
return parser
def main():
parser = build_parser()
options, _args = parser.parse_args()
if not options.warn_memory:
parser.error("Warning level required")
if not options.crit_memory:
parser.error("Critical level required")
try:
info = get_info(options.server, int(options.port), options.timeout / 1000.0, options.auth)
except socket.error, exc:
print "CRITICAL: Error connecting or getting INFO from redis %s:%s: %s" % (options.server, options.port, exc)
sys.exit(EXIT_CRITICAL)
memory = int(info.get("used_memory_rss") or info["used_memory"]) / (1024*1024)
if memory > options.crit_memory:
print "CRITICAL: Redis memory usage is %dMB (threshold %dMB)" % (memory, options.crit_memory)
sys.exit(EXIT_CRITICAL)
elif memory > options.warn_memory:
print "WARN: Redis memory usage is %dMB (threshold %dMB)" % (memory, options.warn_memory)
sys.exit(EXIT_WARN)
print "OK: Redis memory usage is %dMB" % memory
sys.exit(EXIT_OK)
if __name__ == "__main__":
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment