From owner-freebsd-questions Wed Aug 14 14:33:34 2002 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 CDE8E37B400 for ; Wed, 14 Aug 2002 14:33:31 -0700 (PDT) Received: from smtp.infracaninophile.co.uk (happy-idiot-talk.infracaninophile.co.uk [81.2.69.218]) by mx1.FreeBSD.org (Postfix) with ESMTP id 97EAC43E6A for ; Wed, 14 Aug 2002 14:33:30 -0700 (PDT) (envelope-from m.seaman@infracaninophile.co.uk) Received: from happy-idiot-talk.infracaninophile.co.uk ([IPv6:::1]) by smtp.infracaninophile.co.uk (8.12.5/8.12.5) with ESMTP id g7ELXIYk006392; Wed, 14 Aug 2002 22:33:18 +0100 (BST) (envelope-from matthew@happy-idiot-talk.infracaninophile.co.uk) Received: (from matthew@localhost) by happy-idiot-talk.infracaninophile.co.uk (8.12.5/8.12.5/Submit) id g7ELXC3f006391; Wed, 14 Aug 2002 22:33:12 +0100 (BST) Date: Wed, 14 Aug 2002 22:33:12 +0100 From: Matthew Seaman To: Dan Nelson Cc: Tillman Hodgson , freebsd-questions@FreeBSD.ORG Subject: Re: Extracting the 1-minute loadavg in a portable yet low-impact fashion Message-ID: <20020814213312.GD2827@happy-idiot-talk.infracaninophi> References: <20020814083253.C11913@seekingfire.com> <20020814144729.GA50066@dan.emsphone.com> Mime-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <20020814144729.GA50066@dan.emsphone.com> User-Agent: Mutt/1.5.1i Sender: owner-freebsd-questions@FreeBSD.ORG Precedence: bulk List-ID: List-Archive: (Web Archive) List-Help: (List Instructions) List-Subscribe: List-Unsubscribe: X-Loop: FreeBSD.ORG On Wed, Aug 14, 2002 at 09:47:29AM -0500, Dan Nelson wrote: > In the last episode (Aug 14), Tillman Hodgson said: > > I'm trying to extract the 1-minute loadavg in a portable (between > > RedHat Linux and FreeBSD, at least) way without having to invoke a > > lot of expensive subprocesses. On Linux, awk '{print \$1}' < > > /proc/loadavg has been working nicely (though non-portably). When I > > log onto a BSD host and my NFS-mounted home dir follows me, this > > obviously doesn't work very well :-) > > > > uptime | awk '{print $10}' works, but leaves a trailing comma. Adding > > a sed statement to the end of that would start to get too expensive. > > That actually won't work, since if the system has been up for under an > hour, you get > > 9:43AM up 28 mins, 6 users, load averages: 0.28, 0.67, 0.43 > > How about uptime | sed -e 's/.*: \([0-9.]*\).*/\1/' ? That should work > on any system where the loadavg is immediately preceded by ": ", which > is every OS I can lay my hands on at the moment. That should work. Or this trivial little bit of C code should be portable to most Unixoid O/Ses: happy-idiot-talk:/tmp:% cat ldavg.c #include #include int main(int argc, char *argv[]) { double ldavg; if (getloadavg(&ldavg, 1) == -1) { perror("Load average unavailable\n"); exit(EXIT_FAILURE); } else { printf("%.3f\n", ldavg); } exit(EXIT_SUCCESS); } Compile like so: cc -o ldavg ldavg.c That should be about as low impact as you can get, portably. Cheers, Matthew -- Dr Matthew J Seaman MA, D.Phil. 26 The Paddocks Savill Way Tel: +44 1628 476614 Marlow Fax: +44 0870 0522645 Bucks., SL7 1TH UK To Unsubscribe: send mail to majordomo@FreeBSD.org with "unsubscribe freebsd-questions" in the body of the message