home *** CD-ROM | disk | FTP | other *** search
/ NetNews Usenet Archive 1992 #16 / NN_1992_16.iso / spool / comp / unix / wizards / 3334 < prev    next >
Encoding:
Internet Message Format  |  1992-07-27  |  7.8 KB

  1. Xref: sparky comp.unix.wizards:3334 comp.lang.perl:4990 comp.unix.admin:4348
  2. Newsgroups: comp.unix.wizards,comp.lang.perl,comp.unix.admin
  3. Path: sparky!uunet!cs.utexas.edu!convex!convex!tchrist
  4. From: Tom Christiansen <tchrist@convex.COM>
  5. Subject: Re: sa -e ????
  6. Message-ID: <1992Jul28.122913.19341@news.eng.convex.com>
  7. Followup-To: comp.unix.admin,comp.lang.perl
  8. Originator: tchrist@pixel.convex.com
  9. Sender: usenet@news.eng.convex.com (news access account)
  10. Nntp-Posting-Host: pixel.convex.com
  11. Reply-To: tchrist@convex.COM (Tom Christiansen)
  12. Organization: CONVEX Realtime Development, Colorado Springs, CO
  13. References: <1992Jul23.143726.12180@news.eng.convex.com> <1992Jul27.155418.9036@news.eng.convex.com> <840@silence.princeton.nj.us>
  14. Date: Tue, 28 Jul 1992 12:29:13 GMT
  15. X-Disclaimer: This message was written by a user at CONVEX Computer
  16.               Corp. The opinions expressed are those of the user and
  17.               not necessarily those of CONVEX.
  18. Lines: 223
  19.  
  20. [I'm redirecting this thread into comp.unix.admin, where would 
  21.  seem to be a better home for it; change the newsgroups/followup-to
  22.  line if you disagree. 
  23.  
  24.  It's also a good example of using c2ph-generate require files.
  25.  Followups probably ought to go to comp.lang.perl about that.]
  26.  
  27. From the keyboard of jay@silence.princeton.nj.us (Jay Plett):
  28. :In article <1992Jul27.155418.9036@news.eng.convex.com>, tchrist@convex.COM (Tom Christiansen) writes:
  29. :> Given sys/acct.h, you could very easily write either a Perl or a C
  30. :> program to emulate what my "sa -e" does.
  31. :
  32. :Well, it's pretty easy once you've figured out how to interpret
  33. :all of the fields in the accounting file (sys/acct.h isn't always
  34. :awfully helpful with this).
  35. :
  36. :A C program which formats the accounting file according to
  37. :printf-like arguments is available by anonymous ftp in
  38. :pub/facct.tar.Z on ee-ftp.princeton.edu.  It works on SunOS
  39. :and Convex-OS and should be fairly easy to port to other
  40. :systems.
  41.  
  42. There are several problems with that approach. The most
  43. important of these is that you have to hack the C file
  44. for each possible change in the acct structure.  There may 
  45. be new fields, or fields with a different type.  Here are 
  46. four example acct files:
  47.  
  48.     ::::::::::::::
  49.     acct.convex
  50.     ::::::::::::::
  51.     char    ac_comm[10];            
  52.     struct timeval  ac_nutime;      
  53.     struct timeval  ac_nstime;      
  54.     struct timeval  ac_netime;      
  55.     time_t  ac_btime;               
  56.     u_short ac_uid;                 
  57.     u_short ac_gid;                 
  58.     long    ac_aid;                 
  59.     s64_t   ac_kbsec;               
  60.     long    ac_io;                  
  61.     dev_t   ac_tty;                 
  62.     char    ac_flag;                
  63.     char    ac_unused[5];           
  64.     float   ac_avconcur;            
  65.  
  66.     ::::::::::::::
  67.     acct.mips
  68.     ::::::::::::::
  69.     char    ac_flag;                
  70.     char    ac_stat;                
  71.     ushort  ac_uid;                 
  72.     ushort  ac_gid;                 
  73.     dev_t   ac_tty;                 
  74.     time_t  ac_btime;               
  75.     comp_t  ac_utime;               
  76.     comp_t  ac_stime;               
  77.     comp_t  ac_etime;               
  78.     comp_t  ac_mem;                 
  79.     comp_t  ac_io;                  
  80.     comp_t  ac_rw;                  
  81.     char    ac_comm[8];             
  82.  
  83.     ::::::::::::::
  84.     acct.sun
  85.     ::::::::::::::
  86.     char    ac_flag;                
  87.     char    ac_stat;                
  88.     uid_t   ac_uid;                 
  89.     gid_t   ac_gid;                 
  90.     dev_t   ac_tty;                 
  91.     time_t  ac_btime;               
  92.     comp_t  ac_utime;               
  93.     comp_t  ac_stime;               
  94.     comp_t  ac_etime;               
  95.     comp_t  ac_mem;                 
  96.     comp_t  ac_io;                  
  97.     comp_t  ac_rw;                  
  98.     char    ac_comm[8];             
  99.  
  100.     ::::::::::::::
  101.     acct.vax
  102.     ::::::::::::::
  103.     char    ac_comm[10];            
  104.     #ifdef vax
  105.     float   ac_utime;               
  106.     float   ac_stime;               
  107.     float   ac_etime;               
  108.     #else mips
  109.     comp_t  ac_utime;
  110.     comp_t  ac_stime;
  111.     comp_t  ac_etime;
  112.     #endif mips
  113.     time_t  ac_btime;               
  114.     short   ac_uid;                 
  115.     short   ac_gid;                 
  116.     #ifdef vax
  117.     float   ac_mem;                 
  118.     float   ac_io;                  
  119.     #else mips
  120.     short   ac_mem;
  121.     comp_t  ac_io;
  122.     #endif mips
  123.     dev_t   ac_tty;                 
  124.     char    ac_flag;                
  125.  
  126.  
  127. Another problem with the facct program is that you have to learn a whole
  128. new set of %blah escapes rather than using regular printf ones.
  129.  
  130. Rather than hack the C program each time, another approach is 
  131. to write your code in Perl instead.  I have a prototype, called 
  132. pacctf, which behaves this way:
  133.  
  134.     pacctf "cmd = %-10s %5.2fs %5.2fu uid = %5d concur=%3.1f\n" \
  135.             comm nstime nutime uid concur
  136.  
  137.  
  138. As you see, I am using Convex-style naming.  If I were to use
  139. the rw field, which Convex doesn't have, I'd've gotten this:
  140.  
  141.     pacctf: No such field in struct acct: rw
  142.     Valid field names are: aid, avconcur, btime, comm, flag, gid, io,
  143.                kbsec, netime, nstime, nutime, tty, uid, unused
  144.  
  145. There's a lot of room for improvement here.  It should probably be made
  146. into a perl library package.  There's no contigency for xlating things
  147. like uid, dev, gid into strings, although I do fix up the timevals.  It
  148. should be able to read from an alternate accounting file.  It mihgt be
  149. nice for some further logic in the args (like converting ac_btime into
  150. ctime() fmt) , although that's where a library would help.
  151.  
  152. But hey, it's a prototype -- whadya want?  No, seriously, what might
  153. one like in this kind of program?
  154.  
  155. --tom
  156.  
  157.  
  158. #! /bin/sh
  159. # This is a shell archive, meaning:
  160. # 1. Remove everything above the #! /bin/sh line.
  161. # 2. Save the resulting text in a file.
  162. # 3. Execute the file with /bin/sh (not csh) to create:
  163. #    pacctf
  164. # This archive created: Tue Jul 28 07:23:13 1992
  165. export PATH; PATH=/bin:/usr/bin:$PATH
  166. echo shar: "extracting 'pacctf'" '(1335 characters)'
  167. if test -f 'pacctf'
  168. then
  169.     echo shar: "will not over-write existing file 'pacctf'"
  170. else
  171. sed 's/^    X//' << \SHAR_EOF > 'pacctf'
  172.     X$ACCT = '/usr/adm/acct';
  173.     X
  174.     Xif (@ARGV == 0) {
  175.     X    die <<EOF
  176.     Xusage: $0 fmt args ...
  177.     X
  178.     Xeg:    $0 "cmd = %-8s uid = %d\\n" comm uid
  179.     XEOF
  180.     X} 
  181.     X
  182.     Xopen ACCT || die "can't open $ACCT: $!";
  183.     X
  184.     Xrequire 'sys/acct.ph';
  185.     X
  186.     X$size = &acct'sizeof();
  187.     X($binfmt = &acct'typedef()) =~ s/\bc(\d+)/A$1/g; 
  188.     X# fix c10 to A10, as v1.2 c2ph did this wrong
  189.     X
  190.     X$mask = shift;
  191.     Xeval qq(\$mask = "$mask");
  192.     Xdie $@ if $@;
  193.     X
  194.     Xfor (@ARGV) {
  195.     X    $i = eval "&acct'ac_$_";
  196.     X    if ($@ eq "Undefined subroutine \"acct'ac_" . $_ . 
  197.     X        "\" called at (eval) line 1.\n") {
  198.     X    select(STDERR);
  199.     X    print "$0: No such field in struct acct: $_\n";
  200.     X    $fieldnames = join(", ", sort grep(s/ac_//, @acct'fieldnames));
  201.     X    write;
  202.     Xformat STDERR =
  203.     XValid field names are: ^<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
  204.     X$fieldnames
  205.     X            ~~ ^<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
  206.     X$fieldnames
  207.     X.
  208.     X    exit 1;
  209.     X    }
  210.     X    die $@ if $@;
  211.     X    push(@indices, $i);
  212.     X    $wantidx[$i]++;
  213.     X}
  214.     X
  215.     X$| = 1;
  216.     X
  217.     X@tvalidx = grep($wantidx[$_] && $acct'typeof[$_] eq 'timeval', @acct'indices);
  218.     X@sidx = grep($wantidx[$_] && $acct'typedef[$_] =~ /^[ca]\d/i, @acct'indices);
  219.     X
  220.     Xwhile (read(ACCT, $ac, $size) == $size) {
  221.     X    @ac = unpack($binfmt, $ac);
  222.     X    # fix timevals
  223.     X    for $i (@tvalidx) { $ac[$i] += $ac[$i+1] / 1_000_000; } 
  224.     X    # fix nulls in strings
  225.     X    grep (s/\0[\000-\377]*//, @ac[@sidx]);  
  226.     X    printf $mask, @ac[@indices];
  227.     X
  228.     X} 
  229. SHAR_EOF
  230. if test 1335 -ne "`wc -c < 'pacctf'`"
  231. then
  232.     echo shar: "error transmitting 'pacctf'" '(should have been 1335 characters)'
  233. fi
  234. chmod 775 'pacctf'
  235. fi
  236. exit 0
  237. #    End of shell archive
  238. -- 
  239.     Tom Christiansen      tchrist@convex.com      convex!tchrist
  240.  
  241.     "* Unix is a footnote of AT&T Bell Laboratories."
  242.                              --Barry Shein
  243.