def test_generate_threshold_config(self): """ Test: generate_threshold_config, utility function * generates exact config from a given set of thresholds """ obj1 = Threshold(type="counter", warning_min=0, warning_max=1000, failure_min=0, failure_max=1200, invert=False, persist=False, type_instance="some_instance") obj2 = Threshold(type="df", warning_max=90, percentage=True) obj3 = Threshold(type="load", datasource="midterm", warning_max=1, hysteresis=0.3) obj4 = Threshold(type="cpu", type_instance="user", warning_max=85, hits=6) obj5 = Threshold(plugin="interface", plugin_instance="eth0", type="if_octets", datasource="rx", failure_max=10000000) obj6 = Threshold(host="hostname", type="cpu", type_instance="idle", failure_min=10) obj7 = Threshold(host="hostname", plugin="memory", type="memory", type_instance="cached", warning_min=100000000) for obj in [obj1, obj2, obj3, obj4, obj5, obj6, obj7]: db.session.add(obj) db.session.commit() self.assertEqual(Threshold.query.count(), 7) result_set = Threshold.query.order_by(Threshold.host). \ order_by(Threshold.plugin).order_by(Threshold.type). \ order_by(Threshold.id) with open(self.app.config["collectd_threshold_file"], "r") as f: self.assertEqual(f.read(), generate_threshold_config(result_set))
def config_thresholds(pid=None): """ Saves data from database into the file (set up in settings.settings["collectd_threshold_file"].) After successful save, restarts the server. """ # backup current config filename = current_app.config.get("collectd_threshold_file", "thresholds.conf") filename = os.path.join(os.path.dirname(__file__), filename) backup = filename + ".bak" try: shutil.copyfile(filename, backup) except IOError: return "Configuration file not spotted.", 404 result_set = Threshold.query.order_by(Threshold.host). \ order_by(Threshold.plugin).order_by(Threshold.type). \ order_by(Threshold.id) try: F = open(filename, "w") F.write(generate_threshold_config(result_set)) F.close() except IOError: shutil.move(backup, filename) return "Cannot save file.", 404 try: # test if the new config works result = subprocess.check_output(["collectd", "-t"]) if result: # unfortunately there might be errors, even though process' return # code is 0. But possible errors appear in the output, so we check # if it exists raise CalledProcessError("Should be no output", 1) except (CalledProcessError, OSError): # restore backup in case of failure shutil.move(backup, filename) return "Something in config is wrong, reverting.", 500 else: os.remove(backup) # restart the server in case of success try: pid = pid or subprocess.check_output(["pidof", "collectdmon" ]).strip().split()[0] os.kill(int(pid), signal.SIGHUP) except (subprocess.CalledProcessError, OSError): return "Cannot restart collectd daemon. You should restart it " + \ "manually on your own.", 503 else: return "Configuration updated, server restarted.", 200
def config_thresholds(pid=None): """ Saves data from database into the file (set up in settings.settings["collectd_threshold_file"].) After successful save, restarts the server. """ # backup current config filename = current_app.config.get("collectd_threshold_file", "thresholds.conf") filename = os.path.join(os.path.dirname(__file__), filename) backup = filename + ".bak" try: shutil.copyfile(filename, backup) except IOError: return "Configuration file not spotted.", 404 result_set = Threshold.query.order_by(Threshold.host). \ order_by(Threshold.plugin).order_by(Threshold.type). \ order_by(Threshold.id) try: F = open(filename, "w") F.write(generate_threshold_config(result_set)) F.close() except IOError: shutil.move(backup, filename) return "Cannot save file.", 404 try: # test if the new config works result = subprocess.check_output(["collectd", "-t"]) if result: # unfortunately there might be errors, even though process' return # code is 0. But possible errors appear in the output, so we check # if it exists raise CalledProcessError("Should be no output", 1) except (CalledProcessError, OSError): # restore backup in case of failure shutil.move(backup, filename) return "Something in config is wrong, reverting.", 500 else: os.remove(backup) # restart the server in case of success try: pid = pid or subprocess.check_output(["pidof", "collectdmon"]).strip().split()[0] os.kill(int(pid), signal.SIGHUP) except (subprocess.CalledProcessError, OSError): return "Cannot restart collectd daemon. You should restart it " + \ "manually on your own.", 503 else: return "Configuration updated, server restarted.", 200