#!/usr/bin/env perl # Usage: glusterfsd-heartbeat # # Continously pings the GlusterFS server running on the given host at the given # port every given time period (in seconds), exiting if the connection is lost. # # Copyright (C) Geoff Kassel, 2007. Released under the LGPL. use strict; use IO::Socket; my ($host, $port, $period, $pingPacket, $line); $host = "localhost"; $port = "6996"; $period = 1; if (@ARGV == 1) { ($host) = @ARGV; } if (@ARGV == 2) { ($host, $port) = @ARGV; } if (@ARGV == 3) { ($host, $port, $period) = @ARGV; } if (@ARGV > 3) { die "Usage: $0 "; } # Try to open a connection to the server. my $sock = new IO::Socket::INET ( PeerAddr => $host, PeerPort => $port, Proto => 'tcp', ); die "Could not connect to GlusterFS server $host:$port: $!\n" unless $sock; # Set synchronous communication. $sock->autoflush(1); # Synchronous communication. # Generate ping packet to send. $pingPacket = "Block Start\n"; # Start of protocol $pingPacket .= "0000000000000042\n"; # Stream ID - 42 will do. $pingPacket .= "00000001\n"; # GF_OP_TYPE_MOP_REQUEST - management request. $pingPacket .= "00000002\n"; # GF_MOP_STATS - get server stats. $pingPacket .= "Ping!---------------------------\n"; # Description of packet. $pingPacket .= "00000000000000000000000000000022\n"; # Message contents size. # Start of message contents - serialized dictionary type. (Total size 35 - 1.) $pingPacket .= "00000001\n"; # Number of keys in message dictionary. (len: 9) $pingPacket .= "00000006:"; # Key length (incl. null) + ':' (len: 9) $pingPacket .= "00000002\n"; # Key value length (incl. null.) (len: 18) $pingPacket .= "FLAGS\00\0"; # Dummy dictionary entry. (len: 8) # End of message contents. $pingPacket .= "Block End\n"; # End of protocol # Send an initial ping packet, before starting the heartbeat loop. print $sock $pingPacket; # Loop until the socket closes. while (<$sock>) { # Read in everything sent. while (defined ($line = <$sock>)) { #print STDOUT $line; # Have we now read a full response message? if ($line =~ /Block End/) { # Sleep for the given time period, then send another ping. sleep $period; print $sock $pingPacket; } } } close($sock); die "GlusterFS server $host:$port has died!\n";