[net.sources] 4.x tty usage report program

dunigan@ornl-msr (Tom Dunigan) (12/05/84)

ttystats.c reports on tty port usage (logins per port) and summary
login  and connect profile by hour.  You will need to make the 
variable "lbl" match your local tty definitions.  Sample output
from ttystats is:


wtmp begins Mon Nov 26 00:55 
 1482 logins over 8 working days

  Total and average logins and connect per hour
 0:00      9      1      0      0   12:00     88     11     61      7
 1:00      9      1      0      0   13:00    129     16     79      9
 2:00      9      1      0      0   14:00    128     16     83     10
 3:00      9      1      0      0   15:00    129     16     76      9
 4:00      9      1      0      0   16:00     91     11     34      4
 5:00     12      1      3      0   17:00     28      3      5      0
 6:00     22      2      6      0   18:00     18      2      1      0
 7:00     85     10     35      4   19:00     30      3      9      1
 8:00    186     23     88     11   20:00     29      3     10      1
 9:00    146     18     88     11   21:00     25      3      8      1
10:00    133     16     80     10   22:00     16      2      4      0
11:00    129     16     69      8   23:00     13      1      0      0
 14 console logins
          0   1   2   3   4   5   6   7   8   9   a   b   c   d   e   f  
 tty0_    0   1   0   0   0   0   0   4   0   0   0   0   0   0   0   0
 ttyh_    3   3   0   0  28  31  69   2   0  27  59  32  67  89   0  29
 ttyi_   21  22  15   0   4  15  31  29  40   0  19   4  23   0  23   4
 ttyj_   77  88  66  58  36  13   3   0 220  71  19   4  66   4   0   0
 ttyk_    0   0   0   0   0   0   0   0   0   0   0   5   1   1   0  27
 ttyp_   10   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0
3  shutdowns

------------------------cut here --------------------------------
static	char *sccsid = "@(#)ttystats.c";
/*			2/2/84
 * prints interesting things about tty usage, number of logins
 *   and working days
 *  CHANGE definition of lbl below to reflect your ttys
 * 
 */
#include <sys/types.h>
#include <stdio.h>
#include <signal.h>
#include <sys/stat.h>
#include <utmp.h>

static char lbl[] = "s0hijkp";  /* console plus other ttys */

#define NMAX	sizeof(buf[0].ut_name)
#define LMAX	sizeof(buf[0].ut_line)
#define	SECDAY	(24*60*60)
#define LPERDAY 53

#define	lineq(a,b)	(!strncmp(a,b,LMAX))
#define	nameq(a,b)	(!strncmp(a,b,NMAX))

char *index();
#define MAXCTLR sizeof lbl
int ttyusage [MAXCTLR][16]; /* dz and dhs */
#define MAXTTYS 256

int logincnt, loginhrs[24], activehrs[24];
int conscnt, ttycnt[MAXTTYS];
int inhour,outhour;
int day, prevday, days, daylogins;
int shutcnt, crashcnt;

char	**argv;
int	argc;

struct	utmp buf[128];
char	ttnames[MAXTTYS][LMAX+1];
long	logouts[MAXTTYS];

char	*ctime();

main()
{
	register int i, k;
	int bl, wtmp;
	char *ct;
	register struct utmp *bp;
	long otime;
	struct stat stb;
	int print;
	char * crmsg = (char *)0;
	long crtime;
 
	time(&buf[0].ut_time);
	wtmp = open("/usr/adm/wtmp", 0);
	if (wtmp < 0) {
		perror("/usr/adm/wtmp");
		exit(1);
	}
	fstat(wtmp, &stb);
	bl = (stb.st_size + sizeof (buf)-1) / sizeof (buf);
	for (bl--; bl >= 0; bl--) {
		lseek(wtmp, bl * sizeof (buf), 0);
		bp = &buf[read(wtmp, buf, sizeof (buf)) / sizeof(buf[0]) - 1];
		for ( ; bp >= buf; bp--) {
			print = want(bp);
			if (print) {
				ct = ctime(&bp->ut_time);
				inhour = atoi(ct+11);
				loginhrs[inhour]++;
				logincnt++;

				day = atoi(ct+8);
				if (day != prevday) {
				  if (daylogins > LPERDAY ) days++;
				  prevday=day;
				  daylogins=0;
				}
				daylogins++;
			}
			for (i = 0; i < MAXTTYS; i++) {
				if (ttnames[i][0] == 0) {
					strncpy(ttnames[i], bp->ut_line,
					    sizeof(bp->ut_line));
					otime = logouts[i];
					logouts[i] = bp->ut_time;
					if (print) ttycnt[i]++;
					break;
				}
				if (lineq(ttnames[i], bp->ut_line)) {
					otime = logouts[i];
					logouts[i] = bp->ut_time;
					if (print) ttycnt[i]++;
					break;
				}
			}
			if (print) {
				if (otime){
					if (otime < 0) otime = -otime;
					outhour = atoi(ctime(&otime)+11);
					for (i=inhour;i<outhour;i++)
					 activehrs[i]++;
				}
				fflush(stdout);
			}
			if (lineq(bp->ut_line, "~")) {
				for (i = 0; i < MAXTTYS; i++)
					logouts[i] = -bp->ut_time;
				if (nameq(bp->ut_name, "shutdown"))
					shutcnt++;
				else
					crashcnt++;
			}
		}
	}
	ct = ctime(&buf[0].ut_time);
	printf("\nwtmp begins %10.10s %5.5s \n", ct, ct + 11);
	if (days == 0 | daylogins) days++;
	printf(" %d logins over %d working days\n",logincnt,days);
	printf("\n  Total and average logins and connect per hour\n");
	for (i=0;i<12;i++)
	 printf("%2d:00 %6d %6d %6d %6d   %2d:00 %6d %6d %6d %6d\n",
	  i,loginhrs[i],loginhrs[i]/days,activehrs[i],activehrs[i]/days,
	  i+12,loginhrs[i+12],loginhrs[i+12]/days,activehrs[i+12],
	   activehrs[i+12]/days);
	ttyprint();
	printf("%d  shutdowns\n",crashcnt);
	exit(0);
}


want(bp)
	struct utmp *bp;
{
	register char **av;
	register int ac;

	if (bp->ut_line[0] == '~' && bp->ut_name[0] == '\0')
		strcpy(bp->ut_name, "reboot");		/* bandaid */
	if (bp->ut_name[0] == 0)
		return (0);
	if (argc == 0)
		return (1);
	av = argv;
	for (ac = 0; ac < argc; ac++) {
		if (nameq(*av, bp->ut_name) || lineq(*av, bp->ut_line))
			return (1);
		av++;
	}
	return (0);
}

ttyprint()
{
	/* display tty usage info */
	int i,j,k;
	char *p;

	for(i=0;i<MAXTTYS;i++)
	 if (ttycnt[i] && (p=index(lbl,ttnames[i][3]))!=NULL) {
		if (k = p-lbl){
			sscanf(ttnames[i]+4,"%1x",&j);
			ttyusage[k][j] = ttycnt[i];
		} else
			printf(" %d console logins\n",ttycnt[i]);
	}
	printf("         ");
	for (i=0;i<16;i++)printf(" %x  ",i);
	printf("\n");
	for (i=1;i<MAXCTLR - 1;i++) {
		printf(" tty%c_ ",lbl[i]);
		for (j=0;j<16;j++)printf("%4d",ttyusage[i][j]);
		printf("\n");
	}
}