forked from 45Drives/scripts
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathadd-pcs-monitoring.sh
120 lines (103 loc) · 3.15 KB
/
add-pcs-monitoring.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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
# Matthew Hutchinson
# July 19 2024
#!/bin/bash
# Get the pcs status output
pcs_status=$(pcs status)
# Count the number of VIPs in the pcs status
vip_count=$(echo "$pcs_status" | grep -oP 'IPaddr2' | wc -l)
# Check if there is more than one VIP
if [ "$vip_count" -gt 1 ]; then
echo "Error: More than one VIP found. Unsupported for this script. VIP count: $vip_count"
exit 1
else
echo "Only one VIP found. Proceeding."
fi
# Extract IP addresses from pcs status
nodes=$(pcs status | awk '
/Node List:/ {flag=1; next}
/Full List of Resources:/ {flag=0}
flag && /Online:/ {
gsub(/.*\[ /, "", $0) # Remove everything before the IPs
gsub(/ \]/, "", $0) # Remove the closing bracket
gsub(/ /, "\n", $0) # Replace spaces with newlines to separate IPs
print
}
')
# Function to install packages on a node
install_packages() {
local node_ip=$1
ssh -T "$node_ip" << 'EOF'
if command -v dnf &> /dev/null; then
dnf install -y resource-agents
elif command -v apt &> /dev/null; then
apt update && apt install -y resource-agents
else
echo "Unsupported package manager. Please install resource-agents manually."
exit 1
fi
EOF
}
# Function to deploy the resource script on a node
deploy_resource_script() {
local node_ip=$1
ssh -T "$node_ip" << 'EOF'
#!/bin/bash
# Create the nfs-monitor resource script
cat << 'SCRIPT' > /usr/lib/ocf/resource.d/heartbeat/nfs-monitor
#!/bin/bash
. /usr/lib/ocf/lib/heartbeat/ocf-shellfuncs
case "$1" in
start)
exit $OCF_SUCCESS
;;
stop)
exit $OCF_SUCCESS
;;
monitor)
if systemctl is-active --quiet nfs-server; then
exit $OCF_SUCCESS
else
ocf_exit_reason "NFS server is not running"
exit $OCF_NOT_RUNNING
fi
;;
meta-data)
cat << 'METAEOF'
<?xml version="1.0"?>
<!DOCTYPE resource-agent SYSTEM "ra-api-1.dtd">
<resource-agent name="nfs-monitor">
<version>1.0</version>
<longdesc lang="en">
This resource agent monitors the NFS server service.
</longdesc>
<shortdesc lang="en">NFS server monitor</shortdesc>
<parameters/>
<actions>
<action name="start" timeout="5s" />
<action name="stop" timeout="5s" />
<action name="monitor" timeout="10s" interval="15s" />
<action name="meta-data" timeout="5s" />
</actions>
</resource-agent>
METAEOF
exit $OCF_SUCCESS
;;
*)
echo "Usage: $0 {start|stop|monitor|meta-data}"
exit $OCF_ERR_ARGS
;;
esac
SCRIPT
# Make the script executable
chmod +x /usr/lib/ocf/resource.d/heartbeat/nfs-monitor
echo "Resource script deployment complete."
EOF
}
# Loop through each node IP
for node_ip in $nodes; do
install_packages "$node_ip"
deploy_resource_script "$node_ip"
done
pcs resource create nfs-monitor ocf:heartbeat:nfs-monitor op monitor interval=10s meta migration-threshold=1 failure-timeout=5s
pcs constraint colocation add nfs_ip with nfs-monitor INFINITY
echo "Resource deployment completed on all nodes."