xref: /titanic_50/usr/src/cmd/auditstat/auditstat.c (revision 174bc6499d233e329ecd3d98a880a7b07df16bfa)
1 /*
2  * CDDL HEADER START
3  *
4  * The contents of this file are subject to the terms of the
5  * Common Development and Distribution License (the "License").
6  * You may not use this file except in compliance with the License.
7  *
8  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9  * or http://www.opensolaris.org/os/licensing.
10  * See the License for the specific language governing permissions
11  * and limitations under the License.
12  *
13  * When distributing Covered Code, include this CDDL HEADER in each
14  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15  * If applicable, add the following below this CDDL HEADER, with the
16  * fields enclosed by brackets "[]" replaced with your own identifying
17  * information: Portions Copyright [yyyy] [name of copyright owner]
18  *
19  * CDDL HEADER END
20  */
21 /*
22  * Copyright 2009 Sun Microsystems, Inc.  All rights reserved.
23  * Use is subject to license terms.
24  */
25 
26 #include "statcommon.h"
27 
28 #include <sys/types.h>
29 #include <stdlib.h>
30 #include <ctype.h>
31 #include <stdio.h>
32 #include <bsm/audit.h>
33 #include <bsm/libbsm.h>
34 #include <unistd.h>
35 #include <locale.h>
36 
37 #if !defined(TEXT_DOMAIN)		/* Should be defined by cc -D */
38 #define	TEXT_DOMAIN "SYS_TEST"		/* Use this only if it isn't */
39 #endif
40 
41 
42 /*
43  * Display header every HEADER_MOD lines printed
44  */
45 #define		DFLT_HEADER_MOD (20)
46 #define		ONEK (1024)
47 
48 #define		CFLG (0x01)
49 #define		HFLG (0x02)
50 #define		IFLG (0x04)
51 #define		NFLG (0x08)
52 #define		VFLG (0x10)
53 
54 extern char	*optarg;
55 
56 static int	count;
57 static int	flags;
58 static int	header_mod = DFLT_HEADER_MOD;
59 static int	interval;
60 static uint_t timestamp_fmt = NODATE;
61 
62 static void	display_stats();
63 static void	eauditon();
64 static void	parse_args();
65 static void	usage_exit();
66 static int	strisdigit();
67 
68 int
69 main(argc, argv)
70 int	argc;
71 char	**argv;
72 {
73 	register int	i;
74 	au_stat_t s;
75 
76 	(void) setlocale(LC_ALL, "");
77 	(void) textdomain(TEXT_DOMAIN);
78 
79 	(void) setbuf(stdout, (char *)0);
80 	(void) setbuf(stderr, (char *)0);
81 
82 	parse_args(argc, argv);
83 
84 	if (!flags) {
85 		eauditon(A_GETSTAT, (caddr_t)&s, NULL);
86 		if (timestamp_fmt != NODATE)
87 			print_timestamp(timestamp_fmt);
88 		display_stats(&s, 0);
89 		exit(0);
90 	}
91 
92 	if (flags & VFLG || flags & NFLG)
93 		eauditon(A_GETSTAT, (caddr_t)&s, NULL);
94 
95 	if (flags & VFLG)
96 		(void) printf("version = %d\n", s.as_version);
97 
98 	if (flags & NFLG)
99 		(void) printf("number of kernel events = %d\n", s.as_numevent);
100 
101 	if (!(flags & IFLG))
102 		exit(0);
103 
104 	/* CSTYLED */
105 	for (i = 0;; i++) {
106 		eauditon(A_GETSTAT, (caddr_t)&s, NULL);
107 		if (timestamp_fmt != NODATE)
108 			print_timestamp(timestamp_fmt);
109 		display_stats(&s, i);
110 		if ((flags & CFLG) && count)
111 			if (i == count - 1)
112 				break;
113 		(void) sleep(interval);
114 	}
115 
116 	return (0);
117 }
118 
119 
120 static void
121 display_stats(s, cnt)
122 au_stat_t *s;
123 {
124 	int	offset[12];   /* used to line the header up correctly */
125 	char	buf[512];
126 
127 	(void) sprintf(buf,
128 "%4u %n%4u %n%4u %n%4u %n%4u %n%4u %n%4u %n%4u %n%4u %n%4u %n%4u %n%4u%n",
129 		s->as_generated, 	&(offset[0]),
130 		s->as_nonattrib, 	&(offset[1]),
131 		s->as_kernel, 		&(offset[2]),
132 		s->as_audit, 		&(offset[3]),
133 		s->as_auditctl, 	&(offset[4]),
134 		s->as_enqueue, 		&(offset[5]),
135 		s->as_written, 		&(offset[6]),
136 		s->as_wblocked, 	&(offset[7]),
137 		s->as_rblocked, 	&(offset[8]),
138 		s->as_dropped, 		&(offset[9]),
139 		s->as_totalsize / ONEK,	&(offset[10]),
140 		s->as_memused / ONEK, 	&(offset[11]));
141 
142 	/* print a properly aligned header every HEADER_MOD lines */
143 	if (header_mod && (!cnt || ((timestamp_fmt != NODATE) ?
144 	    !(cnt % (header_mod / 2)) : !(cnt % header_mod)))) {
145 		(void) printf(
146 			"%*s %*s %*s %*s %*s %*s %*s %*s %*s %*s %*s %*s\n",
147 			offset[0] - 1,			"gen",
148 			offset[1] - offset[0] - 1,	"nona",
149 			offset[2] - offset[1] - 1,	"kern",
150 			offset[3] - offset[2] - 1,	"aud",
151 			offset[4] - offset[3] - 1,	"ctl",
152 			offset[5] - offset[4] - 1,	"enq",
153 			offset[6] - offset[5] - 1,	"wrtn",
154 			offset[7] - offset[6] - 1,	"wblk",
155 			offset[8] - offset[7] - 1,	"rblk",
156 			offset[9] - offset[8] - 1,	"drop",
157 			offset[10] - offset[9] - 1,	"tot",
158 			offset[11] - offset[10],	"mem");
159 	}
160 
161 	(void) puts(buf);
162 }
163 
164 
165 static void
166 eauditon(cmd, data, length)
167 int	cmd;
168 caddr_t data;
169 int	length;
170 {
171 	if (auditon(cmd, data, length) == -1) {
172 		perror("auditstat: auditon");
173 		exit(1);
174 	}
175 }
176 
177 
178 static void
179 parse_args(argc, argv)
180 int	argc;
181 char	**argv;
182 {
183 	int	c;
184 
185 	while ((c = getopt(argc, argv, "c:h:i:vnT:")) != -1) {
186 		switch (c) {
187 		case 'c':
188 			if (flags & CFLG)
189 				usage_exit();
190 			flags |= CFLG;
191 			if (strisdigit(optarg)) {
192 				(void) fprintf(stderr,
193 				"auditstat: invalid count specified.\n");
194 				exit(1);
195 			}
196 			count = atoi(optarg);
197 			break;
198 		case 'h':
199 			if (flags & HFLG)
200 				usage_exit();
201 			flags |= HFLG;
202 			if (strisdigit(optarg)) {
203 				(void) fprintf(stderr,
204 				"auditstat: invalid header arg specified.\n");
205 				exit(1);
206 			}
207 			header_mod = atoi(optarg);
208 			break;
209 		case 'i':
210 			if (flags & IFLG)
211 				usage_exit();
212 			flags |= IFLG;
213 			if (strisdigit(optarg)) {
214 				(void) fprintf(stderr,
215 				"auditstat: invalid interval specified.\n");
216 				exit(1);
217 			}
218 			interval = atoi(optarg);
219 			break;
220 		case 'n':
221 			if (flags & NFLG)
222 				usage_exit();
223 			flags |= NFLG;
224 			break;
225 		case 'v':
226 			if (flags & VFLG)
227 				usage_exit();
228 			flags |= VFLG;
229 			break;
230 		case 'T':
231 			if (optarg) {
232 				if (*optarg == 'u')
233 					timestamp_fmt = UDATE;
234 				else if (*optarg == 'd')
235 					timestamp_fmt = DDATE;
236 				else
237 					usage_exit();
238 			} else {
239 				usage_exit();
240 			}
241 			break;
242 		case '?':
243 		default:
244 			usage_exit();
245 			break;
246 		}
247 	}
248 }
249 
250 
251 static void
252 usage_exit()
253 {
254 	(void) fprintf(stderr,
255 	    "auditstat: usage: auditstat [-c count] [-h lines] "
256 	    "[-T d|u] [-i interval] [-n] [-v]\n");
257 	exit(1);
258 }
259 
260 
261 static int
262 strisdigit(s)
263 char	*s;
264 {
265 	for (; *s; s++)
266 		if (!isdigit(*s))
267 			return (1);
268 
269 	return (0);
270 }
271