virsh.c 2.6 KB
Newer Older
1
/*
2
 * virsh.c: a Xen shell used to exercise the libvir API
3 4 5 6 7 8 9 10
 *
 * Copyright (C) 2005 Red Hat, Inc.
 *
 * See COPYING.LIB for the License of this software
 *
 * Daniel Veillard <veillard@redhat.com>
 */

11
#include "libvir.h"
12
#include <stdio.h>
13 14
#include <unistd.h>
#include <sys/types.h>
15

16
#define MAX_DOM 100
17
int errcode = 0;
18 19
virConnectPtr conn;
virDomainPtr dom0;
20 21
int ids[MAX_DOM];

22
static void printDomain(virDomainPtr dom) {
23 24
    virDomainInfo info;

25
    printf("id %d: name %s, ", virDomainGetID(dom), virDomainGetName(dom));
26 27 28 29
    virDomainGetInfo(dom, &info);
    if (virDomainGetInfo(dom, &info) < 0) {
        printf("failed to get informations\n");
    } else {
30 31
        float mem, maxMem;

32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50
        switch (info.state) {
	    case VIR_DOMAIN_RUNNING:
	        printf("running ");
		break;
            case VIR_DOMAIN_BLOCKED:
	        printf("blocked ");
		break;
            case VIR_DOMAIN_PAUSED:
	        printf("paused ");
		break;
            case VIR_DOMAIN_SHUTDOWN:
	        printf("in shutdown ");
		break;
            case VIR_DOMAIN_SHUTOFF:
	        printf("shut off ");
		break;
	    default:
	        break;
	}
51 52 53 54 55 56 57 58 59 60 61
	if (info.cpuTime != 0) {
	    float cpuUsed = info.cpuTime;

	    cpuUsed /= 1000000000;
	    printf("%.1f s CPU time, ", cpuUsed);
	}
	mem = info.memory;
	mem /= 1024 * 1024;
	maxMem = info.maxMem;
	maxMem /= 1024 * 1024;
        printf("%.0f MB mem used, %.0f MB max_mem\n", mem, maxMem);
62 63
    }

64
}
65 66

int main(int argc, char **argv) {
67
    int ret, i;
68
    virDomainPtr dom;
69
    
70
    if (getuid() == 0) {
71
	conn = virConnectOpen(NULL);
72
    } else {
73
	conn = virConnectOpenReadOnly(NULL);
74
    }
75 76 77 78 79
    if (conn == NULL) {
        fprintf(stderr, "Failed to connect to the hypervisor\n");
        errcode = 1;
	goto done;
    }
80
    dom0 = virDomainLookupByID(conn, 0);
81 82 83 84 85
    if (dom0 == NULL) {
        fprintf(stderr, "Failed to get domain 0 informations\n");
	errcode = 2;
	goto done;
    }
86

87 88 89
    printf("Dom0: ");
    printDomain(dom0);

90
    ret = virConnectListDomains(conn, &ids[0], MAX_DOM);
91 92 93 94 95 96 97 98 99 100
    if (ret < 0) {
        fprintf(stderr, "Failed to list active domains\n");
	errcode = 3;
	goto done;
    }
    printf("Found %d more active domains\n", ret - 1);
    for (i = 0;i < ret;i++) {
        if (ids[i] == 0)
	    continue;
        printf("  ");
101
	dom = virDomainLookupByID(conn, ids[i]);
102 103 104 105 106 107 108
	if (dom == NULL) {
	    printf("domain %d disapeared\n", ids[i]);
	} else {
	    printDomain(dom);
	}
    }
    
109 110
done:
    if (conn != NULL) {
111
        ret = virConnectClose(conn);
112 113 114 115 116 117
	if (ret != 0) {
	    fprintf(stderr, "Failed to connect to the hypervisor\n");
	    if (errcode == 0)
		errcode = 1;
	}
    }
118
    return(errcode);
119
}