KVM/Libvirt: How do i get total traffic used by KVM/libvirt Virtual machine - libvirt

i'm want to get the total bandwidth used by a kvm/libvirt VM in C (language). is there function in libvirt?
so, for example if a VM crosses 1TB then i would suspend its network.

In the libvirt XML you need to look at the element to identify the backend device name. eg
<interface type='network'>
<mac address='52:54:00:b4:fc:f2'/>
<source network='default' bridge='virbr0'/>
<target dev='vnet2'/>
<model type='virtio'/>
<alias name='net0'/>
<address type='pci' domain='0x0000' bus='0x00' slot='0x03' function='0x0'/>
</interface>
here the backend is 'vnet2'. Once you have that you can invoke the C API virDomainInterfaceStats (or via a language binding of your choice) to get the rx/tx stats. As an example using virsh tool:
# virsh domifstat demo vnet2
vnet2 rx_bytes 5040490379
vnet2 rx_packets 3292604
vnet2 rx_errs 0
vnet2 rx_drop 0
vnet2 tx_bytes 167286952
vnet2 tx_packets 1859239
vnet2 tx_errs 0
vnet2 tx_drop 0

When you start a KVM guest the host system creates a vnet interface for each individual network interface within the guest, for example vnet3 vnet4.
After that you can monitor the send / received amount of those interfaces by polling the files on the host machine :
cat /sys/class/net/vnet3/statistics/rx_bytes
173110677
cat /sys/class/net/vnet3/statistics/tx_bytes
1640468389
Alternatively you can set up iptables rules with fwmark like in this tutorial :
http://www.tldp.org/HOWTO/Adv-Routing-HOWTO/lartc.netfilter.html
and measure the stats with iptables, but I guess that would be too much of a hassle in C.

Related

How to route users to different backend Hostnames via products in Azure API Management

i am having Set of API's having same backend Host for production and acceptance.
https://api.azurewebsites.net/vendor,
https://api.azurewebsites.net/session,
https://api.azurewebsites.net/api
https://api.acc.azurewebsites.net/vendor,
https://api.acc.azurewebsites.net/session,
https://api.acc.azurewebsites.net/api,
So, i want to create 2 products xyz-production & xyz-acceptance and route clients to respective ENV backend Host.
I tried to achieve same with "set-backend-service" policy. <set-backend-service base-url="https://<Host Name>/<Path>" /> But this policy allowed to change only serviceURL.
But my requirement is to change only Hostname. Could you please suggest, how to achieve it?
Answer for this question is below. By using "context.Api.Path" variable we can append API's base path dynamically with any Host you want.
<set-backend-service base-url="#("https://api.acc.azurewebsites.net" + context.Api.Path)" />

SGE unknown resource "nodes"

I submit a job on SGE with parameter -l like:
qsub -pe orte 4 -l nodes=4 run.sh
However, the system displays that:
Unable to run job: unknown resource "nodes".
Could you tell me why and how to solve it?
Thank you very much!
With Sun Grid Engine, the correct resource parameter is h, not nodes:
echo 'echo `hostname`' | qsub -l h=<some_hostname>
Using this example, you should see the hostname you specified in the standard output file.
There isn't a nodes resource. Instead you request a parallel environment and a number of slots (map to cores usually). The number of nodes you get is determined by the alloaction_rule of the parallel environment. There is usually a simple pe called something like mpi that will pack as many slots(cores) onto each node as will fit. Some people have created configs for grid engine that let it have a more PBS like syntax.

Using a Webpage to Control Robot Arm Written in Linux

Firstly I am no longer a student and currently working on a favour for a friend. I am making a website which has a live video feed of a robotic arm and a set off buttons that will allow users with the basic interaction of the robotic arm.
I have setup the website and live video feed. I do have a 4 second delay using flash media encoder and flash server 4.5. Have any suggestions in reducing the delay time?
I have done the python code required for the maplin robotic arm and now I am stuck and not sure how to link my python code with a webpage interface? Can anyone that has done this before provide with code that I could edit and learn from..
Python Code
import usb.core
import usb.util
import sys
import time
# This program is intended to control a robotic arm via USB from Linux
# The code is written in Python by Neil Polwart (c) 2011
# It is a work in progress and will improved!
# locate the device device
dev = usb.core.find(idVendor=0x1267, idProduct=0x0000)
# assigns the device to the handle "dev"
# can check the device is visible to Linux with command line command lsusb
# which should report a device with the above vendor and id codes.
# was it found?
if dev is None:
raise ValueError('Device not found') # if device not found report an error
# set the active configuration
dev.set_configuration()
# as no arguments, the first configuration will be the active one
# note as commands are sent to device as commands not data streams
# no need to define the endpoint
# defines the command packet to send
datapack=0x80,0,0
# change this packet to make different moves.
# first byte defines most of the movements, second byte shoulder rotation, third byte light
# command structure in more detail:
# http://notbrainsurgery.livejournal.com/38622.html?view=93150#t93150
print "requested move",datapack # reports the requested movement to the user
# send the command
bytesout=dev.ctrl_transfer(0x40, 6, 0x100, 0, datapack, 1000)
# outputs the command to the USB device, using the ctrl_transfer method
# 0x40, 6, 0x100, 0 defines the details of the write - bRequestType, bRequest, wValue, wIndex
# datapack is our command (3 bytes)
# the final value is a timeout (in ms) which is optional
# bytesout = the number of bytes written (i.e. 3 if successful)
print "Written :",bytesout,"bytes" # confirm to user that data was sent OK
# wait for a defined period
time.sleep(1) # waits for 1 second whilst motors move.
# now STOP the motors
datapack=0,0,0
bytesout=dev.ctrl_transfer(0x40, 6, 0x100, 0, datapack, 1000)
if bytesout == 3: print "Motors stopped"
So I need to find a way to edit the datapack line via a website interface. Any help is appreciated! I am using a Windows 7 setup but do have access to vmware
I'd set up an Apache server with mod_python and create a handler that imports your script and runs the necessary code. You can set up an AJAX script in JavaScript (with or without jQuery). Every time you want to run the Python script, a request needs to be made to the server. You can pass any information back and forth as needed via the HTTP.
Here's a good tutorial for Python and the CGI Module.

Result identity changing

I'm using TOR and I want to know, how to switch between result-nodes with need country. I can simply change it by telnet the 9051 port like:
telnet localhost 9051
"AUTHENTICATE\r"
"signal NEWNYM\r"
"quit\r"
This will chose randomly the exit(result) node. My goal is to change that node to the node from need country. I didn't find such information in the documentation, but in some GUI apps for the TOR there is a map with a list of all available nodes/servers and their country, so that I can choose need one.
You can do:
> telnet localhost 9051
AUTHENTICATE
SETCONF ExitNodes={us}
SETCONF StrictNodes=1
SIGNAL NEWNYM
QUIT
Tor will only use exit nodes from the US in this case. The changes are not written to torrc. If you want to write them, use SAVECONF.

Jabber Openfire server v3.6.0a+ - how do I use Hybrid authentication?

I'm setting up a Jabber server for my website. I've already got some user accounts in place in the openfire database, and working IMs between them.
I'm now looking to add (some) of the users from my main database (members table, with login, password[plain text]) and allowed_to_IM[0 or 1] fields) to allow them to communicate between themselves. The Hybrid authentication is a new feature in v3.6.0a however, and there's little documentation in what configuration is required in the openfire.xml file for the database connectivity (to a second database), and what else may go in the properties (which have also taken much of the config's info away of the XML file).
My question is: Does anyone have a complete example that checks multiple databases? All the examples I'm seen seem to be just fragments.
I have it using ldap and mysql and if it helps you my setting from openfire.xml are:
<connectionProvider>
<className>org.jivesoftware.database.DefaultConnectionProvider</className>
</connectionProvider>
<database>
<defaultProvider>
<driver>com.mysql.jdbc.Driver</driver>
<serverURL>jdbc:mysql://127.0.0.1:3306/openfire</serverURL>
<username>username</username>
<password>pass</password>
<minConnections>5</minConnections>
<maxConnections>15</maxConnections>
<connectionTimeout>1.0</connectionTimeout>
</defaultProvider>
</database>
<ldap>
ldapsetting removed
</ldap>
<hybridAuthProvider>
<primaryProvider>
<className>org.jivesoftware.openfire.auth.DefaultAuthProvider</className>
</primaryProvider>
<secondaryProvider>
<className>org.jivesoftware.openfire.ldap.LdapAuthProvider</className>
</secondaryProvider>
</hybridAuthProvider>
<provider>
<auth>
<className>org.jivesoftware.openfire.auth.HybridAuthProvider</className>
</auth>
<vcard>
<className>org.jivesoftware.openfire.auth.DefaultAuthProvider</className>
</vcard>
<user>
<className>org.jivesoftware.openfire.ldap.LdapUserProvider</className>
</user>
<auth>
<className>org.jivesoftware.openfire.ldap.LdapAuthProvider</className>
</auth>
<group>
<className>org.jivesoftware.openfire.ldap.LdapGroupProvider</className>
</group>
</provider>