#!/usr/bin/expect # ############################################################################### # This program is free software: you can redistribute it and/or modify # it under the terms of the GNU General Public License as published by # the Free Software Foundation, either version 3 of the License, or # (at your option) any later version. # # This program is distributed in the hope that it will be useful, # but WITHOUT ANY WARRANTY; without even the implied warranty of # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the # GNU General Public License for more details. # # You should have received a copy of the GNU General Public License # along with this program. If not, see . ############################################################################### ############################################################################### # Author: Viktor Kertesz (vkertesz2@gmail.com) ############################################################################### # Version: 1.1 ############################################################################### #OK 0, WARNING 1, CRITICAL 2, UNKNOWN 3, DEPENDENT4 if {$argc <1 || [lindex $argv 0] == "-h"} { puts "Help:\r" puts "check_srx_cluster {HOSTADDRESS} {prompt} {loginuser} {loginpassword}" exit } set remote_host [lindex $argv 0] set my_prompt [lindex $argv 1] set my_login [lindex $argv 2] set my_password [lindex $argv 3] # Number of retry in case of glitches set maxretries 3 set node0 "" set node1 "" set timeout 5 match_max 100000 log_user 0 spawn ssh $remote_host -l $my_login expect { timeout { puts "UNKNOWN - Connection timed out\n"; exit 3 } "assword:" { } } send -- "$my_password\r" expect $my_prompt sleep 0.5 send -- "show chassis cluster status\r"; set i 0 while { 1 } { expect { -re ".*(node\[0-1\])\ *(\[0-9\]+)\ *(\[a-z\]+).*" { switch $expect_out(1,string) { node0 { set node0 $expect_out(3,string); } node1 { set node1 $expect_out(3,string); } } } # No pagination with this command is needed # "=More" {send "\r"; } "$my_prompt" { #Check if node status is correctly recognized if { [string bytelength $node0] == 0 || [string bytelength $node1] == 0 } { incr i if { $i > $maxretries } { puts "UNKNOWN - Couldn't get cluster status\n"; exit 3} sleep 0.5 send -- "show chassis cluster status\r"; } else { break; } } timeout { incr i if { $i <= $maxretries } { # Retrying recognize prompt send -- "\n" } else { puts "UNKNOWN - Connection timed out\n"; exit 3 } } } } send -- "exit\r" #expect "closed" #State primary or secondary is normal. Everything else is problem. if { $node0 == "primary" || $node0 == "secondary" } { if { $node1 == "primary" || $node1 == "secondary" } { puts "OK - node0:$node0,node1:$node1\r\n" exit 0 } else { puts "WARNING - node0:$node0,node1:$node1\r\n" exit 1 } } else { puts "WARNING - node0:$node0,node1:$node1\r\n" exit 1 } puts "CRITICAL - node0:$node0,node1:$node1\r\n" exit 2