/
/
/
1---
2- name: Configure SMART monitoring
3 template:
4 src: smartd.conf.j2
5 dest: /etc/smartd.conf
6 backup: true
7 mode: '0644'
8 owner: root
9 group: root
10 become: true
11 when: nas_smartmontools_enabled | bool
12 notify: restart smartd
13
14- name: Enable and start smartd service
15 systemd:
16 name: smartd
17 state: started
18 enabled: true
19 become: true
20 when: nas_smartmontools_enabled | bool
21
22- name: Create NAS monitoring script
23 template:
24 src: nas-monitor.sh.j2
25 dest: /usr/local/bin/nas-monitor.sh
26 mode: '0755'
27 owner: root
28 group: root
29 become: true
30
31- name: Set up NAS monitoring cron job
32 cron:
33 name: "NAS Health Monitor"
34 minute: "*/10"
35 job: "/usr/local/bin/nas-monitor.sh"
36 user: root
37 become: true
38
39- name: Configure mdadm monitoring daemon
40 lineinfile:
41 path: "{{ nas_mdadm_config_file }}"
42 regexp: '^MAILADDR'
43 line: "MAILADDR {{ nas_email_notifications }}"
44 backup: true
45 become: true
46 when:
47 - nas_email_notifications != ""
48 - nas_raid_monitoring | bool
49
50- name: Enable mdadm monitoring service
51 systemd:
52 name: mdmonitor
53 state: started
54 enabled: true
55 daemon_reload: true
56 become: true
57 when: nas_raid_monitoring | bool
58
59- name: Create RAID health check script
60 copy:
61 content: |
62 #!/bin/bash
63 # RAID Health Check Script for mdadm
64
65 LOG_FILE="{{ nas_log_file }}"
66
67 echo "$(date): Starting RAID health check" >> "$LOG_FILE"
68
69 # Check all RAID arrays
70 for array in $(cat /proc/mdstat | grep "^md" | cut -d: -f1); do
71 echo "Checking /dev/$array..." >> "$LOG_FILE"
72
73 # Get array status
74 STATUS=$(mdadm --detail /dev/$array | grep "State :" | cut -d: -f2 | xargs)
75 echo " Status: $STATUS" >> "$LOG_FILE"
76
77 # Check for failed drives
78 FAILED=$(mdadm --detail /dev/$array | grep "Failed Devices" | cut -d: -f2 | xargs)
79 if [ "$FAILED" -gt 0 ]; then
80 echo "WARNING: /dev/$array has $FAILED failed devices!" >> "$LOG_FILE"
81 {% if nas_email_notifications != "" %}
82 echo "RAID Alert: /dev/$array has $FAILED failed devices on $(hostname)" | mail -s "RAID Alert" {{ nas_email_notifications }}
83 {% endif %}
84 fi
85
86 # Check array sync status
87 if grep -q "recovery\|resync" /proc/mdstat; then
88 PROGRESS=$(grep -A1 "^$array" /proc/mdstat | tail -1)
89 echo " Sync status: $PROGRESS" >> "$LOG_FILE"
90 fi
91 done
92
93 echo "$(date): RAID health check completed" >> "$LOG_FILE"
94 dest: /usr/local/bin/raid-health-check.sh
95 mode: '0755'
96 owner: root
97 group: root
98 become: true
99 when: nas_raid_monitoring | bool
100
101- name: Set up RAID health check cron job
102 cron:
103 name: "RAID Health Check"
104 minute: "*/5" # Every 5 minutes
105 job: "/usr/local/bin/raid-health-check.sh"
106 user: root
107 become: true
108 when: nas_raid_monitoring | bool
109
110- name: Create log rotation for NAS logs
111 template:
112 src: nas-logrotate.j2
113 dest: /etc/logrotate.d/nas
114 mode: '0644'
115 owner: root
116 group: root
117 become: true
118
119- name: Ensure log directory exists
120 file:
121 path: "{{ nas_log_file | dirname }}"
122 state: directory
123 mode: '0755'
124 owner: root
125 group: root
126 become: true
127
128- name: Create initial log file
129 file:
130 path: "{{ nas_log_file }}"
131 state: touch
132 mode: '0644'
133 owner: root
134 group: root
135 become: true
136 changed_when: false