Wrapping prstat on Solaris

Discussion in 'Python' started by skip@pobox.com, Jul 27, 2009.

  1. Guest

    At work we currently use top to monitor ongoing system utilization on our
    Solaris systems. As time has moved on though, use of top has become
    problematic. Our admins want us to switch to prstat, Sun's top-like
    command. It works fine however doesn't emit a timestamp at each display
    interval, so it's essentially impossible looking at a prstat output
    file to determine when the particular "screen" was emitted.

    If figured, "No problem. I'll just write a little wrapper." Alas, that is
    proving harder than I thought. I can run prstat directing output to a file
    and it seems to blast out a block of lines every N seconds, but when run
    from inside a Python script I can't seem to make the damn thing not
    massively buffer its output. Accordingly, my script doesn't really see the
    output as its emitted, so the timestamp line it prepends to a block of
    output is off.

    I'm currently using subprocess.Popen like so:

    os.environ["TERM"] = "dumb"
    cmd = "prstat -c %s" % " ".join(sys.argv[1:])
    pipe = Popen(cmd, shell=True, bufsize=1, stdout=PIPE).stdout

    I've tried these other variants as well:

    * prefacing the prstat command with unbuffer (the tcl/expect thingamabob)

    * explicitly redirect the prstat output to /dev/stdout

    * setting bufsize to 0

    * used os.popen instead of Subprocess.Popen

    Nothing seems to help. I always seem to see about 30 seconds of data at
    once (I'm currently using a 5-second interval and 10 lines of output). I
    would have expected that prstat would simply flush stdout after each block
    of output.

    Any ideas about how to get prstat to cooperate better?

    Thanks,

    --
    Skip Montanaro - - http://www.smontanaro.net/
    That's more than a dress. That's an Audrey Hepburn movie. -- Jerry Maguire
     
    , Jul 27, 2009
    #1
    1. Advertising

  2. schrieb:
    > At work we currently use top to monitor ongoing system utilization on our
    > Solaris systems. As time has moved on though, use of top has become
    > problematic. Our admins want us to switch to prstat, Sun's top-like
    > command. It works fine however doesn't emit a timestamp at each display
    > interval, so it's essentially impossible looking at a prstat output
    > file to determine when the particular "screen" was emitted.
    >
    > If figured, "No problem. I'll just write a little wrapper." Alas, that is
    > proving harder than I thought. I can run prstat directing output to a file
    > and it seems to blast out a block of lines every N seconds, but when run
    > from inside a Python script I can't seem to make the damn thing not
    > massively buffer its output. Accordingly, my script doesn't really see the
    > output as its emitted, so the timestamp line it prepends to a block of
    > output is off.
    >
    > I'm currently using subprocess.Popen like so:
    >
    > os.environ["TERM"] = "dumb"
    > cmd = "prstat -c %s" % " ".join(sys.argv[1:])
    > pipe = Popen(cmd, shell=True, bufsize=1, stdout=PIPE).stdout
    >
    > I've tried these other variants as well:
    >
    > * prefacing the prstat command with unbuffer (the tcl/expect thingamabob)
    >
    > * explicitly redirect the prstat output to /dev/stdout
    >
    > * setting bufsize to 0
    >
    > * used os.popen instead of Subprocess.Popen
    >
    > Nothing seems to help. I always seem to see about 30 seconds of data at
    > once (I'm currently using a 5-second interval and 10 lines of output). I
    > would have expected that prstat would simply flush stdout after each block
    > of output.
    >
    > Any ideas about how to get prstat to cooperate better?


    Use pexpect, which will make the program believe it runs in a terminal,
    and not buffer the output.

    Diez
     
    Diez B. Roggisch, Jul 27, 2009
    #2
    1. Advertising

  3. ryles Guest

    On Jul 27, 10:26 am, wrote:
    > At work we currently use top to monitor ongoing system utilization on our
    > Solaris systems.  As time has moved on though, use of top has become
    > problematic.  Our admins want us to switch to prstat, Sun's top-like
    > command.  It works fine however doesn't emit a timestamp at each display
    > interval, so it's essentially impossible looking at a prstat output
    > file to determine when the particular "screen" was emitted.
    >
    > If figured, "No problem.  I'll just write a little wrapper."  Alas, that is
    > proving harder than I thought.  I can run prstat directing output to a file
    > and it seems to blast out a block of lines every N seconds, but when run
    > from inside a Python script I can't seem to make the damn thing not
    > massively buffer its output.  Accordingly, my script doesn't really see the
    > output as its emitted, so the timestamp line it prepends to a block of
    > output is off.
    >
    > I'm currently using subprocess.Popen like so:
    >
    >     os.environ["TERM"] = "dumb"
    >     cmd = "prstat -c %s" % " ".join(sys.argv[1:])
    >     pipe = Popen(cmd, shell=True, bufsize=1, stdout=PIPE).stdout
    >
    > I've tried these other variants as well:
    >
    >   * prefacing the prstat command with unbuffer (the tcl/expect thingamabob)
    >
    >   * explicitly redirect the prstat output to /dev/stdout
    >
    >   * setting bufsize to 0
    >
    >   * used os.popen instead of Subprocess.Popen
    >
    > Nothing seems to help.  I always seem to see about 30 seconds of data at
    > once (I'm currently using a 5-second interval and 10 lines of output).  I
    > would have expected that prstat would simply flush stdout after each block
    > of output.
    >
    > Any ideas about how to get prstat to cooperate better?
    >
    > Thanks,
    >
    > --
    > Skip Montanaro - -http://www.smontanaro.net/
    >     That's more than a dress. That's an Audrey Hepburn movie. -- Jerry Maguire


    Hey Skip,

    You haven't told us how you are actually reading from prstat's output
    pipe, which may be the cause. For instance, if you are doing

    for line in pipe:
    print line
    ...

    then this could cause your buffering issue.

    Instead try using readline():

    while True:
    line = pipe.readline()
    ...
     
    ryles, Jul 28, 2009
    #3

  4. > You haven't told us how you are actually reading from prstat's output
    > pipe, which may be the cause. For instance, if you are doing
    >
    > for line in pipe:
    > print line
    > ...
    >
    > then this could cause your buffering issue.
    >
    > Instead try using readline():
    >
    > while True:
    > line = pipe.readline()
    > ...


    Yes, I've been using for line in pipe. Switching to your construct
    seems to work like a charm.

    Thanks,

    Skip
     
    Skip Montanaro, Aug 2, 2009
    #4
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Michael Dyremo

    Wrapping old ASP with ASP.NET

    Michael Dyremo, Aug 8, 2003, in forum: ASP .Net
    Replies:
    3
    Views:
    418
    Yan-Hong Huang[MSFT]
    Aug 12, 2003
  2. Mike Casey
    Replies:
    7
    Views:
    3,954
    Scott M.
    Dec 19, 2003
  3. Scott
    Replies:
    1
    Views:
    382
    Andrew de la Harpe
    Jan 8, 2004
  4. =?Utf-8?B?bWc=?=

    Line wrapping with WebForm Label

    =?Utf-8?B?bWc=?=, Jan 19, 2004, in forum: ASP .Net
    Replies:
    1
    Views:
    403
    =?Utf-8?B?QmFycnkgRm9ycmVzdA==?=
    Jan 19, 2004
  5. Steve
    Replies:
    3
    Views:
    4,174
    Steve
    Jan 22, 2004
Loading...

Share This Page