├── .gitignore ├── LICENSE ├── README.md ├── checklib ├── .gitignore ├── README.md ├── setup.py └── src │ └── greplin │ ├── __init__.py │ └── nagios.py ├── checkserver ├── README.md ├── check.sh ├── server.py └── testchecks │ ├── README.md │ ├── check_error.py │ ├── check_fast.py │ └── check_slow.py └── genlib ├── .gitignore ├── README.md ├── setup.py └── src └── greplin ├── __init__.py └── nagiosconf.py /.gitignore: -------------------------------------------------------------------------------- 1 | *.iml 2 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | 2 | Apache License 3 | Version 2.0, January 2004 4 | http://www.apache.org/licenses/ 5 | 6 | TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION 7 | 8 | 1. Definitions. 9 | 10 | "License" shall mean the terms and conditions for use, reproduction, 11 | and distribution as defined by Sections 1 through 9 of this document. 12 | 13 | "Licensor" shall mean the copyright owner or entity authorized by 14 | the copyright owner that is granting the License. 15 | 16 | "Legal Entity" shall mean the union of the acting entity and all 17 | other entities that control, are controlled by, or are under common 18 | control with that entity. For the purposes of this definition, 19 | "control" means (i) the power, direct or indirect, to cause the 20 | direction or management of such entity, whether by contract or 21 | otherwise, or (ii) ownership of fifty percent (50%) or more of the 22 | outstanding shares, or (iii) beneficial ownership of such entity. 23 | 24 | "You" (or "Your") shall mean an individual or Legal Entity 25 | exercising permissions granted by this License. 26 | 27 | "Source" form shall mean the preferred form for making modifications, 28 | including but not limited to software source code, documentation 29 | source, and configuration files. 30 | 31 | "Object" form shall mean any form resulting from mechanical 32 | transformation or translation of a Source form, including but 33 | not limited to compiled object code, generated documentation, 34 | and conversions to other media types. 35 | 36 | "Work" shall mean the work of authorship, whether in Source or 37 | Object form, made available under the License, as indicated by a 38 | copyright notice that is included in or attached to the work 39 | (an example is provided in the Appendix below). 40 | 41 | "Derivative Works" shall mean any work, whether in Source or Object 42 | form, that is based on (or derived from) the Work and for which the 43 | editorial revisions, annotations, elaborations, or other modifications 44 | represent, as a whole, an original work of authorship. For the purposes 45 | of this License, Derivative Works shall not include works that remain 46 | separable from, or merely link (or bind by name) to the interfaces of, 47 | the Work and Derivative Works thereof. 48 | 49 | "Contribution" shall mean any work of authorship, including 50 | the original version of the Work and any modifications or additions 51 | to that Work or Derivative Works thereof, that is intentionally 52 | submitted to Licensor for inclusion in the Work by the copyright owner 53 | or by an individual or Legal Entity authorized to submit on behalf of 54 | the copyright owner. For the purposes of this definition, "submitted" 55 | means any form of electronic, verbal, or written communication sent 56 | to the Licensor or its representatives, including but not limited to 57 | communication on electronic mailing lists, source code control systems, 58 | and issue tracking systems that are managed by, or on behalf of, the 59 | Licensor for the purpose of discussing and improving the Work, but 60 | excluding communication that is conspicuously marked or otherwise 61 | designated in writing by the copyright owner as "Not a Contribution." 62 | 63 | "Contributor" shall mean Licensor and any individual or Legal Entity 64 | on behalf of whom a Contribution has been received by Licensor and 65 | subsequently incorporated within the Work. 66 | 67 | 2. Grant of Copyright License. Subject to the terms and conditions of 68 | this License, each Contributor hereby grants to You a perpetual, 69 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable 70 | copyright license to reproduce, prepare Derivative Works of, 71 | publicly display, publicly perform, sublicense, and distribute the 72 | Work and such Derivative Works in Source or Object form. 73 | 74 | 3. Grant of Patent License. Subject to the terms and conditions of 75 | this License, each Contributor hereby grants to You a perpetual, 76 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable 77 | (except as stated in this section) patent license to make, have made, 78 | use, offer to sell, sell, import, and otherwise transfer the Work, 79 | where such license applies only to those patent claims licensable 80 | by such Contributor that are necessarily infringed by their 81 | Contribution(s) alone or by combination of their Contribution(s) 82 | with the Work to which such Contribution(s) was submitted. If You 83 | institute patent litigation against any entity (including a 84 | cross-claim or counterclaim in a lawsuit) alleging that the Work 85 | or a Contribution incorporated within the Work constitutes direct 86 | or contributory patent infringement, then any patent licenses 87 | granted to You under this License for that Work shall terminate 88 | as of the date such litigation is filed. 89 | 90 | 4. Redistribution. You may reproduce and distribute copies of the 91 | Work or Derivative Works thereof in any medium, with or without 92 | modifications, and in Source or Object form, provided that You 93 | meet the following conditions: 94 | 95 | (a) You must give any other recipients of the Work or 96 | Derivative Works a copy of this License; and 97 | 98 | (b) You must cause any modified files to carry prominent notices 99 | stating that You changed the files; and 100 | 101 | (c) You must retain, in the Source form of any Derivative Works 102 | that You distribute, all copyright, patent, trademark, and 103 | attribution notices from the Source form of the Work, 104 | excluding those notices that do not pertain to any part of 105 | the Derivative Works; and 106 | 107 | (d) If the Work includes a "NOTICE" text file as part of its 108 | distribution, then any Derivative Works that You distribute must 109 | include a readable copy of the attribution notices contained 110 | within such NOTICE file, excluding those notices that do not 111 | pertain to any part of the Derivative Works, in at least one 112 | of the following places: within a NOTICE text file distributed 113 | as part of the Derivative Works; within the Source form or 114 | documentation, if provided along with the Derivative Works; or, 115 | within a display generated by the Derivative Works, if and 116 | wherever such third-party notices normally appear. The contents 117 | of the NOTICE file are for informational purposes only and 118 | do not modify the License. You may add Your own attribution 119 | notices within Derivative Works that You distribute, alongside 120 | or as an addendum to the NOTICE text from the Work, provided 121 | that such additional attribution notices cannot be construed 122 | as modifying the License. 123 | 124 | You may add Your own copyright statement to Your modifications and 125 | may provide additional or different license terms and conditions 126 | for use, reproduction, or distribution of Your modifications, or 127 | for any such Derivative Works as a whole, provided Your use, 128 | reproduction, and distribution of the Work otherwise complies with 129 | the conditions stated in this License. 130 | 131 | 5. Submission of Contributions. Unless You explicitly state otherwise, 132 | any Contribution intentionally submitted for inclusion in the Work 133 | by You to the Licensor shall be under the terms and conditions of 134 | this License, without any additional terms or conditions. 135 | Notwithstanding the above, nothing herein shall supersede or modify 136 | the terms of any separate license agreement you may have executed 137 | with Licensor regarding such Contributions. 138 | 139 | 6. Trademarks. This License does not grant permission to use the trade 140 | names, trademarks, service marks, or product names of the Licensor, 141 | except as required for reasonable and customary use in describing the 142 | origin of the Work and reproducing the content of the NOTICE file. 143 | 144 | 7. Disclaimer of Warranty. Unless required by applicable law or 145 | agreed to in writing, Licensor provides the Work (and each 146 | Contributor provides its Contributions) on an "AS IS" BASIS, 147 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or 148 | implied, including, without limitation, any warranties or conditions 149 | of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A 150 | PARTICULAR PURPOSE. You are solely responsible for determining the 151 | appropriateness of using or redistributing the Work and assume any 152 | risks associated with Your exercise of permissions under this License. 153 | 154 | 8. Limitation of Liability. In no event and under no legal theory, 155 | whether in tort (including negligence), contract, or otherwise, 156 | unless required by applicable law (such as deliberate and grossly 157 | negligent acts) or agreed to in writing, shall any Contributor be 158 | liable to You for damages, including any direct, indirect, special, 159 | incidental, or consequential damages of any character arising as a 160 | result of this License or out of the use or inability to use the 161 | Work (including but not limited to damages for loss of goodwill, 162 | work stoppage, computer failure or malfunction, or any and all 163 | other commercial damages or losses), even if such Contributor 164 | has been advised of the possibility of such damages. 165 | 166 | 9. Accepting Warranty or Additional Liability. While redistributing 167 | the Work or Derivative Works thereof, You may choose to offer, 168 | and charge a fee for, acceptance of support, warranty, indemnity, 169 | or other liability obligations and/or rights consistent with this 170 | License. However, in accepting such obligations, You may act only 171 | on Your own behalf and on Your sole responsibility, not on behalf 172 | of any other Contributor, and only if You agree to indemnify, 173 | defend, and hold each Contributor harmless for any liability 174 | incurred by, or claims asserted against, such Contributor by reason 175 | of your accepting any such warranty or additional liability. 176 | 177 | END OF TERMS AND CONDITIONS 178 | 179 | APPENDIX: How to apply the Apache License to your work. 180 | 181 | To apply the Apache License to your work, attach the following 182 | boilerplate notice, with the fields enclosed by brackets "[]" 183 | replaced with your own identifying information. (Don't include 184 | the brackets!) The text should be enclosed in the appropriate 185 | comment syntax for the file format. We also recommend that a 186 | file or class name and description of purpose be included on the 187 | same "printed page" as the copyright notice for easier 188 | identification within third-party archives. 189 | 190 | Copyright [yyyy] [name of copyright owner] 191 | 192 | Licensed under the Apache License, Version 2.0 (the "License"); 193 | you may not use this file except in compliance with the License. 194 | You may obtain a copy of the License at 195 | 196 | http://www.apache.org/licenses/LICENSE-2.0 197 | 198 | Unless required by applicable law or agreed to in writing, software 199 | distributed under the License is distributed on an "AS IS" BASIS, 200 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 201 | See the License for the specific language governing permissions and 202 | limitations under the License. 203 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | greplin-nagios-utils 2 | ==================== 3 | 4 | Utilities for writing and running nagios checks. 5 | ------------------------------------------------ 6 | 7 | ### Rationale 8 | 9 | Monitoring is of critical importance for any system - tool like Nagios have taken much of the pain away. But we still found it 10 | frustrating to write and modify application checks. 11 | 12 | #### [checklib](/Greplin/greplin-nagios-utils/tree/master/checklib) 13 | 14 | Writing checks for Nagios involves learning a strange language full of semicolons and rules specified in regexes. We 15 | wanted to make creating and modifying checks to be as easy as possible so first we wrote [checklib](/Greplin/greplin-nagios-utils/tree/master/checklib). 16 | 17 | #### [checkserver](/Greplin/greplin-nagios-utils/tree/master/checkserver) 18 | 19 | Next we discovered that when you run checks for 100+ machines with 10+ checks each, you start seeing a lot of system load 20 | used just to start and stop python processes. To solve this problem we wrote [checkserver](/Greplin/greplin-nagios-utils/tree/master/checkserver). 21 | 22 | ### Status: 23 | 24 | This is a very early stage project. It works for our needs. We haven't verified it works beyond that. Issue reports 25 | and patches are very much appreciated! 26 | 27 | ### Authors: 28 | 29 | [Greplin, Inc.](http://www.greplin.com) 30 | Jon Rowland 31 | -------------------------------------------------------------------------------- /checklib/.gitignore: -------------------------------------------------------------------------------- 1 | build -------------------------------------------------------------------------------- /checklib/README.md: -------------------------------------------------------------------------------- 1 | greplin-nagios-utils: checklib 2 | ============================== 3 | 4 | Library that makes writing Nagios checks really easy. 5 | ----------------------------------------------------- 6 | 7 | ### Sample 8 | 9 | The sample assumes you're also using [checkserver](/Greplin/greplin-nagios-utils/tree/master/checkserver) 10 | 11 | from greplin.nagios import parseArgs, parseJsonFile, statValue, ResponseBuilder 12 | 13 | def check(argv): 14 | """Runs the check.""" 15 | args = parseArgs('check_sample.py', ('HOST', str), ('TIMEOUT', int), argv=argv) 16 | data = parseJson(wgetWithTimeout(args['HOST'], 8081, '/metrics', args['TIMEOUT'])) 17 | 18 | (ResponseBuilder() 19 | .addValue('SomeStatToTrack', statValue(data, 'myStat', default = 0)) 20 | .addRule('OutOfMemoryErrors', Maximum(0, 5), statValue(data, 'com.your.server.Servlet', 'OutOfMemory', 'count', default=0)) 21 | ).finish() 22 | 23 | This tracks myStat for performance data, and tracks com.your.server.Servlet.OutOfMemory.count for both performance and status. If 24 | the latter is > 0, the check will return WARN and if it is > 5 it will return CRIT. 25 | 26 | For standalone use (without checkserver) just add 27 | 28 | import sys 29 | 30 | if __name__ == '__main__': 31 | check(sys.argv) 32 | -------------------------------------------------------------------------------- /checklib/setup.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python 2 | # Copyright 2011 The greplin-nagios-utils Authors. 3 | # 4 | # Licensed under the Apache License, Version 2.0 (the "License"); 5 | # you may not use this file except in compliance with the License. 6 | # You may obtain a copy of the License at 7 | # 8 | # http://www.apache.org/licenses/LICENSE-2.0 9 | # 10 | # Unless required by applicable law or agreed to in writing, software 11 | # distributed under the License is distributed on an "AS IS" BASIS, 12 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | # See the License for the specific language governing permissions and 14 | # limitations under the License. 15 | 16 | """Setup script for Greplin utilities for writing Nagios checks.""" 17 | 18 | try: 19 | from setuptools import setup 20 | except ImportError: 21 | from distutils.core import setup 22 | 23 | setup(name='greplin-nagios-checklib', 24 | version='0.1', 25 | description='Python utilties for creating Nagios checks.', 26 | license='Apache', 27 | author='Greplin, Inc.', 28 | url='https://www.github.com/Cue/greplin-nagios-utils/checklib', 29 | package_dir = {'':'src'}, 30 | packages = [ 31 | 'greplin', 32 | ], 33 | namespace_packages = [ 34 | 'greplin', 35 | ], 36 | test_suite = 'nose.collector', 37 | zip_safe = True 38 | ) 39 | -------------------------------------------------------------------------------- /checklib/src/greplin/__init__.py: -------------------------------------------------------------------------------- 1 | # Copyright 2011 The greplin-nagios-utils Authors. 2 | # 3 | # Licensed under the Apache License, Version 2.0 (the "License"); 4 | # you may not use this file except in compliance with the License. 5 | # You may obtain a copy of the License at 6 | # 7 | # http://www.apache.org/licenses/LICENSE-2.0 8 | # 9 | # Unless required by applicable law or agreed to in writing, software 10 | # distributed under the License is distributed on an "AS IS" BASIS, 11 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | # See the License for the specific language governing permissions and 13 | # limitations under the License. 14 | 15 | """The Greplin root package.""" 16 | 17 | import pkg_resources 18 | pkg_resources.declare_namespace('greplin') -------------------------------------------------------------------------------- /checklib/src/greplin/nagios.py: -------------------------------------------------------------------------------- 1 | # Copyright 2011 The greplin-nagios-utils Authors. 2 | # 3 | # Licensed under the Apache License, Version 2.0 (the "License"); 4 | # you may not use this file except in compliance with the License. 5 | # You may obtain a copy of the License at 6 | # 7 | # http://www.apache.org/licenses/LICENSE-2.0 8 | # 9 | # Unless required by applicable law or agreed to in writing, software 10 | # distributed under the License is distributed on an "AS IS" BASIS, 11 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | # See the License for the specific language governing permissions and 13 | # limitations under the License. 14 | 15 | """The Greplin monitoring package.""" 16 | 17 | import httplib 18 | import json 19 | import socket 20 | import sys 21 | import time 22 | import threading 23 | 24 | 25 | UNKNOWN = 3 26 | 27 | CRITICAL = 2 28 | 29 | WARNING = 1 30 | 31 | OK = 0 32 | 33 | STATUS_NAME = ['OK', 'WARN', 'CRIT', 'UNKNOWN'] 34 | 35 | # This is a thread-local variable so clients can override it per-thread. 36 | GLOBAL_CONFIG = threading.local() 37 | GLOBAL_CONFIG.outfile = sys.stdout 38 | 39 | 40 | def output(msg): 41 | """Send output to output stream.""" 42 | GLOBAL_CONFIG.outfile.write(msg) 43 | GLOBAL_CONFIG.outfile.write('\n') 44 | 45 | 46 | def wgetWithTimeout(host, port, path, timeout, secure = False): 47 | """Gets an http page, but times out if it's too slow.""" 48 | start = time.time() 49 | try: 50 | if secure: 51 | conn = httplib.HTTPSConnection(host, port, timeout=timeout) 52 | else: 53 | conn = httplib.HTTPConnection(host, port, timeout=timeout) 54 | conn.request('GET', path) 55 | body = conn.getresponse().read() 56 | return time.time() - start, body 57 | 58 | except (socket.gaierror, socket.error): 59 | output("CRIT: Could not connect to %s" % host) 60 | exit(CRITICAL) 61 | 62 | except socket.timeout: 63 | output("CRIT: Timed out after %s seconds" % timeout) 64 | exit(CRITICAL) 65 | 66 | 67 | def parseJson(text): 68 | """Parses JSON, exiting with CRIT if the parse fails.""" 69 | try: 70 | return json.loads(text) 71 | 72 | except ValueError, e: 73 | output('CRIT: %s (text was %r)' % (e, text)) 74 | exit(CRITICAL) 75 | 76 | 77 | def parseJsonFile(filename): 78 | """Parses JSON from a file, exiting with UNKNOWN if the file does not exist.""" 79 | try: 80 | with open(filename) as f: 81 | return parseJson(f.read()) 82 | except IOError, e: 83 | output('UNKNOWN: %s' % e) 84 | exit(UNKNOWN) 85 | 86 | 87 | def lookup(source, *keys, **kw): 88 | """Successively looks up each key, returning the default keyword arg if a dead end is reached.""" 89 | fallback = kw.get('default') 90 | try: 91 | for key in keys: 92 | source = source[key] 93 | return source 94 | except (KeyError, AttributeError, TypeError): 95 | return fallback 96 | 97 | 98 | def statValue(data, *keys, **kw): 99 | """Returns the value of a stat.""" 100 | return float(lookup(data, *keys, **kw)) 101 | 102 | 103 | def percent(value): 104 | """Formats the given float as a percentage.""" 105 | return "%f%%" % (value * 100) 106 | 107 | 108 | def parseArgs(scriptName, *args, **kw): 109 | """Parses arguments to the script.""" 110 | argv = kw.get('argv', sys.argv) 111 | if len(argv) != len(args) + 1: 112 | output('USAGE: %s %s' % (scriptName, ' '.join([name for name, _ in args]))) 113 | exit(UNKNOWN) 114 | 115 | result = {} 116 | idx = 0 117 | for name, fn in args: 118 | try: 119 | idx += 1 120 | result[name] = fn(argv[idx]) 121 | except ValueError: 122 | output("Invalid value for %s: %r." % (name, argv[1])) 123 | exit(UNKNOWN) 124 | return result 125 | 126 | 127 | 128 | class Rule(object): 129 | """A rule for when to warn or crit based on a stat value.""" 130 | 131 | def check(self, value): 132 | """Checks if this rule should result in a WARN or CRIT.""" 133 | raise NotImplementedError 134 | 135 | 136 | 137 | class Minimum(Rule): 138 | """A rule that specifies minimum acceptable levels for a metric.""" 139 | 140 | def __init__(self, warnLevel, critLevel, unit = ''): 141 | Rule.__init__(self) 142 | assert critLevel <= warnLevel 143 | self.warnLevel = warnLevel 144 | self.critLevel = critLevel 145 | self.unit = unit 146 | 147 | 148 | def check(self, value): 149 | """Checks if the given value is under the minimums.""" 150 | if value < self.critLevel: 151 | return CRITICAL 152 | elif value < self.warnLevel: 153 | return WARNING 154 | else: 155 | return OK 156 | 157 | 158 | def format(self, name, value): 159 | """Formats as perf data.""" 160 | return "'%s'=%.9g%s;%.9g;%.9g;;;" % (name, value, self.unit, self.warnLevel, self.critLevel) 161 | 162 | 163 | def message(self, name, value): 164 | """Create an error message.""" 165 | if self.check(value) == CRITICAL: 166 | return ('%s: %.9g%s < %.9g%s') % (name, value, self.unit, self.critLevel, self.unit) 167 | elif self.check(value) == WARNING: 168 | return ('%s: %.9g%s < %.9g%s') % (name, value, self.unit, self.warnLevel, self.unit) 169 | 170 | 171 | 172 | class Maximum(Rule): 173 | """A rule that specifies maximum acceptable levels for a metric.""" 174 | 175 | def __init__(self, warnLevel, critLevel, unit = ''): 176 | Rule.__init__(self) 177 | assert critLevel >= warnLevel 178 | self.warnLevel = warnLevel 179 | self.critLevel = critLevel 180 | self.unit = unit 181 | 182 | 183 | def check(self, value): 184 | """Checks if the given value exceeds the maximums.""" 185 | if value > self.critLevel: 186 | return CRITICAL 187 | elif value > self.warnLevel: 188 | return WARNING 189 | else: 190 | return OK 191 | 192 | 193 | def format(self, name, value): 194 | """Formats as perf data.""" 195 | return "'%s'=%.9g%s;%.9g;%.9g;;;" % (name, value, self.unit, self.warnLevel, self.critLevel) 196 | 197 | 198 | def message(self, name, value): 199 | """Create an error message.""" 200 | if self.check(value) == CRITICAL: 201 | return '%s: %.9g%s > %.9g%s' % (name, value, self.unit, self.critLevel, self.unit) 202 | elif self.check(value) == WARNING: 203 | return '%s: %.9g%s > %.9g%s' % (name, value, self.unit, self.warnLevel, self.unit) 204 | 205 | 206 | 207 | 208 | class ResponseBuilder(object): 209 | """NRPE response builder.""" 210 | 211 | def __init__(self): 212 | self._stats = [] 213 | self._status = OK 214 | self._messages = [[], [], [], []] 215 | 216 | 217 | def addValue(self, name, value): 218 | """Adds a value to be tracked.""" 219 | self._stats.append("'%s'=%s;;;;;" % (name, str(value))) 220 | return self 221 | 222 | 223 | def addStatLookup(self, name, data, *keys, **kw): 224 | """Adds a stat from a sequential key lookup.""" 225 | value = lookup(data, *keys, **kw) 226 | return self.addValue(name, str(value) + kw.get('suffix', '')) 227 | 228 | 229 | def addStatChildren(self, name, data, *keys, **kw): 230 | """Adds a child for each child of the given dict.""" 231 | values = lookup(data, *keys, **kw) 232 | if values: 233 | for childName, value in values.items(): 234 | self.addValue(name % childName, str(value) + kw.get('suffix', '')) 235 | return self 236 | 237 | 238 | def addRule(self, name, rule, value): 239 | """Adds an alert rule and associated performance data.""" 240 | status = rule.check(value) 241 | if status: 242 | ruleStatus = rule.check(value) 243 | self._status = max(self._status, ruleStatus) 244 | self._messages[ruleStatus].append(rule.message(name, value)) 245 | self._stats.append(rule.format(name, value)) 246 | return self 247 | 248 | 249 | def warnIf(self, condition, message=None): 250 | """Warn on a given condition.""" 251 | if condition: 252 | self.warn(message) 253 | return self 254 | 255 | 256 | def critIf(self, condition, message=None): 257 | """Mark state as critical on the given condition.""" 258 | if condition: 259 | self.crit(message) 260 | return self 261 | 262 | 263 | def unknownIf(self, condition, message=None): 264 | """Mark state as unknown on the given condition.""" 265 | if condition: 266 | self.unknown(message) 267 | return self 268 | 269 | 270 | def warn(self, message=None): 271 | """Mark state as warning.""" 272 | self._status = max(self._status, WARNING) 273 | if message is not None: 274 | self._messages[WARNING].append(message) 275 | return self 276 | 277 | 278 | def crit(self, message=None): 279 | """Mark state as critical.""" 280 | self._status = max(self._status, CRITICAL) 281 | if message is not None: 282 | self._messages[CRITICAL].append(message) 283 | return self 284 | 285 | 286 | def unknown(self, message=None): 287 | """Mark state as unknown.""" 288 | self._status = max(self._status, UNKNOWN) 289 | if message is not None: 290 | self._messages[UNKNOWN].append(message) 291 | return self 292 | 293 | 294 | def message(self, message): 295 | """Set the output message.""" 296 | if message: 297 | self._messages[OK].append(message) 298 | return self 299 | 300 | 301 | def build(self): 302 | """Builds the response.""" 303 | return ' '.join(self._stats) 304 | 305 | 306 | def finish(self): 307 | """Builds the response, prints it, and exits.""" 308 | status = STATUS_NAME[self._status] 309 | messages = self._messages[UNKNOWN] + self._messages[CRITICAL] + self._messages[WARNING] + self._messages[OK] 310 | if messages: 311 | status += ': ' + (', '.join(messages)) 312 | if self._stats: 313 | status += '|' + self.build() 314 | 315 | output(status) 316 | sys.exit(self._status) 317 | -------------------------------------------------------------------------------- /checkserver/README.md: -------------------------------------------------------------------------------- 1 | greplin-nagios-utils: checkserver 2 | ================================= 3 | 4 | Simple server that runs checks without Python startup overhead 5 | -------------------------------------------------------------- 6 | 7 | ### Dependencies 8 | 9 | * [Eventlet](http://eventlet.net/) 10 | 11 | * [Flask](http://flask.pocoo.org/) 12 | 13 | ### Usage 14 | 15 | checkserver loads checks located at 16 | 17 | /usr/lib/nagios/plugins/check_.py 18 | 19 | when you visit 20 | 21 | http://localhost:8111/check/ 22 | 23 | Each check will be run in a thread pool. If you want to include arguments, you can do it 24 | like this: 25 | 26 | http://localhost:8111/check/?arg=hello&arg=world 27 | 28 | When the check changes, you can reload the changed code by visiting 29 | 30 | http://localhost:8111/update/ 31 | 32 | That's it! We also include check.sh which returns the proper exit code from a check. You 33 | can see stats for how many times each check has been called at 34 | 35 | http://localhost:8111/ 36 | 37 | Using checkserver is simple, run an instance of the server, and then add nagios checks like: 38 | 39 | define command { 40 | command_name check_ 41 | command_line /usr/lib/nagios/plugins/check.sh args like $HOSTNAME$ 42 | } 43 | 44 | define service { 45 | use your-service-type 46 | service_description Your Description Here 47 | check_command check_ 48 | hostgroup_name your-hostgroup 49 | } 50 | -------------------------------------------------------------------------------- /checkserver/check.sh: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | 3 | # Copyright 2011 The greplin-nagios-utils Authors. 4 | # 5 | # Licensed under the Apache License, Version 2.0 (the "License"); 6 | # you may not use this file except in compliance with the License. 7 | # You may obtain a copy of the License at 8 | # 9 | # http://www.apache.org/licenses/LICENSE-2.0 10 | # 11 | # Unless required by applicable law or agreed to in writing, software 12 | # distributed under the License is distributed on an "AS IS" BASIS, 13 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 | # See the License for the specific language governing permissions and 15 | # limitations under the License. 16 | 17 | # First argument is the check name. 18 | CHECK=$1 19 | shift 20 | 21 | # Build the rest of the arguments into the arg string for the URL. 22 | CHECK_ARGS='arg=' 23 | if [ "$#" -gt "0" ] 24 | then 25 | CHECK_ARGS="arg="$1 26 | shift 27 | for ARG in "$@" 28 | do 29 | CHECK_ARGS=${CHECK_ARGS}"&arg="${ARG} 30 | done 31 | fi 32 | 33 | RESULT=`curl -s http://localhost:8111/check/${CHECK}?${CHECK_ARGS}` 34 | echo $RESULT 35 | 36 | IFS='|:' 37 | for x in $RESULT; do 38 | case $x in 39 | OK) 40 | exit 0;; 41 | WARN) 42 | exit 1;; 43 | CRIT) 44 | exit 2;; 45 | *) 46 | exit 3;; 47 | esac 48 | done 49 | -------------------------------------------------------------------------------- /checkserver/server.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python 2 | # Copyright 2011 The greplin-nagios-utils Authors. 3 | # 4 | # Licensed under the Apache License, Version 2.0 (the "License"); 5 | # you may not use this file except in compliance with the License. 6 | # You may obtain a copy of the License at 7 | # 8 | # http://www.apache.org/licenses/LICENSE-2.0 9 | # 10 | # Unless required by applicable law or agreed to in writing, software 11 | # distributed under the License is distributed on an "AS IS" BASIS, 12 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | # See the License for the specific language governing permissions and 14 | # limitations under the License. 15 | 16 | """Server that runs Python checks.""" 17 | 18 | from eventlet import wsgi, tpool 19 | import eventlet 20 | from flask import Flask, request, make_response, jsonify, abort 21 | APP = Flask(__name__) 22 | 23 | import imp 24 | import os 25 | import logging 26 | from optparse import OptionParser 27 | from collections import defaultdict 28 | from cStringIO import StringIO 29 | from eloise import nagios 30 | from greplin.nagios import GLOBAL_CONFIG 31 | 32 | # Cache mapping check names to checker modules 33 | CHECK_CACHE = {} 34 | # Arg parser options 35 | OPTIONS = None 36 | # Stats on how many times each checker has run 37 | STATS = defaultdict(int) 38 | # Graphite reporter 39 | GRAPHITE = None 40 | 41 | 42 | def runChecker(fun, name, args): 43 | """Run a checker function with the given args. Return a string.""" 44 | outStream = StringIO() 45 | GLOBAL_CONFIG.outfile = outStream 46 | try: 47 | fun(args) 48 | except SystemExit: 49 | pass 50 | except Exception, e: 51 | logging.exception('Checker %s failed', name) 52 | return 'CRIT: Checker exception: %s' % e 53 | return outStream.getvalue() 54 | 55 | 56 | def checker(name): 57 | """Get a checker function. Caches imports. Writes output to outfile.""" 58 | if name not in CHECK_CACHE: 59 | filename = os.path.join(os.path.dirname(__file__), OPTIONS.checkdir, 'check_%s.py' % name) 60 | if os.path.exists(filename): 61 | CHECK_CACHE[name] = imp.load_source('check_%s' % name, filename) 62 | else: 63 | raise KeyError('No such file: %s' % filename) 64 | 65 | return lambda args: runChecker(CHECK_CACHE[name].check, name, args) 66 | 67 | 68 | @APP.route('/') 69 | def root(): 70 | """Root request handler.""" 71 | return jsonify(STATS) 72 | 73 | 74 | @APP.route('/update/') 75 | def update(name): 76 | """Reload a check module.""" 77 | if name in CHECK_CACHE: 78 | del CHECK_CACHE[name] 79 | return "Reloaded" 80 | else: 81 | abort(404) 82 | 83 | 84 | @APP.route('/check/') 85 | def check(name): 86 | """Run a check.""" 87 | try: 88 | checkFun = checker(name) 89 | except KeyError, e: 90 | print e 91 | return abort(404) 92 | 93 | args = request.args.getlist('arg') 94 | args.insert(0, 'check_%s' % name) 95 | 96 | output = tpool.execute(checkFun, args) 97 | if GRAPHITE: 98 | try: 99 | parsed = nagios.parseResponse(output) 100 | except Exception, e: # ok to catch generic error # pylint: disable=W0703 101 | print 'During %s: %r' % (name, e) 102 | parsed = None 103 | 104 | if parsed and parsed[2]: 105 | for k, v in parsed[2].iteritems(): 106 | if isinstance(v, (int, long, float)): 107 | parts = ['checkserver', name] 108 | parts.extend(args[1:]) 109 | parts.append(k) 110 | GRAPHITE.enqueue('.'.join(parts), v) 111 | if not GRAPHITE.isAlive(): 112 | GRAPHITE.start() 113 | 114 | resp = make_response(output) 115 | STATS[name] += 1 116 | 117 | resp.headers['Content-Type'] = 'text/plain; charset=UTF-8' 118 | return resp 119 | 120 | 121 | def main(): 122 | """Run the server.""" 123 | global OPTIONS # pylint: disable=W0603 124 | parser = OptionParser() 125 | parser.add_option("-d", "--checkdir", dest="checkdir", metavar="DIR", 126 | default="/usr/lib/nagios/plugins", help="directory with check scripts") 127 | parser.add_option("-l", "--log-level", dest="loglevel", metavar="LEVEL", 128 | help="logging level", default='info') 129 | parser.add_option("-g", "--graphite", dest="graphite", metavar="GRAPHITE_HOST", 130 | help="graphite host, specify as host:post", default='') 131 | parser.add_option("-p", "--port", dest="port", metavar="PORT", 132 | help="port to listen on", default=8111, type="int") 133 | OPTIONS = parser.parse_args()[0] 134 | 135 | levelName = {'debug': logging.DEBUG, 'info': logging.INFO, 'warn': logging.WARN, 'error': logging.ERROR} 136 | logging.basicConfig(level=levelName.get(OPTIONS.loglevel.lower(), logging.WARN)) 137 | 138 | if OPTIONS.graphite: 139 | from greplin.scales import util 140 | 141 | host, port = OPTIONS.graphite.split(':') 142 | 143 | global GRAPHITE # pylint: disable=W0603 144 | GRAPHITE = util.GraphiteReporter(host, int(port)) 145 | GRAPHITE.start() 146 | 147 | wsgi.server(eventlet.listen(('', int(OPTIONS.port))), APP) 148 | 149 | 150 | if __name__ == '__main__': 151 | main() 152 | -------------------------------------------------------------------------------- /checkserver/testchecks/README.md: -------------------------------------------------------------------------------- 1 | Example checks for testing 2 | --------- 3 | 4 | These checks are short, don't do much, and are here for testing out the check server. To run them, try this: 5 | 6 | python server.py -d testchecks/ 7 | 8 | And go to one of the following URLs: 9 | 10 | http://localhost:8111/check/slow?arg=hello 11 | http://localhost:8111/check/fast?arg=world 12 | 13 | The fast check is fast, the slow check takes five seconds. 14 | -------------------------------------------------------------------------------- /checkserver/testchecks/check_error.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python 2 | # Copyright 2012 The greplin-nagios-utils Authors. 3 | # 4 | # Licensed under the Apache License, Version 2.0 (the "License"); 5 | # you may not use this file except in compliance with the License. 6 | # You may obtain a copy of the License at 7 | # 8 | # http://www.apache.org/licenses/LICENSE-2.0 9 | # 10 | # Unless required by applicable law or agreed to in writing, software 11 | # distributed under the License is distributed on an "AS IS" BASIS, 12 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | # See the License for the specific language governing permissions and 14 | # limitations under the License. 15 | 16 | """Status 17 | 18 | nagios config: 19 | use regular-service 20 | params $HOSTNAME$ 21 | """ 22 | 23 | 24 | from greplin.nagios import parseArgs, Maximum, ResponseBuilder 25 | 26 | 27 | def check(argv): 28 | """Runs the check.""" 29 | _ = parseArgs('check_fast.py', ('NAME', str), argv=argv) / 0 # Badness! 30 | 31 | 32 | (ResponseBuilder().addRule('seven', Maximum(8, 11), 7)).finish() 33 | 34 | 35 | if __name__ == '__main__': 36 | import sys 37 | check(sys.argv) 38 | -------------------------------------------------------------------------------- /checkserver/testchecks/check_fast.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python 2 | # Copyright 2012 The greplin-nagios-utils Authors. 3 | # 4 | # Licensed under the Apache License, Version 2.0 (the "License"); 5 | # you may not use this file except in compliance with the License. 6 | # You may obtain a copy of the License at 7 | # 8 | # http://www.apache.org/licenses/LICENSE-2.0 9 | # 10 | # Unless required by applicable law or agreed to in writing, software 11 | # distributed under the License is distributed on an "AS IS" BASIS, 12 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | # See the License for the specific language governing permissions and 14 | # limitations under the License. 15 | 16 | """Status 17 | 18 | nagios config: 19 | use regular-service 20 | params $HOSTNAME$ 21 | """ 22 | 23 | 24 | from greplin.nagios import parseArgs, Maximum, ResponseBuilder 25 | 26 | 27 | def check(argv): 28 | """Runs the check.""" 29 | _ = parseArgs('check_fast.py', ('NAME', str), argv=argv) 30 | 31 | (ResponseBuilder().addRule('seven', Maximum(8, 11), 7)).finish() 32 | 33 | 34 | if __name__ == '__main__': 35 | import sys 36 | check(sys.argv) 37 | -------------------------------------------------------------------------------- /checkserver/testchecks/check_slow.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python 2 | # Copyright 2012 The greplin-nagios-utils Authors. 3 | # 4 | # Licensed under the Apache License, Version 2.0 (the "License"); 5 | # you may not use this file except in compliance with the License. 6 | # You may obtain a copy of the License at 7 | # 8 | # http://www.apache.org/licenses/LICENSE-2.0 9 | # 10 | # Unless required by applicable law or agreed to in writing, software 11 | # distributed under the License is distributed on an "AS IS" BASIS, 12 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | # See the License for the specific language governing permissions and 14 | # limitations under the License. 15 | 16 | """Status 17 | 18 | nagios config: 19 | use regular-service 20 | params $HOSTNAME$ 21 | """ 22 | 23 | 24 | from greplin.nagios import parseArgs, Maximum, ResponseBuilder 25 | import time 26 | 27 | 28 | def check(argv): 29 | """Runs the check.""" 30 | _ = parseArgs('check_slow.py', ('NAME', str), argv=argv) 31 | time.sleep(5) 32 | 33 | (ResponseBuilder().addRule('harrypotter', Maximum(42, 108), 69)).finish() 34 | 35 | 36 | if __name__ == '__main__': 37 | import sys 38 | check(sys.argv) 39 | -------------------------------------------------------------------------------- /genlib/.gitignore: -------------------------------------------------------------------------------- 1 | build -------------------------------------------------------------------------------- /genlib/README.md: -------------------------------------------------------------------------------- 1 | greplin-nagios-utils: genlib 2 | ============================== 3 | 4 | Library that makes writing Nagios configuration easy and pythonic 5 | ----------------------------------------------------------------- 6 | 7 | ### Sample 8 | 9 | TODO 10 | -------------------------------------------------------------------------------- /genlib/setup.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python 2 | # Copyright 2011 The greplin-nagios-utils Authors. 3 | # 4 | # Licensed under the Apache License, Version 2.0 (the "License"); 5 | # you may not use this file except in compliance with the License. 6 | # You may obtain a copy of the License at 7 | # 8 | # http://www.apache.org/licenses/LICENSE-2.0 9 | # 10 | # Unless required by applicable law or agreed to in writing, software 11 | # distributed under the License is distributed on an "AS IS" BASIS, 12 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | # See the License for the specific language governing permissions and 14 | # limitations under the License. 15 | 16 | """Setup script for Greplin utilities for generating Nagios configuration.""" 17 | 18 | try: 19 | from setuptools import setup 20 | except ImportError: 21 | from distutils.core import setup 22 | 23 | setup(name='greplin-nagios-genlib', 24 | version='0.1', 25 | description='Python utilties for creating Nagios configurations.', 26 | license='Apache', 27 | author='Greplin, Inc.', 28 | url='https://www.github.com/Cue/greplin-nagios-utils/genlib', 29 | package_dir = {'':'src'}, 30 | packages = [ 31 | 'greplin', 32 | ], 33 | namespace_packages = [ 34 | 'greplin', 35 | ], 36 | test_suite = 'nose.collector', 37 | zip_safe = True 38 | ) 39 | -------------------------------------------------------------------------------- /genlib/src/greplin/__init__.py: -------------------------------------------------------------------------------- 1 | # Copyright 2011 The greplin-nagios-utils Authors. 2 | # 3 | # Licensed under the Apache License, Version 2.0 (the "License"); 4 | # you may not use this file except in compliance with the License. 5 | # You may obtain a copy of the License at 6 | # 7 | # http://www.apache.org/licenses/LICENSE-2.0 8 | # 9 | # Unless required by applicable law or agreed to in writing, software 10 | # distributed under the License is distributed on an "AS IS" BASIS, 11 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | # See the License for the specific language governing permissions and 13 | # limitations under the License. 14 | 15 | """The Greplin root package.""" 16 | 17 | import pkg_resources 18 | pkg_resources.declare_namespace('greplin') 19 | -------------------------------------------------------------------------------- /genlib/src/greplin/nagiosconf.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python 2 | # Copyright 2011 The greplin-nagios-utils Authors. 3 | # 4 | # Licensed under the Apache License, Version 2.0 (the "License"); 5 | # you may not use this file except in compliance with the License. 6 | # You may obtain a copy of the License at 7 | # 8 | # http://www.apache.org/licenses/LICENSE-2.0 9 | # 10 | # Unless required by applicable law or agreed to in writing, software 11 | # distributed under the License is distributed on an "AS IS" BASIS, 12 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | # See the License for the specific language governing permissions and 14 | # limitations under the License. 15 | 16 | """Configuration generator for Nagios.""" 17 | 18 | import sys 19 | 20 | 21 | 22 | class NagObj(object): 23 | """base nagios object 24 | """ 25 | 26 | def __init__(self, name): 27 | object.__init__(self) 28 | self.name = name 29 | self.props = {} 30 | self.meta = {} 31 | 32 | 33 | def __repr__(self): 34 | assert self.typeName != None 35 | if not self.props: 36 | return "# skipped define for empty %s %s\n" % (self.typeName, self.name) 37 | 38 | ret = ["define %s {" % self.typeName ] 39 | mlen = max([ len(k) for k in self.props.keys()]) + 2 40 | for k, v in self.props.items(): 41 | ret.append(" %s%s%s" % (k, ' ' * (mlen-len(k)), v)) 42 | ret.append("}") 43 | return "\n".join(ret) 44 | 45 | 46 | 47 | class NagBag(object): 48 | """bags of nagios objects - take care of creation, name uniqueness, ... 49 | """ 50 | 51 | 52 | def __init__(self, klass): 53 | object.__init__(self) 54 | self.klass = klass 55 | self.bag = {} 56 | 57 | 58 | def create(self, name): 59 | """Create a new object with the given name 60 | """ 61 | assert not name in self.bag 62 | 63 | inst = self.klass(name) 64 | self.bag[name] = inst 65 | return inst 66 | 67 | 68 | def get(self, name): 69 | """Get a object by name 70 | """ 71 | return self.bag.get(name, None) 72 | 73 | 74 | def getOrCreate(self, name): 75 | """Create or get a new object with the given name 76 | """ 77 | assert name is not None 78 | name = name.strip() 79 | assert len(name) > 0 80 | 81 | if name in self.bag: 82 | return self.bag[name] 83 | 84 | return self.create(name) 85 | 86 | 87 | def generate(self, out): 88 | """Write config fragemts for this bag to the given output stream 89 | """ 90 | for item in sorted(self.bag.items()): 91 | out.write('%s\n' % repr(item[1])) 92 | 93 | 94 | 95 | 96 | class HostGroup(NagObj): 97 | """Represent a nagios hostgroup 98 | """ 99 | typeName = 'hostgroup' 100 | 101 | 102 | def __init__(self, name): 103 | NagObj.__init__(self, name) 104 | self.members = [] 105 | 106 | 107 | def add(self, member): 108 | """Add a host to this group 109 | """ 110 | self.members.append(member) 111 | 112 | 113 | def __repr__(self): 114 | self.props['hostgroup_name'] = self.name 115 | return NagObj.__repr__(self) 116 | 117 | 118 | 119 | class HostGroupBag(NagBag): 120 | """The set of host groups 121 | """ 122 | 123 | def __init__(self): 124 | NagBag.__init__(self, HostGroup) 125 | 126 | 127 | 128 | HOSTGROUPS = HostGroupBag() 129 | 130 | 131 | 132 | class Host(NagObj): 133 | """Represent a nagios host 134 | """ 135 | 136 | typeName = 'host' 137 | 138 | 139 | def __init__(self, name): 140 | NagObj.__init__(self, name) 141 | self.hostgroups = set() 142 | 143 | 144 | def addGroup(self, name): 145 | """Mark this host as a member of the given group, creating the group if needed 146 | """ 147 | hg = HOSTGROUPS.getOrCreate(name) 148 | self.hostgroups.add(hg) 149 | hg.add(self) 150 | 151 | 152 | def __repr__(self): 153 | self.props['host_name'] = self.name 154 | self.props['hostgroups'] = ','.join(sorted([hg.name for hg in self.hostgroups])) 155 | return NagObj.__repr__(self) 156 | 157 | 158 | 159 | class HostBag(NagBag): 160 | """The set of hosts 161 | """ 162 | 163 | def __init__(self): 164 | NagBag.__init__(self, Host) 165 | 166 | 167 | 168 | HOSTS = HostBag() 169 | 170 | 171 | 172 | class Service(NagObj): 173 | """Represent a nagios service 174 | """ 175 | typeName = 'service' 176 | 177 | 178 | 179 | class ServiceBag(NagBag): 180 | """The set of services 181 | """ 182 | 183 | def __init__(self): 184 | NagBag.__init__(self, Service) 185 | 186 | 187 | SERVICES = ServiceBag() 188 | 189 | 190 | def generate(out=sys.stdout): 191 | """Print nagios configuration fragments to the given output stream 192 | """ 193 | HOSTGROUPS.generate(out) 194 | HOSTS.generate(out) 195 | SERVICES.generate(out) 196 | --------------------------------------------------------------------------------