There’s no magic there -
is a complex data structure, which you want to transform into another complex structure -
have to read the source file, line by line, separate the information elements of each row using the Python string tools (linha.strip().split("")
should give you a list of each header or data field.)
And, for each desired value, you have to create a new node, at the right place, all of this in order to have an XML in which the information related to the vms will be something like:
<procs><r>0</r><b><0></procs><memory><swpd>0</swpd> <free>9302192</free>...</memory>
Few things would be more boring than creating an xml like this, among which, CONSUME an xml like this.
So - let’s take a step back:
first: if you will sweat Python to manipulate the data relating to the system, why use a complex command line to paste as text file the output of individual commands that has the information you want?
Do everything in Python - including calling external commands free
and vmstat
- For timestam, obviament you don’t 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 from the other side, easier to generate with your Pythn program, more readable when looked at, and significantly smaller! XML loses in all these questions to almost any thing - it is even more verbose and less readable than collage as original text file. To send 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 the use of the machine in someone else, it will make much more sense to put everything in a database, instead of generating XML or JSON packages. (ok, if XML or JSON can be to be transmitted to a remote process that does this).
Well, anyway, you create in the Python program a distinct function to execute the external command, parse the data, and already return the structured infection - as a Python dictionary is cool - Mesmoq I you choose to use XML - the dictionary will be a good intermediate data structure.
Example of a function that calls "free" and parses the data for 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 the "vmstat" data, combine the dictionaries with a timestamp, and use direct "json.dump" to have all the data machine-readable and ready to be transmitted --- or, create another function that creates your XML based on already structured data.