Ejemplo n.º 1
0
int main (int argc, char **argv) {
    /* Local Vars */
    virConnectPtr   conn;
    const char      *hvType;
    unsigned long libVer, libMajor, libMinor, libRelease;
    unsigned long hvVer, hvMajor, hvMinor, hvRelease;

    /* Set signal handling and alarm */
    if (signal(SIGALRM, timeout_alarm_handler) == SIG_ERR)
        critical("Setup SIGALRM trap failed!");

    /* Process check arguments */
    if (process_arguments(argc, argv) != OK)
        unknown("Parsing arguments failed!");

    /* Start plugin timeout */
    alarm(mp_timeout);

    // PLUGIN CODE
    conn = virt_connect();

    hvType = virConnectGetType(conn);
    if (hvType == NULL) {
        if (mp_verbose > 0) {
            virt_showError(conn);
        }
        critical("Failed to get hypervisor type.");
    }

    if (virConnectGetVersion(conn, &hvVer) != 0) {
        if (mp_verbose > 0) {
            virt_showError(conn);
        }
        critical("Failed to get hypervisor version.");
    }

    if (virConnectGetLibVersion(conn, &libVer) != 0) {
        if (mp_verbose > 0) {
            virt_showError(conn);
        }
        critical("Failed to get library version.");
    }

    virConnectClose(conn);

    hvMajor = hvVer / 1000000;
    hvVer %= 1000000;
    hvMinor = hvVer / 1000;
    hvRelease = hvVer % 1000;

    libMajor = libVer / 1000000;
    libVer %= 1000000;
    libMinor = libVer / 1000;
    libRelease = libVer % 1000;

    /* Output and return */
    ok("libvirtd: v.%lu.%lu.%lu Hypervisor: %s (v.%lu.%lu.%lu)", 
            libMajor, libMinor, libRelease,
            hvType, hvMajor, hvMinor, hvRelease);
}
Ejemplo n.º 2
0
static int
showHypervisorInfo(virConnectPtr conn)
{
    int ret = 0;
    unsigned long hvVer, major, minor, release;
    const char *hvType;

    /* virConnectGetType returns a pointer to a static string, so no
     * allocation or freeing is necessary; it is possible for the call
     * to fail if, for example, there is no connection to a
     * hypervisor, so check what it returns. */
    hvType = virConnectGetType(conn);
    if (!hvType) {
        ret = 1;
        printf("Failed to get hypervisor type\n");
        showError(conn);
        goto out;
    }

    if (0 != virConnectGetVersion(conn, &hvVer)) {
        ret = 1;
        printf("Failed to get hypervisor version\n");
        showError(conn);
        goto out;
    }

    major = hvVer / 1000000;
    hvVer %= 1000000;
    minor = hvVer / 1000;
    release = hvVer % 1000;

    printf("Hypervisor: \"%s\" version: %lu.%lu.%lu\n",
           hvType,
           major,
           minor,
           release);

out:
    return ret;
}
Ejemplo n.º 3
0
int main(int argvc, char *argv[]){
    static virConnectPtr conn = NULL;
    static virDomainPtr dom = NULL;
    char * Sysinfo;
    char* Capabilities;
    char* Hostname;
    int Vcpunum ;
    const  char* ConnectType;
    unsigned long * hvVer;
    int IsAlive;


    conn = virConnectOpen("xen:///");
    if(conn == NULL){
        fprintf(stderr,"Failed to open connection to xen");
        return -1;
    }
    dom = virDomainLookupByID(conn,1);
    if(dom == NULL){
        fprintf(stderr,"Failed to create a dom");
        return -1;
    }
    
  
    Capabilities = virConnectGetCapabilities(conn);//get capabilities
     if(Capabilities == NULL){
        fprintf(stdout,"failed to GetCapabilities");
        return -1;
     } 
    fprintf(stdout,"ConnectCapabilities %s\n",Capabilities);
    free(Capabilities);


    Sysinfo = virConnectGetSysinfo(conn,0);//Get Sysinfo
     if(Sysinfo ==NULL){
        fprintf(stdout,"failed to getinfo");
        return -1;
     }
    fprintf(stdout,"Sysinfo:%s\n",Sysinfo);
    free(Sysinfo);    


    Hostname = virConnectGetHostname(conn);//get hostname
    if(Hostname == NULL){
        fprintf(stderr,"failed to get hostname");
        return -1;
    }
    fprintf(stdout,"Hostname:%s\n",Hostname);
    free(Hostname);

  
    Vcpunum = virConnectGetMaxVcpus(conn,"xen");//Get Vcpunum
    if(Vcpunum <0){
        fprintf(stdout,"failed to GetCpunum");
        return -1;
    }
    fprintf(stdout,"Vcpunum: %d\n",Vcpunum);
   

    ConnectType = virConnectGetType(conn);//Connect Type
    if(ConnectType ==NULL){
        fprintf(stdout,"failed to GetConnectType");
        return -1;
    }    
    fprintf(stdout,"ConnectType is that :%s\n",ConnectType);    

    
    if(virConnectGetVersion(conn,hvVer)<0){
        fprintf(stderr,"Failed to Get Version");
        return -1;
    }
    fprintf(stdout,"ConnectVersion: %lu\n",*hvVer);

    
    IsAlive = virConnectIsAlive(conn);
    if(IsAlive == 1 ){
        fprintf(stdout,"alive\n");
    }
    else
        if(IsAlive == 0){
            fprintf(stdout,"dead\n");
        }
        else
            fprintf(stdout,"Failed to get Connect Status\n");


    free(dom);
    virConnectClose(conn);
    return 0;
}
Ejemplo n.º 4
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);
}
Ejemplo n.º 5
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;
}