def get_all_nodes_health():
    """ This function will use the proliantutils library
    module "get_host_health_data" to derive the health status of
    the following components 'BIOS, Fans, Temperature Sensors,
    Battery, Processor, Memory, Network, Storage.

    If the health status of all component is ok then overall
    health of the physical node is returned as "OK"
    """
    try:
        result = get_all_nodes()
        final = {}
        for key, value in result.items():
            dict_object = {}
            if type(value) == type(dict()):
                v = list(value.values())
                ilo_client = client.IloClient(v[1], v[2], v[3])
                dict_object = ilo_client.get_host_health_data()
                q = JSONParser(dict_object)
                bios = q.dict_query(
                    "GET_EMBEDDED_HEALTH_DATA/HEALTH_AT_A_GLANCE/BIOS_HARDWARE/STATUS"
                )
                fans = q.dict_query(
                    "GET_EMBEDDED_HEALTH_DATA/HEALTH_AT_A_GLANCE/FANS/STATUS")
                temperature = q.dict_query(
                    "GET_EMBEDDED_HEALTH_DATA/HEALTH_AT_A_GLANCE/TEMPERATURE/STATUS"
                )
                battery = q.dict_query(
                    "GET_EMBEDDED_HEALTH_DATA/HEALTH_AT_A_GLANCE/BATTERY/STATUS"
                )
                processor = q.dict_query(
                    "GET_EMBEDDED_HEALTH_DATA/HEALTH_AT_A_GLANCE/PROCESSOR/STATUS"
                )
                memory = q.dict_query(
                    "GET_EMBEDDED_HEALTH_DATA/HEALTH_AT_A_GLANCE/MEMORY/STATUS"
                )
                network = q.dict_query(
                    "GET_EMBEDDED_HEALTH_DATA/HEALTH_AT_A_GLANCE/NETWORK/STATUS"
                )
                storage = q.dict_query(
                    "GET_EMBEDDED_HEALTH_DATA/HEALTH_AT_A_GLANCE/STORAGE/STATUS"
                )
                total_health = [bios, fans, temperature, battery, processor,\
                    memory, network, storage]
                result1 = len(set(total_health)) == 1
                if result1:
                    final[v[0]] = "OK"
                else:
                    final[v[0]] = "Degraded"
    except:
        print("Unexpected error:", sys.exc_info()[0])
        raise

    return (final)
def get_all_nodes():
    """ This function will read data from the host.json
    and store it in the form od dictionary object
    """
    try:
        result = {}
        p = JSONParser('hosts.json')
        result = p.all_data()
    except:
        print("Unexpected error:", sys.exc_info()[0])
        raise
    return (result)
Exemplo n.º 3
0
def set_environment_var():
    """
    This function will set the PATH and KUBECONFIG environment variables
    as per the user defined values in "config.json" file
    """
    try:
        data = JSONParser("config.json")
        kubeconfig = data.query("kubeconfig_path")
        #path = data.query("oc_command_path")
        #os.environ["PATH"] += os.pathsep + os.path.join(path)
        os.environ["KUBECONFIG"] = kubeconfig
        user_env = os.environ
    except:
        print("Unexpected error:", sys.exc_info()[0])
        raise
    return (user_env)
Exemplo n.º 4
0
    def parse_json(self, cfg, schema):
        set_def = lambda x, y: self.cfg[x] if self.cfg[x] != "" else y
        self.json = JSONParser(os.path.abspath(schema),
                               os.path.abspath(cfg),
                               extend_defaults=True,
                               logger=self.logger)
        self.cfg = self.json.get_cfg()
        self.logger.debug(self.json.print_cfg())

        self._from = set_def('from', None)
        self._to = set_def('to', None)
        self._cc = self.cfg['cc']
        self._bcc = self.cfg['bcc']

        self.smtp_server = self.cfg['smtp-server']
        self.smtp_port = self.cfg['smtp-port']
        self.auth = set_def('smtp-authentication', None)
        self.username = set_def('smtp-username', None)
        self.password = set_def('smtp-password', None)
Exemplo n.º 5
0
 def __init__(self, categories=None, parser=None):
     if categories is None:
         self._categories = {
             "Food": Food(),
             "Tools": Tools(),
             "Electronics": Electronics(),
             "Books": Books()
         }
     else:
         self._categories = categories
     self._parser = JSONParser() if parser is None else parser
Exemplo n.º 6
0
    def on_data(self, data):

        parser = JSONParser(data)
        try:
            conn = psycopg2.connect(database="twitterdb",
                                    user="******",
                                    password="******",
                                    port=5433)
        except:
            print("I am unable to connect to the database")

        cur = conn.cursor()
        try:
            for tweet in parser.createTweets(parser.data):
                try:
                    insert_user_row(tweet.user, cur)
                except:
                    pass

                try:
                    insert_tweet_row(tweet, cur)
                    for mentioned_user in tweet.mentioned_users:
                        insert_mentioned_user_row(mentioned_user, cur)
                    for hashtag in tweet.hashtags:
                        try:
                            insert_hashtag_row(hashtag, cur)
                            hashtag_id = fetch_hashtag_id(hashtag, cur)
                            insert_row_to_intersection_table(
                                hashtag_id, tweet.id, cur)
                        except:
                            pass

                    conn.commit()
                except:
                    pass
        except:
            pass
        conn.commit()
        conn.close()
        return True
def get_all_node_security_status():
    """ This function will use the proliantutils library
    module "get_current_bios_settings" to derive the status of
    the following BIOS configuration which are important for
    the security: 'secure boot status (enabled), asset tag (locked),
    UEFI Shell Script Verification (enabled), 
    UEFI Shell Startup (disabled), Processor AES (enabled)"
    
    If the value of any of the configuration deviates from the 
    recommended value, overall security status will be marked as 
    degraded. Refer to BIOS user guide for recommended value of these
    configurations
    """
    try:
        result = get_all_nodes()
        final = {}
        for key, value in result.items():
            dict_object = {}
            if type(value) == type(dict()):
                v = list(value.values())
                ilo_client = client.IloClient(v[1], v[2], v[3])
                dict_object = ilo_client.get_current_bios_settings()
                q = JSONParser(dict_object)
                secure_boot_status = q.dict_query("SecureBootStatus")
                asset_tag = q.dict_query("AssetTagProtection")
                shell_script_verify = q.dict_query(
                    "UefiShellScriptVerification")
                shell_startup = q.dict_query("UefiShellStartup")
                processor_aes = q.dict_query("ProcAes")
                if v[0] == "secphyworker1.sec.twentynet.local" and \
                    secure_boot_status.lower() == "disabled":
                    final[v[0]] = "OK"
                elif secure_boot_status.lower() == "enabled" and \
                    asset_tag.lower() == "locked" and \
                        shell_script_verify.lower() == "enabled" and \
                            shell_startup.lower() == "disabled" and \
                                processor_aes.lower() == "enabled":
                    final[v[0]] = "OK"
                else:
                    final[v[0]] = "Degraded"
    except:
        print("Unexpected error:", sys.exc_info()[0])
        raise
    return (final)
Exemplo n.º 8
0
class Email(object):
    def _smtp_setup(self):
        self.smtp_obj = smtplib.SMTP(host=self.smtp_server,
                                     port=self.smtp_port)

        if self.auth is not None and self.auth == 'TLS':
            self.smtp_obj.starttls()

        if self.username is not None and self.password is not None:
            self.smtp_obj.login(self.username, self.password)

        self.logger.debug("SMTP Server Open():%s port:%d\n", self.smtp_server,
                          self.smtp_port)

    def _smtp_close(self):
        self.smtp_obj.quit()
        self.logger.debug("SMTP Server Close()\n")

    def __init__(self,
                 smtp_server=None,
                 smtp_port=0,
                 auth='TLS',
                 username=None,
                 password=None,
                 logger=None):
        self.logger = logger or logging.getLogger(__name__)
        self.smtp_server = smtp_server
        self.smtp_port = smtp_port
        self.auth = auth
        self.username = username
        self.password = password
        self.json = None
        self.cfg = None
        self.smtp_obj = None

    def parse_json(self, cfg, schema):
        set_def = lambda x, y: self.cfg[x] if self.cfg[x] != "" else y
        self.json = JSONParser(os.path.abspath(schema),
                               os.path.abspath(cfg),
                               extend_defaults=True,
                               logger=self.logger)
        self.cfg = self.json.get_cfg()
        self.logger.debug(self.json.print_cfg())

        self._from = set_def('from', None)
        self._to = set_def('to', None)
        self._cc = self.cfg['cc']
        self._bcc = self.cfg['bcc']

        self.smtp_server = self.cfg['smtp-server']
        self.smtp_port = self.cfg['smtp-port']
        self.auth = set_def('smtp-authentication', None)
        self.username = set_def('smtp-username', None)
        self.password = set_def('smtp-password', None)

    def set_header(self, _from, _to, _cc='', _bcc=''):
        set_val = lambda x, y: x if x is not None and x != '' else getattr(
            self, y)

        #update if the field value is vaild
        self._from = set_val(_from, '_from')
        self._to = set_val(_to, '_to')
        self._cc = _cc
        self._bcc = _bcc

    def send_email(self,
                   _from=None,
                   _to=None,
                   _cc='',
                   _bcc='',
                   subject='',
                   content=''):

        set_val = lambda x, y: getattr(self, y) if x is None or x == '' else x

        _from = set_val(_from, '_from')
        _to = set_val(_to, '_to')
        _cc = set_val(_cc, '_cc')
        _bcc = set_val(_bcc, '_bcc')

        if _from is None or _from == '':
            self.logger.warn("Invalid from address")
            return

        if (_to is None or _to == '') and (self._to is None or self._to == ''):
            self.logger.warn("Invalid to address")
            return

        self.logger.info("From: %s\nTo: %s\nCC: %s\nBCC: %s\nSubject: %s\n",
                         _from, _to, _cc, _bcc, subject)

        self._smtp_setup()

        rcpt = map(lambda it: it.strip(),
                   _cc.split(",") + _bcc.split(",") + [_to])

        msg = MIMEMultipart('alternative')
        msg['From'] = _from
        msg['Subject'] = subject
        msg['To'] = _to
        msg['Cc'] = _cc
        msg['Bcc'] = _bcc
        msg.attach(MIMEText(content))

        self.smtp_obj.sendmail(_from, rcpt, msg.as_string())

        self._smtp_close()
Exemplo n.º 9
0
        except AttributeError:
            for item in data:
                if "url" in item:
                    self.urls.append(item)
                else:
                    self.addItem(str(item))

        self.data = data

    @QtCore.pyqtSlot(QtCore.QModelIndex)
    def cell_double_clicked_slot(self, model_index):
        model_row = model_index.row()
        # the way urls_index_row_tuple is [(urls, index)]
        index_row_list = [x[1] for x in self.urls_index_row_tuple]
        if model_row in index_row_list:
            tuple_index = index_row_list.index(model_row)
            url = self.urls_index_row_tuple[tuple_index][0]
            print(url)
            self.url_signal.emit(QtCore.QUrl(url))


if __name__ == '__main__':
    from PyQt5 import QtWidgets
    import sys
    app = QtWidgets.QApplication(sys.argv)
    #j = JSONParser("http://127.0.0.1:8000/projects/2/")
    j = JSONParser("http://127.0.0.1:8000/projects/2/ideas/3/")
    view_widget = InstanceViewWidget(data=j.data)
    view_widget.show()
    sys.exit(app.exec_())
if __name__ == '__main__':

    parser = argparse.ArgumentParser()

    # Sagemaker specific arguments. Defaults are set in the environment variables.
    parser.add_argument('--output-data-dir',
                        type=str,
                        default=os.environ['SM_OUTPUT_DATA_DIR'])
    parser.add_argument('--model-dir',
                        type=str,
                        default=os.environ['SM_MODEL_DIR'])

    args = parser.parse_args()

    preprocessor = JSONParser()

    joblib.dump(preprocessor, os.path.join(args.model_dir, "model.joblib"))

    print("saved model!")


def input_fn(input_data, content_type):

    if content_type == 'application/json':
        return json.loads(input_data)
    else:
        raise ValueError(
            "{} not supported by json_parser!".format(content_type))