There is no magic there -
is a complex data structure, which you want to transform into another complex structure -
You have to read the source file, line by line, separate the information elements of each line using the Python string tools ( linha.strip().split("")
should give you a list with each field of headers or data.)
And for each value you want, you have to create a new node, in the right place, all this in order to have an XML in which the information related to vms is something like:
<procs><r>0</r><b><0></procs><memory><swpd>0</swpd> <free>9302192</free>...</memory>
Some things would be more annoying than creating an xml like this, among which, CONSUME an xml like that.
So - let's go back a few steps back:
first: if you're going to sweat Python to handle system-related data, why use a complex command line to paste as text file the output of individual commands that have the information you want?
Do everything in Python - including the call to external commands free
and vmstat
- For timestam, you obviously do not need to call an external process, just use Python datetime
Second: Think about the program that will consume this information, and generate a structured data file that can be: easier to read on the other side, easier to generate with your Python program, more readable when looked, and significantly lower! XML loses in all these queries for almost any thing - even it is more verbose and less legible than the collage as original text file. For a structured and easily readable format, you can think of a JSON file, for example instead of XML:
{"timestamp: ...
"free": {"mem": {'total': ..., 'used': ..., ...},
"swap": {"total", ..., "used": ...}},
"vmstat": ...
}
Or, if you want to generate several of these to monitor how the machine will be used at some point in time, it will make much more sense to put everything into a database instead of generating XML or JSON packages. (ok if the XML or JSON can be to be passed to a remote process that does this).
Well, anyway, you create a distinct function in the Python program to run the external command, parse the data, and return to the structured information - as a Python dictionary is legal - even if you choose to use the XML - the dictionary will be a good intermediate data structure.
Example of a function that calls "free" and parse the data into a dictionary structure:
import subprocess
def get_free():
data = subprocess.check_output("free").decode("utf-8").split("\n")
memory = data[1].split()
swap = data[2].split()
result = {'memory': {'total': int(memory[1]), used: int(memory[2])},
'swap': {'total': int(swap[1]), used: int(swap[2])}}
return result
You can create a similar function to read vmstat data, combine dictionaries with a timestamp, and use direct "json.dump" to have all data readable by machine and ready to be transmitted --- or , create another function that creates your XML based on the already structured data.