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, MERCHANTIBILITY 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 $
32 * $DragonFly: src/libexec/rpc.rstatd/rstat_proc.c,v 1.5 2005/01/31 21:20:58 joerg Exp $
36 * rstat service: built with rstat.x and derived from rpc.rstatd.c
38 * Copyright (c) 1984 by Sun Microsystems, Inc.
41 #include <sys/param.h>
42 #include <sys/kinfo.h>
43 #include <sys/socket.h>
44 #include <sys/sysctl.h>
46 #include <sys/vmmeter.h>
47 #include <sys/param.h>
61 #include <net/if_mib.h>
63 #undef FSHIFT /* Use protocol's shift and scale values */
70 #include <rpcsvc/rstat.h>
73 void updatexfers (int, int *);
77 extern int from_inetd
;
78 int sincelastreq
= 0; /* number of alarms since last request */
88 static stat_is_init
= 0;
91 #define FSCALE (1 << 8)
100 (void) signal(SIGALRM
, updatestat
);
105 rstatproc_stats_3_svc(argp
, rqstp
)
107 struct svc_req
*rqstp
;
112 return(&stats_all
.s3
);
116 rstatproc_stats_2_svc(argp
, rqstp
)
118 struct svc_req
*rqstp
;
123 return(&stats_all
.s2
);
127 rstatproc_stats_1_svc(argp
, rqstp
)
129 struct svc_req
*rqstp
;
134 return(&stats_all
.s1
);
138 rstatproc_havedisk_3_svc(argp
, rqstp
)
140 struct svc_req
*rqstp
;
152 rstatproc_havedisk_2_svc(argp
, rqstp
)
154 struct svc_req
*rqstp
;
156 return(rstatproc_havedisk_3_svc(argp
, rqstp
));
160 rstatproc_havedisk_1_svc(argp
, rqstp
)
162 struct svc_req
*rqstp
;
164 return(rstatproc_havedisk_3_svc(argp
, rqstp
));
171 struct clockinfo clockrate
;
173 size_t vmm_size
= sizeof(vmm
);
174 struct ifmibdata ifmd
;
176 struct timeval tm
, btm
;
177 struct kinfo_cputime cp_time
;
183 fprintf(stderr
, "entering updatestat\n");
185 if (sincelastreq
>= closedown
) {
187 fprintf(stderr
, "about to closedown\n");
199 mib
[1] = KERN_CLOCKRATE
;
200 len
= sizeof clockrate
;
201 if (sysctl(mib
, 2, &clockrate
, &len
, 0, 0) < 0) {
202 syslog(LOG_ERR
, "sysctl(kern.clockrate): %m");
207 if (kinfo_get_sched_cputime(&cp_time
)) {
208 syslog(LOG_ERR
, "rstat: can't read cp_time from kmem");
211 stats_all
.s1
.cp_time
[0] = cp_time
.cp_user
;
212 stats_all
.s1
.cp_time
[1] = cp_time
.cp_nice
;
213 stats_all
.s1
.cp_time
[2] = cp_time
.cp_sys
;
214 stats_all
.s1
.cp_time
[3] = cp_time
.cp_idle
;
216 (void)getloadavg(avrun
, sizeof(avrun
) / sizeof(avrun
[0]));
218 stats_all
.s2
.avenrun
[0] = avrun
[0] * FSCALE
;
219 stats_all
.s2
.avenrun
[1] = avrun
[1] * FSCALE
;
220 stats_all
.s2
.avenrun
[2] = avrun
[2] * FSCALE
;
223 mib
[1] = KERN_BOOTTIME
;
225 if (sysctl(mib
, 2, &btm
, &len
, 0, 0) < 0) {
226 syslog(LOG_ERR
, "sysctl(kern.boottime): %m");
230 stats_all
.s2
.boottime
.tv_sec
= btm
.tv_sec
;
231 stats_all
.s2
.boottime
.tv_usec
= btm
.tv_usec
;
235 fprintf(stderr
, "%d %d %d %d\n", stats_all
.s1
.cp_time
[0],
236 stats_all
.s1
.cp_time
[1], stats_all
.s1
.cp_time
[2], stats_all
.s1
.cp_time
[3]);
239 if (sysctlbyname("vm.vmmeter", &vmm
, &vmm_size
, NULL
, 0)) {
240 syslog(LOG_ERR
, "sysctlbyname: vm.vmmeter");
243 stats_all
.s1
.v_pgpgin
= vmm
.v_vnodepgsin
;
244 stats_all
.s1
.v_pgpgout
= vmm
.v_vnodepgsout
;
245 stats_all
.s1
.v_pswpin
= vmm
.v_swappgsin
;
246 stats_all
.s1
.v_pswpout
= vmm
.v_swappgsout
;
247 stats_all
.s1
.v_intr
= vmm
.v_intr
;
248 gettimeofday(&tm
, NULL
);
249 stats_all
.s1
.v_intr
-= hz
*(tm
.tv_sec
- btm
.tv_sec
) +
250 hz
*(tm
.tv_usec
- btm
.tv_usec
)/1000000;
251 stats_all
.s2
.v_swtch
= vmm
.v_swtch
;
253 /* update disk transfers */
254 updatexfers(RSTAT_DK_NDRIVE
, stats_all
.s1
.dk_xfer
);
258 mib
[2] = NETLINK_GENERIC
;
259 mib
[3] = IFMIB_SYSTEM
;
260 mib
[4] = IFMIB_IFCOUNT
;
261 len
= sizeof ifcount
;
262 if (sysctl(mib
, 5, &ifcount
, &len
, 0, 0) < 0) {
263 syslog(LOG_ERR
, "sysctl(net.link.generic.system.ifcount): %m");
267 stats_all
.s1
.if_ipackets
= 0;
268 stats_all
.s1
.if_opackets
= 0;
269 stats_all
.s1
.if_ierrors
= 0;
270 stats_all
.s1
.if_oerrors
= 0;
271 stats_all
.s1
.if_collisions
= 0;
272 for (i
= 1; i
<= ifcount
; i
++) {
274 mib
[3] = IFMIB_IFDATA
;
276 mib
[5] = IFDATA_GENERAL
;
277 if (sysctl(mib
, 6, &ifmd
, &len
, 0, 0) < 0) {
278 syslog(LOG_ERR
, "sysctl(net.link.ifdata.%d.general)"
283 stats_all
.s1
.if_ipackets
+= ifmd
.ifmd_data
.ifi_ipackets
;
284 stats_all
.s1
.if_opackets
+= ifmd
.ifmd_data
.ifi_opackets
;
285 stats_all
.s1
.if_ierrors
+= ifmd
.ifmd_data
.ifi_ierrors
;
286 stats_all
.s1
.if_oerrors
+= ifmd
.ifmd_data
.ifi_oerrors
;
287 stats_all
.s1
.if_collisions
+= ifmd
.ifmd_data
.ifi_collisions
;
289 gettimeofday((struct timeval
*)&stats_all
.s3
.curtime
, NULL
);
294 * returns true if have a disk
300 struct statinfo stats
;
301 int num_devices
, retval
= 0;
303 if ((num_devices
= getnumdevs()) < 0) {
304 syslog(LOG_ERR
, "rstatd: can't get number of devices: %s",
309 if (checkversion() < 0) {
310 syslog(LOG_ERR
, "rstatd: %s", devstat_errbuf
);
314 stats
.dinfo
= (struct devinfo
*)malloc(sizeof(struct devinfo
));
315 bzero(stats
.dinfo
, sizeof(struct devinfo
));
317 if (getdevs(&stats
) == -1) {
318 syslog(LOG_ERR
, "rstatd: can't get device list: %s",
322 for (i
= 0; i
< stats
.dinfo
->numdevs
; i
++) {
323 if (((stats
.dinfo
->devices
[i
].device_type
324 & DEVSTAT_TYPE_MASK
) == DEVSTAT_TYPE_DIRECT
)
325 && ((stats
.dinfo
->devices
[i
].device_type
326 & DEVSTAT_TYPE_PASS
) == 0)) {
332 if (stats
.dinfo
->mem_ptr
)
333 free(stats
.dinfo
->mem_ptr
);
340 updatexfers(numdevs
, devs
)
343 register int i
, j
, t
;
344 struct statinfo stats
;
346 u_int64_t total_transfers
;
348 if ((num_devices
= getnumdevs()) < 0) {
349 syslog(LOG_ERR
, "rstatd: can't get number of devices: %s",
354 if (checkversion() < 0) {
355 syslog(LOG_ERR
, "rstatd: %s", devstat_errbuf
);
359 stats
.dinfo
= (struct devinfo
*)malloc(sizeof(struct devinfo
));
360 bzero(stats
.dinfo
, sizeof(struct devinfo
));
362 if (getdevs(&stats
) == -1) {
363 syslog(LOG_ERR
, "rstatd: can't get device list: %s",
368 for (i
= 0, j
= 0; i
< stats
.dinfo
->numdevs
&& j
< numdevs
; i
++) {
369 if (((stats
.dinfo
->devices
[i
].device_type
370 & DEVSTAT_TYPE_MASK
) == DEVSTAT_TYPE_DIRECT
)
371 && ((stats
.dinfo
->devices
[i
].device_type
372 & DEVSTAT_TYPE_PASS
) == 0)) {
373 total_transfers
= stats
.dinfo
->devices
[i
].num_reads
+
374 stats
.dinfo
->devices
[i
].num_writes
+
375 stats
.dinfo
->devices
[i
].num_other
;
377 * XXX KDM If the total transfers for this device
378 * are greater than the amount we can fit in a
379 * signed integer, just set them to the maximum
380 * amount we can fit in a signed integer. I have a
381 * feeling that the rstat protocol assumes 32-bit
382 * integers, so this could well break on a 64-bit
383 * architecture like the Alpha.
385 if (total_transfers
> INT_MAX
)
394 if (stats
.dinfo
->mem_ptr
)
395 free(stats
.dinfo
->mem_ptr
);
401 rstat_service(rqstp
, transp
)
402 struct svc_req
*rqstp
;
409 bool_t (*xdr_argument
)(), (*xdr_result
)();
412 switch (rqstp
->rq_proc
) {
414 (void)svc_sendreply(transp
, (xdrproc_t
)xdr_void
, NULL
);
417 case RSTATPROC_STATS
:
418 xdr_argument
= xdr_void
;
419 xdr_result
= xdr_statstime
;
420 switch (rqstp
->rq_vers
) {
422 local
= (char *(*)()) rstatproc_stats_1_svc
;
424 case RSTATVERS_SWTCH
:
425 local
= (char *(*)()) rstatproc_stats_2_svc
;
428 local
= (char *(*)()) rstatproc_stats_3_svc
;
431 svcerr_progvers(transp
, RSTATVERS_ORIG
, RSTATVERS_TIME
);
437 case RSTATPROC_HAVEDISK
:
438 xdr_argument
= xdr_void
;
439 xdr_result
= xdr_u_int
;
440 switch (rqstp
->rq_vers
) {
442 local
= (char *(*)()) rstatproc_havedisk_1_svc
;
444 case RSTATVERS_SWTCH
:
445 local
= (char *(*)()) rstatproc_havedisk_2_svc
;
448 local
= (char *(*)()) rstatproc_havedisk_3_svc
;
451 svcerr_progvers(transp
, RSTATVERS_ORIG
, RSTATVERS_TIME
);
458 svcerr_noproc(transp
);
461 bzero((char *)&argument
, sizeof(argument
));
462 if (!svc_getargs(transp
, (xdrproc_t
)xdr_argument
, (caddr_t
)&argument
)) {
463 svcerr_decode(transp
);
466 result
= (*local
)(&argument
, rqstp
);
467 if (result
!= NULL
&&
468 !svc_sendreply(transp
, (xdrproc_t
)xdr_result
, result
)) {
469 svcerr_systemerr(transp
);
471 if (!svc_freeargs(transp
, (xdrproc_t
)xdr_argument
, (caddr_t
)&argument
))
472 errx(1, "unable to free arguments");