Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- use Downtime::Monitor;
- my sub keep-line(Str $line) { $line !~~ /^PING/ }
- my sub parse-line(Str $line) {
- given $line {
- when /time\=([\d|\.]+)/ { Duration.new($0.Real / 1000) }
- when /timeout/ { "error: timeout" }
- default { "error: $_" }
- }
- }
- class PingMonitor does Monitor {
- has Supply $.events;
- has Str @!args;
- has Proc::Async $!proc;
- method new(Str $host, Duration $interval, Duration $timeout) {
- my @args = (
- "-i", $interval.Str,
- "-W", ($timeout * 1000).Str,
- $host,
- );
- self.bless(:@args);
- }
- submethod BUILD(:@!args) {
- $!events = Supply.new;
- }
- method start {
- $!proc = Proc::Async.new("stdbuf", "-oL", "-eL", "ping", @!args);
- $!proc.stdout.grep(&keep-line).act: { $!events.emit(parse-line($_)) };
- $!proc.start;
- }
- method stop {
- $!proc.kill;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement