home *** CD-ROM | disk | FTP | other *** search
/ NetNews Usenet Archive 1992 #26 / NN_1992_26.iso / spool / comp / sys / sun / admin / 8049 < prev    next >
Encoding:
Text File  |  1992-11-08  |  6.3 KB  |  258 lines

  1. Path: sparky!uunet!europa.asd.contel.com!emory!swrinde!cs.utexas.edu!usc!rpi!batcomputer!munnari.oz.au!metro!socs.uts.edu.au!kralizec!nick
  2. From: nick@kralizec.zeta.org.au (Nick Andrew)
  3. Newsgroups: comp.sys.sun.admin
  4. Subject: Re: how to chop wtmp
  5. Message-ID: <10093@kralizec.zeta.org.au>
  6. Date: 6 Nov 92 22:24:30 GMT
  7. References: <2NOV92.16141023@cc4.crl.aecl.ca> <Bx4I63.EDF@ccu.umanitoba.ca>
  8. Organization: Kralizec Dialup Unix Sydney: +61-2-837-1183 V.32
  9. Lines: 247
  10.  
  11. mills@ccu.umanitoba.ca (Gary Mills) writes:
  12.  
  13. [ ... a solution to truncating wtmp ... ]
  14.  
  15. >BS=36
  16. >...
  17. >dd if=$LOG   of=$LOG.0 bs=$BS skip=$SK 2>/dev/null
  18.  
  19. Both solutions posted here have a common flaw ... they run dd with a very
  20. small blocksize (36), the exact record length of the wtmp file. On a wtmp
  21. file of any size, these scripts will take much more cpu and real time
  22. than they should.
  23.  
  24. Try using a blocksize of 3600. It works :-)
  25.  
  26. I don't actually truncate the wtmp on my system. I split it into monthly
  27. units, and calculate the time spent online by each of my members during
  28. that month. I have a program to convert the file to readable ASCII, and
  29. another to reverse the transformation. So, to get a wtmp file containing
  30. all the month's information, I generally have to take the copy made on
  31. the first day of the month and then make sure that no login entries are
  32. split from one to the other.
  33.  
  34. Here are my sources:
  35.  
  36. #! /bin/sh
  37. # This is a shell archive.  Remove anything before this line, then unpack
  38. # it by saving it into a file and typing "sh file".  To overwrite existing
  39. # files, type "sh file -c".  You can also feed this as standard input via
  40. # unshar, or by typing "sh <file", e.g..  If this archive is complete, you
  41. # will see the following message at the end:
  42. #        "End of shell archive."
  43. # Contents:  readutmp.c writeutmp.c wtmp.upd
  44. # Wrapped by root@kralizec on Sat Nov  7 09:23:34 1992
  45. PATH=/bin:/usr/bin:/usr/ucb ; export PATH
  46. if test -f 'readutmp.c' -a "${1}" != "-c" ; then 
  47.   echo shar: Will not clobber existing file \"'readutmp.c'\"
  48. else
  49. echo shar: Extracting \"'readutmp.c'\" \(943 characters\)
  50. sed "s/^X//" >'readutmp.c' <<'END_OF_FILE'
  51. X/*  @(#) readutmp.c ... Read wtmp file and output a readable/editable version
  52. X*/
  53. X
  54. X#include <stdio.h>
  55. X#include <utmp.h>
  56. X
  57. Xextern    char    *ctime();
  58. X
  59. Xstruct    utmp    utmprec;
  60. X
  61. XFILE    *infp;
  62. Xint    ulen;
  63. X
  64. Xchar    line[9],
  65. X    name[9],
  66. X    host[17];
  67. X
  68. Xmain(argc, argv)
  69. Xint    argc;
  70. Xchar    *argv[];
  71. X{
  72. X    int    n;
  73. X    char    *cp;
  74. X
  75. X
  76. X    line[8] = name[8] = host[16] = '\0';
  77. X
  78. X    if (argc != 2)
  79. X        usage();
  80. X
  81. X    infp = fopen(argv[1], "r");
  82. X    if (infp == NULL) {
  83. X        perror("readutmp");
  84. X        fprintf(stderr, "Unable to open wtmp\n");
  85. X        exit(1);
  86. X    }
  87. X
  88. X    ulen = sizeof(struct utmp);
  89. X    fprintf(stderr, "ulen is %d\n", ulen);
  90. X
  91. X    while ((n = fread(&utmprec, 1, ulen, infp)) > 0) {
  92. X
  93. X        strncpy(line, utmprec.ut_line, 8);
  94. X        strncpy(name, utmprec.ut_name, 8);
  95. X        strncpy(host, utmprec.ut_host, 16);
  96. X
  97. X        printf("%-8s %-8s %-16s %lx %s",
  98. X            line, name, host, utmprec.ut_time,
  99. X            ctime(&utmprec.ut_time));
  100. X    }
  101. X
  102. X    if (n < 0)
  103. X        perror("readutmp");
  104. X}
  105. X
  106. Xusage() {
  107. X    fprintf(stderr, "Usage: readutmp wtmpfile >output\n");
  108. X    exit(2);
  109. X}
  110. END_OF_FILE
  111. if test 943 -ne `wc -c <'readutmp.c'`; then
  112.     echo shar: \"'readutmp.c'\" unpacked with wrong size!
  113. fi
  114. # end of 'readutmp.c'
  115. fi
  116. if test -f 'writeutmp.c' -a "${1}" != "-c" ; then 
  117.   echo shar: Will not clobber existing file \"'writeutmp.c'\"
  118. else
  119. echo shar: Extracting \"'writeutmp.c'\" \(1058 characters\)
  120. sed "s/^X//" >'writeutmp.c' <<'END_OF_FILE'
  121. X/* @(#) writeutmp.c ... Write a wtmp file from the readable version on the
  122. X**    standard input.
  123. X*/
  124. X
  125. X#include <stdio.h>
  126. X#include <utmp.h>
  127. X
  128. Xstruct    utmp    utmprec;
  129. XFILE    *outfp;
  130. Xint    ulen;
  131. X
  132. Xchar    string[80];
  133. X
  134. Xmain(argc, argv)
  135. Xint    argc;
  136. Xchar    *argv[];
  137. X{
  138. X    int    n;
  139. X
  140. X    if (argc != 2)
  141. X        usage();
  142. X
  143. X    outfp = fopen(argv[1], "w");
  144. X    if (outfp == NULL) {
  145. X        perror("writeutmp");
  146. X        fprintf(stderr, "Unable to open wtmp\n");
  147. X        exit(1);
  148. X    }
  149. X
  150. X    ulen = sizeof(struct utmp);
  151. X    fprintf(stderr, "ulen is %d\n", ulen);
  152. X
  153. X    while (fgets(string, 80, stdin) != NULL) {
  154. X
  155. X        getstring(utmprec.ut_line, string + 0, 8);
  156. X        getstring(utmprec.ut_name, string + 9, 8);
  157. X        getstring(utmprec.ut_host, string + 18, 16);
  158. X        utmprec.ut_time = strtol(string + 35, NULL, 16);
  159. X
  160. X        fwrite(&utmprec, 1, ulen, outfp);
  161. X    }
  162. X
  163. X    fclose(outfp);
  164. X    exit(0);
  165. X}
  166. X
  167. Xgetstring(s1, s2, len)
  168. Xchar    *s1;
  169. Xchar    *s2;
  170. Xint    len;
  171. X{
  172. X    int    i;
  173. X
  174. X    for (i = 0; i < len; ++i) {
  175. X        if (*s2 == ' ')
  176. X            *s1 = '\0';
  177. X        else
  178. X            *s1 = *s2;
  179. X        ++s1;
  180. X        ++s2;
  181. X    }
  182. X}
  183. X
  184. Xusage() {
  185. X    fprintf(stderr, "Usage: writeutmp wtmpfile <input\n");
  186. X    exit(2);
  187. X}
  188. X
  189. X/* end of writeutmp.c */
  190. END_OF_FILE
  191. if test 1058 -ne `wc -c <'writeutmp.c'`; then
  192.     echo shar: \"'writeutmp.c'\" unpacked with wrong size!
  193. fi
  194. # end of 'writeutmp.c'
  195. fi
  196. if test -f 'wtmp.upd' -a "${1}" != "-c" ; then 
  197.   echo shar: Will not clobber existing file \"'wtmp.upd'\"
  198. else
  199. echo shar: Extracting \"'wtmp.upd'\" \(1058 characters\)
  200. sed "s/^X//" >'wtmp.upd' <<'END_OF_FILE'
  201. X#!/bin/csh
  202. X# Concatenate all the wtmp logs which were written since the system
  203. X# started, and put them into /usr/adm/wtmp.user. Find which users
  204. X# have been online for more than 40 hours.
  205. X
  206. Xcd /var/adm
  207. X
  208. X# Test if there is no wtmp file for last month
  209. X
  210. Xset m=`date +%m`
  211. Xset y=`date +%y`
  212. X@ x=$y - 1
  213. Xif ( $m == "01" ) set lm="${x}12"
  214. Xif ( $m == "02" ) set lm="${y}01"
  215. Xif ( $m == "03" ) set lm="${y}02"
  216. Xif ( $m == "04" ) set lm="${y}03"
  217. Xif ( $m == "05" ) set lm="${y}04"
  218. Xif ( $m == "06" ) set lm="${y}05"
  219. Xif ( $m == "07" ) set lm="${y}06"
  220. Xif ( $m == "08" ) set lm="${y}07"
  221. Xif ( $m == "09" ) set lm="${y}08"
  222. Xif ( $m == "10" ) set lm="${y}09"
  223. Xif ( $m == "11" ) set lm="${y}10"
  224. Xif ( $m == "12" ) set lm="${y}11"
  225. Xset wtmpfile="wtmp.$lm"
  226. X
  227. Xtest -f $wtmpfile
  228. X
  229. Xif ( $status != 0 ) then
  230. X#    Save it and make a new one
  231. X    echo `date` "Moving wtmp to $wtmpfile"
  232. X    mv wtmp $wtmpfile
  233. X    touch wtmp
  234. X    chmod 664 wtmp
  235. Xelse
  236. X    set wtmpfile="wtmp"
  237. Xendif
  238. X    
  239. X/usr/etc/ac -w $wtmpfile -p | sed 's/^.//' | sort >/home/guest/usage.today
  240. Xchmod 644 /home/guest/usage.today
  241. X
  242. Xexit 0
  243. X
  244. X# end of wtmp.upd
  245. END_OF_FILE
  246. if test 1058 -ne `wc -c <'wtmp.upd'`; then
  247.     echo shar: \"'wtmp.upd'\" unpacked with wrong size!
  248. fi
  249. chmod +x 'wtmp.upd'
  250. # end of 'wtmp.upd'
  251. fi
  252. echo shar: End of shell archive.
  253. exit 0
  254. -- 
  255. Kralizec Dialup Unix (Public Access)    Data: +61-2-837-1183, 14400 24hrs 8N1
  256. Zeta Microcomputer Software             Data: +61-2-837-1868, 2400 24hrs 8N1
  257. P.O. Box 177, Riverstone NSW 2765       Plan: To beat Gnuchess 4.1 !
  258.