Esempio n. 1
0
int main(int argc, char **argv)
{
    char               opt;
    int                n = 0, x = 0;
    gw_client_t *      gw_session;
    int                num_users, i;
    gw_msg_user_t *    user_status;
    struct sigaction   act;
    gw_return_code_t   rc;
  	
    /* ---------------------------------------------------------------- */
    /* Parse arguments                                                  */
    /* ---------------------------------------------------------------- */
	
    opterr = 0;
    optind = 1;
	
    while((opt = getopt(argc,argv,"nhx"))!= -1)
        switch(opt)
        {
            case 'n': n  = 1;
                break;
            case 'x': x  = 1;
                break; 
		    case 'h':
            	printf("%s", usage);
                exit(0);
                break;
            case '?':
                fprintf(stderr,"error: invalid option \'%c\'\n",optopt);
            	printf("%s", susage);
                exit(1);
                break;                
          	}
	    
	/* ---------------------------------------------------------------- */
	/* Connect to GWD                                                   */
	/* ---------------------------------------------------------------- */

	gw_session = gw_client_init();
	
	if ( gw_session == NULL )
	{
		fprintf(stderr,"Could not connect to gwd\n");
		return (-1);
	}

    act.sa_handler = signal_handler;
    act.sa_flags   = SA_RESTART;
    sigemptyset(&act.sa_mask);
        
    sigaction(SIGTERM||SIGINT,&act,NULL);
    
    
    /* ---------------------------------------------------------------- */
    /* Get job or pool status                                           */
    /* ---------------------------------------------------------------- */

    rc = gw_client_user_status(&user_status, &num_users);
	
    if (rc == GW_RC_SUCCESS)
    {
	  if (x){
		// A.L: It would be nice to include all this settings for header and footer
		// in the gw_client_print_user_xml function
		char command[]="gwuser";
		int xml_header_flag = 1, xml_footer_flag = 1;
		if ( xml_header_flag ){
		  gw_print_xml_header(command);
		  xml_header_flag = 0;
		}
		for (i=0;i<num_users;i++){
		  gw_client_print_user_xml(&(user_status[i]));
		}
		if ( xml_footer_flag ){
		  gw_print_xml_footer(command);
		  xml_footer_flag = 0;
		}
	  }
	  else {
       	if (!n)
		  gw_client_print_user_header();
		
        for (i=0;i<num_users;i++)
		  gw_client_print_user(&(user_status[i]));
	  }
	  gw_client_finalize();
    }  
    else
	  {
    	fprintf(stderr,"FAILED: %s\n",gw_ret_code_string(rc)); 
        
        gw_client_finalize();
        return -1;
	  }  
	
    return 0;
}
Esempio n. 2
0
int main(int argc, char **argv)
{
    int               job_id = -1;
  	char              opt;
  	int               c = 0, n = 0, f = 0, x = 0;
    int               delay = 0;
  	gw_client_t *     gw_session;
	gw_msg_job_t      job_status;
	struct sigaction  act;
  	gw_return_code_t  rc;
  	char *			  hostname     = NULL;
  	char *			  username     = NULL;
    char *			  outoption	   = NULL;    	
  	char 			  jobstate	   = '&'; // If not set to other value all job states will be printed
  										  // (see gw_check_state in gw_cmds_common.c)
    int               i;
    int  	          array_id     = -1;  										    										  
										  
  	
	/* ---------------------------------------------------------------- */
	/* Parse arguments                                                  */
	/* ---------------------------------------------------------------- */
	
    opterr = 0;
    optind = 1;
	
    while((opt = getopt(argc, argv, ":nfxhc:u:r:s:o:A:")) != -1)
        switch(opt)
        {
            case 'c': c  = 1;
                delay = atoi(optarg);
                break;
            case 'n': n = 1;
                break;  
            case 'f': f = 1;
                break;  
		    case 'x': x = 1;
                break;  
            case 'u':                     	
            	username = strdup(optarg);         
                break;
            case 'r':                     	
            	hostname = strdup(optarg);        
                break; 
            case 's':                     	
            	jobstate = strdup(optarg)[0];
            	switch(jobstate)
            	{
            		case 'i':
            		case 'p':
            		case 'h':
            		case 'w':
            		case 'e':
            		case 's':
            		case 'k':
            		case 'm':
            		case 'z':
            		case 'f':
            			break;
            		default:
            			printf("ERROR: Job state must be one of {i,p,h,w,e,s,k,m,z,f}\n");
		            	printf("%s", susage);
		                return (-1);             		
            			break;	
            	}
                break; 
            case 'o':
            	outoption = strdup(optarg);
            	
            	int seen_t=0;
            	
            	for(i=0;i<strlen(outoption);i++)
            		switch(outoption[i])
            		{
						case 't':
							if(seen_t)
							{
		            			printf("ERROR: options t and T are mutually exclusive\n");
				            	printf("%s", susage);
				                return (-1);
							}
				            seen_t++;								
							break;
						case 'T':
							if(seen_t)
							{
		            			printf("ERROR: options t and T are mutually exclusive\n");
				            	printf("%s", susage);
				                return (-1);
							}
				            seen_t++;								
							break;           			
            			case 'e':
            			case 's':
            			case 'u':
            			case 'j':
						case 'h':
						case 'x':
						case 'i':  
						case 'f':
						case 'p':
                        case 'J':
                        case 'y':
                        case 'n':
            				break;
	            		default:
	            			printf("ERROR: Output format must be constructed with {e,s,u,j,t,h,x,i,p,J,y,n}\n");
			            	printf("%s", susage);
			                return (-1);             		
	            			break;
            		}        
                break; 
            case 'A':
            	array_id = atoi(optarg);            	
                break;            	            
            case 'h':
            	printf("%s", usage);
                exit(0);
                break;
            case '?':
                fprintf(stderr,"error: invalid option \'%c\'\n",optopt);
            	printf("%s", susage);
                exit(1);
                break;                
            case ':':
                fprintf(stderr,"error: must provide an argument for option \'%c\'\n",
                        optopt);                 
            	printf("%s", susage);
                exit(1);
                break;
      	}
                	
	if ( optind < argc)
		job_id = atoi(argv[optind]);


	/* ---------------------------------------------------------------- */
	/* Connect to GWD                                                   */
	/* ---------------------------------------------------------------- */

	gw_session = gw_client_init();
	
	if ( gw_session == NULL )
	{
		if(username != NULL)
			free(username);
		if(hostname != NULL)
			free(hostname);	
		if(outoption != NULL)
			free(outoption);					
			
		fprintf(stderr,"Could not connect to gwd\n");
		return (-1);
	}

    act.sa_handler = signal_handler;
    act.sa_flags   = SA_RESTART;
    sigemptyset(&act.sa_mask);
        
    sigaction(SIGTERM||SIGINT,&act,NULL);
    
    
	/* ---------------------------------------------------------------- */
	/* Get job or pool status                                           */
	/* ---------------------------------------------------------------- */

	do
	{
		if (c)
		{
			cls();
            move(0,0);
        }

	    if (job_id != -1){
		  rc = gw_client_job_status(job_id, &job_status);
		}
		else {
		  rc = gw_client_job_status_all( );
		}

    	if (rc == GW_RC_SUCCESS)
        {
		    if (f)
            {
		        if (job_id != -1)
		        	gw_client_print_status_full(&job_status);
    			else	   	    
	    	    	gw_client_print_pool_status_full(username, hostname, jobstate, array_id);
            }
			else if (x)
			{
			  char command[]=GW_PS_COMMAND_XML;
			  char command_open[GW_PS_COMMAND_OPEN_SIZE_XML];

			  if ( username == NULL && hostname == NULL ){
				sprintf (command_open, "%s", command);
			  } 
			  else if ( username != NULL && hostname == NULL ){
				sprintf (command_open, "%s USERNAME=\"%s\"", command, username);
			  } 
			  else if ( username == NULL && hostname != NULL ){
				sprintf (command_open, "%s HOSTNAME=\"%s\"", command, hostname);
			  } 
			  else if ( username != NULL && hostname != NULL ){
				sprintf (command_open, "%s USERNAME=\"%s\" HOSTNAME=\"%s\"", command, username, hostname);
			  }
			  gw_print_xml_header(command_open);		  
			  if (job_id != -1){
				gw_client_print_status_xml(&job_status);
			  }
			  else {
				gw_client_print_pool_status_xml(username, hostname, jobstate, array_id);
			  }
			  gw_print_xml_footer(command);
			}
            else
			{
            	if (!n)
            		gw_client_print_status_header(outoption);

		        if (job_id != -1)
		        	gw_client_print_status(&job_status, outoption);
    			else	   	    
	    	    	gw_client_print_pool_status(username, hostname, jobstate, outoption, array_id);
            }
        }  
	    else
	    {
			if(username != NULL)
				free(username);
			if(hostname != NULL)
				free(hostname);	 
		    if(outoption != NULL)
			    free(outoption);					
							   	
	    	fprintf(stderr,"FAILED: %s\n",gw_ret_code_string(rc)); 
           	
	        gw_client_finalize();
	        return -1;
	    }  

		sleep(delay);	    
		
	} while(c);
	
	if(username != NULL)
		free(username);
	if(hostname != NULL)
		free(hostname);
	if(outoption != NULL)
		free(outoption);					
	
	return 0;
}