From owner-freebsd-questions@FreeBSD.ORG Sun Jan 2 20:30:20 2005 Return-Path: Delivered-To: freebsd-questions@freebsd.org Received: from mx1.FreeBSD.org (mx1.freebsd.org [216.136.204.125]) by hub.freebsd.org (Postfix) with ESMTP id B086816A4CF for ; Sun, 2 Jan 2005 20:30:20 +0000 (GMT) Received: from mx1.mail.ru (mx1.mail.ru [194.67.23.121]) by mx1.FreeBSD.org (Postfix) with ESMTP id 4FA7643D1F for ; Sun, 2 Jan 2005 20:30:20 +0000 (GMT) (envelope-from infofarmer@mail.ru) Received: from [83.237.208.152] (port=3919 helo=[172.17.0.69]) by mx1.mail.ru with esmtp id 1ClCMl-00084B-00; Sun, 02 Jan 2005 23:30:19 +0300 Message-ID: <41D859DC.5080400@mail.ru> Date: Sun, 02 Jan 2005 23:30:20 +0300 From: "Andrew P." User-Agent: Mozilla Thunderbird 1.0 (Windows/20041206) X-Accept-Language: en-us, en MIME-Version: 1.0 To: cpghost@cordula.ws References: <41D8395E.4020803@mail.ru> <20050102194539.GG2583@bsdbox.farid-hajji.net> In-Reply-To: <20050102194539.GG2583@bsdbox.farid-hajji.net> Content-Type: text/plain; charset=ISO-8859-1; format=flowed Content-Transfer-Encoding: 7bit X-Spam: Not detected cc: questions@freebsd.org Subject: Re: basic freebsd programming X-BeenThere: freebsd-questions@freebsd.org X-Mailman-Version: 2.1.1 Precedence: list Reply-To: infofarmer@mail.ru List-Id: User questions List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Sun, 02 Jan 2005 20:30:20 -0000 cpghost@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 >>documentation? > > > 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. Thanks anyway! Best wishes, Andrew P.