示例#1
0
static int _isdefault_old(List acct_list)
{
    int rc = 0;
    slurmdb_user_cond_t user_cond;
    List ret_list = NULL;

    if (!acct_list || !list_count(acct_list))
        return rc;

    memset(&user_cond, 0, sizeof(slurmdb_user_cond_t));
    user_cond.def_acct_list = acct_list;

    ret_list = acct_storage_g_get_users(db_conn, my_uid, &user_cond);
    if (ret_list && list_count(ret_list)) {
        ListIterator itr = list_iterator_create(ret_list);
        slurmdb_user_rec_t *user = NULL;
        fprintf(stderr," Users listed below have these "
                "as their Default Accounts.\n");
        while((user = list_next(itr))) {
            fprintf(stderr, " User - %-10.10s Account - %s\n",
                    user->name, user->default_acct);
        }
        list_iterator_destroy(itr);
        rc = 1;
    }

    if (ret_list)
        list_destroy(ret_list);

    return rc;
}
示例#2
0
extern int sacctmgr_dump_cluster (int argc, char **argv)
{
	slurmdb_user_cond_t user_cond;
	slurmdb_user_rec_t *user = NULL;
	slurmdb_hierarchical_rec_t *slurmdb_hierarchical_rec = NULL;
	slurmdb_assoc_rec_t *assoc = NULL;
	slurmdb_assoc_cond_t assoc_cond;
	List assoc_list = NULL;
	List acct_list = NULL;
	List user_list = NULL;
	List slurmdb_hierarchical_rec_list = NULL;
	char *cluster_name = NULL;
	char *file_name = NULL;
	char *user_name = NULL;
	char *line = NULL;
	int i, command_len = 0;
	FILE *fd = NULL;
	char *class_str = NULL;

	for (i = 0; i < argc; i++) {
		int end = parse_option_end(argv[i]);

		if (!end)
			command_len = strlen(argv[i]);
		else {
			command_len = end - 1;
			if (argv[i][end] == '=') {
				end++;
			}
		}
		if (!end || !strncasecmp(argv[i], "Cluster",
					 MAX(command_len, 1))) {
			if (cluster_name) {
				exit_code = 1;
				fprintf(stderr,
					" Can only do one cluster at a time.  "
					"Already doing %s\n", cluster_name);
				continue;
			}
			cluster_name = xstrdup(argv[i]+end);
		} else if (!strncasecmp(argv[i], "File",
					MAX(command_len, 1))) {
			if (file_name) {
				exit_code = 1;
				fprintf(stderr,
					" File name already set to %s\n",
					file_name);
				continue;
			}
			file_name = xstrdup(argv[i]+end);
		} else {
			exit_code = 1;
			fprintf(stderr, " Unknown option: %s\n", argv[i]);
		}
	}

	if (!cluster_name) {
		exit_code = 1;
		fprintf(stderr, " We need a cluster to dump.\n");
		xfree(file_name);
		return SLURM_ERROR;
	} else {
		List temp_list = NULL;
		slurmdb_cluster_cond_t cluster_cond;
		slurmdb_cluster_rec_t *cluster_rec = NULL;

		slurmdb_init_cluster_cond(&cluster_cond, 0);
		cluster_cond.cluster_list = list_create(NULL);
		list_push(cluster_cond.cluster_list, cluster_name);

		temp_list = acct_storage_g_get_clusters(db_conn, my_uid,
							&cluster_cond);
		FREE_NULL_LIST(cluster_cond.cluster_list);
		if (!temp_list) {
			exit_code = 1;
			fprintf(stderr,
				" Problem getting clusters from database.  "
				"Contact your admin.\n");
			xfree(cluster_name);
			xfree(file_name);
			return SLURM_ERROR;
		}

		cluster_rec = list_peek(temp_list);
		if (!cluster_rec) {
			exit_code = 1;
			fprintf(stderr, " Cluster %s doesn't exist.\n",
				cluster_name);
			xfree(cluster_name);
			xfree(file_name);
			FREE_NULL_LIST(temp_list);
			return SLURM_ERROR;
		}
		class_str = get_classification_str(cluster_rec->classification);
		FREE_NULL_LIST(temp_list);
	}

	if (!file_name) {
		file_name = xstrdup_printf("./%s.cfg", cluster_name);
		printf(" No filename given, using %s.\n", file_name);
	}

	memset(&user_cond, 0, sizeof(slurmdb_user_cond_t));
	user_cond.with_coords = 1;
	user_cond.with_wckeys = 1;
	user_cond.with_assocs = 1;

	memset(&assoc_cond, 0, sizeof(slurmdb_assoc_cond_t));
	assoc_cond.without_parent_limits = 1;
	assoc_cond.with_raw_qos = 1;
	assoc_cond.cluster_list = list_create(NULL);
	list_append(assoc_cond.cluster_list, cluster_name);
	/* this is needed for getting the correct wckeys */
	user_cond.assoc_cond = &assoc_cond;

	user_list = acct_storage_g_get_users(db_conn, my_uid, &user_cond);
	/* If not running with the DBD assoc_cond.user_list can be set,
	 * which will mess other things up.
	 */
	if (assoc_cond.user_list) {
		FREE_NULL_LIST(assoc_cond.user_list);
		assoc_cond.user_list = NULL;
	}

	/* make sure this person running is an admin */
	user_name = uid_to_string_cached(my_uid);
	if (!(user = sacctmgr_find_user_from_list(user_list, user_name))) {
		exit_code = 1;
		fprintf(stderr, " Your uid (%u) is not in the "
			"accounting system, can't dump cluster.\n", my_uid);
		FREE_NULL_LIST(assoc_cond.cluster_list);
		xfree(cluster_name);
		xfree(file_name);
		FREE_NULL_LIST(user_list);
		return SLURM_ERROR;

	} else {
		if (my_uid != slurm_get_slurm_user_id() && my_uid != 0
		    && user->admin_level < SLURMDB_ADMIN_SUPER_USER) {
			exit_code = 1;
			fprintf(stderr, " Your user does not have sufficient "
				"privileges to dump clusters.\n");
			FREE_NULL_LIST(assoc_cond.cluster_list);
			xfree(cluster_name);
			xfree(file_name);
			FREE_NULL_LIST(user_list);
			return SLURM_ERROR;
		}
	}
	xfree(user_name);

	/* assoc_cond is set up above */
	assoc_list = acct_storage_g_get_assocs(db_conn, my_uid,
						     &assoc_cond);
	FREE_NULL_LIST(assoc_cond.cluster_list);
	if (!assoc_list) {
		exit_code = 1;
		fprintf(stderr, " Problem with query.\n");
		xfree(cluster_name);
		xfree(file_name);
		return SLURM_ERROR;
	} else if (!list_count(assoc_list)) {
		exit_code = 1;
		fprintf(stderr, " Cluster %s returned nothing.\n",
			cluster_name);
		FREE_NULL_LIST(assoc_list);
		xfree(cluster_name);
		xfree(file_name);
		return SLURM_ERROR;
	}

	slurmdb_hierarchical_rec_list = slurmdb_get_acct_hierarchical_rec_list(
		assoc_list);

	acct_list = acct_storage_g_get_accounts(db_conn, my_uid, NULL);

	if ((fd = fopen(file_name,"w")) == NULL) {
		fprintf(stderr, "Can't open file %s, %s\n", file_name,
			slurm_strerror(errno));
		FREE_NULL_LIST(acct_list);
		FREE_NULL_LIST(assoc_list);
		xfree(cluster_name);
		xfree(file_name);
		FREE_NULL_LIST(slurmdb_hierarchical_rec_list);
		return SLURM_ERROR;
	}

	/* Add header */
	if (fprintf(fd,
		    "# To edit this file start with a cluster line "
		    "for the new cluster\n"
		    "# Cluster - 'cluster_name':MaxNodesPerJob=50\n"
		    "# Followed by Accounts you want in this fashion "
		    "(root is created by default)...\n"
		    "# Parent - 'root'\n"
		    "# Account - 'cs':MaxNodesPerJob=5:MaxJobs=4:"
		    "MaxTRESMins=cpu=20:FairShare=399:"
		    "MaxWallDuration=40:Description='Computer Science':"
		    "Organization='LC'\n"
		    "# Any of the options after a ':' can be left out and "
		    "they can be in any order.\n"
		    "# If you want to add any sub accounts just list the "
		    "Parent THAT HAS ALREADY \n"
		    "# BEEN CREATED before the account line in this "
		    "fashion...\n"
		    "# Parent - 'cs'\n"
		    "# Account - 'test':MaxNodesPerJob=1:MaxJobs=1:"
		    "MaxTRESMins=cpu=1:FairShare=1:"
		    "MaxWallDuration=1:"
		    "Description='Test Account':Organization='Test'\n"
		    "# To add users to a account add a line like this after a "
		    "Parent - 'line'\n"
		    "# User - 'lipari':MaxNodesPerJob=2:MaxJobs=3:"
		    "MaxTRESMins=cpu=4:FairShare=1:"
		    "MaxWallDurationPerJob=1\n") < 0) {
		exit_code = 1;
		fprintf(stderr, "Can't write to file");
		FREE_NULL_LIST(acct_list);
		FREE_NULL_LIST(assoc_list);
		xfree(cluster_name);
		xfree(file_name);
		FREE_NULL_LIST(slurmdb_hierarchical_rec_list);
		return SLURM_ERROR;
	}

	line = xstrdup_printf("Cluster - '%s'", cluster_name);

	if (class_str)
		xstrfmtcat(line, ":Classification='%s'", class_str);

	slurmdb_hierarchical_rec = list_peek(slurmdb_hierarchical_rec_list);
	assoc = slurmdb_hierarchical_rec->assoc;
	if (xstrcmp(assoc->acct, "root")) {
		fprintf(stderr, "Root association not on the top it was %s\n",
			assoc->acct);
	} else
		print_file_add_limits_to_line(&line, assoc);

	if (fprintf(fd, "%s\n", line) < 0) {
		exit_code = 1;
		fprintf(stderr, " Can't write to file");
		FREE_NULL_LIST(acct_list);
		FREE_NULL_LIST(assoc_list);
		xfree(cluster_name);
		xfree(file_name);
		xfree(line);
		FREE_NULL_LIST(slurmdb_hierarchical_rec_list);
		return SLURM_ERROR;
	}
	info("%s", line);
	xfree(line);

	print_file_slurmdb_hierarchical_rec_list(
		fd, slurmdb_hierarchical_rec_list, user_list, acct_list);

	FREE_NULL_LIST(acct_list);
	FREE_NULL_LIST(assoc_list);
	xfree(cluster_name);
	xfree(file_name);
	FREE_NULL_LIST(slurmdb_hierarchical_rec_list);
	fclose(fd);

	return SLURM_SUCCESS;
}
示例#3
0
extern List slurmdb_report_user_top_usage(void *db_conn,
        slurmdb_user_cond_t *user_cond,
        bool group_accounts)
{
    List cluster_list = NULL;
    ListIterator itr = NULL;
    ListIterator itr2 = NULL;
    ListIterator itr3 = NULL;
    ListIterator cluster_itr = NULL;
    slurmdb_cluster_cond_t cluster_cond;
    List user_list = NULL;
    List usage_cluster_list = NULL;
    char *object = NULL;
    int exit_code = 0;
    slurmdb_user_rec_t *user = NULL;
    slurmdb_cluster_rec_t *cluster = NULL;
    slurmdb_assoc_rec_t *assoc = NULL;
    slurmdb_report_user_rec_t *slurmdb_report_user = NULL;
    slurmdb_report_cluster_rec_t *slurmdb_report_cluster = NULL;
    uid_t my_uid = getuid();
    bool delete_user_cond = 0, delete_assoc_cond = 0,
         delete_cluster_list = 0;
    time_t start_time, end_time;

    if (!user_cond) {
        delete_user_cond = 1;
        user_cond = xmalloc(sizeof(slurmdb_user_cond_t));
    }

    if (!user_cond->assoc_cond) {
        delete_assoc_cond = 1;
        user_cond->assoc_cond =
            xmalloc(sizeof(slurmdb_assoc_cond_t));
    }

    if (!user_cond->assoc_cond->cluster_list) {
        delete_cluster_list = 1;
        user_cond->assoc_cond->cluster_list =
            list_create(slurm_destroy_char);
    }

    user_cond->with_deleted = 1;
    user_cond->with_assocs = 1;
    user_cond->assoc_cond->with_usage = 1;
    user_cond->assoc_cond->without_parent_info = 1;

    /* This needs to be done on some systems to make sure
       assoc_cond isn't messed up.  This has happened on some 64
       bit machines and this is here to be on the safe side.
    */
    start_time = user_cond->assoc_cond->usage_start;
    end_time = user_cond->assoc_cond->usage_end;
    slurmdb_report_set_start_end_time(&start_time, &end_time);
    user_cond->assoc_cond->usage_start = start_time;
    user_cond->assoc_cond->usage_end = end_time;

    user_list = acct_storage_g_get_users(db_conn, my_uid, user_cond);
    if (!user_list) {
        exit_code=1;
        fprintf(stderr, " Problem with user query.\n");
        goto end_it;
    }

    /* We have to get the clusters here or we will be unable to
       get the correct total time for the cluster if associations
       are not enforced.
    */
    slurmdb_init_cluster_cond(&cluster_cond, 0);
    cluster_cond.with_usage = 1;
    cluster_cond.with_deleted = 1;
    cluster_cond.usage_end = user_cond->assoc_cond->usage_end;
    cluster_cond.usage_start = user_cond->assoc_cond->usage_start;
    cluster_cond.cluster_list = user_cond->assoc_cond->cluster_list;

    usage_cluster_list = acct_storage_g_get_clusters(
                             db_conn, my_uid, &cluster_cond);
    if (!usage_cluster_list) {
        exit_code=1;
        fprintf(stderr, " Problem with cluster query.\n");
        goto end_it;
    }

    cluster_list = list_create(slurmdb_destroy_report_cluster_rec);

    itr = list_iterator_create(usage_cluster_list);
    while((cluster = list_next(itr))) {
        /* check to see if this cluster is around during the
           time we are looking at */
        if (!cluster->accounting_list
                || !list_count(cluster->accounting_list))
            continue;

        slurmdb_report_cluster = slurmdb_cluster_rec_2_report(cluster);

        list_append(cluster_list, slurmdb_report_cluster);

        slurmdb_report_cluster->user_list =
            list_create(slurmdb_destroy_report_user_rec);
    }
    list_iterator_destroy(itr);
    list_destroy(usage_cluster_list);

    itr = list_iterator_create(user_list);
    cluster_itr = list_iterator_create(cluster_list);
    while((user = list_next(itr))) {
        struct passwd *passwd_ptr = NULL;
        if (!user->assoc_list || !list_count(user->assoc_list))
            continue;

        passwd_ptr = getpwnam(user->name);
        if (passwd_ptr)
            user->uid = passwd_ptr->pw_uid;
        else
            user->uid = (uint32_t)NO_VAL;

        itr2 = list_iterator_create(user->assoc_list);
        while((assoc = list_next(itr2))) {

            if (!assoc->accounting_list
                    || !list_count(assoc->accounting_list))
                continue;

            while((slurmdb_report_cluster =
                        list_next(cluster_itr))) {
                if (!strcmp(slurmdb_report_cluster->name,
                            assoc->cluster)) {
                    ListIterator user_itr = NULL;
                    if (!group_accounts) {
                        slurmdb_report_user = NULL;
                        goto new_user;
                    }
                    user_itr = list_iterator_create(
                                   slurmdb_report_cluster->
                                   user_list);
                    while((slurmdb_report_user
                            = list_next(user_itr))) {
                        if (slurmdb_report_user->uid
                                != NO_VAL) {
                            if (slurmdb_report_user->
                                    uid
                                    == user->uid)
                                break;
                        } else if (slurmdb_report_user->
                                   name
                                   && !strcasecmp(
                                       slurmdb_report_user->
                                       name,
                                       user->name))
                            break;
                    }
                    list_iterator_destroy(user_itr);
new_user:
                    if (!slurmdb_report_user) {
                        slurmdb_report_user = xmalloc(
                                                  sizeof
                                                  (slurmdb_report_user_rec_t));
                        slurmdb_report_user->name =
                            xstrdup(assoc->user);
                        slurmdb_report_user->uid =
                            user->uid;
                        slurmdb_report_user->acct_list =
                            list_create
                            (slurm_destroy_char);
                        list_append(slurmdb_report_cluster->
                                    user_list,
                                    slurmdb_report_user);
                    }
                    break;
                }
            }
            if (!slurmdb_report_cluster) {
                error("This cluster '%s' hasn't "
                      "registered yet, but we have jobs "
                      "that ran?", assoc->cluster);
                slurmdb_report_cluster =
                    xmalloc(sizeof(slurmdb_report_cluster_rec_t));
                list_append(cluster_list, slurmdb_report_cluster);

                slurmdb_report_cluster->name = xstrdup(assoc->cluster);
                slurmdb_report_cluster->user_list =
                    list_create(slurmdb_destroy_report_user_rec);
                slurmdb_report_user =
                    xmalloc(sizeof(slurmdb_report_user_rec_t));
                slurmdb_report_user->name = xstrdup(assoc->user);
                slurmdb_report_user->uid = user->uid;
                slurmdb_report_user->acct_list =
                    list_create(slurm_destroy_char);
                list_append(slurmdb_report_cluster->user_list,
                            slurmdb_report_user);
            }
            list_iterator_reset(cluster_itr);

            itr3 = list_iterator_create(
                       slurmdb_report_user->acct_list);
            while((object = list_next(itr3))) {
                if (!strcmp(object, assoc->acct))
                    break;
            }
            list_iterator_destroy(itr3);

            if (!object)
                list_append(slurmdb_report_user->acct_list,
                            xstrdup(assoc->acct));
            slurmdb_transfer_acct_list_2_tres(
                assoc->accounting_list,
                &slurmdb_report_user->tres_list);
        }
        list_iterator_destroy(itr2);
    }
    list_iterator_destroy(itr);
    list_iterator_destroy(cluster_itr);

end_it:
    if (delete_cluster_list) {
        list_destroy(user_cond->assoc_cond->cluster_list);
        user_cond->assoc_cond->cluster_list = NULL;
    }

    if (delete_assoc_cond) {
        slurmdb_destroy_assoc_cond(user_cond->assoc_cond);
        user_cond->assoc_cond = NULL;
    }

    if (delete_user_cond) {
        slurmdb_destroy_user_cond(user_cond);
        user_cond = NULL;
    }

    if (user_list) {
        list_destroy(user_list);
        user_list = NULL;
    }

    if (exit_code) {
        if (cluster_list) {
            list_destroy(cluster_list);
            cluster_list = NULL;
        }
    }

    return cluster_list;
}
示例#4
0
/*
 * get info from the storage
 * IN:  slurmdb_user_cond_t *
 * IN:  params void *
 * returns List of slurmdb_user_rec_t *
 * note List needs to be freed with slurm_list_destroy() when called
 */
extern List slurmdb_users_get(void *db_conn, slurmdb_user_cond_t *user_cond)
{
    return acct_storage_g_get_users(db_conn, getuid(), user_cond);
}