2 * Sun RPC is a product of Sun Microsystems, Inc. and is provided for
3 * unrestricted use provided that this legend is included on all tape
4 * media and as a part of the software program in whole or part. Users
5 * may copy or modify Sun RPC without charge, but are not authorized
6 * to license or distribute it to anyone else except as part of a product or
7 * program developed by the user.
9 * SUN RPC IS PROVIDED AS IS WITH NO WARRANTIES OF ANY KIND INCLUDING THE
10 * WARRANTIES OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR
11 * PURPOSE, OR ARISING FROM A COURSE OF DEALING, USAGE OR TRADE PRACTICE.
13 * Sun RPC is provided with no support and without any obligation on the
14 * part of Sun Microsystems, Inc. to assist in its use, correction,
15 * modification or enhancement.
17 * SUN MICROSYSTEMS, INC. SHALL HAVE NO LIABILITY WITH RESPECT TO THE
18 * INFRINGEMENT OF COPYRIGHTS, TRADE SECRETS OR ANY PATENTS BY SUN RPC
19 * OR ANY PART THEREOF.
21 * In no event will Sun Microsystems, Inc. be liable for any lost revenue
22 * or profits or other special, indirect and consequential damages, even if
23 * Sun has been advised of the possibility of such damages.
25 * Sun Microsystems, Inc.
27 * Mountain View, California 94043
29 * @(#)rpc.rstatd.c 1.1 86/09/25 Copyr 1984 Sun Micro
30 * @(#)rstat_proc.c 2.2 88/08/01 4.0 RPCSRC
31 * $FreeBSD: src/libexec/rpc.rstatd/rstat_proc.c,v 1.14.2.1 2002/07/11 17:17:56 alfred Exp $
35 * rstat service: built with rstat.x and derived from rpc.rstatd.c
37 * Copyright (c) 1984 by Sun Microsystems, Inc.
40 #include <sys/param.h>
41 #include <sys/socket.h>
42 #include <sys/sysctl.h>
44 #include <sys/vmmeter.h>
59 #include <net/if_mib.h>
61 #undef FSHIFT /* Use protocol's shift and scale values */
68 #include <rpcsvc/rstat.h>
71 void updatexfers (int, int *);
75 extern int from_inetd
;
76 int sincelastreq
= 0; /* number of alarms since last request */
86 static int stat_is_init
= 0;
89 #define FSCALE (1 << 8)
98 (void) signal(SIGALRM
, updatestat
);
103 rstatproc_stats_3_svc(void *argp
, struct svc_req
*rqstp
)
108 return(&stats_all
.s3
);
112 rstatproc_stats_2_svc(void *argp
, struct svc_req
*rqstp
)
117 return(&stats_all
.s2
);
121 rstatproc_stats_1_svc(void *argp
, struct svc_req
*rqstp
)
126 return(&stats_all
.s1
);
130 rstatproc_havedisk_3_svc(void *argp
, struct svc_req
*rqstp
)
142 rstatproc_havedisk_2_svc(void *argp
, struct svc_req
*rqstp
)
144 return(rstatproc_havedisk_3_svc(argp
, rqstp
));
148 rstatproc_havedisk_1_svc(void *argp
, struct svc_req
*rqstp
)
150 return(rstatproc_havedisk_3_svc(argp
, rqstp
));
157 struct clockinfo clockrate
;
159 size_t vmm_size
= sizeof(vmm
);
160 struct ifmibdata ifmd
;
162 struct timeval tm
, btm
;
163 struct kinfo_cputime cp_time
;
169 fprintf(stderr
, "entering updatestat\n");
171 if (sincelastreq
>= closedown
) {
173 fprintf(stderr
, "about to closedown\n");
185 mib
[1] = KERN_CLOCKRATE
;
186 len
= sizeof clockrate
;
187 if (sysctl(mib
, 2, &clockrate
, &len
, 0, 0) < 0) {
188 syslog(LOG_ERR
, "sysctl(kern.clockrate): %m");
193 if (kinfo_get_sched_cputime(&cp_time
)) {
194 syslog(LOG_ERR
, "rstat: can't read cp_time from kmem");
197 stats_all
.s1
.cp_time
[0] = cp_time
.cp_user
;
198 stats_all
.s1
.cp_time
[1] = cp_time
.cp_nice
;
199 stats_all
.s1
.cp_time
[2] = cp_time
.cp_sys
;
200 stats_all
.s1
.cp_time
[3] = cp_time
.cp_idle
;
202 (void)getloadavg(avrun
, NELEM(avrun
));
204 stats_all
.s2
.avenrun
[0] = avrun
[0] * FSCALE
;
205 stats_all
.s2
.avenrun
[1] = avrun
[1] * FSCALE
;
206 stats_all
.s2
.avenrun
[2] = avrun
[2] * FSCALE
;
209 mib
[1] = KERN_BOOTTIME
;
211 if (sysctl(mib
, 2, &btm
, &len
, 0, 0) < 0) {
212 syslog(LOG_ERR
, "sysctl(kern.boottime): %m");
216 stats_all
.s2
.boottime
.tv_sec
= btm
.tv_sec
;
217 stats_all
.s2
.boottime
.tv_usec
= btm
.tv_usec
;
221 fprintf(stderr
, "%d %d %d %d\n", stats_all
.s1
.cp_time
[0],
222 stats_all
.s1
.cp_time
[1], stats_all
.s1
.cp_time
[2], stats_all
.s1
.cp_time
[3]);
225 if (sysctlbyname("vm.vmmeter", &vmm
, &vmm_size
, NULL
, 0)) {
226 syslog(LOG_ERR
, "sysctlbyname: vm.vmmeter");
229 stats_all
.s1
.v_pgpgin
= vmm
.v_vnodepgsin
;
230 stats_all
.s1
.v_pgpgout
= vmm
.v_vnodepgsout
;
231 stats_all
.s1
.v_pswpin
= vmm
.v_swappgsin
;
232 stats_all
.s1
.v_pswpout
= vmm
.v_swappgsout
;
233 stats_all
.s1
.v_intr
= vmm
.v_intr
;
234 gettimeofday(&tm
, NULL
);
235 stats_all
.s1
.v_intr
-= hz
*(tm
.tv_sec
- btm
.tv_sec
) +
236 hz
*(tm
.tv_usec
- btm
.tv_usec
)/1000000;
237 stats_all
.s2
.v_swtch
= vmm
.v_swtch
;
239 /* update disk transfers */
240 updatexfers(RSTAT_DK_NDRIVE
, stats_all
.s1
.dk_xfer
);
244 mib
[2] = NETLINK_GENERIC
;
245 mib
[3] = IFMIB_SYSTEM
;
246 mib
[4] = IFMIB_IFCOUNT
;
247 len
= sizeof ifcount
;
248 if (sysctl(mib
, 5, &ifcount
, &len
, 0, 0) < 0) {
249 syslog(LOG_ERR
, "sysctl(net.link.generic.system.ifcount): %m");
253 stats_all
.s1
.if_ipackets
= 0;
254 stats_all
.s1
.if_opackets
= 0;
255 stats_all
.s1
.if_ierrors
= 0;
256 stats_all
.s1
.if_oerrors
= 0;
257 stats_all
.s1
.if_collisions
= 0;
258 for (i
= 1; i
<= ifcount
; i
++) {
260 mib
[3] = IFMIB_IFDATA
;
262 mib
[5] = IFDATA_GENERAL
;
263 if (sysctl(mib
, 6, &ifmd
, &len
, 0, 0) < 0) {
264 syslog(LOG_ERR
, "sysctl(net.link.ifdata.%d.general)"
269 stats_all
.s1
.if_ipackets
+= ifmd
.ifmd_data
.ifi_ipackets
;
270 stats_all
.s1
.if_opackets
+= ifmd
.ifmd_data
.ifi_opackets
;
271 stats_all
.s1
.if_ierrors
+= ifmd
.ifmd_data
.ifi_ierrors
;
272 stats_all
.s1
.if_oerrors
+= ifmd
.ifmd_data
.ifi_oerrors
;
273 stats_all
.s1
.if_collisions
+= ifmd
.ifmd_data
.ifi_collisions
;
275 gettimeofday((struct timeval
*)&stats_all
.s3
.curtime
, NULL
);
280 * returns true if have a disk
286 struct statinfo stats
;
287 int num_devices
, retval
= 0;
289 if ((num_devices
= getnumdevs()) < 0) {
290 syslog(LOG_ERR
, "rstatd: can't get number of devices: %s",
295 if (checkversion() < 0) {
296 syslog(LOG_ERR
, "rstatd: %s", devstat_errbuf
);
300 stats
.dinfo
= (struct devinfo
*)malloc(sizeof(struct devinfo
));
301 bzero(stats
.dinfo
, sizeof(struct devinfo
));
303 if (getdevs(&stats
) == -1) {
304 syslog(LOG_ERR
, "rstatd: can't get device list: %s",
308 for (i
= 0; i
< stats
.dinfo
->numdevs
; i
++) {
309 if (((stats
.dinfo
->devices
[i
].device_type
310 & DEVSTAT_TYPE_MASK
) == DEVSTAT_TYPE_DIRECT
)
311 && ((stats
.dinfo
->devices
[i
].device_type
312 & DEVSTAT_TYPE_PASS
) == 0)) {
318 if (stats
.dinfo
->mem_ptr
)
319 free(stats
.dinfo
->mem_ptr
);
326 updatexfers(int numdevs
, int *devs
)
328 register int i
, j
, t
;
329 struct statinfo stats
;
331 u_int64_t total_transfers
;
333 if ((num_devices
= getnumdevs()) < 0) {
334 syslog(LOG_ERR
, "rstatd: can't get number of devices: %s",
339 if (checkversion() < 0) {
340 syslog(LOG_ERR
, "rstatd: %s", devstat_errbuf
);
344 stats
.dinfo
= (struct devinfo
*)malloc(sizeof(struct devinfo
));
345 bzero(stats
.dinfo
, sizeof(struct devinfo
));
347 if (getdevs(&stats
) == -1) {
348 syslog(LOG_ERR
, "rstatd: can't get device list: %s",
353 for (i
= 0, j
= 0; i
< stats
.dinfo
->numdevs
&& j
< numdevs
; i
++) {
354 if (((stats
.dinfo
->devices
[i
].device_type
355 & DEVSTAT_TYPE_MASK
) == DEVSTAT_TYPE_DIRECT
)
356 && ((stats
.dinfo
->devices
[i
].device_type
357 & DEVSTAT_TYPE_PASS
) == 0)) {
358 total_transfers
= stats
.dinfo
->devices
[i
].num_reads
+
359 stats
.dinfo
->devices
[i
].num_writes
+
360 stats
.dinfo
->devices
[i
].num_other
;
362 * XXX KDM If the total transfers for this device
363 * are greater than the amount we can fit in a
364 * signed integer, just set them to the maximum
365 * amount we can fit in a signed integer. I have a
366 * feeling that the rstat protocol assumes 32-bit
367 * integers, so this could well break on a 64-bit
368 * architecture like the Alpha.
370 if (total_transfers
> INT_MAX
)
379 if (stats
.dinfo
->mem_ptr
)
380 free(stats
.dinfo
->mem_ptr
);
386 rstat_service(struct svc_req
*rqstp
, SVCXPRT
*transp
)
392 bool_t (*xdr_argument
)(), (*xdr_result
)();
395 switch (rqstp
->rq_proc
) {
397 (void)svc_sendreply(transp
, (xdrproc_t
)xdr_void
, NULL
);
400 case RSTATPROC_STATS
:
401 xdr_argument
= xdr_void
;
402 xdr_result
= xdr_statstime
;
403 switch (rqstp
->rq_vers
) {
405 local
= (char *(*)()) rstatproc_stats_1_svc
;
407 case RSTATVERS_SWTCH
:
408 local
= (char *(*)()) rstatproc_stats_2_svc
;
411 local
= (char *(*)()) rstatproc_stats_3_svc
;
414 svcerr_progvers(transp
, RSTATVERS_ORIG
, RSTATVERS_TIME
);
420 case RSTATPROC_HAVEDISK
:
421 xdr_argument
= xdr_void
;
422 xdr_result
= xdr_u_int
;
423 switch (rqstp
->rq_vers
) {
425 local
= (char *(*)()) rstatproc_havedisk_1_svc
;
427 case RSTATVERS_SWTCH
:
428 local
= (char *(*)()) rstatproc_havedisk_2_svc
;
431 local
= (char *(*)()) rstatproc_havedisk_3_svc
;
434 svcerr_progvers(transp
, RSTATVERS_ORIG
, RSTATVERS_TIME
);
441 svcerr_noproc(transp
);
444 bzero((char *)&argument
, sizeof(argument
));
445 if (!svc_getargs(transp
, (xdrproc_t
)xdr_argument
, (caddr_t
)&argument
)) {
446 svcerr_decode(transp
);
449 result
= (*local
)(&argument
, rqstp
);
450 if (result
!= NULL
&&
451 !svc_sendreply(transp
, (xdrproc_t
)xdr_result
, result
)) {
452 svcerr_systemerr(transp
);
454 if (!svc_freeargs(transp
, (xdrproc_t
)xdr_argument
, (caddr_t
)&argument
))
455 errx(1, "unable to free arguments");