简体   繁体   中英

How to collect system statistics from scratch?

I would like to get some system statistics. I know I can use collectd, but I am doing this for fun and learning. I would do it by examining contents of files in /proc/:id , eventually by parsing results of some commands, like netstat . I simply want to check content of file or command output every 1 second - since I want to have detailed insights to system. Is such method ok, or it would slow down and false benchmarks?

If this is for fun and learning, you'll have more fun and learn more by parsing /proc yourself. Another argument against parsing the output of commands is that in my experience, for production code, which is not your case, it can be unreliable (ie depend on the version of the tools and the LOCALE).

You can learn by studying implementations of existing tools. Busybox versions, when they exist, are simpler than full blown.

This is how busybox's route parse /proc/net/route :

http://git.busybox.net/busybox/tree/networking/route.c#n516

netstat is much more complicated, not surprisingly.

http://git.busybox.net/busybox/tree/networking/netstat.c

...sometimes though, this can't be done by parsing /proc files. Here's how ifconfig get some informations about an interface using IO controls:

http://git.busybox.net/busybox/tree/networking/interface.c#n607

Is the method OK?

Yes.

Or it would slow down and false benchmarks?

Well, this is a third argument against parsing the output of existing commands: the closer you get to the source of information, the more up to date. Use of a speedy language help too. Does this really matter if you're a couple of seconds late? It's up to you. If this is just to display some info, the slowest element in the path, by far, will be the human brain reading the results and taking a decision out of it anyway.

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