-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathrundeck-healthcheck.sh
executable file
·66 lines (54 loc) · 2.35 KB
/
rundeck-healthcheck.sh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
#!/bin/bash
##################
# Rundeck healthcheck + takeover script
#
# This runs a health check, and then takes over all jobs
# when the other server goes down.
# Note: Use this only with Rundeck in cluster mode!
#
##################
# Edit the following section:
# See http://rundeck.org/2.6.9/api/index.html#token-authentication for token authentication
API_TOKEN="somesecretstring"
# Detect and set hostnames based on own hostname
### This script assumes you have two servers.
### You can set the two hostnames/IPs below, and run the same script on both servers
### without any changes, and the script will detect and set the correct IPs
if [ `hostname -I` == "IP_of_First_Server" ]
then
OWN_IP="IP_of_First_Server"
REMOTE_IP="IP_of_Second_Server"
else
REMOTE_IP="IP_of_First_Server"
OWN_IP="IP_of_Second_Server"
fi
##################
# Editable section ends
##################
# Set up logging
LOG="/var/log/rundeck-healthcheck"
exec > >(tee -a "$LOG") 2>&1
# Check if other server responds
curl -H "X-Rundeck-Auth-Token: $API_TOKEN" "http://$REMOTE_IP:4440/api/1/system/info"| grep "result success='true'"
# If other server is up
if [ $? -eq 0 ];then
echo "`date`: Rundeck running"
# If other server is down
else
# Get a list of running jobs (that might be stuck)
for execution in $(curl -s -H "Content-Type: application/json" -H "X-Rundeck-Auth-Token: $API_TOKEN" "http://$OWN_IP:4440/api/17/project/Production/executions/running" |grep execution|grep id|grep running|cut -f2 -d'='|sed "s/\'\(.*\)\' href/\1/g" | sed "s/'//g"| cut -f1 -d' ' )
do
# Abort currently running jobs
curl -s -H "Content-Type: application/xml" -H "X-Rundeck-Auth-Token: $API_TOKEN" "http://$OWN_IP:4440/api/17/execution/$execution/abort" | grep 'execution id'| grep "status='aborted'"
if [ $? -eq 0 ]
then
echo "`date`: Execution $execution aborted"
else
echo "`date`: Failed to abort execution $execution"
fi
done
# Jobs aborted, call takeover API
echo "`date`: Rundeck down, calling takeover API"
curl -s -H "Content-Type: application/xml" -H "X-Rundeck-Auth-Token: $API_TOKEN" -X PUT "http://$OWN_IP:4440/api/14/scheduler/takeover" -d "<takeoverSchedule><server all=\"true\"/></takeoverSchedule>" >/dev/null 2>&1
if [ $? -eq 0 ]; then echo "`date`: Takeover successfull"; fi
fi