コード例 #1
0
 def __getNetworkByName(self, name):
     if name == "schoolconnect":
         mainconfig = open(config.servicepath + "config.json", "r")
         globalNetwork = network.network(
             True,
             json.loads(mainconfig.read())["globalNetwork"], None, False)
         mainconfig.close()
         return globalNetwork
     for nw in self.networks:
         if nw.getName() == name:
             return nw
     return False
コード例 #2
0
    def collect_data(self):
        if os.path.exists('/opt/hector-agent/hectoragent.ini'):
            config = configparser.ConfigParser()
            config.read(
                '/opt/hector-agent/hectoragent.ini')  # Loading configuration
            token = config['API']['token'].strip(
                '"')  # Get token from .ini configuration
            is_agent_installed = config['GENERAL'].getboolean('installed')

            if is_agent_installed:
                # Collecting data
                print('\nCollecting Data...')
                mem = memory().collect()
                swap_mem = swap().collect()
                disk_usage_overall = disk().collect_overall_disk_usage()
                disk_partitions = disk().collect_partitions()
                disk_io = disk().collect_io()
                net = network().collect_network_addrs()
                server_loads = load().collect()
                processes = process().collect()
                extern_ping = ping().collect()
                server_cpu = cpu().collect()
                boot_time = float(psutil.boot_time())
                servers_ips = ip().collect_ips()

                # Sending data to the API
                try:
                    print('Sending data to API...')
                    res = requests.post(
                        API_ENDPOINT +
                        "/servers/sendmonitoring?server_token=" + token,
                        data={
                            'os_fullname':
                            self._server_platform(),
                            'boot_time':
                            boot_time,
                            'memory':
                            helpers.dict_to_base64(mem),
                            'swap':
                            helpers.dict_to_base64(swap_mem),
                            'disk_usage_overall':
                            helpers.dict_to_base64(disk_usage_overall),
                            'disk_partitions':
                            helpers.dict_to_base64(disk_partitions),
                            'disk_io':
                            helpers.dict_to_base64(disk_io),
                            'network':
                            helpers.dict_to_base64(net),
                            'loads':
                            helpers.dict_to_base64(server_loads),
                            'processes':
                            helpers.dict_to_base64(processes),
                            'extern_ping':
                            helpers.dict_to_base64(extern_ping),
                            'cpu':
                            helpers.dict_to_base64(server_cpu),
                            'ips':
                            helpers.dict_to_base64(servers_ips),
                            'request_send_at':
                            datetime.datetime.now(),
                        })

                    if res.status_code == 200 and json.loads(
                            res.text)['success']:
                        print(
                            colors.GREEN +
                            '\nThe data has been correctly sent to the API!' +
                            colors.NORMAL)

                except requests.exceptions.RequestException:
                    sys.exit(1)
            else:
                print(
                    colors.RED +
                    "The agent does not seem to be correctly installed, please restart the installation script!"
                    + colors.NORMAL)
コード例 #3
0
 def __init__(self, name, firstinstall):
     # Init vars
     self.__name = name
     self.__containers = {"actual": [], "previous": []}
     self.__volumes = []
     self.__description = None
     self.__errors = 0
     self.__installThread = None
     self.__updateThread = None
     self.__revertThread = None
     self.__deleteThread = None
     self.__rebuildThread = None
     self.__localEnv = None
     if not firstinstall:
         # Read container configuration
         configFile = open(config.servicepath + name + "/config.json", "r")
         self.__config = json.loads(configFile.read())
         configFile.close()
         # Create service description object
         self.__desc = description.serviceDescription(
             False, self.__name, self.__config["actualVersion"])
         self.__localEnv = envman(self.__name)
         # Create docker references for volumes
         for storedVolume in self.__config["volumes"]:
             self.__volumes.append(
                 volume.volume(True, storedVolume["id"], None))
         # Create docker references for networks
         for storedNetwork in self.__config["networks"]:
             self.networks.append(
                 network.network(True, storedNetwork["id"], None, None))
         # Create docker references for containers
         for storedContainer in self.__config["containers"]["actual"]:
             self.__containers["actual"].append(
                 container.container(True, storedContainer["id"],
                                     self.__name))
         for storedContainer in self.__config["containers"]["previous"]:
             self.__containers["previous"].append(
                 container.container(True, storedContainer["id"],
                                     self.__name))
         # Check container status and turn containers to wanted status
         if self.__config["wanted"]:
             for containerObject in self.__containers["actual"]:
                 if not containerObject.isRunning():
                     containerObject.start()
             self.__config["status"] = "running"
         else:
             for containerObject in self.__containers["actual"]:
                 if containerObject.isRunning():
                     containerObject.stop()
             self.__config["status"] = "paused"
     else:
         self.__config = {
             "networks": [],
             "volumes": [],
             "containers": {
                 "actual": [],
                 "previous": []
             },
             "wanted": True,
             "status": "empty",
             "actualVersion": "",
             "previousVersion": "",
         }
コード例 #4
0
 def __createNewNetwork(self, name, internal):
     return network.network(False, None, name, internal)