Skip to content

Commit 6b10c5c

Browse files
committed
first commit
0 parents  commit 6b10c5c

File tree

4 files changed

+255
-0
lines changed

4 files changed

+255
-0
lines changed

.gitignore

+165
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,165 @@
1+
# Created by .ignore support plugin (hsz.mobi)
2+
### Python template
3+
# Byte-compiled / optimized / DLL files
4+
__pycache__/
5+
*.py[cod]
6+
*$py.class
7+
8+
# C extensions
9+
*.so
10+
11+
# Distribution / packaging
12+
.Python
13+
build/
14+
develop-eggs/
15+
dist/
16+
downloads/
17+
eggs/
18+
.eggs/
19+
lib/
20+
lib64/
21+
parts/
22+
sdist/
23+
var/
24+
wheels/
25+
*.egg-info/
26+
.installed.cfg
27+
*.egg
28+
MANIFEST
29+
30+
# PyInstaller
31+
# Usually these files are written by a python script from a template
32+
# before PyInstaller builds the exe, so as to inject date/other infos into it.
33+
*.manifest
34+
*.spec
35+
36+
# Installer logs
37+
pip-log.txt
38+
pip-delete-this-directory.txt
39+
40+
# Unit test / coverage reports
41+
htmlcov/
42+
.tox/
43+
.coverage
44+
.coverage.*
45+
.cache
46+
nosetests.xml
47+
coverage.xml
48+
*.cover
49+
.hypothesis/
50+
.pytest_cache/
51+
52+
# Translations
53+
*.mo
54+
*.pot
55+
56+
# Django stuff:
57+
*.log
58+
local_settings.py
59+
db.sqlite3
60+
61+
# Flask stuff:
62+
instance/
63+
.webassets-cache
64+
65+
# Scrapy stuff:
66+
.scrapy
67+
68+
# Sphinx documentation
69+
docs/_build/
70+
71+
# PyBuilder
72+
target/
73+
74+
# Jupyter Notebook
75+
.ipynb_checkpoints
76+
77+
# pyenv
78+
.python-version
79+
80+
# celery beat schedule file
81+
celerybeat-schedule
82+
83+
# SageMath parsed files
84+
*.sage.py
85+
86+
# Environments
87+
.env
88+
.venv
89+
env/
90+
venv/
91+
ENV/
92+
env.bak/
93+
venv.bak/
94+
95+
# Spyder project settings
96+
.spyderproject
97+
.spyproject
98+
99+
# Rope project settings
100+
.ropeproject
101+
102+
# mkdocs documentation
103+
/site
104+
105+
# mypy
106+
.mypy_cache/
107+
### JetBrains template
108+
# Covers JetBrains IDEs: IntelliJ, RubyMine, PhpStorm, AppCode, PyCharm, CLion, Android Studio and WebStorm
109+
# Reference: https://intellij-support.jetbrains.com/hc/en-us/articles/206544839
110+
111+
# User-specific stuff
112+
.idea/**/workspace.xml
113+
.idea/**/tasks.xml
114+
.idea/**/dictionaries
115+
.idea/**/shelf
116+
117+
# Sensitive or high-churn files
118+
.idea/**/dataSources/
119+
.idea/**/dataSources.ids
120+
.idea/**/dataSources.local.xml
121+
.idea/**/sqlDataSources.xml
122+
.idea/**/dynamic.xml
123+
.idea/**/uiDesigner.xml
124+
.idea/**/dbnavigator.xml
125+
126+
# Gradle
127+
.idea/**/gradle.xml
128+
.idea/**/libraries
129+
130+
# CMake
131+
cmake-build-debug/
132+
cmake-build-release/
133+
134+
# Mongo Explorer plugin
135+
.idea/**/mongoSettings.xml
136+
137+
# File-based project format
138+
*.iws
139+
140+
# IntelliJ
141+
out/
142+
143+
# mpeltonen/sbt-idea plugin
144+
.idea_modules/
145+
146+
# JIRA plugin
147+
atlassian-ide-plugin.xml
148+
149+
# Cursive Clojure plugin
150+
.idea/replstate.xml
151+
152+
# Crashlytics plugin (for Android Studio and IntelliJ)
153+
com_crashlytics_export_strings.xml
154+
crashlytics.properties
155+
crashlytics-build.properties
156+
fabric.properties
157+
158+
# Editor-based Rest Client
159+
.idea/httpRequests
160+
/.idea/vcs.xml
161+
.idea/.name
162+
.idea/check_redis.iml
163+
.idea/encodings.xml
164+
.idea/misc.xml
165+
.idea/modules.xml

README.md

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
# py_check_redis

check_redis.py

+89
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
1+
#!/usr/bin/env python
2+
# -*- coding: utf-8 -*-
3+
4+
import redis
5+
import sys
6+
from optparse import OptionParser
7+
8+
9+
class NagiosRedis(object):
10+
11+
def __init__(self, host, port, password, bdname):
12+
self.host = host
13+
self.password = password
14+
self.port = port
15+
self.dbname = bdname
16+
17+
try:
18+
self.conn = redis.Redis(host=self.host, port=self.port, password=self.password, socket_timeout=1)
19+
self.info_out = self.conn.info()
20+
self.conn.ping()
21+
22+
except Exception as e:
23+
print('REDIS CRITICAL : ', e)
24+
sys.exit(2)
25+
26+
def get_version(self):
27+
28+
return 'version: %s' % self.info_out['redis_version']
29+
30+
def get_client_connection(self):
31+
32+
return 'connected_clients: %d' % self.info_out['connected_clients']
33+
34+
def get_number_keys(self):
35+
36+
return self.dbname + ": %d" % self.info_out[self.dbname]['keys']
37+
38+
def get_uptime(self):
39+
40+
return "uptime_in_days: %s" % self.info_out['uptime_in_days']
41+
42+
def get_used_memory(self):
43+
44+
return "used_memory_human: %s" % self.info_out['used_memory_human']
45+
46+
def nagios_check(self):
47+
number_keys = ''
48+
version = self.get_version()
49+
client_connected = self.get_client_connection()
50+
if self.dbname in str(self.info_out):
51+
number_keys = self.get_number_keys()
52+
memory = self.get_used_memory()
53+
uptime = self.get_uptime()
54+
if number_keys == '':
55+
print('OK REDIS No keys, %s, %s, %s' % (version, memory, uptime))
56+
sys.exit(0)
57+
else:
58+
print('OK REDIS %s, %s, %s, %s, %s' % (version, client_connected, number_keys, memory, uptime))
59+
sys.exit(0)
60+
61+
62+
def build_parser():
63+
"""
64+
define param command line
65+
:return: parser config
66+
"""
67+
parser = OptionParser(usage="usage: %prog [options]", version="%prog 1.0")
68+
parser.add_option("-H", "--host", dest="host", help="Redis server to connect to.", default=False)
69+
parser.add_option("-p", "--port", dest="port", help="Redis port to connect to.", type="int", default=6379)
70+
parser.add_option("-P", "--password", dest="password", help="Redis password to connect to.", default='')
71+
parser.add_option("-d", "--dbname", dest="dbname", help="Redis database name, default is db0", default='db0')
72+
parser.add_option("-t", "--timeout", dest="timeout",
73+
help="Number of milliesconds to wait before timing out and considering redis down",
74+
type="int", default=2000)
75+
return parser
76+
77+
78+
if __name__ == "__main__":
79+
80+
parser = build_parser()
81+
options, args = parser.parse_args()
82+
83+
if len(args) != 0:
84+
parser.error("Wrong number of arguments")
85+
sys.exit(0)
86+
87+
else:
88+
server = NagiosRedis(options.host, options.port, options.password, options.dbname)
89+
server.nagios_check()

requirements.txt

Whitespace-only changes.

0 commit comments

Comments
 (0)