Guest User

Untitled

a guest
Jun 4th, 2012
32
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.27 KB | None | 0 0
  1. package IO::Socket::StatusPublisher;
  2. use strict;
  3. use warnings;
  4. use IO::Socket;
  5.  
  6. sub help {
  7. die "Usage: $0 process status [weight] [hostname]\n"
  8. . "\tprocess: name of process (http,dmgr,etc)\n"
  9. . "\tstatus: status of process (start[ed],run[ning],stop[ped],term[inated])\n"
  10. . "\tweight: (optional) number for size of the pie slice. defaults to 0\n"
  11. . "\thostname: (optional) defaults to `hostname`\n"
  12. }
  13.  
  14. my $process = $ARGV[0] || help ;
  15. my $status = $ARGV[1] || help ;
  16. my $weight = $ARGV[2] || 0;
  17. my $hostname = $ARGV[3] || `hostname`;
  18.  
  19. sub new {
  20. my ($class, %args ) = @_;
  21. my $self = bless ({}, ref ($class) || $class);
  22. my $host = $args{host};
  23. my $port = $args{port};
  24. $self->connect ( $host, $port );
  25. return $self;
  26. }
  27.  
  28. sub connect {
  29. my ( $self, $host, $port ) = @_;
  30. $self->close;
  31. $self->{connection} = IO::Socket::INET->new ( PeerAddr => "$host:$port" );
  32. $self->{connection} && $self->{connection}->connected ||
  33. die "Could not connect to status dashboard $host $port" ;
  34. my ( $myport, $myaddr ) = sockaddr_in ( $self->{connection}->sockname );
  35.  
  36. return ( inet_ntoa ( $myaddr ), $myport );
  37. }
  38.  
  39. sub status {
  40. my ( $self, $process, $status, $weight, $hostname ) = @_;
  41. my $conn = $self->{connection};
  42. my $skey = "status:hostnames";
  43. my $hkey = "status:hostname:" . $process;
  44. my $zkey = "status:hostname:" . $process . ":weight";
  45. my $hkeyl = length $hkey;
  46. my $skeyl = length $skey;
  47. my $zkeyl = length $zkey;
  48. my $processl = length $process;
  49. my $statusl = length $status;
  50. my $weightl = length $weight;
  51. my $hostnamel = length $hostname;
  52. my $select = "*2\r\n\$6\r\nselect\r\n\$1\r\n9\r\n";
  53. my $sadd = "*3\r\n\$4\r\nsadd\r\n\$$skeyl\r\n$skey\r\n\$$hostnamel\r\n$hostname\r\n";
  54. my $hset = "*4\r\n\$4\r\nhset\r\n\$$hkeyl\r\n$hkey\r\n\$$processl\r\n$process\r\n\$$statusl\r\n$status\r\n";
  55. my $zadd = "*4\r\n\$4\r\nzadd\r\n\$$zkeyl\r\n$zkey\r\n\$$weightl\r\n$weight\r\n\$$processl\r\n$process\r\n";
  56. $conn->print ( $select . $sadd . $hset . $zadd );
  57.  
  58. return $self;
  59. }
  60.  
  61. sub close {
  62. my $self = shift;
  63. my $conn = $self->{connection};
  64.  
  65. return $conn->close if $conn && $conn->connected;
  66. }
  67.  
  68. IO::Socket::StatusPublisher
  69. ->new( ( "host", "localhost", "port", 6379 ) )
  70. ->status( $process, $status, $weight, chomp($hostname) )
  71. ->close;
Advertisement
Add Comment
Please, Sign In to add comment