2 * raduse Shows the usage of the modem lines on a terminal server.
4 * Version: @(#)raduse 1.12 30-Jun-1999 miquels@cistron.nl
19 # define ut_time ut_xtime
22 #define UTSIZE (sizeof(struct utmp))
33 struct line lines[MAXLINES];
37 * List the terminal servers in the logfile.
50 /* Open the sort pipe. */
51 pp = popen("sort -u", "w");
53 /* Go back max. 1 day. */
55 stop = now - 3600 * HOURS;
58 if ((fp = fopen(RADWTMP, "r")) == NULL) {
62 fseek(fp, -UTSIZE, SEEK_END);
64 /* Read structs backwards. */
66 /* Rewind if needed. */
67 if (!first && fseek(fp, -2 * UTSIZE, SEEK_CUR) < 0) break;
70 /* Read struct and see if we have to stop. */
71 if (ftell(fp) < UTSIZE) break;
72 fread(&ut, UTSIZE, 1, fp);
73 if (ut.ut_time < stop) break;
75 memset(buf, 0, sizeof(buf));
76 strncpy(buf, ut.ut_line, sizeof(ut.ut_line));
78 /* Get terminal server name. */
79 if ((p = strchr(buf, ':')) != NULL)
83 fprintf(pp, "%s\n", p);
90 printf("raduse: no data found over the last 24 hours.\n");
94 * Find out the usage of the ttys defined in
95 * struct line lines[] for the last 21 hours.
97 int fillstruct(int offset, char *nas)
109 /* Go back max. 1 day. */
111 stop = now - 3600 * HOURS;
113 /* Open wtmp file. */
114 if ((fp = fopen(RADWTMP, "r")) == NULL) {
118 fseek(fp, -UTSIZE, SEEK_END);
120 /* Read structs backwards. */
122 /* Rewind if needed. */
123 if (!first && fseek(fp, -2 * UTSIZE, SEEK_CUR) < 0) break;
126 /* Read struct and see if we have to stop. */
127 if (ftell(fp) < UTSIZE) break;
128 fread(&ut, UTSIZE, 1, fp);
129 if (ut.ut_time < stop) break;
131 memset(buf, 0, sizeof(buf));
132 strncpy(buf, ut.ut_line, sizeof(ut.ut_line));
134 /* Compare terminal server name. */
135 if ((p = strchr(buf, ':')) != NULL)
139 if (strcmp(p, nas) != 0)
144 /* Now see which tty this was. */
145 if ((p = strchr(buf, ':')) != NULL)
151 if (port == 0 && buf[0] != '0')
153 if (port < offset || port > offset + nrlines)
158 /* Login or logout? */
159 if (ut.ut_user[0] == 0) {
160 lines[i].lastout = ut.ut_time;
164 /* Skip type LOGIN */
165 if (strncmp(ut.ut_user, "LOGIN", 5) == 0)
168 /* A login. Fill out the hour string. */
169 beg = (now - ut.ut_time) / 3600;
170 if (lines[i].lastout == 0) {
174 end = (now - lines[i].lastout) / 3600;
175 if (beg >= HOURS) beg = HOURS - 1;
176 if (end >= HOURS) end = HOURS - 1;
178 for(n = end; n <= beg; n++)
179 lines[i].hour[n] = lines[i].lastout ? '*' : '@';
184 return found ? 0 : -1;
188 * Draw something that vaguely resembles a graph showing the
189 * usage of the tty lines over the last 21 hours.
191 void drawit(int wide, int offset)
199 /* Find out current time. */
201 tm = localtime(&now);
202 thishour = tm->tm_hour;
204 /* Change lower spaces to dots. */
205 for(i = 0; i < nrlines; i++) {
207 for(hour = 0; hour < HOURS; hour++) {
208 if (lines[i].hour[hour]) {
211 lines[i].hour[hour] = '.';
215 /* Prefix (show if line is in use now) */
217 for(i = 0; i < nrlines; i++) {
219 printf(" %c ", lines[i].inuse ? '@' : ' ');
221 printf("%c ", lines[i].inuse ? '@' : ' ');
226 for(hour = 0; hour < HOURS; hour++) {
227 printf("%02d |", thishour);
229 if (thishour < 0) thishour = 23;
231 for(i = 0; i < nrlines; i++) {
233 if (lines[i].hour[hour])
234 star = lines[i].hour[hour];
236 printf(" %c ", star);
244 /* Print a short suffix. */
246 for(i = 0; i < nrlines; i++)
247 printf(wide ? "---" : "--");
249 for(i = 0; i < nrlines; i++) {
251 printf("%02d|", i + offset);
253 printf("%d|", (i + offset) / 10);
257 for(i = 0; i < nrlines; i++)
258 printf("%d|", (i + offset) % 10);
265 fprintf(stderr, "Usage: raduse [-w] [-o offset] terminal-server\n");
266 fprintf(stderr, " use raduse -l to find out a list of terminal servers\n");
270 int main(int argc, char **argv)
277 while((c = getopt(argc, argv, "lwo:")) != EOF) switch(c) {
282 offset = atoi(optarg);
297 if (wide) nrlines = 25;
299 if (optind >= argc) usage();
301 if (fillstruct(offset, argv[optind]) < 0) {
303 "raduse: %s: no data found (over the last 24 hours) (try raduse -l).\n",
307 drawit(wide, offset);