Beispiel #1
0
int
main(int argc, char *argv[])
{
    int ret = 0;
    virConnectPtr conn;
    char *uri;

    printf("Attempting to connect to hypervisor\n");

    uri = (argc > 0 ? argv[1] : NULL);

    /* virConnectOpenAuth is called here with all default parameters,
     * except, possibly, the URI of the hypervisor. */
    conn = virConnectOpenAuth(uri, virConnectAuthPtrDefault, 0);

    if (!conn) {
        ret = 1;
        printf("No connection to hypervisor\n");
        showError(conn);
        goto out;
    }

    uri = virConnectGetURI(conn);
    if (!uri) {
        ret = 1;
        printf("Failed to get URI for hypervisor connection\n");
        showError(conn);
        goto disconnect;
    }

    printf("Connected to hypervisor at \"%s\"\n", uri);
    free(uri);

    if (0 != showHypervisorInfo(conn)) {
        ret = 1;
        goto disconnect;
    }

    if (0 != showDomains(conn)) {
        ret = 1;
        goto disconnect;
    }

disconnect:
    if (0 != virConnectClose(conn)) {
        printf("Failed to disconnect from hypervisor\n");
        showError(conn);
        ret = 1;
    } else {
        printf("Disconnected from hypervisor\n");
    }

out:
    return ret;
}
Beispiel #2
0
virConnectPtr *
check_hypervisor_conn()
{
	if (nc_state.conn == NULL || virConnectGetURI(nc_state.conn) == NULL) {
		nc_state.conn = virConnectOpen (nc_state.uri);
		if (nc_state.conn == NULL) {
			logprintfl (EUCAFATAL, "Failed to connect to %s\n", nc_state.uri);
			return NULL;
		}
	}

	return &(nc_state.conn);
}
Beispiel #3
0
virConnectPtr libvirtConnect(char *uri)
{
	virConnectPtr cp;

	virSetErrorFunc("vmrunner", errHandler);
	domainIsOff = 0;
	lastErrorCode = 0;
	DPRINTF("Opening connection to hypervisor, uri %s\n", uri ? uri : "probed");
	cp = virConnectOpen(uri);
	if (cp == NULL) {
		DPRINTF("virConnectOpen call failed\n");
		return NULL;
	}

	DPRINTF("Connected to %s\n", virConnectGetURI(cp));
	return cp;
}
Beispiel #4
0
/*
 * virshCatchDisconnect:
 *
 * We get here when the connection was closed.  We can't do much in the
 * handler, just save the fact it was raised.
 */
static void
virshCatchDisconnect(virConnectPtr conn,
                     int reason,
                     void *opaque)
{
    if (reason != VIR_CONNECT_CLOSE_REASON_CLIENT) {
        vshControl *ctl = opaque;
        const char *str = "unknown reason";
        virErrorPtr error;
        char *uri;

        error = virSaveLastError();
        uri = virConnectGetURI(conn);

        switch ((virConnectCloseReason) reason) {
        case VIR_CONNECT_CLOSE_REASON_ERROR:
            str = N_("Disconnected from %s due to I/O error");
            break;
        case VIR_CONNECT_CLOSE_REASON_EOF:
            str = N_("Disconnected from %s due to end of file");
            break;
        case VIR_CONNECT_CLOSE_REASON_KEEPALIVE:
            str = N_("Disconnected from %s due to keepalive timeout");
            break;
        /* coverity[dead_error_condition] */
        case VIR_CONNECT_CLOSE_REASON_CLIENT:
        case VIR_CONNECT_CLOSE_REASON_LAST:
            break;
        }
        vshError(ctl, _(str), NULLSTR(uri));
        VIR_FREE(uri);

        if (error) {
            virSetError(error);
            virFreeError(error);
        }
        disconnected++;
        vshEventDone(ctl);
    }
}
Beispiel #5
0
NodeWrap::NodeWrap(ManagementAgent* _agent, string _name) : name(_name), agent(_agent)
{
    virNodeInfo info;
    char *hostname;
    char libvirt_version[256] = "Unknown";
    char api_version[256] = "Unknown";
    char hv_version[256] = "Unknown";
    char *uri;
    const char *hv_type;
    unsigned long api_v;
    unsigned long libvirt_v;
    unsigned long hv_v;
    int ret;
    unsigned int major;
    unsigned int minor;
    unsigned int rel;

    conn = virConnectOpen(NULL);
    if (!conn) {
        REPORT_ERR(conn, "virConnectOpen");
        throw -1;
    }

    hostname = virConnectGetHostname(conn);
    if (hostname == NULL) {
        REPORT_ERR(conn, "virConnectGetHostname");
        throw -1;
    }

    hv_type = virConnectGetType(conn);
    if (hv_type == NULL) {
        REPORT_ERR(conn, "virConnectGetType");
        throw -1;
    }

    uri = virConnectGetURI(conn);
    if (uri == NULL) {
        REPORT_ERR(conn, "virConnectGetURI");
        throw -1;
    }

    ret = virGetVersion(&libvirt_v, hv_type, &api_v);
    if (ret < 0) {
        REPORT_ERR(conn, "virGetVersion");
    } else {
        major = libvirt_v / 1000000;
        libvirt_v %= 1000000;
        minor = libvirt_v / 1000;
        rel = libvirt_v % 1000;
        snprintf(libvirt_version, sizeof(libvirt_version), "%d.%d.%d", major, minor, rel);

        major = api_v / 1000000;
        api_v %= 1000000;
        minor = api_v / 1000;
        rel = api_v % 1000;
        snprintf(api_version, sizeof(api_version), "%d.%d.%d", major, minor, rel);
    }

    ret = virConnectGetVersion(conn, &hv_v);
    if (ret < 0) {
        REPORT_ERR(conn, "virConnectGetVersion");
    } else {
        major = hv_v / 1000000;
        hv_v %= 1000000;
        minor = hv_v / 1000;
        rel = hv_v % 1000;
        snprintf(hv_version, sizeof(hv_version), "%d.%d.%d", major, minor, rel);
    }

    ret = virNodeGetInfo(conn, &info);
    if (ret < 0) {
        REPORT_ERR(conn, "virNodeGetInfo");
        memset((void *) &info, sizeof(info), 1);
    }

    mgmtObject = new _qmf::Node(agent, this, hostname, uri, libvirt_version, api_version, hv_version, hv_type,
                                info.model, info.memory, info.cpus, info.mhz, info.nodes, info.sockets,
                                info.cores, info.threads);
    agent->addObject(mgmtObject);
}
Beispiel #6
0
int
main(int argc, char *argv[])
{
    int ret = 0;
    virConnectPtr conn;
    char *uri;
    AuthData authData;

    if (argc != 4) {
        ret = 1;
        printf("Usage: %s <uri> <username> <password>\n", argv[0]);
        goto out;
    }

    uri = argv[1];
    authData.username = argv[2];
    authData.password = argv[3];
    auth.cbdata = &authData;

    printf("Attempting to connect to hypervisor\n");

    conn = virConnectOpenAuth(uri, &auth, 0);

    if (NULL == conn) {
        ret = 1;
        printf("No connection to hypervisor\n");
        showError(conn);
        goto out;
    }

    uri = virConnectGetURI(conn);
    if (uri == NULL) {
        ret = 1;
        printf("Failed to get URI for hypervisor connection\n");
        showError(conn);
        goto disconnect;
    }

    printf("Connected to hypervisor at \"%s\"\n", uri);
    free(uri);

    if (showHypervisorInfo(conn) != 0) {
        ret = 1;
        goto disconnect;
    }

    if (showDomains(conn) != 0) {
        ret = 1;
        goto disconnect;
    }

  disconnect:
    if (virConnectClose(conn) != 0) {
        printf("Failed to disconnect from hypervisor\n");
        showError(conn);
        ret = 1;
    } else {
        printf("Disconnected from hypervisor\n");
    }

  out:
    return ret;
}
Beispiel #7
0
/**
 * @brief provides the uri used to access the domain
 * @return string containing the uri
 */
QString Domain::uri()
{
    virConnectPtr conn = virDomainGetConnect(m_domain);
    return QString(virConnectGetURI(conn));
}
Beispiel #8
0
int main()
{
    int idCount;
    int i;
    int id;
    //int ids[MAXID];
	int *ids;
    //timeInfoNode timeInfos[MAXID];

    printf("--------------------------------------------------------\n");
    printf("             XEN Domain Monitor \n");
    printf("--------------------------------------------------------\n");

    /* NULL means connect to local Xen hypervisor */
    conn = virConnectOpenReadOnly(NULL);
    if (conn == NULL)
    {
        fprintf(stderr, "Failed to connect to hypervisor\n");
        closeConn();
        return 0;
    }

	/*char* caps;
	caps = virConnectGetCapabilities(conn);
	printf("Capabilities:\n%s\n",caps);
	free(caps);*/
	char *host;
	host = virConnectGetHostname(conn);
	fprintf(stdout, "Hostname:%s\n",host);
	free(host);
	int vcpus;
	vcpus = virConnectGetMaxVcpus(conn,NULL);
	fprintf(stdout, "Maxmum support vcpus:%d\n",vcpus);
	unsigned long long node_free_memory;
	node_free_memory = virNodeGetFreeMemory(conn);
	fprintf(stdout, "free memory:%lld\n",node_free_memory);
	virNodeInfo nodeinfo;
	virNodeGetInfo(conn,&nodeinfo);
	fprintf(stdout, "Model: %s\n", nodeinfo.model);
    fprintf(stdout, "Memory size: %lukb\n", nodeinfo.memory);
    fprintf(stdout, "Number of CPUs: %u\n", nodeinfo.cpus);
    fprintf(stdout, "MHz of CPUs: %u\n", nodeinfo.mhz);
    fprintf(stdout, "Number of NUMA nodes: %u\n", nodeinfo.nodes);
    fprintf(stdout, "Number of CPU sockets: %u\n", nodeinfo.sockets);
    fprintf(stdout, "Number of CPU cores per socket: %u\n", nodeinfo.cores);
    fprintf(stdout, "Number of CPU threads per core: %u\n", nodeinfo.threads);	
    fprintf(stdout, "Virtualization type: %s\n", virConnectGetType(conn));
	unsigned long ver;
	virConnectGetVersion(conn, &ver);
	fprintf(stdout, "Version: %lu\n", ver);
	/*unsigned long Libver;
	virConnectGetLibVersion(conn, &Libver);
	fprintf(stdout, "Libvirt Version: %lu\n", Libver);*/
	char *uri;
	uri = virConnectGetURI(conn);
	fprintf(stdout, "Canonical URI: %s\n", uri);
	free(uri);
	/* get the count of IDs and save these ID into ids[] */
    idCount = virConnectNumOfDomains(conn);
	ids = malloc(sizeof(int) *idCount);
	idCount = virConnectListDomains(conn,ids,idCount);
	//idCount = virConnectListDomains(conn, &ids[0], MAXID);
    if (idCount < 0)
    {
        fprintf(stderr, "Failed to list the domains\n");
        closeConn();
        return 0;
    }

    timeInfoNode timeInfos[idCount];
	printf("Domain Totals: %d\n", idCount);
    printf("ID\tCPU\tMEM\tMaxMEM\tVCPUs\tState\tNAME\n");

    /* loop get the CPUtime info by IDs */
    for (i = 0; i < idCount; i++)
    {
        id = ids[i];
        getTimeInfo(id, &(timeInfos[i]));
    }

    sleep(1);

    /* loop print the domain info and calculate the usage of cpus*/
    for (i = 0; i < idCount; i++)
    {
        id = ids[i];
        getDomainInfo(id, timeInfos[i]);
    }

    free(ids);
	printf("--------------------------------------------------------\n");
    closeConn();
    return 0;
}
int
main(int argc, char *argv[])
{
    int ret = 0;
    virConnectPtr conn;
    char *uri;
    
    char inpFileName[256] = { 0 };
    char lz4FileName[256] = { 0 };
    char decFileName[256] = { 0 };
    char pepe1[] = "someFile.txt";
    
    printf("Attempting to connect to hypervisor\n");

    uri = (argc > 0 ? argv[1] : NULL);

    /* virConnectOpenAuth is called here with all default parameters,
     * except, possibly, the URI of the hypervisor. */
    conn = virConnectOpenAuth(uri, virConnectAuthPtrDefault, 0);

    if (!conn) {
        ret = 1;
        printf("No connection to hypervisor: %s\n",
               virGetLastErrorMessage());
        goto out;
    }

    uri = virConnectGetURI(conn);
    if (!uri) {
        ret = 1;
        printf("Failed to get URI for hypervisor connection: %s\n",
               virGetLastErrorMessage());
        goto disconnect;
    }

    printf("Connected to hypervisor at \"%s\"\n", uri);
    


	
	
//	snprintf(inpFilename, 256, "%s", argv[1]);
 //   snprintf(lz4Filename, 256, "%s.lz4s-%d", argv[1], BLOCK_BYTES);
  //  snprintf(decFilename, 256, "%s.lz4s-%d.dec", argv[1], BLOCK_BYTES);

	snprintf(inpFileName, 256, "%s", pepe1);
    snprintf(lz4FileName, 256, "%s.lz4s-%d", pepe1, BLOCK_BYTES);
    snprintf(decFileName, 256, "%s.lz4s-%d.dec", pepe1, BLOCK_BYTES);



	printf("inp = [%s]\n", inpFileName);
    printf("lz4 = [%s]\n", lz4FileName);
    printf("dec = [%s]\n", decFileName);
    
    compress(inpFileName, lz4FileName);
    decompress(lz4FileName, decFileName);
    
	free(uri);
	
	
 disconnect:
    if (0 != virConnectClose(conn)) {
        printf("Failed to disconnect from hypervisor: %s\n",
               virGetLastErrorMessage());
        ret = 1;
    } else {
        printf("Disconnected from hypervisor\n");
    }

 out:
    return ret;
}