Advertisement
Guest User

Untitled

a guest
Jul 29th, 2014
286
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.38 KB | None | 0 0
  1. #!/usr/bin/perl -w
  2. #
  3. # check_red5 - Nagios check plugin for Red5
  4. #
  5. # Copyright (c) 2013 Revolution Linux - Pascal Bernier <pb@rlnx.com>
  6. #
  7.  
  8. use POSIX;
  9. use Getopt::Long;
  10. use vars qw($opt_P $opt_V $opt_h $opt_H $opt_W $opt_a $verbose);
  11. use vars qw($PROGNAME);
  12. use lib "/usr/lib/nagios/plugins";
  13. use utils qw($TIMEOUT %ERRORS &print_revision &support &usage);
  14. use Socket qw(:all);
  15.  
  16. sub print_help();
  17. sub print_usage();
  18.  
  19. $PROGNAME = "check_red5";
  20.  
  21. $ENV{'PATH'}='';
  22. $ENV{'BASH_ENV'}='';
  23. $ENV{'ENV'}='';
  24.  
  25. Getopt::Long::Configure('bundling');
  26. GetOptions
  27. ("v" => \$verbose, "verbose" => \$verbose,
  28. "P=s" => \$opt_P, "port=s" => \$opt_P,
  29. "V" => \$opt_V, "version" => \$opt_V,
  30. "h" => \$opt_h, "help" => \$opt_h,
  31. "H=s" => \$opt_H, "host=s" => \$opt_H,);
  32.  
  33. if ($opt_V) {
  34. print_revision($PROGNAME,'0.1');
  35. exit $ERRORS{'OK'};
  36. }
  37.  
  38. if ($opt_h) {
  39. print_help();
  40. exit $ERRORS{'OK'};
  41. }
  42.  
  43. # Options checking
  44.  
  45. ($opt_H) || ($opt_H = shift) || usage("ERROR : Host name not specified\nUse -h for more info\n");
  46. my $host = $opt_H if ($opt_H =~ /^([-_.A-Za-z0-9 ]+\$?)$/);
  47. ($host) || usage("ERROR Invalid host: $opt_H\n");
  48.  
  49. ($opt_P) || ($opt_P = shift) || usage("ERROR : Port not specified\nUse -h for more info\n");
  50. my $port = $opt_P if (isdigit $opt_P);
  51. ($port) || usage("ERROR Invalid port: $opt_P\n");
  52.  
  53. my $packedip = gethostbyname($host) || usage("ERROR : Unable to resolve $host\n");
  54. #my $ip = inet_ntoa($packedip);
  55.  
  56. # End of options checking
  57.  
  58. my $state = "OK";
  59. my $answer = "RED5 OK\n";
  60.  
  61. # Just in case of problems, let's not hang Nagios
  62. $SIG{'ALRM'} = sub {
  63. $answer = "RED5 UNKNOWN - Bad arguments or no answer from client\n";
  64. $state = "UNKNOWN";
  65. print $answer;
  66. print "$state\n" if ($verbose);
  67. exit $ERRORS{$state};
  68. };
  69. alarm($TIMEOUT);
  70.  
  71. # Phase 1 open a socket
  72. my $address = sockaddr_in($port, $packedip);
  73. socket(SOCK, AF_INET, SOCK_STREAM, IPPROTO_TCP);
  74. my $connection = connect(SOCK, $address);
  75.  
  76. if (!defined $connection) {
  77. $state = "CRITICAL";
  78. $answer = "RED5 CRITICAL - Connection refused\n";
  79. }
  80.  
  81. # Phase 2 version handshake
  82. # See docs for handshake : https://www.adobe.com/content/dam/Adobe/en/devnet/rtmp/pdf/rtmp_specification_1.0.pdf
  83.  
  84. if ($connection) {
  85. my @c0 = qw(01);
  86. red5_send_hex(\@c0);
  87.  
  88. my @c1 = qw(00 00 00 00); # time - 4 bytes
  89. push @c1, qw(00 00 00 00); # zero - 4 bytes
  90. for (1..1528) { push @c1, '99';}
  91. red5_send_hex(\@c1);
  92.  
  93. my $s0 = red5_recv(1);
  94. if ($s0 ne "\x03") {
  95. $state = "CRITICAL";
  96. $answer = "RED5 CRTICIAL - Handshake failed\n";
  97. }
  98.  
  99. }
  100.  
  101. # Turn off alarm
  102. alarm(0);
  103.  
  104. print $answer;
  105. print "$state\n" if ($verbose);
  106.  
  107. # restart red5 if critical
  108. if ($state eq "CRITICAL") {
  109. system("/usr/bin/sudo /usr/local/bin/restart_red5.sh &");
  110. }
  111.  
  112. exit $ERRORS{$state};
  113.  
  114. sub red5_send_hex
  115. {
  116. my ($ref_hex_array) = @_;
  117. my $send_str = '';
  118. foreach (@$ref_hex_array)
  119. {
  120. my $one_byte = '0x'.$_;
  121. $send_str.= pack('C', oct($one_byte));
  122. }
  123. my $sent = syswrite(SOCK, $send_str);
  124. return $sent;
  125. }
  126.  
  127. sub red5_recv
  128. {
  129. my ($wanted_length, $time_out) = @_;
  130. return unless $wanted_length =~ /^[1-9][\d]*$/;
  131. $time_out = 3 unless $time_out;
  132.  
  133. my $start_time = time;
  134. my $bytes = '';
  135. while(1)
  136. {
  137. if (time - $start_time >= $time_out)
  138. {
  139. print "func: red5_recv(), timeout for $time_out Seconds\n";
  140. return '';
  141. }
  142. my $buf = '';
  143. my $success_length = sysread(SOCK, $buf, $wanted_length);
  144. next unless $success_length; # for no-block IO
  145. $bytes .= $buf;
  146. $wanted_length = $wanted_length - $success_length;
  147. last if $wanted_length <= 0;
  148. }
  149. return $bytes;
  150. }
  151.  
  152.  
  153. sub print_usage() {
  154. print "Usage: $PROGNAME -H <host> -P <port>\n";
  155. }
  156.  
  157. sub print_help() {
  158. print_revision($PROGNAME,'0.1');
  159. print "\nPerl Check RED5 plugin for Nagios\n";
  160. print "\nCopyright (c) Revolution Linux 2013 - Pascal Bernier <pb\@rlnx.com>\n\n";
  161. print_usage();
  162. print "\n-h, --help\n";
  163. print " Help\n";
  164. print "-V, --version\n";
  165. print " Version of this script\n";
  166. print "-H, --hostname=HOST\n";
  167. print " Hostname of the server\n";
  168. print "-P, --port=INTEGER\n";
  169. print " Port to be used to connect to.\n\n";
  170. #support();
  171. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement