Beispiel #1
0
    def discover_instances(self):
        config = self._config
        discovery_interval = config.instance.get('discovery_interval', 3600)
        while self._running:
            start_time = time.time()
            for host in config.ip_network.hosts():
                host = str(host)
                if host in config.discovered_instances:
                    continue
                instance = config.instance.copy()
                instance.pop('network_address')
                instance['ip_address'] = host

                host_config = self._build_config(instance)
                try:
                    sys_object_oid = self.fetch_sysobject_oid(host_config)
                except Exception as e:
                    self.log.debug("Error scanning host %s: %s", host, e)
                    continue
                if sys_object_oid not in self.profiles_by_oid:
                    if not (host_config.table_oids or host_config.raw_oids):
                        self.log.warn("Host %s didn't match a profile for sysObjectID %s", host, sys_object_oid)
                        continue
                else:
                    profile = self.profiles_by_oid[sys_object_oid]
                    host_config.refresh_with_profile(self.profiles[profile], self.warning, self.log)
                config.discovered_instances[host] = host_config

            write_persistent_cache(self.check_id, json.dumps(list(config.discovered_instances)))

            time_elapsed = time.time() - start_time
            if discovery_interval - time_elapsed > 0:
                time.sleep(discovery_interval - time_elapsed)
Beispiel #2
0
    def write_persistent_cache(self, key, value):
        # type: (str, str) -> None
        """Stores `value` in a persistent cache for this check instance.
        The cache is located in a path where the agent is guaranteed to have read & write permissions. Namely in
            - `%ProgramData%\\Datadog\\run` on Windows.
            - `/opt/datadog-agent/run` everywhere else.
        The cache is persistent between agent restarts but will be rebuilt if the check instance configuration changes.

        - **key** (_str_) - Identifier used to build the filename
        - **value** (_str_) - Value to store
        """
        datadog_agent.write_persistent_cache(self._persistent_cache_id(key), value)
Beispiel #3
0
    def _start_discovery(self):
        cache = read_persistent_cache(self.check_id)
        if cache:
            hosts = json.loads(cache)
            for host in hosts:
                try:
                    ipaddress.ip_address(host)
                except ValueError:
                    write_persistent_cache(self.check_id, json.dumps([]))
                    break
                instance = self.instance.copy()
                instance.pop('network_address')
                instance['ip_address'] = host

                host_config = self._build_config(instance)
                self._config.discovered_instances[host] = host_config

        self._thread = threading.Thread(target=self.discover_instances, name=self.name)
        self._thread.daemon = True
        self._thread.start()
 def write_persistent_cache(self, key, value):
     # type: (str, str) -> None
     datadog_agent.write_persistent_cache(self._persistent_cache_id(key),
                                          value)