Esempio n. 1
0
    def _postAgentInfoToServer(self):
        import json
        import time
        import uuid
        import requests
        import platform, socket

        if not self._send_controler % 12:
            self._send_controler += 1
            return
        self._send_controler += 1
        hostname = get_hostname(self._agentConfig)
        if self._agentConfig['tags']:
            tags = self._agentConfig['tags'].split(',')
        else:
            tags = []
        hostid = uuid.uuid5(uuid.NAMESPACE_DNS,
                            platform.node() + str(uuid.getnode())).hex
        url = self._agentConfig['m_url'].replace(
            'api/v2/gateway/dd-agent',
            'api/v2/agent/info/intake?api_key=') + self._agentConfig['api_key']
        modified = time.mktime(time.localtime(time.time()))

        path = ''
        if platform.system() == 'Linux':
            path = _unix_confd_path()
        elif platform.system() == 'Windows' and os.path.exists(
                'C:/ProgramData/Datamonitor/conf.d'):
            path = _windows_confd_path()
        elif platform.system() == 'Windows' and os.path.exists(
                'C:/Documents and Settings/All Users/Application Data/Datamonitor'
        ):
            path = 'C:/Documents and Settings/All Users/Application Data/Datamonitor'
        if path:
            file_list = os.walk(path).next()[2]
            apps = [
                f for f in file_list
                if f.endswith(".yaml") or f.endswith(".yaml.default")
            ]

        post_data = {
            "id": str(hostid),
            "hostname": hostname,
            "ip": self.ip,
            "tags": tags,
            "apps": apps,
            "source": "agent",
            "modified": int(modified),
        }
        try:
            headers = {"content-type": "application/json"}
            r = requests.post(url=url,
                              data=json.dumps(post_data),
                              headers=headers,
                              verify=False)
            log.debug("http return code: %s" % str(r.status_code))
        except Exception, e:
            log.error(
                "Uncaught exception on self._postAgentInfoToServer:  %s" % e)
Esempio n. 2
0
 def __init__(self):
     c = get_config()
     self.update_interval = c['updater_interval']
     self.post_interval = c['post_interval']
     self.server = 'updater_server'
     self.linux_conf = os.path.abspath(
         os.path.join(_unix_confd_path(), '../'))
     self.windows_conf = 'C:/ProgramData/Datamonitor/'
     self.central_configuration_switch = c['central_configuration_switch']
     self.central_configuration_api_key = c['api_key']
     self.central_configuration_url = c['m_url'].replace(
         'api/v2/gateway/dd-agent', 'api/v2/agent/config/')
     self.api_url = c['m_url'].replace('api/v2/gateway/dd-agent',
                                       'api/autosync/client/')
     self.dirs = {
         'checks': os.path.abspath(os.path.join(_unix_checksd_path(),
                                                '../')),
         'conf': os.path.abspath(os.path.join(_unix_confd_path(), '../'))
     }
Esempio n. 3
0
    def copy_conf(self):
        temp_dir = os.path.join(tempfile.gettempdir(), 'monitor_update_temp')
        if os.path.exists(temp_dir):
            shutil.rmtree(temp_dir)

        confd_path = _windows_confd_path() if IS_WINDOWS else _unix_confd_path(
        )
        temp_confd_path = os.path.join(temp_dir, 'conf.d')
        shutil.copytree(confd_path, temp_confd_path)

        conf_path = _windows_config_path(
        ) if IS_WINDOWS else _unix_config_path()
        if os.path.isfile(conf_path):
            shutil.copy2(conf_path,
                         os.path.join(temp_dir, os.path.basename(conf_path)))
        else:
            shutil.copytree(conf_path, temp_dir)