def check_one_file(self, fname): self.log("file to check: %s" % fname, 1) if not os.path.exists(fname): msg = "File not found: %s" % fname if self.options.missing_warn: self.exit_warn(msg) self.exit_crit(msg) size_check = (self.options.size_min > -1) or (self.options.size_max > -1) if size_check: self.ensure_size_is_ok(fname) warn = TimeUnits() if self.options.age_warn: try: warn = TimeUnits(self.options.age_warn) except: self.exit_crit("invalid syntax for option -w: %s" % self.options.age_warn) crit = TimeUnits() try: crit = TimeUnits(self.options.age_crit) except: self.exit_crit("invalid syntax for option -c: %s" % self.options.age_crit) if size_check and ((warn.value == 0) and (crit.value == 0)): self.exit_ok("file size good") if not self.options.age_crit: self.exit_crit("-c options must be specified!") if warn: self.log("warn level: %s" % warn.get(), 1) self.log("crit level: %s" % crit.get(), 1) if self.options.age_min: if warn and (warn <= crit): self.exit_crit("warning age must be higher than critical age") else: if warn and (warn >= crit): self.exit_crit("warning age must be less than critical age") last_changed = os.stat(fname).st_mtime self.log("last changed timestamp for file: %s" % last_changed, 2) age = TimeUnits(time.time() - last_changed) self.add_perf_data("age", age.value, warn.value, crit.value, 0) self.log("File age: %s" % age, 2) msg = "Age of file %s is %s" % (fname, age.get()) if self.options.age_min: if age < crit: self.exit_crit(msg) elif warn.value and (age < warn): self.exit_warn(msg) else: if age > crit: self.exit_crit(msg) elif warn.value and (age > warn): self.exit_warn(msg) self.exit_ok(msg)
def workload(self): if not self.options.query: self.exit_crit('-m option must be specified!') if not self.options.db: self.exit_crit('-d option must be specified!') if not (self.options.age_crit and self.options.age_warn): self.exit_crit('-w and -c options must be specified!') warn = TimeUnits(self.options.age_warn) crit = TimeUnits(self.options.age_crit) self.log('mysql query to run: %s' % self.options.query, 1) self.log('warn level: %s' % warn.get(), 1) self.log('crit level: %s' % crit.get(), 1) if warn >= crit: self.exit_crit('warning age must be less than critical age') retcode, stdout, stderr = self.cmd_execute_output( 'echo "%s" | mysql %s | tail -n 1' % (self.options.query, self.options.db)) if retcode: self.exit_crit('mysql failed: %s' % stderr) dt = datetime.datetime.strptime(stdout.strip(), '%Y-%m-%d %H:%M:%S') age = TimeUnits(date_time=dt) self.log('mysql timestamp: %s' % age, 2) msg = 'timestamp is %s old' % age.get() if age > crit: self.exit_crit(msg) elif age > warn: self.exit_warn(msg) self.exit_ok(msg)