summaryrefslogtreecommitdiffstats
path: root/lib
diff options
context:
space:
mode:
authorTon Voon <tonvoon@users.sourceforge.net>2003-02-19 00:45:43 (GMT)
committerTon Voon <tonvoon@users.sourceforge.net>2003-02-19 00:45:43 (GMT)
commit0490d0830234062b4055e4b0fd2131fb54d7743f (patch)
tree7adba02daf24b8169265659714b2cf69fe648efa /lib
parent841198076937d15155a6812472ad4da0d789649c (diff)
downloadmonitoring-plugins-0490d0830234062b4055e4b0fd2131fb54d7743f.tar.gz
Move getloadavg to lib directory. Must run "aclocal -I lib"
git-svn-id: https://nagiosplug.svn.sourceforge.net/svnroot/nagiosplug/nagiosplug/trunk@345 f882894a-f735-0410-b71e-b25c423dba1c
Diffstat (limited to 'lib')
-rw-r--r--lib/Makefile.am4
-rw-r--r--lib/getloadavg.c1086
-rw-r--r--lib/getloadavg.m4108
3 files changed, 1197 insertions, 1 deletions
diff --git a/lib/Makefile.am b/lib/Makefile.am
index 7727b28..afa91a6 100644
--- a/lib/Makefile.am
+++ b/lib/Makefile.am
@@ -1,10 +1,12 @@
1## Process this file with automake to produce Makefile.in 1## Process this file with automake to produce Makefile.in
2 2
3EXTRA_DIST = getloadavg.m4
4
3noinst_LIBRARIES = libnagiosplug.a 5noinst_LIBRARIES = libnagiosplug.a
4 6
5noinst_HEADERS = getopt.h 7noinst_HEADERS = getopt.h
6 8
7libnagiosplug_a_SOURCES = getopt.c getopt1.c 9libnagiosplug_a_SOURCES = getopt.c getopt1.c getloadavg.c
8 10
9INCLUDES = -I$(srcdir) 11INCLUDES = -I$(srcdir)
10 12
diff --git a/lib/getloadavg.c b/lib/getloadavg.c
new file mode 100644
index 0000000..e9c4c08
--- /dev/null
+++ b/lib/getloadavg.c
@@ -0,0 +1,1086 @@
1/* Get the system load averages.
2 Copyright (C) 1985, 86, 87, 88, 89, 91, 92, 93, 1994, 1995, 1997
3 Free Software Foundation, Inc.
4
5 This program is free software; you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published by
7 the Free Software Foundation; either version 2, or (at your option)
8 any later version.
9
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU General Public License for more details.
14
15 You should have received a copy of the GNU General Public License
16 along with this program; if not, write to the Free Software
17 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307,
18 USA. */
19
20/* Compile-time symbols that this file uses:
21
22 HAVE_PSTAT_GETDYNAMIC Define this if your system has the
23 pstat_getdynamic function. I think it
24 is unique to HPUX9. The best way to get the
25 definition is through the AC_FUNC_GETLOADAVG
26 macro that comes with autoconf 2.13 or newer.
27 If that isn't an option, then just put
28 AC_CHECK_FUNCS(pstat_getdynamic) in your
29 configure.in file.
30 FIXUP_KERNEL_SYMBOL_ADDR() Adjust address in returned struct nlist.
31 KERNEL_FILE Pathname of the kernel to nlist.
32 LDAV_CVT() Scale the load average from the kernel.
33 Returns a double.
34 LDAV_SYMBOL Name of kernel symbol giving load average.
35 LOAD_AVE_TYPE Type of the load average array in the kernel.
36 Must be defined unless one of
37 apollo, DGUX, NeXT, or UMAX is defined;
38 otherwise, no load average is available.
39 NLIST_STRUCT Include nlist.h, not a.out.h, and
40 the nlist n_name element is a pointer,
41 not an array.
42 NLIST_NAME_UNION struct nlist has an n_un member, not n_name.
43 LINUX_LDAV_FILE [__linux__]: File containing load averages.
44
45 Specific system predefines this file uses, aside from setting
46 default values if not emacs:
47
48 apollo
49 BSD Real BSD, not just BSD-like.
50 convex
51 DGUX
52 eunice UNIX emulator under VMS.
53 hpux
54 __MSDOS__ No-op for MSDOS.
55 NeXT
56 sgi
57 sequent Sequent Dynix 3.x.x (BSD)
58 _SEQUENT_ Sequent DYNIX/ptx 1.x.x (SYSV)
59 sony_news NEWS-OS (works at least for 4.1C)
60 UMAX
61 UMAX4_3
62 VMS
63 WINDOWS32 No-op for Windows95/NT.
64 __linux__ Linux: assumes /proc filesystem mounted.
65 Support from Michael K. Johnson.
66 __NetBSD__ NetBSD: assumes /kern filesystem mounted.
67
68 In addition, to avoid nesting many #ifdefs, we internally set
69 LDAV_DONE to indicate that the load average has been computed.
70
71 We also #define LDAV_PRIVILEGED if a program will require
72 special installation to be able to call getloadavg. */
73
74/* This should always be first. */
75#ifdef HAVE_CONFIG_H
76# include <config.h>
77#endif
78
79#include <sys/types.h>
80
81/* Both the Emacs and non-Emacs sections want this. Some
82 configuration files' definitions for the LOAD_AVE_CVT macro (like
83 sparc.h's) use macros like FSCALE, defined here. */
84#ifdef unix
85# include <sys/param.h>
86#endif
87
88
89/* Exclude all the code except the test program at the end
90 if the system has its own `getloadavg' function.
91
92 The declaration of `errno' is needed by the test program
93 as well as the function itself, so it comes first. */
94
95#include <errno.h>
96
97#ifndef errno
98extern int errno;
99#endif
100
101#if HAVE_LOCALE_H
102# include <locale.h>
103#endif
104#if !HAVE_SETLOCALE
105# define setlocale(Category, Locale) /* empty */
106#endif
107
108#ifndef HAVE_GETLOADAVG
109
110/* The existing Emacs configuration files define a macro called
111 LOAD_AVE_CVT, which accepts a value of type LOAD_AVE_TYPE, and
112 returns the load average multiplied by 100. What we actually want
113 is a macro called LDAV_CVT, which returns the load average as an
114 unmultiplied double.
115
116 For backwards compatibility, we'll define LDAV_CVT in terms of
117 LOAD_AVE_CVT, but future machine config files should just define
118 LDAV_CVT directly. */
119
120# if !defined(LDAV_CVT) && defined(LOAD_AVE_CVT)
121# define LDAV_CVT(n) (LOAD_AVE_CVT (n) / 100.0)
122# endif
123
124# if !defined (BSD) && defined (ultrix)
125/* Ultrix behaves like BSD on Vaxen. */
126# define BSD
127# endif
128
129# ifdef NeXT
130/* NeXT in the 2.{0,1,2} releases defines BSD in <sys/param.h>, which
131 conflicts with the definition understood in this file, that this
132 really is BSD. */
133# undef BSD
134
135/* NeXT defines FSCALE in <sys/param.h>. However, we take FSCALE being
136 defined to mean that the nlist method should be used, which is not true. */
137# undef FSCALE
138# endif
139
140/* Set values that are different from the defaults, which are
141 set a little farther down with #ifndef. */
142
143
144/* Some shorthands. */
145
146# if defined (HPUX) && !defined (hpux)
147# define hpux
148# endif
149
150# if defined (__hpux) && !defined (hpux)
151# define hpux
152# endif
153
154# if defined (__sun) && !defined (sun)
155# define sun
156# endif
157
158# if defined(hp300) && !defined(hpux)
159# define MORE_BSD
160# endif
161
162# if defined(ultrix) && defined(mips)
163# define decstation
164# endif
165
166# if defined (__SVR4) && !defined (SVR4)
167# define SVR4
168# endif
169
170# if (defined(sun) && defined(SVR4)) || defined (SOLARIS2)
171# define SUNOS_5
172# endif
173
174# if defined (__osf__) && (defined (__alpha) || defined (__alpha__))
175# define OSF_ALPHA
176# include <sys/mbuf.h>
177# include <sys/socket.h>
178# include <net/route.h>
179# include <sys/table.h>
180# endif
181
182# if defined (__osf__) && (defined (mips) || defined (__mips__))
183# define OSF_MIPS
184# include <sys/table.h>
185# endif
186
187/* UTek's /bin/cc on the 4300 has no architecture specific cpp define by
188 default, but _MACH_IND_SYS_TYPES is defined in <sys/types.h>. Combine
189 that with a couple of other things and we'll have a unique match. */
190# if !defined (tek4300) && defined (unix) && defined (m68k) && defined (mc68000) && defined (mc68020) && defined (_MACH_IND_SYS_TYPES)
191# define tek4300 /* Define by emacs, but not by other users. */
192# endif
193
194
195/* VAX C can't handle multi-line #ifs, or lines longer than 256 chars. */
196# ifndef LOAD_AVE_TYPE
197
198# ifdef MORE_BSD
199# define LOAD_AVE_TYPE long
200# endif
201
202# ifdef sun
203# define LOAD_AVE_TYPE long
204# endif
205
206# ifdef decstation
207# define LOAD_AVE_TYPE long
208# endif
209
210# ifdef _SEQUENT_
211# define LOAD_AVE_TYPE long
212# endif
213
214# ifdef sgi
215# define LOAD_AVE_TYPE long
216# endif
217
218# ifdef SVR4
219# define LOAD_AVE_TYPE long
220# endif
221
222# ifdef sony_news
223# define LOAD_AVE_TYPE long
224# endif
225
226# ifdef sequent
227# define LOAD_AVE_TYPE long
228# endif
229
230# ifdef OSF_ALPHA
231# define LOAD_AVE_TYPE long
232# endif
233
234# if defined (ardent) && defined (titan)
235# define LOAD_AVE_TYPE long
236# endif
237
238# ifdef tek4300
239# define LOAD_AVE_TYPE long
240# endif
241
242# if defined(alliant) && defined(i860) /* Alliant FX/2800 */
243# define LOAD_AVE_TYPE long
244# endif
245
246# ifdef _AIX
247# define LOAD_AVE_TYPE long
248# endif
249
250# ifdef convex
251# define LOAD_AVE_TYPE double
252# ifndef LDAV_CVT
253# define LDAV_CVT(n) (n)
254# endif
255# endif
256
257# endif /* No LOAD_AVE_TYPE. */
258
259# ifdef OSF_ALPHA
260/* <sys/param.h> defines an incorrect value for FSCALE on Alpha OSF/1,
261 according to ghazi@noc.rutgers.edu. */
262# undef FSCALE
263# define FSCALE 1024.0
264# endif
265
266# if defined(alliant) && defined(i860) /* Alliant FX/2800 */
267/* <sys/param.h> defines an incorrect value for FSCALE on an
268 Alliant FX/2800 Concentrix 2.2, according to ghazi@noc.rutgers.edu. */
269# undef FSCALE
270# define FSCALE 100.0
271# endif
272
273
274# ifndef FSCALE
275
276/* SunOS and some others define FSCALE in sys/param.h. */
277
278# ifdef MORE_BSD
279# define FSCALE 2048.0
280# endif
281
282# if defined(MIPS) || defined(SVR4) || defined(decstation)
283# define FSCALE 256
284# endif
285
286# if defined (sgi) || defined (sequent)
287/* Sometimes both MIPS and sgi are defined, so FSCALE was just defined
288 above under #ifdef MIPS. But we want the sgi value. */
289# undef FSCALE
290# define FSCALE 1000.0
291# endif
292
293# if defined (ardent) && defined (titan)
294# define FSCALE 65536.0
295# endif
296
297# ifdef tek4300
298# define FSCALE 100.0
299# endif
300
301# ifdef _AIX
302# define FSCALE 65536.0
303# endif
304
305# endif /* Not FSCALE. */
306
307# if !defined (LDAV_CVT) && defined (FSCALE)
308# define LDAV_CVT(n) (((double) (n)) / FSCALE)
309# endif
310
311# ifndef NLIST_STRUCT
312# if HAVE_NLIST_H
313# define NLIST_STRUCT
314# endif
315# endif
316
317/* VAX C can't handle multi-line #ifs, or lines longer that 256 characters. */
318# ifndef NLIST_STRUCT
319
320# ifdef MORE_BSD
321# define NLIST_STRUCT
322# endif
323
324# ifdef sun
325# define NLIST_STRUCT
326# endif
327
328# ifdef decstation
329# define NLIST_STRUCT
330# endif
331
332# ifdef hpux
333# define NLIST_STRUCT
334# endif
335
336# if defined (_SEQUENT_) || defined (sequent)
337# define NLIST_STRUCT
338# endif
339
340# ifdef sgi
341# define NLIST_STRUCT
342# endif
343
344# ifdef SVR4
345# define NLIST_STRUCT
346# endif
347
348# ifdef sony_news
349# define NLIST_STRUCT
350# endif
351
352# ifdef OSF_ALPHA
353# define NLIST_STRUCT
354# endif
355
356# if defined (ardent) && defined (titan)
357# define NLIST_STRUCT
358# endif
359
360# ifdef tek4300
361# define NLIST_STRUCT
362# endif
363
364# ifdef butterfly
365# define NLIST_STRUCT
366# endif
367
368# if defined(alliant) && defined(i860) /* Alliant FX/2800 */
369# define NLIST_STRUCT
370# endif
371
372# ifdef _AIX
373# define NLIST_STRUCT
374# endif
375
376# endif /* defined (NLIST_STRUCT) */
377
378
379# if defined(sgi) || (defined(mips) && !defined(BSD))
380# define FIXUP_KERNEL_SYMBOL_ADDR(nl) ((nl)[0].n_value &= ~(1 << 31))
381# endif
382
383
384# if !defined (KERNEL_FILE) && defined (sequent)
385# define KERNEL_FILE "/dynix"
386# endif
387
388# if !defined (KERNEL_FILE) && defined (hpux)
389# define KERNEL_FILE "/hp-ux"
390# endif
391
392# if !defined(KERNEL_FILE) && (defined(_SEQUENT_) || defined(MIPS) || defined(SVR4) || defined(ISC) || defined (sgi) || (defined (ardent) && defined (titan)))
393# define KERNEL_FILE "/unix"
394# endif
395
396
397# if !defined (LDAV_SYMBOL) && defined (alliant)
398# define LDAV_SYMBOL "_Loadavg"
399# endif
400
401# if !defined(LDAV_SYMBOL) && ((defined(hpux) && !defined(hp9000s300)) || defined(_SEQUENT_) || defined(SVR4) || defined(ISC) || defined(sgi) || (defined (ardent) && defined (titan)) || defined (_AIX))
402# define LDAV_SYMBOL "avenrun"
403# endif
404
405# ifdef HAVE_UNISTD_H
406# include <unistd.h>
407# endif
408
409# include <stdio.h>
410
411/* LOAD_AVE_TYPE should only get defined if we're going to use the
412 nlist method. */
413# if !defined(LOAD_AVE_TYPE) && (defined(BSD) || defined(LDAV_CVT) || defined(KERNEL_FILE) || defined(LDAV_SYMBOL))
414# define LOAD_AVE_TYPE double
415# endif
416
417# ifdef LOAD_AVE_TYPE
418
419# ifndef VMS
420# ifndef __linux__
421# ifndef NLIST_STRUCT
422# include <a.out.h>
423# else /* NLIST_STRUCT */
424# include <nlist.h>
425# endif /* NLIST_STRUCT */
426
427# ifdef SUNOS_5
428# include <fcntl.h>
429# include <kvm.h>
430# include <kstat.h>
431# endif
432
433# if defined (hpux) && defined (HAVE_PSTAT_GETDYNAMIC)
434# include <sys/pstat.h>
435# endif
436
437# ifndef KERNEL_FILE
438# define KERNEL_FILE "/vmunix"
439# endif /* KERNEL_FILE */
440
441# ifndef LDAV_SYMBOL
442# define LDAV_SYMBOL "_avenrun"
443# endif /* LDAV_SYMBOL */
444# endif /* __linux__ */
445
446# else /* VMS */
447
448# ifndef eunice
449# include <iodef.h>
450# include <descrip.h>
451# else /* eunice */
452# include <vms/iodef.h>
453# endif /* eunice */
454# endif /* VMS */
455
456# ifndef LDAV_CVT
457# define LDAV_CVT(n) ((double) (n))
458# endif /* !LDAV_CVT */
459
460# endif /* LOAD_AVE_TYPE */
461
462# if defined(__GNU__) && !defined (NeXT)
463/* Note that NeXT Openstep defines __GNU__ even though it should not. */
464/* GNU system acts much like NeXT, for load average purposes,
465 but not exactly. */
466# define NeXT
467# define host_self mach_host_self
468# endif
469
470# ifdef NeXT
471# ifdef HAVE_MACH_MACH_H
472# include <mach/mach.h>
473# else
474# include <mach.h>
475# endif
476# endif /* NeXT */
477
478# ifdef sgi
479# include <sys/sysmp.h>
480# endif /* sgi */
481
482# ifdef UMAX
483# include <stdio.h>
484# include <signal.h>
485# include <sys/time.h>
486# include <sys/wait.h>
487# include <sys/syscall.h>
488
489# ifdef UMAX_43
490# include <machine/cpu.h>
491# include <inq_stats/statistics.h>
492# include <inq_stats/sysstats.h>
493# include <inq_stats/cpustats.h>
494# include <inq_stats/procstats.h>
495# else /* Not UMAX_43. */
496# include <sys/sysdefs.h>
497# include <sys/statistics.h>
498# include <sys/sysstats.h>
499# include <sys/cpudefs.h>
500# include <sys/cpustats.h>
501# include <sys/procstats.h>
502# endif /* Not UMAX_43. */
503# endif /* UMAX */
504
505# ifdef DGUX
506# include <sys/dg_sys_info.h>
507# endif
508
509# if defined(HAVE_FCNTL_H) || defined(_POSIX_VERSION)
510# include <fcntl.h>
511# else
512# include <sys/file.h>
513# endif
514
515/* Avoid static vars inside a function since in HPUX they dump as pure. */
516
517# ifdef NeXT
518static processor_set_t default_set;
519static int getloadavg_initialized;
520# endif /* NeXT */
521
522# ifdef UMAX
523static unsigned int cpus = 0;
524static unsigned int samples;
525# endif /* UMAX */
526
527# ifdef DGUX
528static struct dg_sys_info_load_info load_info; /* what-a-mouthful! */
529# endif /* DGUX */
530
531# ifdef LOAD_AVE_TYPE
532/* File descriptor open to /dev/kmem or VMS load ave driver. */
533static int channel;
534/* Nonzero iff channel is valid. */
535static int getloadavg_initialized;
536/* Offset in kmem to seek to read load average, or 0 means invalid. */
537static long offset;
538
539# if !defined(VMS) && !defined(sgi) && !defined(__linux__)
540static struct nlist nl[2];
541# endif /* Not VMS or sgi */
542
543# ifdef SUNOS_5
544static kvm_t *kd;
545# endif /* SUNOS_5 */
546
547# endif /* LOAD_AVE_TYPE */
548
549/* Put the 1 minute, 5 minute and 15 minute load averages
550 into the first NELEM elements of LOADAVG.
551 Return the number written (never more than 3, but may be less than NELEM),
552 or -1 if an error occurred. */
553
554int
555getloadavg (loadavg, nelem)
556 double loadavg[];
557 int nelem;
558{
559 int elem = 0; /* Return value. */
560
561# ifdef NO_GET_LOAD_AVG
562# define LDAV_DONE
563 /* Set errno to zero to indicate that there was no particular error;
564 this function just can't work at all on this system. */
565 errno = 0;
566 elem = -1;
567# endif
568
569# if !defined (LDAV_DONE) && defined (HAVE_LIBKSTAT)
570/* Use libkstat because we don't have to be root. */
571# define LDAV_DONE
572 kstat_ctl_t *kc;
573 kstat_t *ksp;
574 kstat_named_t *kn;
575
576 kc = kstat_open ();
577 if (kc == 0)
578 return -1;
579 ksp = kstat_lookup (kc, "unix", 0, "system_misc");
580 if (ksp == 0 )
581 return -1;
582 if (kstat_read (kc, ksp, 0) == -1)
583 return -1;
584
585
586 kn = kstat_data_lookup (ksp, "avenrun_1min");
587 if (kn == 0)
588 {
589 /* Return -1 if no load average information is available. */
590 nelem = 0;
591 elem = -1;
592 }
593
594 if (nelem >= 1)
595 loadavg[elem++] = (double) kn->value.ul/FSCALE;
596
597 if (nelem >= 2)
598 {
599 kn = kstat_data_lookup (ksp, "avenrun_5min");
600 if (kn != 0)
601 {
602 loadavg[elem++] = (double) kn->value.ul/FSCALE;
603
604 if (nelem >= 3)
605 {
606 kn = kstat_data_lookup (ksp, "avenrun_15min");
607 if (kn != 0)
608 loadavg[elem++] = (double) kn->value.ul/FSCALE;
609 }
610 }
611 }
612
613 kstat_close (kc);
614# endif /* HAVE_LIBKSTAT */
615
616# if !defined (LDAV_DONE) && defined (hpux) && defined (HAVE_PSTAT_GETDYNAMIC)
617/* Use pstat_getdynamic() because we don't have to be root. */
618# define LDAV_DONE
619# undef LOAD_AVE_TYPE
620
621 struct pst_dynamic dyn_info;
622 if (pstat_getdynamic (&dyn_info, sizeof (dyn_info), 0, 0) < 0)
623 return -1;
624 if (nelem > 0)
625 loadavg[elem++] = dyn_info.psd_avg_1_min;
626 if (nelem > 1)
627 loadavg[elem++] = dyn_info.psd_avg_5_min;
628 if (nelem > 2)
629 loadavg[elem++] = dyn_info.psd_avg_15_min;
630
631# endif /* hpux && HAVE_PSTAT_GETDYNAMIC */
632
633# if !defined (LDAV_DONE) && defined (__linux__)
634# define LDAV_DONE
635# undef LOAD_AVE_TYPE
636
637# ifndef LINUX_LDAV_FILE
638# define LINUX_LDAV_FILE "/proc/loadavg"
639# endif
640
641 char ldavgbuf[40];
642 double load_ave[3];
643 int fd, count;
644
645 fd = open (LINUX_LDAV_FILE, O_RDONLY);
646 if (fd == -1)
647 return -1;
648 count = read (fd, ldavgbuf, 40);
649 (void) close (fd);
650 if (count <= 0)
651 return -1;
652
653 /* The following sscanf must use the C locale. */
654 setlocale (LC_NUMERIC, "C");
655 count = sscanf (ldavgbuf, "%lf %lf %lf",
656 &load_ave[0], &load_ave[1], &load_ave[2]);
657 setlocale (LC_NUMERIC, "");
658 if (count < 1)
659 return -1;
660
661 for (elem = 0; elem < nelem && elem < count; elem++)
662 loadavg[elem] = load_ave[elem];
663
664 return elem;
665
666# endif /* __linux__ */
667
668# if !defined (LDAV_DONE) && defined (__NetBSD__)
669# define LDAV_DONE
670# undef LOAD_AVE_TYPE
671
672# ifndef NETBSD_LDAV_FILE
673# define NETBSD_LDAV_FILE "/kern/loadavg"
674# endif
675
676 unsigned long int load_ave[3], scale;
677 int count;
678 FILE *fp;
679
680 fp = fopen (NETBSD_LDAV_FILE, "r");
681 if (fp == NULL)
682 return -1;
683 count = fscanf (fp, "%lu %lu %lu %lu\n",
684 &load_ave[0], &load_ave[1], &load_ave[2],
685 &scale);
686 (void) fclose (fp);
687 if (count != 4)
688 return -1;
689
690 for (elem = 0; elem < nelem; elem++)
691 loadavg[elem] = (double) load_ave[elem] / (double) scale;
692
693 return elem;
694
695# endif /* __NetBSD__ */
696
697# if !defined (LDAV_DONE) && defined (NeXT)
698# define LDAV_DONE
699 /* The NeXT code was adapted from iscreen 3.2. */
700
701 host_t host;
702 struct processor_set_basic_info info;
703 unsigned info_count;
704
705 /* We only know how to get the 1-minute average for this system,
706 so even if the caller asks for more than 1, we only return 1. */
707
708 if (!getloadavg_initialized)
709 {
710 if (processor_set_default (host_self (), &default_set) == KERN_SUCCESS)
711 getloadavg_initialized = 1;
712 }
713
714 if (getloadavg_initialized)
715 {
716 info_count = PROCESSOR_SET_BASIC_INFO_COUNT;
717 if (processor_set_info (default_set, PROCESSOR_SET_BASIC_INFO, &host,
718 (processor_set_info_t) &info, &info_count)
719 != KERN_SUCCESS)
720 getloadavg_initialized = 0;
721 else
722 {
723 if (nelem > 0)
724 loadavg[elem++] = (double) info.load_average / LOAD_SCALE;
725 }
726 }
727
728 if (!getloadavg_initialized)
729 return -1;
730# endif /* NeXT */
731
732# if !defined (LDAV_DONE) && defined (UMAX)
733# define LDAV_DONE
734/* UMAX 4.2, which runs on the Encore Multimax multiprocessor, does not
735 have a /dev/kmem. Information about the workings of the running kernel
736 can be gathered with inq_stats system calls.
737 We only know how to get the 1-minute average for this system. */
738
739 struct proc_summary proc_sum_data;
740 struct stat_descr proc_info;
741 double load;
742 register unsigned int i, j;
743
744 if (cpus == 0)
745 {
746 register unsigned int c, i;
747 struct cpu_config conf;
748 struct stat_descr desc;
749
750 desc.sd_next = 0;
751 desc.sd_subsys = SUBSYS_CPU;
752 desc.sd_type = CPUTYPE_CONFIG;
753 desc.sd_addr = (char *) &conf;
754 desc.sd_size = sizeof conf;
755
756 if (inq_stats (1, &desc))
757 return -1;
758
759 c = 0;
760 for (i = 0; i < conf.config_maxclass; ++i)
761 {
762 struct class_stats stats;
763 bzero ((char *) &stats, sizeof stats);
764
765 desc.sd_type = CPUTYPE_CLASS;
766 desc.sd_objid = i;
767 desc.sd_addr = (char *) &stats;
768 desc.sd_size = sizeof stats;
769
770 if (inq_stats (1, &desc))
771 return -1;
772
773 c += stats.class_numcpus;
774 }
775 cpus = c;
776 samples = cpus < 2 ? 3 : (2 * cpus / 3);
777 }
778
779 proc_info.sd_next = 0;
780 proc_info.sd_subsys = SUBSYS_PROC;
781 proc_info.sd_type = PROCTYPE_SUMMARY;
782 proc_info.sd_addr = (char *) &proc_sum_data;
783 proc_info.sd_size = sizeof (struct proc_summary);
784 proc_info.sd_sizeused = 0;
785
786 if (inq_stats (1, &proc_info) != 0)
787 return -1;
788
789 load = proc_sum_data.ps_nrunnable;
790 j = 0;
791 for (i = samples - 1; i > 0; --i)
792 {
793 load += proc_sum_data.ps_nrun[j];
794 if (j++ == PS_NRUNSIZE)
795 j = 0;
796 }
797
798 if (nelem > 0)
799 loadavg[elem++] = load / samples / cpus;
800# endif /* UMAX */
801
802# if !defined (LDAV_DONE) && defined (DGUX)
803# define LDAV_DONE
804 /* This call can return -1 for an error, but with good args
805 it's not supposed to fail. The first argument is for no
806 apparent reason of type `long int *'. */
807 dg_sys_info ((long int *) &load_info,
808 DG_SYS_INFO_LOAD_INFO_TYPE,
809 DG_SYS_INFO_LOAD_VERSION_0);
810
811 if (nelem > 0)
812 loadavg[elem++] = load_info.one_minute;
813 if (nelem > 1)
814 loadavg[elem++] = load_info.five_minute;
815 if (nelem > 2)
816 loadavg[elem++] = load_info.fifteen_minute;
817# endif /* DGUX */
818
819# if !defined (LDAV_DONE) && defined (apollo)
820# define LDAV_DONE
821/* Apollo code from lisch@mentorg.com (Ray Lischner).
822
823 This system call is not documented. The load average is obtained as
824 three long integers, for the load average over the past minute,
825 five minutes, and fifteen minutes. Each value is a scaled integer,
826 with 16 bits of integer part and 16 bits of fraction part.
827
828 I'm not sure which operating system first supported this system call,
829 but I know that SR10.2 supports it. */
830
831 extern void proc1_$get_loadav ();
832 unsigned long load_ave[3];
833
834 proc1_$get_loadav (load_ave);
835
836 if (nelem > 0)
837 loadavg[elem++] = load_ave[0] / 65536.0;
838 if (nelem > 1)
839 loadavg[elem++] = load_ave[1] / 65536.0;
840 if (nelem > 2)
841 loadavg[elem++] = load_ave[2] / 65536.0;
842# endif /* apollo */
843
844# if !defined (LDAV_DONE) && defined (OSF_MIPS)
845# define LDAV_DONE
846
847 struct tbl_loadavg load_ave;
848 table (TBL_LOADAVG, 0, &load_ave, 1, sizeof (load_ave));
849 loadavg[elem++]
850 = (load_ave.tl_lscale == 0
851 ? load_ave.tl_avenrun.d[0]
852 : (load_ave.tl_avenrun.l[0] / (double) load_ave.tl_lscale));
853# endif /* OSF_MIPS */
854
855# if !defined (LDAV_DONE) && (defined (__MSDOS__) || defined (WINDOWS32))
856# define LDAV_DONE
857
858 /* A faithful emulation is going to have to be saved for a rainy day. */
859 for ( ; elem < nelem; elem++)
860 {
861 loadavg[elem] = 0.0;
862 }
863# endif /* __MSDOS__ || WINDOWS32 */
864
865# if !defined (LDAV_DONE) && defined (OSF_ALPHA)
866# define LDAV_DONE
867
868 struct tbl_loadavg load_ave;
869 table (TBL_LOADAVG, 0, &load_ave, 1, sizeof (load_ave));
870 for (elem = 0; elem < nelem; elem++)
871 loadavg[elem]
872 = (load_ave.tl_lscale == 0
873 ? load_ave.tl_avenrun.d[elem]
874 : (load_ave.tl_avenrun.l[elem] / (double) load_ave.tl_lscale));
875# endif /* OSF_ALPHA */
876
877# if !defined (LDAV_DONE) && defined (VMS)
878 /* VMS specific code -- read from the Load Ave driver. */
879
880 LOAD_AVE_TYPE load_ave[3];
881 static int getloadavg_initialized = 0;
882# ifdef eunice
883 struct
884 {
885 int dsc$w_length;
886 char *dsc$a_pointer;
887 } descriptor;
888# endif
889
890 /* Ensure that there is a channel open to the load ave device. */
891 if (!getloadavg_initialized)
892 {
893 /* Attempt to open the channel. */
894# ifdef eunice
895 descriptor.dsc$w_length = 18;
896 descriptor.dsc$a_pointer = "$$VMS_LOAD_AVERAGE";
897# else
898 $DESCRIPTOR (descriptor, "LAV0:");
899# endif
900 if (sys$assign (&descriptor, &channel, 0, 0) & 1)
901 getloadavg_initialized = 1;
902 }
903
904 /* Read the load average vector. */
905 if (getloadavg_initialized
906 && !(sys$qiow (0, channel, IO$_READVBLK, 0, 0, 0,
907 load_ave, 12, 0, 0, 0, 0) & 1))
908 {
909 sys$dassgn (channel);
910 getloadavg_initialized = 0;
911 }
912
913 if (!getloadavg_initialized)
914 return -1;
915# endif /* VMS */
916
917# if !defined (LDAV_DONE) && defined(LOAD_AVE_TYPE) && !defined(VMS)
918
919 /* UNIX-specific code -- read the average from /dev/kmem. */
920
921# define LDAV_PRIVILEGED /* This code requires special installation. */
922
923 LOAD_AVE_TYPE load_ave[3];
924
925 /* Get the address of LDAV_SYMBOL. */
926 if (offset == 0)
927 {
928# ifndef sgi
929# ifndef NLIST_STRUCT
930 strcpy (nl[0].n_name, LDAV_SYMBOL);
931 strcpy (nl[1].n_name, "");
932# else /* NLIST_STRUCT */
933# ifdef NLIST_NAME_UNION
934 nl[0].n_un.n_name = LDAV_SYMBOL;
935 nl[1].n_un.n_name = 0;
936# else /* not NLIST_NAME_UNION */
937 nl[0].n_name = LDAV_SYMBOL;
938 nl[1].n_name = 0;
939# endif /* not NLIST_NAME_UNION */
940# endif /* NLIST_STRUCT */
941
942# ifndef SUNOS_5
943 if (
944# if !(defined (_AIX) && !defined (ps2))
945 nlist (KERNEL_FILE, nl)
946# else /* _AIX */
947 knlist (nl, 1, sizeof (nl[0]))
948# endif
949 >= 0)
950 /* Omit "&& nl[0].n_type != 0 " -- it breaks on Sun386i. */
951 {
952# ifdef FIXUP_KERNEL_SYMBOL_ADDR
953 FIXUP_KERNEL_SYMBOL_ADDR (nl);
954# endif
955 offset = nl[0].n_value;
956 }
957# endif /* !SUNOS_5 */
958# else /* sgi */
959 int ldav_off;
960
961 ldav_off = sysmp (MP_KERNADDR, MPKA_AVENRUN);
962 if (ldav_off != -1)
963 offset = (long) ldav_off & 0x7fffffff;
964# endif /* sgi */
965 }
966
967 /* Make sure we have /dev/kmem open. */
968 if (!getloadavg_initialized)
969 {
970# ifndef SUNOS_5
971 channel = open ("/dev/kmem", 0);
972 if (channel >= 0)
973 {
974 /* Set the channel to close on exec, so it does not
975 litter any child's descriptor table. */
976# ifdef FD_SETFD
977# ifndef FD_CLOEXEC
978# define FD_CLOEXEC 1
979# endif
980 (void) fcntl (channel, F_SETFD, FD_CLOEXEC);
981# endif
982 getloadavg_initialized = 1;
983 }
984# else /* SUNOS_5 */
985 /* We pass 0 for the kernel, corefile, and swapfile names
986 to use the currently running kernel. */
987 kd = kvm_open (0, 0, 0, O_RDONLY, 0);
988 if (kd != 0)
989 {
990 /* nlist the currently running kernel. */
991 kvm_nlist (kd, nl);
992 offset = nl[0].n_value;
993 getloadavg_initialized = 1;
994 }
995# endif /* SUNOS_5 */
996 }
997
998 /* If we can, get the load average values. */
999 if (offset && getloadavg_initialized)
1000 {
1001 /* Try to read the load. */
1002# ifndef SUNOS_5
1003 if (lseek (channel, offset, 0) == -1L
1004 || read (channel, (char *) load_ave, sizeof (load_ave))
1005 != sizeof (load_ave))
1006 {
1007 close (channel);
1008 getloadavg_initialized = 0;
1009 }
1010# else /* SUNOS_5 */
1011 if (kvm_read (kd, offset, (char *) load_ave, sizeof (load_ave))
1012 != sizeof (load_ave))
1013 {
1014 kvm_close (kd);
1015 getloadavg_initialized = 0;
1016 }
1017# endif /* SUNOS_5 */
1018 }
1019
1020 if (offset == 0 || !getloadavg_initialized)
1021 return -1;
1022# endif /* LOAD_AVE_TYPE and not VMS */
1023
1024# if !defined (LDAV_DONE) && defined (LOAD_AVE_TYPE) /* Including VMS. */
1025 if (nelem > 0)
1026 loadavg[elem++] = LDAV_CVT (load_ave[0]);
1027 if (nelem > 1)
1028 loadavg[elem++] = LDAV_CVT (load_ave[1]);
1029 if (nelem > 2)
1030 loadavg[elem++] = LDAV_CVT (load_ave[2]);
1031
1032# define LDAV_DONE
1033# endif /* !LDAV_DONE && LOAD_AVE_TYPE */
1034
1035# ifdef LDAV_DONE
1036 return elem;
1037# else
1038 /* Set errno to zero to indicate that there was no particular error;
1039 this function just can't work at all on this system. */
1040 errno = 0;
1041 return -1;
1042# endif
1043}
1044
1045#endif /* ! HAVE_GETLOADAVG */
1046
1047#ifdef TEST
1048void
1049main (argc, argv)
1050 int argc;
1051 char **argv;
1052{
1053 int naptime = 0;
1054
1055 if (argc > 1)
1056 naptime = atoi (argv[1]);
1057
1058 while (1)
1059 {
1060 double avg[3];
1061 int loads;
1062
1063 errno = 0; /* Don't be misled if it doesn't set errno. */
1064 loads = getloadavg (avg, 3);
1065 if (loads == -1)
1066 {
1067 perror ("Error getting load average");
1068 exit (1);
1069 }
1070 if (loads > 0)
1071 printf ("1-minute: %f ", avg[0]);
1072 if (loads > 1)
1073 printf ("5-minute: %f ", avg[1]);
1074 if (loads > 2)
1075 printf ("15-minute: %f ", avg[2]);
1076 if (loads > 0)
1077 putchar ('\n');
1078
1079 if (naptime == 0)
1080 break;
1081 sleep (naptime);
1082 }
1083
1084 exit (0);
1085}
1086#endif /* TEST */
diff --git a/lib/getloadavg.m4 b/lib/getloadavg.m4
new file mode 100644
index 0000000..ff8cf30
--- /dev/null
+++ b/lib/getloadavg.m4
@@ -0,0 +1,108 @@
1#serial 9
2
3# A replacement for autoconf's macro by the same name. This version
4# accepts an optional argument specifying the name of the $srcdir-relative
5# directory in which the file getloadavg.c may be found. It is unusual
6# (but justified, imho) that this file is required at ./configure time.
7
8undefine([AC_FUNC_GETLOADAVG])
9
10# AC_FUNC_GETLOADAVG
11# ------------------
12AC_DEFUN([AC_FUNC_GETLOADAVG],
13[ac_have_func=no # yes means we've found a way to get the load average.
14
15AC_CHECK_HEADERS(locale.h unistd.h mach/mach.h fcntl.h)
16AC_CHECK_FUNCS(setlocale)
17
18# By default, expect to find getloadavg.c in $srcdir/.
19ac_lib_dir_getloadavg=$srcdir
20# But if there's an argument, DIR, expect to find getloadavg.c in $srcdir/DIR.
21m4_ifval([$1], [ac_lib_dir_getloadavg=$srcdir/$1])
22# Make sure getloadavg.c is where it belongs, at ./configure-time.
23test -f $ac_lib_dir_getloadavg/getloadavg.c \
24 || AC_MSG_ERROR([getloadavg.c is not in $ac_lib_dir_getloadavg])
25# FIXME: Add an autoconf-time test, too?
26
27ac_save_LIBS=$LIBS
28
29# Check for getloadavg, but be sure not to touch the cache variable.
30(AC_CHECK_FUNC(getloadavg, exit 0, exit 1)) && ac_have_func=yes
31
32# On HPUX9, an unprivileged user can get load averages through this function.
33AC_CHECK_FUNCS(pstat_getdynamic)
34
35# Solaris has libkstat which does not require root.
36AC_CHECK_LIB(kstat, kstat_open)
37test $ac_cv_lib_kstat_kstat_open = yes && ac_have_func=yes
38
39# Some systems with -lutil have (and need) -lkvm as well, some do not.
40# On Solaris, -lkvm requires nlist from -lelf, so check that first
41# to get the right answer into the cache.
42# For kstat on solaris, we need libelf to force the definition of SVR4 below.
43if test $ac_have_func = no; then
44 AC_CHECK_LIB(elf, elf_begin, LIBS="-lelf $LIBS")
45fi
46if test $ac_have_func = no; then
47 AC_CHECK_LIB(kvm, kvm_open, LIBS="-lkvm $LIBS")
48 # Check for the 4.4BSD definition of getloadavg.
49 AC_CHECK_LIB(util, getloadavg,
50 [LIBS="-lutil $LIBS" ac_have_func=yes ac_cv_func_getloadavg_setgid=yes])
51fi
52
53if test $ac_have_func = no; then
54 # There is a commonly available library for RS/6000 AIX.
55 # Since it is not a standard part of AIX, it might be installed locally.
56 ac_getloadavg_LIBS=$LIBS
57 LIBS="-L/usr/local/lib $LIBS"
58 AC_CHECK_LIB(getloadavg, getloadavg,
59 [LIBS="-lgetloadavg $LIBS"], [LIBS=$ac_getloadavg_LIBS])
60fi
61
62# Make sure it is really in the library, if we think we found it,
63# otherwise set up the replacement function.
64AC_CHECK_FUNCS(getloadavg, [],
65 [_AC_LIBOBJ_GETLOADAVG])
66
67# Some definitions of getloadavg require that the program be installed setgid.
68AC_CACHE_CHECK(whether getloadavg requires setgid,
69 ac_cv_func_getloadavg_setgid,
70[AC_EGREP_CPP([Yowza Am I SETGID yet],
71[#include "$ac_lib_dir_getloadavg/getloadavg.c"
72#ifdef LDAV_PRIVILEGED
73Yowza Am I SETGID yet
74@%:@endif],
75 ac_cv_func_getloadavg_setgid=yes,
76 ac_cv_func_getloadavg_setgid=no)])
77if test $ac_cv_func_getloadavg_setgid = yes; then
78 NEED_SETGID=true
79 AC_DEFINE(GETLOADAVG_PRIVILEGED, 1,
80 [Define if the `getloadavg' function needs to be run setuid
81 or setgid.])
82else
83 NEED_SETGID=false
84fi
85AC_SUBST(NEED_SETGID)dnl
86
87if test $ac_cv_func_getloadavg_setgid = yes; then
88 AC_CACHE_CHECK(group of /dev/kmem, ac_cv_group_kmem,
89[ # On Solaris, /dev/kmem is a symlink. Get info on the real file.
90 ac_ls_output=`ls -lgL /dev/kmem 2>/dev/null`
91 # If we got an error (system does not support symlinks), try without -L.
92 test -z "$ac_ls_output" && ac_ls_output=`ls -lg /dev/kmem`
93 ac_cv_group_kmem=`echo $ac_ls_output \
94 | sed -ne ['s/[ ][ ]*/ /g;
95 s/^.[sSrwx-]* *[0-9]* *\([^0-9]*\) *.*/\1/;
96 / /s/.* //;p;']`
97])
98 AC_SUBST(KMEM_GROUP, $ac_cv_group_kmem)dnl
99fi
100if test "x$ac_save_LIBS" = x; then
101 GETLOADAVG_LIBS=$LIBS
102else
103 GETLOADAVG_LIBS=`echo "$LIBS" | sed "s!$ac_save_LIBS!!"`
104fi
105LIBS=$ac_save_LIBS
106
107AC_SUBST(GETLOADAVG_LIBS)dnl
108])# AC_FUNC_GETLOADAVG