|
47 | 47 | import sys |
48 | 48 | import time |
49 | 49 |
|
| 50 | +from vsc.utils import _script_name |
50 | 51 | from vsc.utils.cache import FileCache |
51 | 52 | from vsc.utils.fancylogger import getLogger |
52 | 53 |
|
53 | 54 | log = getLogger(__name__) |
54 | 55 |
|
55 | 56 | NAGIOS_CACHE_DIR = '/var/cache' |
| 57 | +NAGIOS_CACHE_FILENAME = 'cache.nagios.json.gz' |
56 | 58 | NAGIOS_CACHE_FILENAME_TEMPLATE = '%s.nagios.json.gz' |
57 | 59 |
|
58 | 60 | NAGIOS_OK = 'OK' |
@@ -241,6 +243,72 @@ def alert(self, test): |
241 | 243 | """ |
242 | 244 | return not self.range_fn(test) |
243 | 245 |
|
| 246 | +class NagiosStatusMixin: |
| 247 | + """ |
| 248 | + A mixin class providing methods for Nagios status codes. |
| 249 | +
|
| 250 | + Note that these methods do not return, they exit the script. |
| 251 | +
|
| 252 | + Options come from NAGIOS_MIXIN_OPTIONS. |
| 253 | + """ |
| 254 | + |
| 255 | + NAGIOS_MIXIN_OPTIONS = { |
| 256 | + 'nagios-report': ('print out nagios information', None, 'store_true', False, 'n'), |
| 257 | + 'nagios-check-filename': |
| 258 | + ('filename of where the nagios check data is stored', 'str', 'store', |
| 259 | + os.path.join( |
| 260 | + NAGIOS_CACHE_DIR, |
| 261 | + NAGIOS_CACHE_FILENAME_TEMPLATE % (_script_name(sys.argv[0]),) |
| 262 | + ) |
| 263 | + ), |
| 264 | + 'nagios-check-interval-threshold': ('threshold of nagios checks timing out', 'int', 'store', 0), |
| 265 | + 'nagios-user': ('user nagios runs as', 'str', 'store', 'nrpe'), |
| 266 | + 'nagios-world-readable-check': ('make the nagios check data file world readable', None, 'store_true', False), |
| 267 | + } |
| 268 | + |
| 269 | + def nagios_prologue(self): |
| 270 | + """ |
| 271 | + This will set up the reporter, but exit immediately of the report is requested |
| 272 | + """ |
| 273 | + # bail if nagios report is requested |
| 274 | + self.nagios = SimpleNagios( |
| 275 | + _cache=self.options.nagios_check_filename, |
| 276 | + _report_and_exit=self.options.nagios_report, |
| 277 | + _threshold=self.options.nagios_check_interval_threshold, |
| 278 | + _cache_user=self.options.nagios_user, |
| 279 | + _world_readable=self.options.nagios_world_readable_check, |
| 280 | + ) |
| 281 | + |
| 282 | + def nagios_epilogue(self, nagios_exit, nagios_message): |
| 283 | + """ |
| 284 | + This will write the result to the cache file |
| 285 | + """ |
| 286 | + self.nagios._exit(nagios_exit, nagios_message) |
| 287 | + |
| 288 | + def ok(self, msg): |
| 289 | + """ |
| 290 | + Convenience method that exits with Nagios OK exit code. |
| 291 | + """ |
| 292 | + exit_from_errorcode(0, msg) |
| 293 | + |
| 294 | + def warning(self, msg): |
| 295 | + """ |
| 296 | + Convenience method that exits with Nagios WARNING exit code. |
| 297 | + """ |
| 298 | + exit_from_errorcode(1, msg) |
| 299 | + |
| 300 | + def critical(self, msg): |
| 301 | + """ |
| 302 | + Convenience method that exits with Nagios CRITICAL exit code. |
| 303 | + """ |
| 304 | + exit_from_errorcode(2, msg) |
| 305 | + |
| 306 | + def unknown(self, msg): |
| 307 | + """ |
| 308 | + Convenience method that exits with Nagios UNKNOWN exit code. |
| 309 | + """ |
| 310 | + exit_from_errorcode(3, msg) |
| 311 | + |
244 | 312 |
|
245 | 313 | class NagiosReporter: |
246 | 314 | """Reporting class for Nagios/Icinga reports. |
|
0 commit comments