Beispiel #1
0
    def start(self, stdout=False):
        """
        Start the Kibana process

        :param stdout: Print output to console
        :return: True, if started successfully
        """
        def start_shell_out():

            # We use su instead of runuser here because of nodes' weird dependency on PAM
            # when calling from within a sub-shell
            subprocess.call(
                'su -l dynamite -c "{}/bin/kibana -c {} -l {} & > /dev/null &"'
                .format(
                    self.config.kibana_home,
                    os.path.join(self.config.kibana_path_conf, 'kibana.yml'),
                    os.path.join(self.config.kibana_logs, 'kibana.log')),
                shell=True,
                env=utilities.get_environment_file_dict())

        if not os.path.exists('/var/run/dynamite/kibana/'):
            subprocess.call('mkdir -p {}'.format('/var/run/dynamite/kibana/'),
                            shell=True)
        utilities.set_ownership_of_file('/var/run/dynamite',
                                        user='******',
                                        group='dynamite')

        if not utilities.check_pid(self.pid):
            Process(target=start_shell_out).start()
        else:
            sys.stderr.write(
                '[-] Kibana is already running on PID [{}]\n'.format(self.pid))
            return True
        retry = 0
        self.pid = -1
        time.sleep(5)
        while retry < 6:
            start_message = '[+] [Attempt: {}] Starting Kibana on PID [{}]\n'.format(
                retry + 1, self.pid)
            try:
                with open('/var/run/dynamite/kibana/kibana.pid') as f:
                    self.pid = int(f.read())
                start_message = '[+] [Attempt: {}] Starting Kibana on PID [{}]\n'.format(
                    retry + 1, self.pid)
                if stdout:
                    sys.stdout.write(start_message)
                if not utilities.check_pid(self.pid):
                    retry += 1
                    time.sleep(5)
                else:
                    return True
            except IOError:
                if stdout:
                    sys.stdout.write(start_message)
                retry += 1
                time.sleep(3)
        return False
Beispiel #2
0
    def start(self, stdout=False):
        """
        Start the ElasticSearch process
        :param stdout: Print output to console
        :return: True, if started successfully
        """
        def start_shell_out():
            subprocess.call(
                'runuser -l dynamite -c "{} {}/bin/elasticsearch '
                '-p /var/run/dynamite/elasticsearch/elasticsearch.pid --quiet &>/dev/null &"'
                .format(utilities.get_environment_file_str(),
                        self.config.es_home),
                shell=True)

        if not os.path.exists('/var/run/dynamite/elasticsearch/'):
            subprocess.call(
                'mkdir -p {}'.format('/var/run/dynamite/elasticsearch/'),
                shell=True)
        utilities.set_ownership_of_file('/var/run/dynamite',
                                        user='******',
                                        group='dynamite')

        if not utilities.check_pid(self.pid):
            Process(target=start_shell_out).start()
        else:
            sys.stderr.write(
                '[-] ElasticSearch is already running on PID [{}]\n'.format(
                    self.pid))
            return True
        retry = 0
        self.pid = -1
        time.sleep(5)
        while retry < 6:
            start_message = '[+] [Attempt: {}] Starting ElasticSearch on PID [{}]\n'.format(
                retry + 1, self.pid)
            try:
                with open('/var/run/dynamite/elasticsearch/elasticsearch.pid'
                          ) as f:
                    self.pid = int(f.read())
                start_message = '[+] [Attempt: {}] Starting ElasticSearch on PID [{}]\n'.format(
                    retry + 1, self.pid)
                if stdout:
                    sys.stdout.write(start_message)
                if not utilities.check_pid(self.pid):
                    retry += 1
                    time.sleep(5)
                else:
                    return True
            except IOError:
                if stdout:
                    sys.stdout.write(start_message)
                retry += 1
                time.sleep(3)
        return False
Beispiel #3
0
    def start(self, stdout=False):
        """
        Start the JupyterHub process
        :param stdout: Print output to console
        :return: True, if started successfully
        """
        def start_shell_out():
            subprocess.call('jupyterhub -f {} &>/dev/null &'.format(
                os.path.join(self.configuration_directory,
                             'jupyterhub_config.py')),
                            shell=True,
                            stderr=subprocess.PIPE,
                            stdout=None)

        if not os.path.exists('/var/run/dynamite/jupyterhub/'):
            subprocess.call(
                'mkdir -p {}'.format('/var/run/dynamite/jupyterhub/'),
                shell=True)

        if not utilities.check_pid(self.pid):
            Process(target=start_shell_out).start()
        else:
            sys.stderr.write(
                '[-] JupyterHub is already running on PID [{}]\n'.format(
                    self.pid))
            return True
        retry = 0
        self.pid = -1
        time.sleep(5)
        while retry < 6:
            start_message = '[+] [Attempt: {}] Starting JupyterHub on PID [{}]\n'.format(
                retry + 1, self.pid)
            try:
                with open('/var/run/dynamite/jupyterhub/jupyterhub.pid') as f:
                    self.pid = int(f.read())
                start_message = '[+] [Attempt: {}] Starting JupyterHub on PID [{}]\n'.format(
                    retry + 1, self.pid)
                if stdout:
                    sys.stdout.write(start_message)
                if not utilities.check_pid(self.pid):
                    retry += 1
                    time.sleep(5)
                else:
                    return True
            except IOError:
                if stdout:
                    sys.stdout.write(start_message)
                retry += 1
                time.sleep(3)
        return False
Beispiel #4
0
    def stop(self, stdout=False):
        """
        Stop the LogStash process

        :param stdout: Print output to console
        :return: True if stopped successfully
        """
        alive = True
        attempts = 0
        while alive:
            try:
                if stdout:
                    sys.stdout.write(
                        '[+] Attempting to stop LogStash [{}]\n'.format(
                            self.pid))
                if attempts > 3:
                    sig_command = signal.SIGKILL
                else:
                    sig_command = signal.SIGINT
                attempts += 1
                if self.pid != -1:
                    os.kill(self.pid, sig_command)
                time.sleep(10)
                alive = utilities.check_pid(self.pid)
            except Exception as e:
                sys.stderr.write(
                    '[-] An error occurred while attempting to stop LogStash: {}\n'
                    .format(e))
                return False
        return True
Beispiel #5
0
    def stop(self, stdout=False):
        """
        Stop the Suricata process

        :param stdout: Print output to console
        :return: True if stopped successfully
        """
        alive = True
        attempts = 0
        while alive:
            try:
                if stdout:
                    sys.stdout.write('[+] Attempting to stop Suricata [{}]\n'.format(self.pid))
                if attempts > 3:
                    sig_command = signal.SIGKILL
                else:
                    # Kill the zombie after the third attempt of asking it to kill itself
                    sig_command = signal.SIGTERM
                attempts += 1
                if self.pid != -1:
                    os.kill(self.pid, sig_command)
                time.sleep(1)
                alive = utilities.check_pid(self.pid)
            except Exception as e:
                sys.stderr.write('[-] An error occurred while attempting to stop Suricata: {}\n'.format(e))
                return False
        return True
Beispiel #6
0
    def start(self):
        """
        Start the JupyterHub process

        :return: True, if started successfully
        """
        def start_shell_out():
            subprocess.call('jupyterhub -f {} &>/dev/null &'.format(
                os.path.join(self.configuration_directory,
                             'jupyterhub_config.py')),
                            shell=True,
                            stderr=subprocess.PIPE,
                            stdout=None)

        utilities.makedirs(PID_DIRECTORY, exist_ok=True)

        if not utilities.check_pid(self.pid):
            Process(target=start_shell_out).start()
        else:
            self.logger.info(
                'JupyterHub is already running on PID [{}]'.format(self.pid))
            return True
        retry = 0
        self.pid = -1
        time.sleep(5)
        while retry < 6:
            try:
                with open(os.path.join(PID_DIRECTORY, 'jupyterhub.pid')) as f:
                    self.pid = int(f.read())
                start_message = '[Attempt: {}] Starting JupyterHub on PID [{}]'.format(
                    retry + 1, self.pid)
                self.logger.info(start_message)
                if not utilities.check_pid(self.pid):
                    retry += 1
                    time.sleep(5)
                else:
                    return True
            except IOError as e:
                self.logger.warning(
                    "An issue occurred while attempting to start.")
                self.logger.debug(
                    "An issue occurred while attempting to start; {}".format(
                        e))
                retry += 1
                time.sleep(3)
        return False
Beispiel #7
0
    def start(self, stdout=False):
        """
        Start the LogStash process
        :param stdout: Print output to console
        :return: True if started successfully
        """
        self.pid = -1

        def start_shell_out():
            command = 'runuser -l dynamite -c "{} {}/bin/logstash ' \
                      '--path.settings={} &>/dev/null & echo \$! > /var/run/dynamite/logstash/logstash.pid"'.format(
                utilities.get_environment_file_str(), self.config.ls_home, self.config.ls_path_conf)
            subprocess.call(command, shell=True, cwd=self.config.ls_home)

        if not utilities.check_pid(self.pid):
            Process(target=start_shell_out).start()
        else:
            sys.stderr.write(
                '[-] Logstash is already running on PID [{}]\n'.format(
                    self.pid))
            return True
        retry = 0
        time.sleep(5)
        while retry < 6:
            start_message = '[+] [Attempt: {}] Starting Logstash on PID [{}]\n'.format(
                retry + 1, self.pid)
            try:
                with open('/var/run/dynamite/logstash/logstash.pid') as f:
                    self.pid = int(f.read()) + 1
                start_message = '[+] [Attempt: {}] Starting LogStash on PID [{}]\n'.format(
                    retry + 1, self.pid)
                if stdout:
                    sys.stdout.write(start_message)
                if not utilities.check_pid(self.pid):
                    retry += 1
                    time.sleep(3)
                else:
                    return True
            except IOError:
                if stdout:
                    sys.stdout.write(start_message)
                retry += 1
                time.sleep(3)
        return False
Beispiel #8
0
    def start(self, stdout=False):
        """
        Start the Filebeat daemon
        :param stdout: Print output to console
        :return: True if started successfully
        """
        def start_shell_out():
            command = '{}/filebeat -c {}/filebeat.yml & echo $! > /var/run/dynamite/filebeat/filebeat.pid'.format(
                self.config.install_directory, self.config.install_directory)
            subprocess.call(command, shell=True)

        if stdout:
            sys.stdout.write('[+] Starting Filebeat\n')
        if not utilities.check_pid(self.pid):
            Process(target=start_shell_out).start()
        else:
            sys.stderr.write(
                '[-] Filebeat is already running on PID [{}]\n'.format(
                    self.pid))
            return True
        retry = 0
        self.pid = -1
        time.sleep(5)
        while retry < 6:
            start_message = '[+] [Attempt: {}] Starting FileBeat on PID [{}]\n'.format(
                retry + 1, self.pid)
            try:
                with open('/var/run/dynamite/filebeat/filebeat.pid') as f:
                    self.pid = int(f.read())
                start_message = '[+] [Attempt: {}] Starting FileBeat on PID [{}]\n'.format(
                    retry + 1, self.pid)
                if stdout:
                    sys.stdout.write(start_message)
                if not utilities.check_pid(self.pid):
                    retry += 1
                    time.sleep(5)
                else:
                    return True
            except IOError:
                if stdout:
                    sys.stdout.write(start_message)
                retry += 1
                time.sleep(3)
        return False
Beispiel #9
0
    def status(self):
        """
        Check the status of the Suricata process

        :return: A dictionary containing the run status and relevant configuration options
        """
        return {
            'PID': self.pid,
            'RUNNING': utilities.check_pid(self.pid),
            'LOG': os.path.join(self.config.default_log_directory, 'suricata.log')
        }
Beispiel #10
0
    def status(self):
        """
        Check the status of the JupyterHub process

        :return: A dictionary containing the run status and relevant configuration options
        """

        return {
            'PID': self.pid,
            'RUNNING': utilities.check_pid(self.pid),
            'USER': '******'
        }
Beispiel #11
0
    def status(self):
        """
        Check the status of the ElasticSearch process

        :return: A dictionary containing the run status and relevant configuration options
        """
        log_path = os.path.join(self.config.kibana_logs, 'kibana.log')

        return {
            'PID': self.pid,
            'RUNNING': utilities.check_pid(self.pid),
            'USER': '******',
            'LOGS': log_path
        }
Beispiel #12
0
    def status(self):
        """
        Check the status of the FileBeat process

        :return: A dictionary containing the run status and relevant configuration options
        """
        log_path = os.path.join(self.config.install_directory, 'logs',
                                'filebeat')

        return {
            'PID': self.pid,
            'RUNNING': utilities.check_pid(self.pid),
            'LOGS': log_path
        }
Beispiel #13
0
    def status(self):
        """
        Check the status of the LogStash process

        :return: A dictionary containing the run status and relevant configuration options
        """
        log_path = os.path.join(self.config.get_log_path(), 'logstash-plain.log')

        return {
            'PID': self.pid,
            'RUNNING': utilities.check_pid(self.pid),
            'USER': '******',
            'LOGS': log_path
        }
Beispiel #14
0
    def start(self, stdout=False):
        """
        Start Suricata IDS process in daemon mode

        :param stdout: Print output to console
        :return: True, if started successfully
        """
        if not os.path.exists('/var/run/dynamite/suricata/'):
            subprocess.call(
                'mkdir -p {}'.format('/var/run/dynamite/suricata/'),
                shell=True)
        p = subprocess.Popen(
            'bin/suricata -i {} '
            '--pfring-int={} --pfring-cluster-type=cluster_flow -D '
            '--pidfile /var/run/dynamite/suricata/suricata.pid '
            '-c {}'.format(
                self.config.get_monitor_interface(),
                self.config.get_monitor_interface(),
                os.path.join(self.configuration_directory, 'suricata.yaml')),
            shell=True,
            cwd=self.install_directory)
        p.communicate()
        retry = 0
        while retry < 6:
            start_message = '[+] [Attempt: {}] Starting Suricata on PID [{}]\n'.format(
                retry + 1, self.pid)
            try:
                with open('/var/run/dynamite/suricata/suricata.pid') as f:
                    self.pid = int(f.read())
                start_message = '[+] [Attempt: {}] Starting Suricata on PID [{}]\n'.format(
                    retry + 1, self.pid)
                if stdout:
                    sys.stdout.write(start_message)
                if not utilities.check_pid(self.pid):
                    retry += 1
                    time.sleep(5)
                else:
                    return True
            except IOError:
                if stdout:
                    sys.stdout.write(start_message)
                retry += 1
                time.sleep(3)
        return False
Beispiel #15
0
    def stop(self):
        """
        Stop the Jupyterhub process

        :return: True if stopped successfully
        """

        alive = True
        attempts = 0
        while alive:
            try:
                self.logger.info('Attempting to stop JupyterHub [{}]'.format(
                    self.pid))
                if attempts > 3:
                    self.logger.warning(
                        'Attempting to force stop JupyterHub after {} failed attempts. [{}].'
                        .format(attempts, self.pid))
                    sig_command = signal.SIGKILL
                else:
                    # Kill the zombie after the third attempt of asking it to kill itself
                    sig_command = signal.SIGINT
                attempts += 1
                if self.pid != -1:
                    os.kill(self.pid, sig_command)
                time.sleep(10)

                alive = utilities.check_pid(self.pid)
            except Exception as e:
                self.logger.error(
                    'An error occurred while attempting to stop JupyterHub.')
                self.logger.debug(
                    'An error occurred while attempting to stop JupyterHub; {}'
                    .format(e))
                return False
        self.logger.info("Deleting JupyterHub PID [{}].".format(self.pid))
        utilities.safely_remove_file(
            os.path.join(PID_DIRECTORY, 'jupyterhub.pid'))
        return True