basic freebsd programming
infofarmer at mail.ru
Sun Jan 2 12:30:20 PST 2005
cpghost at cordula.ws wrote:
> On Sun, Jan 02, 2005 at 09:11:42PM +0300, Andrew P. wrote:
>>The ones that are the most interesting for me now is how to
>>write small daemons best and how to read ipfw info from a program.
>>Of course I can refresh my C skills and gain some Unix-coding knowledge
>>by reading a couple' thousand pages, but I don't feel like it's
>>necessary for what I want to write - just a basic statistics collector.
> why not just go for a scripted solution in Perl or Python?
Well, I am going to dump all the ipfw counters to disk
(and process some data) in a loop of a single second.
Perl adds too much overhead for this task.
>>Should I explore FreeBSD source code or is there some solid piece of
> That's not necessary. If you want to write that in C, you'll have
> to familiarize yourself with the popen(3) call for executing a program
> and capturing its output. Then you need a few string processing functions
> like str*(3) sscanf() etc... to parse the output (that's the tricky part).
> Finally you will need a small example of a client and server in C that
> uses the sockets API (that's pretty generic and not FreeBSD-specific at
> all, just google for it). Combine all this and voila, you've got your
> nice monitoring app in C.
As a matter of fact, I already do have a functional C program,
processing and dumping data, which it gets from stdin. So I have
a shell loop, invoking `ipfw show | c_program` every 10 seconds.
But it seems to be ineffective. What I'm thinking about is a
closer-to-real-time daemon dumper.
> Alternatively, you could extract the info directly from the kernel
> by performing exactly the same steps that your utility program (ipfw...)
> does, but it's overkill for such a simple app.
ipfw show takes up to 0.1s and 500kb to run on my system. Which
is great for manual checks, but almost unacceptable for continuous
monitoring on a server under heavy load. I guess I'll have to learn
how to look up the counters in the kernel.
More information about the freebsd-questions