Beefy Boxes and Bandwidth Generously Provided by pair Networks
Pathologically Eclectic Rubbish Lister
 
PerlMonks  

Comment on

( #3333=superdoc: print w/ replies, xml ) Need Help??
Dear brethren,

I'm looking for an elegant way of reading data from a file on one hand, and serve it (preferably through a websocket) on the other hand. So far I managed to solve elegantly the problem of reading the output from several programs simultaneously by using on of the big powers of Unix : named pipes. However now I'm stuck on the next part: serving data. As reading from a pipe is blocking, and so is listening to a socket, I don't know how to get out of this.

So here's the code:

#!/usr/bin/perl use strict; use warnings; use Data::Dumper; use Net::WebSocket::Server; use JSON; my %data; my $json; # Net::WebSocket::Server->new( # listen => 8080, # on_connect => sub { # my ($serv, $conn) = @_; # $conn->on( # handshake => sub { # my ($conn, $handshake) = @_; # }, # utf8 => sub { # my ($conn) = @_; # $_->send_utf8($json) for $conn->server->connections; # }, # ); # }, # )->start; open my $fh, '<', 'toto1' or die "can't open fifo toto1: $?"; while ( my $line = <$fh> ) { my @list = ( split /\s+/, $line ); # skip empty lines next if $#list <= 1; shift @list if $list[0] eq ''; # test for iostat header next if $list[0] eq 'avg-cpu:'; next if $list[0] eq 'Device:'; # test for vmstat header next if $list[0] eq 'procs'; if ( $#list == 11 ) { ( undef, $data{io}{ $list[0] }{rrqms}, $data{io}{ $list[0] }{wrqms}, $data{io}{ $list[0] }{reads}, $data{io}{ $list[0] }{writes}, $data{io}{ $list[0] }{rsecs}, $data{io}{ $list[0] }{wsecs}, $data{io}{ $list[0] }{avgrqsz}, $data{io}{ $list[0] }{avgqusz}, $data{io}{ $list[0] }{await}, $data{io}{ $list[0] }{svctm}, $data{io}{ $list[0] }{util} ) = @list; } elsif ( $#list == 15 ) { ( $data{vm}{r}, $data{vm}{b}, $data{vm}{swpd}, $data{vm}{free}, $data{vm}{buff}, $data{vm}{cache}, $data{vm}{swapin}, $data{vm}{swapout}, $data{vm}{ioin}, $data{vm}{ioout}, $data{vm}{sysin}, $data{vm}{syscs}, $data{vm}{user}, $data{vm}{sys}, $data{vm}{idle}, $data{vm}{wait} ) = @list; } elsif ( $#list == 5 ) { $data{vm}{nice} = $list[1]; } $json = encode_json( \%data); sleep 1; }

Of course the script work by commenting out one of the two loops, either the websocket one, or the file reading one. To use this script, create a named pipe

mkfifo toto1

run continuously vmstat and/or iostat and redirect their output to the pipe:

vmstat 3 |tee toto1

and

iostat -x 3 | tee toto1.

I don't know how to get any further from there... maybe golang? :)


In reply to Simultaneously reading from a file and serving data by wazoox

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post; it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.
  • Log In?
    Username:
    Password:

    What's my password?
    Create A New User
    Chatterbox?
    and the web crawler heard nothing...

    How do I use this? | Other CB clients
    Other Users?
    Others making s'mores by the fire in the courtyard of the Monastery: (21)
    As of 2015-07-02 13:24 GMT
    Sections?
    Information?
    Find Nodes?
    Leftovers?
      Voting Booth?

      The top three priorities of my open tasks are (in descending order of likelihood to be worked on) ...









      Results (39 votes), past polls