简体   繁体   中英

How to save the output of top command in XML format?

top -o %MEM -b > file.txt

The file.txt is like:

top - 15:49:36 up 32 min,  2 users,  load average: 1.56, 1.40, 1.01
Tasks:  83 total,   2 running,  79 sleeping,   2 stopped,   0 zombie
%Cpu(s): 24.4 us,  8.1 sy,  0.0 ni, 55.0 id,  0.0 wa,  0.0 hi, 12.5 si,  0.0 st
KiB Mem:    195252 total,   191568 used,     3684 free,     3200 buffers
KiB Swap:   901116 total,       44 used,   901072 free.    66940 cached Mem

  PID USER      PR  NI    VIRT    RES    SHR S %CPU %MEM     TIME+ COMMAND
  485 root      20   0   68160  61420   4552 R 58.7 31.5   5:06.10 python3
  712 www-data  20   0  230940   4436   2044 S 26.1  2.3   3:54.04 apache2
  819 root      20   0    8488   4400   3856 S  6.5  2.3   1:06.85 tcpdump
  713 www-data  20   0  230780   4344   2044 S 13.0  2.2   2:34.97 apache2
  868 root      20   0    6352   3976   2720 S  0.0  2.0   0:00.07 bash
  785 root      20   0    6340   3876   2636 S  0.0  2.0   0:00.06 bash
  707 root      20   0    6056   3704   2580 S  0.0  1.9   0:00.59 apache2
  137 root      20   0    8284   3256   2996 S  0.0  1.7   0:00.34 systemd-journal
    1 root      20   0    5476   3112   2072 S  0.0  1.6   0:01.04 systemd
  456 ntp       20   0    5820   3012   2576 S  0.0  1.5   0:00.71 ntpd
  865 root      20   0    4008   2792   2360 S  0.0  1.4   0:00.04 login
  938 nobody    20   0    5244   2756   2404 S  0.0  1.4   0:00.04 vsftpd
  941 nobody    20   0    5244   2756   2404 S  0.0  1.4   0:00.03 vsftpd
  457 root      20   0   31220   2740   2080 S  0.0  1.4   0:00.01 rsyslogd
  484 root      20   0    4008   2692   2268 S  0.0  1.4   0:00.05 login
  944 root      20   0    5076   2620   2352 R  0.0  1.3   0:00.00 top
  903 root      20   0    5080   2580   2236 T  0.0  1.3   0:00.00 top
  908 root      20   0    5052   2560   2288 T  0.0  1.3   0:00.08 top
  425 root      20   0    5416   2420   2100 S  0.0  1.2   0:00.00 cron
  406 root      20   0    5012   2348   2120 S  0.0  1.2   0:00.00 cron
  148 root      20   0   12272   2320   1856 S  0.0  1.2   0:00.09 systemd-udevd
  414 message+  20   0    5244   2268   1960 S  0.0  1.2   0:00.00 dbus-daemon
  390 statd     20   0    4576   2264   1764 S  0.0  1.2   0:00.00 rpc.statd

I should save the information in XML format instead of txt file! How to do it?

I would like to read file.txt and convert it to XML.

I think I could use Python but I dont know how:

#coding:utf-8

from xml.dom.minidom import Document

doc = Document()
root = doc.createElement('InfoMemoria')

The top command has no option to output as XML, so you'd have a lot of parsing to do (and such a request would be too broad for this site as such).

However, sysstat comes with sadf which has a -x option that might meet your needs. There are DTD and XSD files available on their download page .

@kjhughes is right, there is no integrated output scheme. And you are also right, you can use python. You can see some infos about xml and minidom here: https://docs.python.org/2/library/xml.dom.minidom.html

However, XML is a model-file. That means you describe a structure with it, not a primitive set of data.

Here you see an example for a website: https://pubs.vmware.com/vfabric52/index.jsp?topic=/com.vmware.vfabric.tc-server.2.8/getting-started/tutwebapp-web-xml-file.html

Or see here the maven file for compiling a java program (4th code block): https://spring.io/guides/gs/maven/

So, you can do it by deciding what is your strucutre, but you have to decide it for yourself because there is no standard for it concerning the top result. Thus, we come up with the question ...why should you choose this format ?

As a direkt answer to your question: How do add nodes, childnodes, and childnodes to childnodes ... you can find here: https://wiki.python.org/moin/MiniDom

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM