char * libvirt_hostname(void) { if (g_conn == NULL) return NULL; char * n = virConnectGetHostname(g_conn); if (n) return n; else { logerror(_("error in %s(%d)\n"), __func__, __LINE__); return NULL; } }
int main(int argc, char *argv[]) { virConnectPtr conn; char *host; conn = virConnectOpen("xen:///"); if (conn == NULL) { fprintf(stderr, "Failed to open connection to xen:///\n"); return 1; } host = virConnectGetHostname(conn); fprintf(stdout, "Hostname:%s\n", host); free(host); virConnectClose(conn); return 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; }
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); }
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; }