/
/
/
1#!/bin/bash
2# NFS Mount Monitoring Script for Runner Services
3# Generated by Ansible - DO NOT EDIT MANUALLY
4
5set -euo pipefail
6
7# Configuration
8SCRIPT_NAME="runner-nfs-check"
9LOG_FILE="/var/log/runner-nfs-monitor.log"
10TIMESTAMP=$(date '+%Y-%m-%d %H:%M:%S')
11
12# Service configuration
13RUNNER_USER="{{ runner_user }}"
14RUNNER_GROUP="{{ runner_group }}"
15
16# NFS mounts to check
17declare -A NFS_MOUNTS
18{% for mount in runner_nfs_mounts %}
19NFS_MOUNTS["{{ mount.name }}"]="{{ mount.local_path }}:{{ mount.host }}:{{ mount.nfs_path }}"
20{% endfor %}
21
22# Simple logging function
23log_message() {
24 local level="$1"
25 local message="$2"
26 echo "[$TIMESTAMP] [$level] $message" | tee -a "$LOG_FILE"
27}
28
29# Check if mount point is accessible
30check_mount() {
31 local mount_path="$1"
32
33 # Check if directory exists and is mounted
34 if [[ ! -d "$mount_path" ]]; then
35 return 1
36 fi
37
38 if ! mountpoint -q "$mount_path"; then
39 return 2
40 fi
41
42 # Test basic read access
43 if ! timeout 10 ls "$mount_path" >/dev/null 2>&1; then
44 return 3
45 fi
46
47 # Test write access
48 local test_file="$mount_path/.health-check-$$"
49 if ! timeout 10 touch "$test_file" 2>/dev/null; then
50 return 4
51 fi
52 rm -f "$test_file" 2>/dev/null || true
53
54 return 0
55}
56
57# Main monitoring function
58main() {
59 mkdir -p "$(dirname "$LOG_FILE")"
60
61 local overall_status=0
62 local failed_mounts=()
63
64 log_message "INFO" "Starting NFS mount health check"
65
66 for mount_name in "${!NFS_MOUNTS[@]}"; do
67 local mount_info="${NFS_MOUNTS[$mount_name]}"
68 IFS=':' read -r mount_path nfs_host nfs_path <<< "$mount_info"
69
70 case $(check_mount "$mount_path") in
71 0)
72 log_message "INFO" "$mount_name: OK ($mount_path)"
73 ;;
74 1)
75 log_message "ERROR" "$mount_name: Directory missing ($mount_path)"
76 failed_mounts+=("$mount_name")
77 overall_status=1
78 ;;
79 2)
80 log_message "ERROR" "$mount_name: Not mounted ($mount_path)"
81 failed_mounts+=("$mount_name")
82 overall_status=1
83 ;;
84 3)
85 log_message "ERROR" "$mount_name: Read access failed ($mount_path)"
86 failed_mounts+=("$mount_name")
87 overall_status=1
88 ;;
89 4)
90 log_message "WARNING" "$mount_name: Write access failed ($mount_path)"
91 ;;
92 esac
93 done
94
95 if [[ $overall_status -eq 0 ]]; then
96 log_message "INFO" "All NFS mounts healthy"
97 else
98 log_message "ERROR" "Failed mounts: ${failed_mounts[*]}"
99 fi
100
101 # Create simple status file
102 echo "{\"status\": \"$([ $overall_status -eq 0 ] && echo healthy || echo unhealthy)\", \"timestamp\": \"$TIMESTAMP\"}" > /var/lib/runner-nfs-status.json
103
104 exit $overall_status
105}
106
107# Run main function
108main "$@"