class PortExtension(RemoteBasePlugin): def initialize(self, **kwargs): # The Dynatrace API client self.client = DynatraceAPI(self.config.get("api_url"), self.config.get("api_token"), log=log) self.executions = 0 def query(self, **kwargs) -> None: log.setLevel(self.config.get("log_level")) name = self.config.get("test_name") target_ip = self.config.get("test_target_ip") target_ports = self.config.get("test_target_ports", "").split(",") location = self.config.get( "test_location", "") if self.config.get("test_location") else "ActiveGate" frequency = int(self.config.get("frequency")) if self.config.get( "frequency") else 1 if self.executions % frequency == 0: for port in target_ports: if port: success, response_time = test_port(target_ip, int(port)) test_name = f"{name} {target_ip}:{port}" log.info( f"{target_ip}:{port} = {success}, {response_time}") self.client.report_simple_test(test_name, location, success, response_time, test_type="Port", interval=frequency * 60) if not success: self.client.report_simple_event( test_name, f"Port check failed for {name}, target: {target_ip}:{port}", location, state="open", test_type="Port", ) else: self.client.report_simple_event( test_name, f"Port check failed for {name}, target: {target_ip}:{port}", location, state="resolved", test_type="Port", ) self.executions += 1
class DNSExtension(RemoteBasePlugin): def initialize(self, **kwargs): # The Dynatrace API client self.client = DynatraceAPI(self.config.get("api_url"), self.config.get("api_token"), log=log) self.executions = 0 def query(self, **kwargs) -> None: log.setLevel(self.config.get("log_level")) name = self.config.get("test_name") dns_server = self.config.get("dns_server") host = self.config.get("host") location = self.config.get("test_location") if self.config.get( "test_location") else "ActiveGate" frequency = int(self.config.get("frequency")) if self.config.get( "frequency") else 15 if self.executions % frequency == 0: success, response_time = test_dns(dns_server, host) log.info( f"DNS test, DNS server: {dns_server}, host: {host}, success: {success}, time: {response_time} " ) self.client.report_simple_test( name, location, success, response_time, test_type="DNS", interval=frequency * 60, edit_link= f"#settings/customextension;id={self.plugin_info.name}", ) if not success: self.client.report_simple_event( name, f"DNS lookup failed for {name}, server: {dns_server}, host: {host}", location) else: self.client.report_simple_event( name, f"DNS lookup failed for {name}, server: {dns_server}, host: {host}", location, state="resolved") self.executions += 1
class PingExtension(RemoteBasePlugin): def initialize(self, **kwargs): # The Dynatrace API client self.client = DynatraceAPI(self.config.get("api_url"), self.config.get("api_token"), log=log) self.executions = 0 def query(self, **kwargs) -> None: log.setLevel(self.config.get("log_level")) name = self.config.get("test_name") target = self.config.get("test_target") location = self.config.get( "test_location", "") if self.config.get("test_location") else "ActiveGate" frequency = int(self.config.get("frequency")) if self.config.get( "frequency") else 15 if self.executions % frequency == 0: ping_result = ping(target) log.info(ping_result.as_dict()) self.client.report_simple_test( name, location, ping_result.packet_loss_rate is not None and ping_result.packet_loss_rate == 0, ping_result.rtt_avg or 0, interval=frequency * 60, edit_link= f"#settings/customextension;id={self.plugin_info.name}", ) if ping_result.packet_loss_rate is None or ping_result.packet_loss_rate > 0: self.client.report_simple_event( name, f"Ping failed for {name}, target: {target}", location) else: self.client.report_simple_event( name, f"Ping failed for {name}, target: {target}", location, state="resolved") self.executions += 1