Exemple #1
0
 def process(self):
     # 采集资产
     # from src.package import pack
     # data_dict = pack
     # hostname=self.file_host()
     # if hostname:
     #     data_dict["hostname"]=hostname
     # else:
     #     #获取当前主机名
     #     #写入nid文件
     #     data_dict["hostname"]="slave1"
     # 将资产数据发送到API(POST)
     # self.send_data(data_dict)
     # 获取指定主机名的资产信息
     # {"status":True,"message":None,"error":None,"data":{"disk":<lib.response.BaseResponse object at 0x0}}
     server_info = plugins.get_server_info()
     if not server_info.status:
         return
     local_cert = self.load_local_cert()
     if local_cert:
         if local_cert == server_info.data["hostname"]:
             pass
         else:
             server_info.data["hostname"] = local_cert
     else:
         self.write_local_cert(server_info.data["hostname"])
     # 序列化成字符串
     server_json = Json.dumps(server_info.data)
     # 发送到API
     self.post_asset(server_json, self.callback)
Exemple #2
0
    def process(self):
        """
        获取当前资产信息
        1。在资产中获取主机名,cert_new
        2.在本地cert文件中获取主机名 cert_old
        如果cert文件中为空,表示是新资产
            则将cert_new写入该文件中,发送数据到服务器(新资产)
        如果两个名字不相等
            如果db=new, 应主动修改, new为唯一ID

        :return:
        """
        server_info = plugins.get_server_info()
        if not server_info.status:
            return
        local_cert = self.load_local_cert()
        if local_cert:
            if local_cert == server_info.data['hostname']:
                pass
            else:
                server_info.data['hostname'] = local_cert
        else:
            self.write_local_cert(server_info.data['hostname'])
        server_json = Json.dumps(server_info.data)
        self.post_asset(server_json, self.callback)
Exemple #3
0
    def handler(self):
        # agent 下采集信息
        info = get_server_info(handler=self)
        # 根据主机名  判断 具体的操作
        if not os.path.exists(settings.CERT_PATH):
            # 文件不存在 新增
            info['type'] = 'create'
        else:
            # 更新
            with open(settings.CERT_PATH, 'r', encoding='utf-8') as f:
                old_hostname = f.read()
            hostname = info['basic']['data']['hostname']  # 最新主机名
            if old_hostname == hostname:
                # 更新资产
                info['type'] = 'update'
            else:
                info['type'] = 'update_hostname'
                info['old_hostname'] = old_hostname

        # 汇报 api
        now = time.time()
        response = requests.post(
            url=self.url,
            params={
                'key': gen_key(now),
                'ctime': now
            },
            data=encrypt(json.dumps(info)),
            headers={'content-type': 'application/json'},
        )
        ret = response.json()

        if ret['status'] is True:
            with open(settings.CERT_PATH, 'w', encoding='utf-8') as f1:
                f1.write(ret['hostname'])
Exemple #4
0
 def handler(self):
     info = get_server_info(self)
     print(info["mem"])
     if not os.path.exists(settings.CERT_FILE_PATH):
         info['type'] = 'create'
     else:
         with open(settings.CERT_FILE_PATH, 'r') as f:
             cert = f.read()
             if cert == info['basic']['data']['hostname']:
                 info['type'] = 'update'
             else:
                 info['cert'] = cert
                 info['type'] = 'host_update'
     ctime = int(time.time() * 1000)
     r1 = requests.post(url=self.asset_api,
                        params={
                            "sign": gen_sign(ctime),
                            "ctime": ctime
                        },
                        data=encrypt(json.dumps(info).encode("utf-8")),
                        headers={'Content-Type': 'application/json'})
     response = r1.json()
     print("text", response['data'])
     if response['status']:
         with open(settings.CERT_FILE_PATH, 'w') as f:
             f.write(response['data'])
Exemple #5
0
 def process(self):
     """
     获取当前资产信息
     1. 在资产中获取主机名 cert_new
     2. 在本地cert文件中获取主机名 cert_old
     如果cert文件中为空,表示是新资产
         - 则将 cert_new 写入该文件中,发送数据到服务器(新资产)
     如果两个名称不相等
         - 如果 db=new 则,表示应该主动修改,new为唯一ID
         - 如果 db=old 则,表示
     :return:
     """
     server_info = plugins.get_server_info()
     if not server_info.status:
         return
     local_cert = self.load_local_cert()
     if local_cert:
         if local_cert == server_info.data['hostname']:
             pass
         else:
             server_info.data['hostname'] = local_cert
     else:
         self.write_local_cert(server_info.data['hostname'])
     # 发送的数据格式:
     # {"hostname":"","disk":{"status":"","data":{},"error":"","message":""},"cpu":{}}
     server_json = Json.dumps(server_info.data)
     self.post_asset(server_json, self.callback)
Exemple #6
0
    def task(self, hostname):
        info = get_server_info(self, hostname)
        r1 = requests.post(url='http://127.0.0.1:8000/api/asset/',
                           data=json.dumps(info).encode('gbk'),
                           headers={'content-type': 'application/json'})

        print(r1)
        print(r1.text)
Exemple #7
0
 def run(self, hostname):
     # 获取指定主机名的资产信息
     # {"status":True,"message":None,"error":None,"data":{"disk":<lib.response.BaseResponse object at 0x0}}
     server_info = plugins.get_server_info(hostname)
     # 序列化成字符串
     server_json = Json.dumps(server_info.data)
     # 发送到API
     self.post_asset(server_json, self.callback)
Exemple #8
0
 def task(self, hostname):
     # 采集资产
     info = get_server_info(self, hostname)
     # 上报采集结果
     r2 = requests.post(url=settings.HOST_ASSET_API,
                        data=json.dumps(info),  # url编码a=1&b=2 ,直接用不方便
                        headers={'Content-Type': 'application/json'}
                        )
Exemple #9
0
 def run(self, manager_ip, info_dict):
     # 获取硬件服务器资产信息
     server_info = plugins.get_server_info(manager_ip=manager_ip,
                                           info_dict=info_dict)
     # 序列化获取到的资产
     server_json = Json.dumps(server_info.data)
     # 发送资产数据到api接口
     self.post_asset(server_json, self.callback)
Exemple #10
0
 def run(self, hostname):
     # 获取指定主机名的资产信息
     # {'status': True, 'message': None, 'error': None, 'data': {'disk': <lib.response.BaseResponse object at 0x00000000014686A0>, 'main_board': <lib.response.BaseResponse object at 0x00000000014689B0>, 'nic': <lib.response.BaseResponse object at 0x0000000001478278>, 'memory': <lib.response.BaseResponse object at 0x0000000001468F98>, 'os_platform': 'linux', 'os_version': 'CentOS release 6.6 (Final)', 'hostname': 'c1.com', 'cpu': <lib.response.BaseResponse object at 0x0000000001468E10>}}
     server_info = plugins.get_server_info(hostname)
     # 序列化成字符串
     server_json = Json.dumps(server_info.data)
     # 发送到API
     self.post_asset(server_json, self.callback)
Exemple #11
0
    def process(self):
        """
        获取当前资产信息

        :return:
        """
        server_info = plugins.get_server_info()
        # print(type(server_info))
        # print(server_info)
        return server_info
Exemple #12
0
 def run(self, hostname, module):
     print(hostname)
     # ssh salt 方式会链接hostname来进行执行命令
     server_info = plugins.get_server_info(hostname)
     server_info.data['hostname'] = hostname
     server_json = Json.dumps(server_info.data)
     print(server_json)
     if module == 'database':
         self.post_database(server_json, self.callback)
     elif module == 'asset':
         self.post_asset(server_json, self.callback)
Exemple #13
0
    def process(self):
        """
        :return:
        """
        server_info = plugins.get_server_info()
        if not server_info.status:
            return
        server_json = Json.dumps(server_info.data)
        print(server_json)

        return server_json
Exemple #14
0
    def handler(self):
        """

        :return:
        """
        # from src.plugins.disk import get_disk
        # get_disk(self, '108.160.135.123')

# cc = SSHHandler().cmd('df -h')
# print(cc)

        ret = get_server_info(self, '108.160.135.123')
        print(ret)
Exemple #15
0
 def process(self):
     """
     获取软件服务器资产信息
     cmdb数据库中保存的hostname要与Agent端settings文件中设置的HOSTNAME保持一致
     :return:
     """
     # 获取资产
     server_info = plugins.get_server_info()
     # 如果获取失败退出此函数
     if not server_info.status:
         return
     # 序列化获取到的资产
     server_json = Json.dumps(server_info.data)
     # 发送资产数据到api接口
     self.post_asset(server_json, self.callback)
Exemple #16
0
    def handler(self, hostname=None):
        print('agent')
        # 资产采集
        info = get_server_info(self, hostname)

        # 数据分析

        if not os.path.exists(settings.CERT_PATH):
            # 新增
            info['type'] = 'create'


        else:
            with open(settings.CERT_PATH, 'r', encoding='utf-8') as f:
                cert = f.read()

            hostname = info['basic']['data']['hostname']

            if cert == hostname:
                # 主机名没变 更新资产信息
                info['type'] = 'update'

            else:
                # 主机名变了,更新资产信息,并提交旧的主机名
                info['type'] = 'host_change'
                info['cert'] = cert

        # 资产上报

        ctime = time.time()
        r1 = requests.post(
            url='http://127.0.0.1:8000/api/asset/',
            params={'key':gen_key(ctime),'ctime':ctime},
            data=encrypt(json.dumps(info).encode('utf-8')),
            headers={
                'content-type': 'application/json'
            }
        )

        # 保存唯一标识

        data = r1.json()
        print(data)

        if data.get('status'):
            with open(settings.CERT_PATH, 'w', encoding='utf-8')as f:
                f.write(data.get('hostname'))
Exemple #17
0
 def task(self, hostname):
     """
     执行采集器,拿到采集结果汇报给API
     1. 执行所有的采集器拿到info
     2. 汇报info给api
     :param hostname:
     :return:
     """
     try:
         info = get_server_info(self, hostname)
         r1 = requests.post(url=self.asset_api,
                            data=json.dumps(info).encode('utf-8'),
                            headers={'Content-Type': 'application/json'})
         print(r1)
     except Exception as e:
         msg = traceback.format_exc()
         log.error(msg)
         print(msg)
Exemple #18
0
 def process(self):
     '''
     获取当前资产的信息
     1,在资产中获取主机名 cert_new
     2,在本地cert文件中获取主机名 cert_old
     :return:
     '''
     server_info = plugins.get_server_info()
     if not server_info.status:
         return
     local_sert = self.load_local_cert()
     if local_sert:
         if local_sert == server_info.data['hostname']:
             pass
         else:
             server_info.data['hostname'] = local_sert
     else:
         self.write_local_cert(server_info.data['hostname'])
     server_json = Json.dumps(server_info.data)
     self.post_asset(server_json, self.callback)
    def process(self):
        server_info = plugins.get_server_info(
        )  # 这里server_info是一个对象 BaseResponse的实例 对象。
        # print(server_info.data)
        # print(type(server_info.data))
        if not server_info.status:
            # 如果这个值为  false 说明,发成了错误。那就直接返回。不再进行下一步的操作
            return
        local_cert = self.load_local_cert()  # 获取到所有的主机名
        if local_cert:
            if local_cert == server_info.data['hostname']:
                pass
            else:
                server_info.data['hostname'] = local_cert
                # 如果不相等将本地保存的 主机名赋值给他。保证后端接收到的是,这个唯一的主机名
        else:
            self.write_local_cert(
                server_info.data['hostname'])  # 如果当前文件中没有主机名,就写入

        server_json = Json.dumps(server_info.data)  # 使用自定制的 序列化工具,将对象序列化。
        self.post_asset(server_json, self.callback)  # 使用父类的方法。发送所有的数据。
Exemple #20
0
    def handler(self):
        """
        调用此方法: 执行操作, 获取信息,解析, 发送请求上报结果
        :return: 
        """
        print("agent模式")
        # 采集资产信息: 硬盘, 内存, 网卡
        info = get_server_info(self)
        # 唯一标识: 判断采集信息是否第一次提交        
        if not os.path.exists(settings.CERT_FILE_PATH):
            info["type"] = "create"  # 新增       
        else:
            # 主机信息文件存在, 读取
            with open(settings.CERT_FILE_PATH, "r", encoding="utf-8") as f:
                cert = f.read()
                hostname = info["basic"]["data"]["hostname"]
                print(cert)
                print(hostname)
                if cert == hostname:  # 文件保存的主机名与本地监测到的相同
                    info["type"] = "update"
                else:
                    # 主机名变更过, 更新资产信息
                    info["type"] = "host_update"
                    info["cert"] = cert
        # 3上报给api
        r1 = requests.post(url="http://127.0.0.1:8000/api/asset", data=json.dumps(info),
                           headers={'Content-Type': 'application/json'})
        print(r1)
        print(r1.text)
        # 4 更新唯一标识
        response = re.json
        if response.get("status"):
            """
            是否修改主机名
            """
            with open("settings.CERT_FILE_PATH", "w", encoding="utf-8") as f:
                f.write(response["data"])
                print("update host")

        print("end")
Exemple #21
0
 def process(self):
     """
     获取当前资产信息
     1. 在资产中获取主机名 cert_new
     2. 在本地cert文件中获取主机名 cert_old
     如果cert文件中为空,表示是新资产
         - 则将 cert_new 写入该文件中,发送数据到服务器(新资产)
     如果两个名称不相等
         - 如果 db=new 则,表示应该主动修改,new为唯一ID
         - 如果 db=old 则,表示
     :return:
     """
     server_info = plugins.get_server_info()
     """server_info = {
                 'status': True, 
                 'message': None, 
                 'data': {'os_platform': 'linux',
                         'os_version': 'CentOS release 6.6',
                         'hostname': 'c1.com',
                         'cpu': {'status': True, 
                                 'message': None, 
                                 'data': XXOO,
                                 'error': None},
                         },
                 'error': None}"""
     if not server_info.status:
         return
     local_cert = self.load_local_cert()
     if local_cert:
         if local_cert == server_info.data['hostname']:
             pass
         else:
             server_info.data['hostname'] = local_cert
     else:
         self.write_local_cert(server_info.data['hostname'])
     server_json = Json.dumps(server_info.data)
     self.post_asset(server_json, self.callback)
Exemple #22
0
    def handler(self):
        """
        处理Agent模式下的资产采集:网卡、内存、硬盘
        :return:
        """
        # 1. 通过调用get_server_info获取所有的资产信息:网卡、内存、硬盘
        info = get_server_info(self)
        print('采集到服务器资产信息:', info)

        # 2 获取本地文件中的唯一标识
        if not os.path.exists(settings.CERT_FILE_PATH):
            # 新服务器,给API之后,应该在数据库中增加数据
            info['type'] = 'create'
        else:
            with open(settings.CERT_FILE_PATH, 'r', encoding='utf-8') as f:
                cert = f.read()
            if cert == info['basic']['data']['hostname']:
                # 主机名未做变更 汇报给API,API做更新
                info['type'] = 'update'
            else:
                info['cert'] = cert
                info['type'] = 'host_update'

        # 3. 发送到api
        ctime = int(time.time() * 1000)
        r1 = requests.post(url=self.asset_api,
                           params={
                               'sign': gen_sign(ctime),
                               'ctime': ctime
                           },
                           data=encrypt(json.dumps(info).encode('utf-8')),
                           headers={'Content-Type': 'application/json'})
        response = r1.json()
        if response['status']:
            with open(settings.CERT_FILE_PATH, 'w', encoding='utf-8') as f:
                f.write(response['data'])
Exemple #23
0
 def run(self, hostname):
     server_info = plugins.get_server_info(hostname)  #主机信息(基本信息、指标信息)
     server_json = Json.dumps(server_info.data)
     self.post_asset(server_json, self.callback)  #以post方式提交数据到服务端
Exemple #24
0
 def run(self, hostname):
     server_info = plugins.get_server_info(hostname)
     server_json = Json.dumps(server_info.data)
     self.post_asset(server_json, self.callback)
Exemple #25
0
 def run(self, hostname):
     # ssh salt 方式会链接hostname来进行执行命令
     server_info = plugins.get_server_info(hostname)
     server_json = Json.dumps(server_info.data)
     self.post_asset(server_json, self.callback)
Exemple #26
0
 def task(self, hostname):
     # 采集资产信息
     info = get_server_info(self, hostname)
     requests.post(url=self.url,
                   data=json.dumps(info).encode('utf-8'),
                   headers={'content-type': 'application/json'})
Exemple #27
0
 def run(self, hostname):
     server_info = plugins.get_server_info(hostname)
     server_json = Json.dumps(server_info.data)
     print(server_json)
Exemple #28
0
    def process(self):
        # 获取指定主机名的资产信息

        server_info = plugins.get_server_info('hostname')
        server_json = Json.dumps(server_info.data)
        return server_json