Beefy Boxes and Bandwidth Generously Provided by pair Networks
XP is just a number

Quick 'n' dirty plotting

by Mur (Pilgrim)
on Nov 12, 2002 at 18:34 UTC ( #212348=snippet: print w/replies, xml ) Need Help??
Description: Recently I needed to plot the output of another program, which was recording minute-by-minute values in a CSV file. What I wanted to do was to 'tail' this file and display a plot of the data. Having recently "mastered" Term::Screen for another application involving a continuously updated display, I figured this would be a piece of cake. The code is very simple, and not entirely complete (it still burps some sort of error message as it starts and as it exits, but I haven't had an inspiration as to what's causing the error). It plots data that scrolls down the page with new values appearing at the top (not the natural way to do things, but Term::Screen only has a method to insert a line which moves the rest of the screen down).
#!/usr/bin/perl -w
# Quick 'n' dirty plotting of the free.csv report.

use strict;
use English;

  # Argument processing.
  if (@ARGV == 2 and $ARGV[1] =~ /\D/) {
    # 123 file.csv
    # Yuck, because the 'missing' value could be either top limit.
    die "Arguments '@ARGV' too hard to interpret:  use 'NNN NNN file.c

  } elsif (@ARGV == 1 and $ARGV[0] =~ /\D/) {
    # file.csv
    # Stick in a couple of empty strings to get the TOP_* constants to
+ work.
    unshift @ARGV, '','';

my $screen;            # screen control panel

use constant TERM_SIZE => 75;    # how big is the screen?
use constant TIME_FLD  =>  6;      # how wide is the time display?
use constant TOP_LOAD => shift || 20;        # scale factor for load a
use constant TOP_BUFF => shift || 20000;    # scale factor for buffer 

    no warnings;
    require Term::Screen;

  # Set up the screen.
  $screen = Term::Screen->new() or die "Something's wrong with Term::S
                     '|----' x (TERM_SIZE/5),
                     '*/L' . TOP_LOAD,
                     ' o/F+/B' . TOP_BUFF,
                    ) );

  # Cleanup.
  $screen->at($screen->{ROWS}-1, 0);

$|++;                # no buffered output

# Ctrl-C handler.
$SIG{INT} = sub { exit };

# Do we have a filename to read?
if (my $file = shift) {
  open(INPUT, $file) or die $!;
} else {
  open(INPUT, "<&STDIN") or die $!;

while (<INPUT>) {        # process all the input

                # CSV file has 'time,loadavg,junk,buff,...'
  my($time,$load,$buff,$free) = (split /,/)[0,1,3,10];
  next if ($load . $buff . $free) =~ /[^\d\.]/;

  $screen->at(1,0)->il;        # new line after the axis

  $time =~ s/\D//g;        # reduce time to HHMMSS
  $time =~ s/.*(\d{6})$/$1/;

  $_ ||= 0E0              # for old format CSV with missing fields
    foreach ($load,$buff,$free);

                # create scaling
  my $load_sp = min(TERM_SIZE, TERM_SIZE * ($load/TOP_LOAD));
  my $buff_sp = min(TERM_SIZE, TERM_SIZE * ($buff/TOP_BUFF));
  my $free_sp = min(TERM_SIZE, TERM_SIZE * ($free/TOP_BUFF));

  if ($load_sp == $buff_sp) {
    # The overlap case.
  } else {
    # The normal case.
    if ($buff_sp == $free_sp) {
    } else {

sub min {
  my @list = grep(defined,@_);
  return $list[0] unless @list > 1;
  return (sort {$a <=> $b} @list)[0];
Replies are listed 'Best First'.
Re: Quick 'n' dirty plotting
by zentara (Archbishop) on Nov 13, 2002 at 14:52 UTC
    This needs a way to quit the program, even control-c dosn't kill it.
      That's odd, it works for me.
      $SIG{INT} = sub { exit };
      is the intended method.
      Jeff Boes
      Database Engineer
      Nexcerpt, Inc.
      vox 269.226.9550 ext 24
      fax 269.349.9076
      ...Nexcerpt...Connecting People With Expertise
Re: Quick 'n' dirty plotting
by petral (Curate) on Nov 13, 2002 at 17:27 UTC
    Of course, if the display should ever need to wait for the data recorder, you'll also want something like File::Tail or Event::File::Tail.

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: snippet [id://212348]
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others studying the Monastery: (5)
As of 2020-07-12 10:18 GMT
Find Nodes?
    Voting Booth?

    No recent polls found