diff --git a/Docker_Build/config.ini b/Docker_Build/config.ini index 65d3a47..1e68004 100644 --- a/Docker_Build/config.ini +++ b/Docker_Build/config.ini @@ -23,6 +23,11 @@ # host2 = 8.8.4.4 # host3 = 1.1.1.1 +[hosts_timeout] # OPTIONAL; ping timeout in seconds (float); default is '1' +# host1 = 1 +# host2 = 0.5 +# host3 = 0.2 + [hosts_timer] # OPTIONAL; ping interval in seconds; default is '5' # host1 = 1 # host2 = 3 diff --git a/Docker_Build/latency_monitor.py b/Docker_Build/latency_monitor.py index 92f1e60..c29fc9c 100644 --- a/Docker_Build/latency_monitor.py +++ b/Docker_Build/latency_monitor.py @@ -29,7 +29,7 @@ class MyInfluxDB(): writes received data to Influxdb: host: string which includes IP or FQDN host_location: string which includes location of the host - ping_response: string which includes the ping reply in ms + ping_response: float which includes the ping reply in ms """ def __init__(self): @@ -100,20 +100,23 @@ class ThreadPing(Thread): ---------- db: InfluxDB Object host: string which includes IP or FQDN + host_timeout: float which defines how long we wait for a reply host_timer: integer which defines how often pings are send in seconds (min. 1) host_location: string which includes location of the host """ - def __init__(self, db, host, host_timer, host_location): + + def __init__(self, db, host, host_timeout, host_timer, host_location): Thread.__init__(self) self.MyDB = db self.host = host + self.host_timeout = host_timeout self.host_timer = host_timer self.host_location = host_location def run(self): self.starttime = time() while True: - self.ping_response_list = ping(self.host, count=1) + self.ping_response_list = ping(self.host, timeout=self.host_timeout, count=1) self.ping_response = "{:.2f}".format(self.ping_response_list.rtt_avg_ms) self.MyDB.write(self.host, self.host_location, self.ping_response) sleep(self.host_timer - ((time() - self.starttime) % 1)) @@ -129,6 +132,7 @@ def main(): ## IF ENVIRONMENT VARIABLES ARE PASSED IGNORE CONFIG FILE if 'TARGET_HOST' in os.environ: host = os.environ['TARGET_HOST'] + host_timeout = float(os.getenv('TARGET_TIMEOUT', 1)) host_timer = int(os.getenv('TARGET_TIMER', 5)) host_location = os.getenv('TARGET_LOCATION', 'unknown') @@ -149,6 +153,9 @@ def main(): # Create thread for each configured host for key, host in host_items: + # Check if hosts timeout is set otherwise use "1" (means 1 seconds) + host_timeout = float(config.get('hosts_timeout', key, fallback=1)) + # Check if hosts timer is set otherwise use "5" (means 5 seconds) host_timer = int(config.get('hosts_timer', key, fallback=5)) @@ -157,7 +164,7 @@ def main(): # Create Thread print("Creating thread for: %s, with interval: %s and location: %s" %(host, host_timer, host_location)) - thread = ThreadPing(MyDB, host, host_timer, host_location) + thread = ThreadPing(MyDB, host, host_timeout, host_timer, host_location) my_threads.append(thread) thread.start() diff --git a/README.md b/README.md index eb2f97c..67a607c 100644 --- a/README.md +++ b/README.md @@ -77,6 +77,8 @@ TARGET_LOCATION | Google | decript. location | option See [./latency-monitor/config.ini](./latency-monitor/config.ini) +**ENV wins over file** + #### Docker-Compose Style diff --git a/docker-compose-full_stack.yml b/docker-compose-full_stack.yml index bcc3607..44f576f 100644 --- a/docker-compose-full_stack.yml +++ b/docker-compose-full_stack.yml @@ -134,6 +134,7 @@ services: - INFLUX_BUCKET - INFLUX_ORG - TARGET_HOST + - TARGET_TIMEOUT - TARGET_TIMER - TARGET_LOCATION networks: diff --git a/env b/env index 524f797..9145dbe 100644 --- a/env +++ b/env @@ -36,5 +36,6 @@ INFLUX_TOKEN=YOUR_ADMIN_TOKEN INFLUX_BUCKET=YOUR_BUCKET_NAME INFLUX_ORG=YOUR_ORGANIZATION TARGET_HOST=YOUR_MONITORED_TARGET +TARGET_TIMEOUT=YOUR_MONITORED_TARGET_TIMEOUT TARGET_TIMER=YOUR_MONITORED_TARGET_TIMER TARGET_LOCATION=YOUR_MONITORED_TARGET_LOCATION \ No newline at end of file diff --git a/latency-monitor/config-template.ini b/latency-monitor/config-template.ini index 65d3a47..1e68004 100644 --- a/latency-monitor/config-template.ini +++ b/latency-monitor/config-template.ini @@ -23,6 +23,11 @@ # host2 = 8.8.4.4 # host3 = 1.1.1.1 +[hosts_timeout] # OPTIONAL; ping timeout in seconds (float); default is '1' +# host1 = 1 +# host2 = 0.5 +# host3 = 0.2 + [hosts_timer] # OPTIONAL; ping interval in seconds; default is '5' # host1 = 1 # host2 = 3