also sprach Craig Small <[email protected]> [2009.04.27.0322 +0200]:
> I had a look at some old bugs I had for psmisc and saw the pstat one.
> The problem is the url you had in the bug gives permission denied.

Yeah, I finally killed my SVN. Sorry.

> >From what I remember of it, the program was written in perl or python
> which was one of the problems for me as the rest of them are C.  I was
> thinking of re-writing it in C.

/bin/bash

> Do you know how closely pstat worked like its namesakes in AIX or BSD?
> Does it do the same thing?

Probably not at all, since I just coded it up from scratch to parse
/proc/(\d+)/stat for a given value of $1, e.g.

piper:~|master|% .bin/pstat $$                                                  
                  #10015
         pid: 18615                                         comm: (zsh)
       state: R                                             ppid: 18656
        pgrp: 18615                                      session: 18615
      tty_nr: 34826                                        tpgid: 19701
       flags: 0x402000                                    minflt: 7935
     cminflt: 84399                                       majflt: 1
     cmajflt: 2                                            utime: 30
       stime: 20                                          cutime: 362
      cstime: 45                                        priority: 25
        nice: 5                                             zero: 1
 itrealvalue: 0                                        starttime: 100517735
       vsize: 44507136                                       rss: 1342
        rlim: 18446744073709551615                     startcode: 0x400000
     endcode: 0x4a0934                                startstack: 0x7fffe47dcee0
     kstkesp: 0xffffffffffffffff                         kstkeip: 0x7f0ddba379cf
      signal: 0x0                                        blocked: 0x10000
   sigignore: 0x384004                                  sigcatch: 0x8012003
       wchan: 0                                            nswap: 0
      cnswap: 0                                      exit_signal: 17
   processor: 0                                      rt_priority: 0
      policy: 0                                     delayacct_blkio_ticks: 0
  guest_time: 0                                     cguqest_time: 0

I've attached the latest version. I am sure this could be
implemented differently and trivially, since all it does is
basically label words in /proc/*/stat and print a formatted output.

-- 
 .''`.   martin f. krafft <[email protected]>      Related projects:
: :'  :  proud Debian developer               http://debiansystem.info
`. `'`   http://people.debian.org/~madduck    http://vcs-pkg.org
  `-  Debian - when you have better things to do than fixing systems
#!/bin/bash
#
# pstat -- a simple parser for /proc/*/stat information
#
# Copyright © martin f. krafft <[email protected]>
# Released under the terms of the Artistic Licence 2.0
#
set -eu

PROCNAME=${0##*/}
VERSION=0.1.3

if [[ ! -f /proc/stat ]]; then
  cat <<EOF >&2
E: /proc has not been mounted. Please run:
     mount -t proc none /proc
EOF
  exit -1
fi

usage() {
  cat <<EOF >&2
$PROCNAME $VERSION -- a simple parser for /proc/*/stat information

Usage:
  $PROCNAME [options] pid|name

  This will parse /proc/\$pid/stat and format it for easy post-processing
  by human beings. For example:
    $PROCNAME $$
    $PROCNAME zsh

  If a process name is specified rather than a PID, the pidof(8) is used to
  find the PID of the first process by that name.

  The following options may be specified, using standard conventions:

    -1 | --single               Use only one column to output
    -s | --simple               Output optimised for parsing
                                (implies --single)
    -h | --help                 Display this help text

$PROCNAME is (c) 2004 by martin f. krafft <[email protected]>.

The programme has been published under the terms of the Artistic Licence.
Please see http://www.opensource.org/licenses/artistic-license.php for
more information. On Debian systems, you may find the text of the licence in
/usr/share/common-licenses/Artistic as well.
EOF
}

SINGLE= PID= SIMPLE=

SHORTOPTS=h1s
LONGOPTS=help,single,simple
for opt in $(getopt -o $SHORTOPTS -l $LONGOPTS --n $PROCNAME -- $@); do
  opt=${opt//\'/}
  case $opt in
    -1|--single) SINGLE=1;;
    -s|--simple) SIMPLE=1; SINGLE=1;;
    -h|--help) usage; exit 0;;
    --) continue;;
    *) 
      if [[ -n $PID ]]; then
        echo E: unknown argument: $opt >&2
        usage
        exit -1
      else
        case $opt in
          [0-9]*) PID=$opt;;
          *)
            if ! PID=$(pidof -sx $opt); then
              echo E: no process named $opt exists. >&2
              exit 1
            fi;;
        esac
      fi;;
  esac
done

if [[ -z $PID ]]; then
  usage
  exit -1
fi

STATFILE=/proc/$PID/stat

if [[ ! -f $STATFILE ]]; then
  echo E: no process with PID $PID exists. >&2
  exit 1
fi

ITEMS=(pid,d comm,s state,c ppid,d pgrp,d session,d tty_nr,d tpgid,d
  flags,x minflt,lu cminflt,lu majflt,lu cmajflt,lu utime,lu stime,lu
  cutime,ld cstime,ld priority,ld nice,ld zero,ld itrealvalue,ld starttime,lu
  vsize,lu rss,ld rlim,lu startcode,x endcode,x startstack,x kstkesp,x
  kstkeip,x signal,x blocked,x sigignore,x sigcatch,x wchan,lu nswap,lu
  cnswap,lu exit_signal,d processor,d rt_priority,u policy,u
  delayacct_blkio_ticks,llu guest_time,lu cguqest_time,ld)

set -- $(<$STATFILE)

format_item() {
  [[ -z $2 ]] && return
  [[ $2 = x ]] && PREFIX=0x
  [[ $SIMPLE != 1 ]] && WIDTH=12
  printf "%${WIDTH:-0}s: %s%$2" "$1" "${PREFIX:-}" "$3"
}

TERMWIDTH=($(stty size))
COLSIZE=$((${TERMWIDTH[1]} / 2 - 1))

for item in ${ite...@]}; do
  fmt=${item#*,}
  item=${item%,*}
  if [[ $SINGLE = 1 ]]; then
    format_item $item $fmt $1; shift
    echo
  else
    if [[ -z ${a:-} ]]; then
      a=$(format_item $item $fmt $1); shift
    else
      b=$(format_item $item $fmt $1); shift
      printf "%-${COLSIZE}s %-${COLSIZE}s\n" "$a" "$b"
      unset a
    fi
  fi
done
[[ $SINGLE != 1 ]] && [[ -n ${a:-} ]] && echo "$a"

exit 0

Attachment: digital_signature_gpg.asc
Description: Digital signature (see http://martin-krafft.net/gpg/)

Reply via email to