1. #!/usr/bin/perl -w
  2.  
  3. ####################################################
  4. # check_heartbeat_link v0.1.1 #
  5. # by Brandon Lee Poyner bpoyner / CCAC.edu #
  6. ####################################################
  7.  
  8. use strict;
  9. use File::Basename;
  10. use Getopt::Long;
  11. use Sys::Hostname;
  12.  
  13. my $prog_name=basename($0);
  14. my $prog_revision='0.1.1';
  15. my ($debug_mode);
  16. my $hb_node = hostname;
  17. my $cl_status = "/usr/bin/cl_status";
  18. my @nodes;
  19. my (%node, %if, %exclude);
  20.  
  21. my %errorcodes = (
  22. 'OK' => { 'value' => 0 },
  23. 'WARNING' => { 'value' => 1 },
  24. 'CRITICAL' => { 'value' => 2 },
  25. 'UNKNOWN' => { 'value' => 3 }
  26. );
  27.  
  28. &parse_options;
  29. &check_cl_status;
  30. &check_heartbeat_status;
  31. &find_nodes;
  32. &find_links;
  33. &check_links;
  34. &myexit('UNKNOWN',"$prog_name should never reach here");
  35.  
  36. sub print_usage {
  37. print <<EOF
  38. Usage: $prog_name [ -p path ] [-n node name ] [ -x exclude node ] [--debug]
  39. Options:
  40. -n STRING [ Hostname of our node. ]
  41. -p STRING [ Path to cl_status. Default: $cl_status ]
  42. -x STRING [ Exclude hostname from check ]
  43. EOF
  44. }
  45.  
  46. sub print_revision {
  47. print <<EOF;
  48. $prog_name $prog_revision
  49.  
  50. The nagios plugins come with ABSOLUTELY NO WARRANTY. You may redistribute
  51. copies of the plugins under the terms of the GNU General Public License.
  52. For more information about these matters, see the file named COPYING.
  53. EOF
  54.  
  55. }
  56.  
  57. sub print_help {
  58. &print_revision;
  59. print "\n";
  60. &print_usage;
  61. print <<EOF;
  62.  
  63. Send email to nagios-users\@lists.sourceforge.net if you have questions
  64. regarding use of this software. To submit patches or suggest improvements,
  65. send email to nagiosplug-devel\@lists.sourceforge.net
  66. EOF
  67. exit $errorcodes{'UNKNOWN'}->{'value'};
  68. }
  69.  
  70. sub parse_options {
  71. my ($help, $version, $debug, $exclude_node, @exclude);
  72. #
  73. # Get command line options
  74. #
  75. GetOptions("h|help" => \$help,
  76. "V|version" => \$version,
  77. "n|node=s" => \$hb_node,
  78. "p|path=s" => \$cl_status,
  79. "x|exclude=s" => \$exclude_node,
  80. "debug" => \$debug);
  81. if (defined($help) && ($help ne "")) {
  82. &print_help;
  83. exit $errorcodes{'UNKNOWN'}->{'value'};
  84. }
  85. if (defined($version) && ($version ne "")) {
  86. &print_revision;
  87. exit $errorcodes{'UNKNOWN'}->{'value'};
  88. }
  89. if (defined($exclude_node) && ($exclude_node ne "")) {
  90. @exclude=split(/,/,$exclude_node);
  91. for my $i ( @exclude ) {
  92. $exclude{$i} = 1;
  93. }
  94. }
  95. if (defined($debug) && ($debug ne "")) {
  96. #
  97. # Debugging information
  98. #
  99. $debug_mode=1;
  100. print STDERR "<$prog_name settings>\n";
  101. printf STDERR "Heartbeat Node: %s\n", defined($hb_node)?$hb_node:"";
  102. printf STDERR "Heartbeat Exclude Node: %s\n", @exclude?join(" ",@exclude):"";
  103. printf STDERR "Path to cl_status: %s\n", defined($cl_status)?$cl_status:"";
  104. print STDERR "</$prog_name settings>\n";
  105. }
  106. }
  107.  
  108. sub check_cl_status {
  109. #
  110. # Bail out if cl_status is not executable
  111. #
  112. if ( ! -x "$cl_status" ) {
  113. &myexit('CRITICAL',"$prog_name could not execute $cl_status");
  114. }
  115. }
  116.  
  117. sub check_heartbeat_status {
  118. #
  119. # Check to see if heartbeat is running
  120. #
  121. my ($result);
  122. open(CL,"$cl_status hbstatus|") || &myexit('CRITICAL',"Could not open $cl_status");
  123. while(<CL>) {
  124. chop($_);
  125. $result .= $_;
  126. }
  127. close(CL);
  128. if ($? > 0) {
  129. &myexit('CRITICAL',sprintf("%s",defined($result)?$result:"Unknown error"));
  130. }
  131. }
  132.  
  133. sub find_nodes {
  134. #
  135. # Find all nodes that are not ourself and not on the exclude list
  136. #
  137. my $self;
  138. my @exclude;
  139. open(CL,"$cl_status listnodes|") || &myexit('CRITICAL',"Could not open $cl_status");
  140. while(<CL>) {
  141. chop($_);
  142. if (defined($exclude{$_})) {
  143. push(@exclude,$_);
  144. } elsif ($_ ne $hb_node) {
  145. push(@nodes,$_);
  146. } else {
  147. $self = $_;
  148. }
  149. }
  150. close(CL);
  151. if ((defined($debug_mode)) && ($debug_mode == 1)) {
  152. #
  153. # Debugging information
  154. #
  155. print STDERR "<$prog_name nodes>\n";
  156. printf STDERR "Heartbeat Exclude Nodes: %s\n", @exclude?join(" ",sort(@exclude)):"";
  157. printf STDERR "Heartbeat External Nodes: %s\n", @nodes?join(" ",sort(@nodes)):"";
  158. printf STDERR "Heartbeat Internal Nodes: %s\n", defined($self)?$self:"";
  159. print STDERR "</$prog_name nodes>\n";
  160. }
  161. if (!(@nodes)) {
  162. &myexit('CRITICAL',"No other nodes found");
  163. }
  164. }
  165.  
  166. sub find_links {
  167. #
  168. # For nodes we wish to check, find all available links
  169. #
  170. for my $node (@nodes) {
  171. my $count = 0;
  172. open(CL,"$cl_status listhblinks $node|") || &myexit('CRITICAL',"Could not open $cl_status");
  173. while(<CL>) {
  174. if (/^\s*(.*)/) {
  175. $if{$node}{$1} = $1;
  176. $node{$node} = $node;
  177. $count++;
  178. }
  179. }
  180. close(CL);
  181. }
  182. if ((defined($debug_mode)) && ($debug_mode == 1)) {
  183. #
  184. # Debugging information
  185. #
  186. print STDERR "<$prog_name interfaces>\n";
  187. for my $key ( values %node ) {
  188. for my $key2 ( keys %{$if{$key}} ) {
  189. print STDERR "$key $key2\n";
  190. }
  191. }
  192. print STDERR "</$prog_name interfaces>\n";
  193. }
  194. }
  195.  
  196. sub check_links {
  197. #
  198. # Now that we have nodes and link types, check them.
  199. #
  200. my ($result);
  201. my ($critical)=0;
  202. for my $key ( values %node ) {
  203. for my $key2 ( keys %{$if{$key}} ) {
  204. open(CL,"$cl_status hblinkstatus $key $key2|") || &myexit('CRITICAL',"Could not open $cl_status");
  205. while(<CL>) {
  206. chop($_);
  207. $result .= "$key:$key2:$_ ";
  208. }
  209. close(CL);
  210. if ($? > 0) {
  211. $critical = 1;
  212. }
  213. }
  214. }
  215. if ($critical == 1) {
  216. &myexit('CRITICAL',"$result");
  217. } else {
  218. &myexit('OK',"$result");
  219. }
  220. }
  221.  
  222. sub myexit {
  223. #
  224. # Print status message and exit
  225. #
  226. my ($error, $message) = @_;
  227. if (!(defined($errorcodes{$error}))) {
  228. printf STDERR "Error code $error not known\n";
  229. print "Heartbeat Link UNKNOWN: $message\n";
  230. exit $errorcodes{'UNKNOWN'}->{'value'};
  231. }
  232. print "Heartbeat Link $error: $message\n";
  233. exit $errorcodes{$error}->{'value'};
  234. }
  235.