added software field encoded with base64 and zlib

This commit is contained in:
Mi³osz Stocki 2022-03-28 13:05:06 +02:00
parent 500567a0b0
commit c3abb1e6eb
Signed by: osiu97
GPG Key ID: E3D1D83FA04F51D6
1 changed files with 19 additions and 0 deletions

View File

@ -3,6 +3,7 @@ import json
from telegraf.client import TelegrafClient from telegraf.client import TelegrafClient
import yaml import yaml
from logging import log from logging import log
from codecs import encode, decode
with open('config.yaml') as f: with open('config.yaml') as f:
config = yaml.load(f, Loader=yaml.FullLoader) config = yaml.load(f, Loader=yaml.FullLoader)
@ -34,6 +35,11 @@ class Api(object):
response = json.load(urllib.urlopen(self.get_request(url))) response = json.load(urllib.urlopen(self.get_request(url)))
return response return response
def get_software_list(self):
url = self.url + 'software'
response = json.load(urllib.urlopen(self.get_request(url)))
return response
class Agent(object): class Agent(object):
@ -49,6 +55,7 @@ class Agent(object):
self.status = agent_json['status'] self.status = agent_json['status']
self.details = self.download_agent_details() self.details = self.download_agent_details()
self.wmi_details = self.extract_wmi_details() self.wmi_details = self.extract_wmi_details()
self.software = Software(self.agent_id,self.api).software_json
def download_agent_details(self): def download_agent_details(self):
url = self.api.url + 'agents' + '/' + str(self.agent_id) url = self.api.url + 'agents' + '/' + str(self.agent_id)
@ -85,6 +92,9 @@ class Agent(object):
new_values = self.prefix_keys(v, k) new_values = self.prefix_keys(v, k)
new_values = self.delete_conflict_characters(new_values) new_values = self.delete_conflict_characters(new_values)
new_dict[k] = new_values new_dict[k] = new_values
# Let's add software as a dict(json) not a string
software_string = json.dumps(self.software).encode('utf-8')
new_dict['agent_info']['agent_info_software'] = encode(encode(software_string, "zlib"), "base64").decode('ascii')
return new_dict return new_dict
@staticmethod @staticmethod
@ -117,6 +127,15 @@ class Agent(object):
return str(params) return str(params)
class Software(object):
def __init__(self, agent_id, api_object):
self.software_json = self.get_json(agent_id, api_object)
def get_json(self, agent_id, api_object):
url = api_object.url + '/' + 'software' + '/' + agent_id
response = urllib.urlopen(api_object.get_request(url))
return json.load(response)
api = Api(URL, TOKEN) api = Api(URL, TOKEN)
agent_list = api.get_agent_list() agent_list = api.get_agent_list()
telegraf_client = TelegrafClient(host=config['telegraf']['host'], port=config['telegraf']['port']) telegraf_client = TelegrafClient(host=config['telegraf']['host'], port=config['telegraf']['port'])