⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 lrmadmin.c

📁 在LINUX下实现HA的源代码
💻 C
📖 第 1 页 / 共 2 页
字号:
/* File: lrmadmin.c * Description: A adminstration tool for Local Resource Manager * * Author: Sun Jiang Dong <sunjd@cn.ibm.com> * Copyright (c) 2004 International Business Machines * * Todo: security verification * * This program is free software; you can redistribute it and/or * modify it under the terms of the GNU General Public * License as published by the Free Software Foundation; either * version 2.1 of the License, or (at your option) any later version. *  * This software is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU * General Public License for more details. *  * You should have received a copy of the GNU General Public * License along with this library; if not, write to the Free Software * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA */#include <portability.h>#include <stdio.h>#include <sys/types.h>#include <unistd.h>#include <stdlib.h>#ifndef __USE_GNU#define __USE_GNU/* For strnlen protype */ #include <string.h>#undef __USE_GNU#else#include <string.h>#endif#include <errno.h>#ifdef HAVE_GETOPT_H#include <getopt.h>#endif /* HAVE_GETOPT_H */#include <uuid/uuid.h>#include <uuid/uuid.h>#include <clplumbing/cl_log.h>#include <lrm/lrm_api.h>/*#include <clplumbing/lsb_exitcodes.h>*/const char * optstring = "AD:dEF:d:Msg:c:S:LI:Rh";static struct option long_options[] = {	{"daemon", 0, 0, 'd'},	{"executera", 1, 0, 'E'},	{"flush",1,0,'F'},	{"monitor",0,0,'M'},	{"status",1,0,'S'},	{"listall",0,0,'L'},	{"information",1,0,'I'},	{"add",1,0,'A'},	{"delete",1,0,'D'},	{"rasupported",1,0,'R'},	{"help",0,0,'h'},	{0,0,0,0}};GMainLoop *mainloop = NULL;const char * lrmadmin_name = "lrmadmin";/* 20 is the length limit for a argv[x] */const int ARGVI_MAX_LEN = 20;typedef enum {	ERROR_OPTION = -1,	NULL_OP, 	DAEMON_OP,	EXECUTE_RA,	FLUSH,	MONITOR,	MONITOR_SET,	MONITOR_GET,	MONITOR_CLS,	RSC_STATUS,	LIST_ALLRSC,	INF_RSC,	ADD_RSC,	DEL_RSC,	RATYPE_SUPPORTED,	HELP} lrmadmin_cmd_t;static const char * status_msg[5] = {	"Succeed", 		  /* LRM_OP_DONE         */        "Cancelled", 		  /* LRM_OP_CANCELLED    */        "Timeout",		  /* LRM_OP_TIMEOUT 	 */        "Not Supported",	  /* LRM_OP_NOTSUPPORTED */        "Failed Due to a Error"   /* LRM_OP_ERROR	 */};static gboolean QUIT_GETOPT = FALSE;static lrmadmin_cmd_t lrmadmin_cmd = NULL_OP;static gboolean ASYN_OPS = FALSE; static int call_id = 0;const char * simple_help_screen ="lrmadmin {-d|--deamon}\n"\"         {-A|--add} <rscid> <ratype> <raname> [<rsc_params_list>]\n"\"         {-D|--delete} <rscid>\n"\"         {-F|--flush} <rscid>\n"\"         {-E|--execute} <rscid> <operator> <timeout> [<operator_parameters_"\"list>]\n"\"         {-M|--monitor} -s <rscid> <operator> <timeout> <interval> "\"[<operator_parameters_list>]\n""         {-M|--monitor} {-g|-c} <rscid>\n"\"         {-S|--status} <rscid>\n"\"         {-L|--listall}\n"\"         {-I|--information} <rsc_id>\n"\"         {-R|--rasupported}\n"\"         {-h|--help}\n";#define OPTION_OBSCURE_CHECK \				if ( lrmadmin_cmd != NULL_OP ) { \					cl_log(LOG_ERR,"Obscure options."); \					return -1; \				}/* the begin of the internal used function list */static int resource_operation(ll_lrm_t * lrmd, int argc, int optind, 			      char * argv[]);static int add_resource(ll_lrm_t * lrmd, int argc, int optind, char * argv[]);static int transfer_cmd_params(int amount, int start, char * argv[], 			   const char * ra_type, GHashTable ** params_ht);static void g_ratype_supported(gpointer data, gpointer user_data);static void g_print_ops(gpointer data, gpointer user_data);static void g_get_rsc_description(gpointer data, gpointer user_data);static void print_rsc_inf(lrm_rsc_t * lrmrsc);static char * params_hashtable_to_str(const char * ra_type, GHashTable * ht);static void free_stritem_of_hashtable(gpointer key, gpointer value, 				      gpointer user_data);static void ocf_params_hash_to_str(gpointer key, gpointer value, 				   gpointer user_data);static void normal_params_hash_to_str(gpointer key, gpointer value, 				      gpointer user_data);static lrm_rsc_t * get_lrm_rsc(ll_lrm_t * lrmd, rsc_id_t rscid);static void g_print_monitor(gpointer lrm_mon, gpointer user_data);static int set_monitor(ll_lrm_t * lrmd, int argc, int optind, char * argv[]);/* the end of the internal used function list */static void lrm_op_done_callback(lrm_op_t* op);static void lrm_monitor_callback(lrm_mon_t* mon);static gboolean post_query_call_result(gpointer data);int main(int argc, char **argv){	int option_char;	rsc_id_t rscid_arg_tmp;	int ret_value = 0;         ll_lrm_t* lrmd;	lrm_rsc_t * lrm_rsc;	GList *ratype, *rscid_list;	/* Prevent getopt_long to print error message on stderr isself */	/*opterr = 0; */  		if (argc == 1) {		printf("%s",simple_help_screen);		return 0;	}	        cl_log_set_entity(lrmadmin_name);	cl_log_enable_stderr(TRUE);	cl_log_set_facility(LOG_USER);	memset(rscid_arg_tmp, '\0', sizeof(rsc_id_t));	do {		option_char = getopt_long (argc, argv, optstring,			long_options, NULL);		if (option_char == -1) {			break;		}		switch (option_char) {			case 'd':				OPTION_OBSCURE_CHECK 				lrmadmin_cmd = DAEMON_OP;				QUIT_GETOPT = TRUE;				break;			case 'A':				OPTION_OBSCURE_CHECK 				lrmadmin_cmd = ADD_RSC;				break;			case 'D':				OPTION_OBSCURE_CHECK 				lrmadmin_cmd = DEL_RSC;				if (optarg) {					uuid_parse(optarg, rscid_arg_tmp);				}				break;			case 'R':				OPTION_OBSCURE_CHECK 				lrmadmin_cmd = RATYPE_SUPPORTED;				break;			case 'F':				OPTION_OBSCURE_CHECK 				lrmadmin_cmd = FLUSH;				if (optarg) {					uuid_parse(optarg, rscid_arg_tmp);				}				break;			case 'E':				OPTION_OBSCURE_CHECK 				lrmadmin_cmd = EXECUTE_RA;				break;			case 'M':				OPTION_OBSCURE_CHECK 				lrmadmin_cmd = MONITOR;				break;			case 's':				if (lrmadmin_cmd != MONITOR) {					cl_log(LOG_ERR,"Option error.");					return -1;				}				lrmadmin_cmd = MONITOR_SET;				break;			case 'g':				if (lrmadmin_cmd != MONITOR) {					cl_log(LOG_ERR,"Option error.");					return -1;				}				lrmadmin_cmd = MONITOR_GET;				if (optarg) {					uuid_parse(optarg, rscid_arg_tmp);				}				break;			case 'c':				if (lrmadmin_cmd != MONITOR) {					cl_log(LOG_ERR,"Option error.");					return -1;				}				lrmadmin_cmd = MONITOR_CLS;				if (optarg) {					uuid_parse(optarg, rscid_arg_tmp);				}				break;			case 'S':				OPTION_OBSCURE_CHECK 				lrmadmin_cmd = RSC_STATUS;				if (optarg) {					uuid_parse(optarg, rscid_arg_tmp);				}				break;			case 'L':				OPTION_OBSCURE_CHECK 				lrmadmin_cmd = LIST_ALLRSC;				break;			case 'I':				OPTION_OBSCURE_CHECK 				if (optarg) {					uuid_parse(optarg, rscid_arg_tmp);				}				lrmadmin_cmd = INF_RSC;				break;			case 'h':				OPTION_OBSCURE_CHECK 				/* print detailed help screen? */				printf("%s",simple_help_screen);				return 0;			case '?':				/* cl_log(LOG_ERR,"There is a unrecognized 				   option %s", optarg);				*/				printf("%s", simple_help_screen);				return -1;			default:				cl_log(LOG_ERR,"Error:getopt returned character"\					 " code %c.", option_char);				return -1;               }	} while (!QUIT_GETOPT);        lrmd = ll_lrm_new("lrm");        if (NULL == lrmd) {               	cl_log(LOG_ERR,"ll_lrn_new return null.");               	return -2;        }        if (lrmd->lrm_ops->signon(lrmd, lrmadmin_name) != 1) { /* != HA_OK */		if (lrmadmin_cmd == DAEMON_OP) { 			printf("lrmd daemon is not running.\n");			return 0;		} else {			cl_log(LOG_ERR,"Can't connect to lrmd, quit!");			return -2;		}	}		if (lrmadmin_cmd == DAEMON_OP) { 		printf("lrmd daemon is running.\n");		lrmd->lrm_ops->signoff(lrmd);		return 0;	}		switch (lrmadmin_cmd) {		case EXECUTE_RA:			call_id = resource_operation(lrmd, argc, optind, argv);			if (call_id < 0) {				if ( call_id == -2 ) {					cl_log(LOG_ERR, "Failed to operate "\					   "resource %s due to parameter error."					  , argv[optind]);					ret_value = -3;				}				if ( call_id == -1 ) {					cl_log(LOG_ERR, "Failed! no this "\					   "resource %s.", argv[optind]);					ret_value = -2;				}				cl_log(LOG_ERR, "Failed to operate "\				   "resource %s due to unknown error."				  , argv[optind]);				ret_value = -3;				ASYN_OPS = FALSE;			} else { 				/* Return value: HA_OK = 1 Or  HA_FAIL = 0 */				if ( call_id == 0 ) {					cl_log(LOG_ERR, "Resource operation "\					"Failed." );					ret_value = -3;					ASYN_OPS = FALSE;				} else { 					ASYN_OPS = TRUE;				}			}			break;			case ADD_RSC:			if (add_resource(lrmd, argc, optind, argv) == 0) {				printf("Succeeded in adding this resource.\n");			} else {				printf("Failed to add this resource.\n");				ret_value = -3;			}			ASYN_OPS = FALSE;			break;			case DEL_RSC:			/* Return value: HA_OK = 1 Or  HA_FAIL = 0 */			if (lrmd->lrm_ops->delete_rsc(lrmd, rscid_arg_tmp)==1) {				printf("Succeeded in delete this resource.\n");			} else {				printf("Failed to delete this resource.\n");				ret_value = -3;			}			ASYN_OPS = FALSE;			break;			case FLUSH:			lrm_rsc = get_lrm_rsc(lrmd, rscid_arg_tmp);			if (!(lrm_rsc)) {				ret_value = -3;			} else { 				/* Return value: HA_OK = 1 Or  HA_FAIL = 0 */				if (lrm_rsc->ops->flush_ops(lrm_rsc) == 1 ) {					printf("Succeeded in flushing.\n");				} else {					printf("Falied to flush.\n");					ret_value = -3;				}			}			ASYN_OPS = FALSE;			break;			case RATYPE_SUPPORTED:			ratype = lrmd->lrm_ops->get_ra_supported(lrmd);			printf("List size: %d\n", g_list_length(ratype));			if (ratype) {				g_list_foreach(ratype, g_ratype_supported, NULL);				g_list_free(ratype);			} else {				printf("No resource agency is supported\n");			}			ASYN_OPS = FALSE;			break;			case LIST_ALLRSC:			rscid_list = lrmd->lrm_ops->get_all_rscs(lrmd);			if (rscid_list) {				g_list_foreach(rscid_list, g_get_rsc_description						, lrmd);				g_list_free(rscid_list);			} else				printf("Currently no resource is managed by "\					 "LRM.\n");			ASYN_OPS = FALSE;			break;			case INF_RSC:			lrm_rsc = get_lrm_rsc(lrmd, rscid_arg_tmp);			if (!(lrm_rsc)) {				ret_value = -3;			} else {				print_rsc_inf(lrm_rsc);				g_free(lrm_rsc);			}			ASYN_OPS = FALSE;			break;			case RSC_STATUS: 			lrm_rsc = get_lrm_rsc(lrmd, rscid_arg_tmp);			if (!(lrm_rsc)) {				ret_value = -3;			} else { 				state_flag_t cur_state;				GList * ops_queue;				ops_queue = lrm_rsc->ops->get_cur_state(lrm_rsc, 								&cur_state);				if (!ops_queue) {					cl_log(LOG_ERR, "Operation queue "\					  "pointer is null when try to get the"\					  " operation status on a RA.");					ret_value = -3;				} else {					if (cur_state == LRM_RSC_IDLE) {						printf("No operation is doing"\						 "on the resource, and the "\						 "operation is the last one "\						 "executed on the resource.\n");					} else {						printf("The following "\						 "operations are those in the "\						 "queue, and the first one is "\						 "running now.\n");					}					g_list_foreach(ops_queue, g_print_ops, 							NULL);					g_list_free(ops_queue);				}			}			ASYN_OPS = FALSE;			break;		case MONITOR: 			fprintf(stderr, "Need one more definite option.\n");			ret_value = -1;			ASYN_OPS = FALSE;			break;		/* Don't finished ops */	  	case MONITOR_SET: 			call_id = set_monitor(lrmd, argc, optind, argv);			if (call_id < 0) {				cl_log(LOG_ERR, "There are invalid parameters");				ret_value = -3;				ASYN_OPS = FALSE;			} else { 

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -