1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74
|
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="content-type" content="text/html; charset=UTF-8">
<title>Examples</title>
</head>
<body><div class="manualnavbar" style="text-align: center;">
<div class="prev" style="text-align: left; float: left;"><a href="libvirt.longlong.html">Interger ranges between libvirt and PHP</a></div>
<div class="next" style="text-align: right; float: right;"><a href="ref.libvirt.html">Libvirt Functions</a></div>
<div class="up"><a href="book.libvirt.html">Libvirt</a></div>
<div class="home"><a href="index.html">PHP Manual</a></div>
</div><hr /><div>
<h1>Examples</h1>
<p class="para">
A few basic examples
</p>
<div class="example">
<p><b>Example #1 Libvirt Example</b></p>
<div class="example-contents programlisting">
<div class="phpcode"><code><span style="color: #000000">
<span style="color: #0000BB"><?php<br />$uri</span><span style="color: #007700">=</span><span style="color: #DD0000">"qemu+tcp:///system"</span><span style="color: #007700">;<br /></span><span style="color: #0000BB">$credentials</span><span style="color: #007700">=Array(</span><span style="color: #0000BB">VIR_CRED_AUTHNAME</span><span style="color: #007700">=></span><span style="color: #DD0000">"fred"</span><span style="color: #007700">,</span><span style="color: #0000BB">VIR_CRED_PASSPHRASE</span><span style="color: #007700">=></span><span style="color: #DD0000">"fred"</span><span style="color: #007700">);<br />echo (</span><span style="color: #DD0000">"Connecting to libvirt (URI:$uri)\n"</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">$conn</span><span style="color: #007700">=</span><span style="color: #0000BB">libvirt_connect</span><span style="color: #007700">(</span><span style="color: #0000BB">$uri</span><span style="color: #007700">,</span><span style="color: #0000BB">false</span><span style="color: #007700">,</span><span style="color: #0000BB">$credentials</span><span style="color: #007700">);<br />if (</span><span style="color: #0000BB">$conn</span><span style="color: #007700">==</span><span style="color: #0000BB">false</span><span style="color: #007700">)<br />{<br /> echo (</span><span style="color: #DD0000">"Libvirt last error: "</span><span style="color: #007700">.</span><span style="color: #0000BB">libvirt_get_last_error</span><span style="color: #007700">().</span><span style="color: #DD0000">"\n"</span><span style="color: #007700">);<br /> exit;<br />}<br />else<br />{<br /> </span><span style="color: #0000BB">$hostname</span><span style="color: #007700">=</span><span style="color: #0000BB">libvirt_get_hostname</span><span style="color: #007700">(</span><span style="color: #0000BB">$conn</span><span style="color: #007700">);<br /> echo (</span><span style="color: #DD0000">"hostname:$hostname\n"</span><span style="color: #007700">);<br /> echo (</span><span style="color: #DD0000">"Domain count: Active "</span><span style="color: #007700">.</span><span style="color: #0000BB">libvirt_get_active_domain_count</span><span style="color: #007700">(</span><span style="color: #0000BB">$conn</span><span style="color: #007700">).</span><span style="color: #DD0000">",Inactive "</span><span style="color: #007700">.</span><span style="color: #0000BB">libvirt_get_inactive_domain_count</span><span style="color: #007700">(</span><span style="color: #0000BB">$conn</span><span style="color: #007700">).</span><span style="color: #DD0000">", Total "</span><span style="color: #007700">.</span><span style="color: #0000BB">libvirt_get_domain_count</span><span style="color: #007700">(</span><span style="color: #0000BB">$conn</span><span style="color: #007700">).</span><span style="color: #DD0000">"\n"</span><span style="color: #007700">);<br /> <br /> </span><span style="color: #0000BB">$domains</span><span style="color: #007700">=</span><span style="color: #0000BB">libvirt_list_domains</span><span style="color: #007700">(</span><span style="color: #0000BB">$conn</span><span style="color: #007700">);<br /> foreach (</span><span style="color: #0000BB">$domains </span><span style="color: #007700">as </span><span style="color: #0000BB">$dom</span><span style="color: #007700">)<br /> {<br /> echo (</span><span style="color: #DD0000">"Name:\t"</span><span style="color: #007700">.</span><span style="color: #0000BB">libvirt_domain_get_name</span><span style="color: #007700">(</span><span style="color: #0000BB">$dom</span><span style="color: #007700">).</span><span style="color: #DD0000">"\n"</span><span style="color: #007700">);<br /> echo(</span><span style="color: #DD0000">"UUID:\t"</span><span style="color: #007700">.</span><span style="color: #0000BB">libvirt_domain_get_uuid_string</span><span style="color: #007700">(</span><span style="color: #0000BB">$dom</span><span style="color: #007700">).</span><span style="color: #DD0000">"\n"</span><span style="color: #007700">);<br /> </span><span style="color: #0000BB">$dominfo</span><span style="color: #007700">=</span><span style="color: #0000BB">libvirt_domain_get_info</span><span style="color: #007700">(</span><span style="color: #0000BB">$dom</span><span style="color: #007700">);<br /> </span><span style="color: #0000BB">print_r</span><span style="color: #007700">(</span><span style="color: #0000BB">$dominfo</span><span style="color: #007700">);<br /> }<br />}<br /></span><span style="color: #0000BB">?></span>
</span>
</code></div>
</div>
<div class="example-contents para"><p>The above example will output
something similar to:</p></div>
<div class="example-contents screen">
<div class="cdata"><pre>
Connecting to libvirt (URI:qemu+tcp:///system)
hostname:kvmtest
Domain count: Active 2,Inactive 5, Total 7
Name: zlobsd1
UUID: 16890be9-bcb0-ef35-3d43-c2553ea972ea
Array
(
[maxMem] => 1048576
[memory] => 524288
[state] => 1
[nrVirtCpu] => 2
[cpuUsed] => 98718.23
)
Name: node4
UUID: 25ab2490-7c4c-099f-b647-45ff8efa73f6
Array
(
[maxMem] => 524288
[memory] => 524288
[state] => 1
[nrVirtCpu] => 1
[cpuUsed] => 2323601.51
)
Name: test1
UUID: 355fcd8f-ca53-e5e7-5935-47382ba754a0
Array
(
[maxMem] => 1053696
[memory] => 1053696
[state] => 5
[nrVirtCpu] => 1
[cpuUsed] => 0
)
....
</pre></div>
</div>
</div>
</div>
<hr /><div class="manualnavbar" style="text-align: center;">
<div class="prev" style="text-align: left; float: left;"><a href="libvirt.longlong.html">Interger ranges between libvirt and PHP</a></div>
<div class="next" style="text-align: right; float: right;"><a href="ref.libvirt.html">Libvirt Functions</a></div>
<div class="up"><a href="book.libvirt.html">Libvirt</a></div>
<div class="home"><a href="index.html">PHP Manual</a></div>
</div></body></html>
|