def post_process(proc_data, args_array, cfg, **kwargs): """Function: post_process Description: Send the data to the requested output and format if requested. Arguments: (input) proc_data -> Dictionary of process data. (input) args_array -> Dictionary of command line options and values. (input) cfg -> Configuration module settings. """ proc_data = dict(proc_data) args_array = dict(args_array) if "-n" not in args_array: if "-f" in args_array: gen_libs.display_data(proc_data) else: print(proc_data) if "-m" in args_array: mongo_libs.ins_doc(cfg, cfg.db, cfg.coll, proc_data)
def _process_json(outdata, **kwargs): """Function: _process_json Description: Private function for chk_mem_rep_lag(). Process JSON data. Arguments: (input) outdata -> JSON document from chk_mem_rep_lag function. (input) **kwargs: ofile -> file name - Name of output file. db_tbl -> database:collection - Name of db and collection. class_cfg -> Server class configuration settings. mail -> Mail instance. args_array -> Array of command line options and values. (output) status -> Tuple on connection status. status[0] - True|False - Connection successful. status[1] - Error message if connection failed. """ status = (True, None) mode = "w" indent = 4 mongo_cfg = kwargs.get("class_cfg", None) db_tbl = kwargs.get("db_tbl", None) ofile = kwargs.get("ofile", None) mail = kwargs.get("mail", None) args_array = dict(kwargs.get("args_array", {})) if args_array.get("-a", False): mode = "a" if args_array.get("-f", False): indent = None jdata = json.dumps(outdata, indent=indent) if mongo_cfg and db_tbl: dbs, tbl = db_tbl.split(":") status = mongo_libs.ins_doc(mongo_cfg, dbs, tbl, outdata) if not status[0]: status = (status[0], "_process_json: " + status[1]) if ofile: gen_libs.write_file(ofile, mode, jdata) if mail: mail.add_2_msg(jdata) mail.send_mail() if not args_array.get("-z", False): gen_libs.display_data(jdata) return status
def node_chk(mongo, args_array, **kwargs): """Function: node_chk Description: Check the status of all Mongo nodes. Will only output something if a node is down or an error is detected. Arguments: (input) mongo -> Mongo instance. (input) args_array -> Array of command line options and values. (input) **kwargs: mail -> Mail instance. (output) status -> Tuple on connection status. status[0] - True|False - Connection successful. status[1] - Error message if connection failed. """ status = (True, None) args_array = dict(args_array) mail = kwargs.get("mail", None) node_status = {} indent = None if args_array.get("-f", False) else 4 for node in mongo.adm_cmd("replSetGetStatus").get("members"): status2 = single_node_chk(node) if status2: node_status[node.get("name")] = status2 if node_status: jnode_status = json.dumps(node_status, indent=indent) if not args_array.get("-z", False): gen_libs.display_data(jnode_status) if mail: if not mail.subj: subj = "Node Status Check for Rep Set: %s" % mongo.repset mail.create_subject(subj=subj) mail.add_2_msg(jnode_status) mail.send_mail() return status
def process_yum(args_array, yum, dict_key, func_name, **kwargs): """Function: process_yum Description: Create and populate dictionary form based on the dict_key and func_name. Send dictionary to output. Arguments: (input) args_array -> Array of command line options and values. (input) yum -> Yum class instance. (input) dict_key -> Dictionary key value. (input) func_name -> Name of class method to call. (input) **kwargs: class_cfg -> Mongo server configuration. (output) status -> Tuple on connection status. status[0] - True|False - Mongo connection successful. status[1] - Error message if Mongo connection failed. """ status = (True, None) indent = 4 mode = "w" args_array = dict(args_array) os_distro = yum.get_distro() data = { "Server": yum.get_hostname(), "OsRelease": os_distro[0] + " " + os_distro[1], "AsOf": datetime.datetime.strftime(datetime.datetime.now(), "%Y-%m-%d %H:%M:%S"), dict_key: func_name() } ofile = args_array.get("-o", False) db_tbl = args_array.get("-i", False) class_cfg = kwargs.get("class_cfg", False) if args_array.get("-a", False): mode = "a" if args_array.get("-f", False): indent = None if db_tbl and class_cfg: dbn, tbl = db_tbl.split(":") status = mongo_libs.ins_doc(class_cfg, dbn, tbl, data) if not status[0]: status = (status[0], "Mongo_Insert: " + status[1]) data = json.dumps(data, indent=indent) if ofile: gen_libs.write_file(ofile, mode, data) if not args_array.get("-z", False): gen_libs.display_data(data) if args_array.get("-e", False): mail = gen_class.setup_mail(args_array.get("-e"), subj=args_array.get("-s", None)) mail.add_2_msg(data) use_mailx = args_array.get("-u", False) mail.send_mail(use_mailx=use_mailx) return status
def status(server, args_array, **kwargs): """Function: status Description: Retrieves a number of database status variables and sends them out either in standard out (print) or to a JSON format which is printed and/or insert into the database. Arguments: (input) server -> Database server instance. (input) args_array -> Array of command line options and values. (input) **kwargs: ofile -> file name - Name of output file. db_tbl database:table_name -> Mongo database and table name. class_cfg -> Mongo Rep Set server configuration. mail -> Mail instance. (output) err_flag -> True|False - If an error has occurred. (output) err_msg -> Error message. """ err_flag = False err_msg = None mode = "w" indent = 4 args_array = dict(args_array) server.upd_srv_stat() outdata = {"Application": "MongoDB", "Server": server.name, "AsOf": datetime.datetime.strftime(datetime.datetime.now(), "%Y-%m-%d %H:%M:%S")} outdata.update({"Memory": {"CurrentUsage": server.cur_mem, "MaxUsage": server.max_mem, "PercentUsed": server.prct_mem}, "UpTime": server.days_up, "Connections": {"CurrentConnected": server.cur_conn, "MaxConnections": server.max_conn, "PercentUsed": server.prct_conn}}) ofile = kwargs.get("ofile", None) mail = kwargs.get("mail", None) mongo_cfg = kwargs.get("class_cfg", None) db_tbl = kwargs.get("db_tbl", None) if args_array.get("-a", False): mode = "a" if args_array.get("-g", False): indent = None if "-j" in args_array: outdata = json.dumps(outdata, indent=indent) if mongo_cfg and db_tbl: dbn, tbl = db_tbl.split(":") if isinstance(outdata, dict): state = mongo_libs.ins_doc(mongo_cfg, dbn, tbl, outdata) else: state = mongo_libs.ins_doc(mongo_cfg, dbn, tbl, ast.literal_eval(outdata)) if not state[0]: err_flag = True err_msg = "Inserting into Mongo database: %s" % state[1] if ofile and "-j" in args_array: gen_libs.write_file(ofile, mode, outdata) elif ofile: gen_libs.display_data(outdata, f_hdlr=gen_libs.openfile(ofile, mode)) if mail and isinstance(outdata, dict): mail.add_2_msg(json.dumps(outdata, indent=indent)) mail.send_mail() elif mail: mail.add_2_msg(outdata) mail.send_mail() if not args_array.get("-z", False): gen_libs.display_data(outdata) return err_flag, err_msg