-
Notifications
You must be signed in to change notification settings - Fork 0
/
install.sh
executable file
·143 lines (119 loc) · 4.09 KB
/
install.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
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
#!/usr/bin/env bash
# This script is designed to help you install smartmon. It was created and
# tested for Ubuntu but should be usable for other Linux distributions
# (with small adjustments).
# The path for textfile directory and the location of the smartmon.sh script
# can be configuration as environment variables:
SMARTMON_PATH="${SMARTMON_PATH:-/usr/local/bin}"
TEXTFILE_DIR="${TEXTFILE_DIR:-/var/lib/node_exporter/textfile_collector}"
# Check node_exporter status and configuration
check_node_exporter () {
if systemctl is-active --quiet node_exporter; then
echo "Node Exporter is running. Verifying textfile collector configuration..."
if ! systemctl show node_exporter --property=ExecStart | grep -q -- "--collector.textfile.directory"; then
echo "❌ The Prometheus Node Exporter is not configured with the required \
--collector.textfile.directory option." >&2
echo "ℹ️ Please configure the Node Exporter with \
--collector.textfile.directory=${TEXTFILE_DIR}" >&2
exit 1
fi
echo "✅ Node Exporter is correctly configured for the textfile collector at ${TEXTFILE_DIR}."
else
echo "❌ ERROR: Node Exporter is not running. \
Please start it and ensure correct configuration." >&2
exit 1
fi
}
#
# Check if the textfile collector directory exists
ensure_textfile_directory () {
if [ ! -d "${TEXTFILE_DIR}" ]; then
echo "ℹ️ The directory ${TEXTFILE_DIR} does not exist. Creating it..."
mkdir -p "${TEXTFILE_DIR}" || {
echo "❌ ERROR: Failed to create directory ${TEXTFILE_DIR}. Check permissions." >&2
exit 1
}
echo "✅ Directory ${TEXTFILE_DIR} created successfully."
else
echo "✅ Directory ${TEXTFILE_DIR} already exists."
fi
}
prepare_script () {
# Download the smartmon.sh script from the repository or a release asset
echo "Downloading and preparing smartmon.sh..."
wget -O "${SMARTMON_PATH}/smartmon.sh" https://raw.githubusercontent.com/micha37-martins/S.M.A.R.T-disk-monitoring-for-Prometheus/master/src/smartmon.sh
# Set the appropriate permissions on the smartmon.sh script
chmod +x "${SMARTMON_PATH}/smartmon.sh"
}
# Create the systemd service unit file
create_systemd_service () {
echo "Creating systemd service for smartmon..."
local seagate_flag=""
if [[ $SEAGATE_SPECIAL == true ]]; then
seagate_flag="--seagate_special"
fi
cat <<EOF > /etc/systemd/system/smartmon.service
[Unit]
Description=SMART Disk Exporter for Prometheus
After=network-online.target
[Service]
Type=simple
ExecStart=/usr/bin/env bash ${SMARTMON_PATH}/smartmon.sh $seagate_flag
StandardOutput=truncate:/var/lib/node_exporter/textfile_collector/smart_metrics.prom
WorkingDirectory=/var/lib/node_exporter/textfile_collector/
User=root
Group=root
SyslogIdentifier=smartmon
StandardError=journal
RemainAfterExit=no
[Install]
WantedBy=multi-user.target
EOF
# Reload the systemd daemon to load the new service unit file
systemctl daemon-reload
# Enable the service to start automatically at boot
systemctl enable smartmon.service
# Start the service immediately
systemctl start smartmon.service
}
# Create the systemd timer unit file
create_systemd_timer () {
echo "Creating systemd timer for smartmon..."
cat <<EOF > /etc/systemd/system/smartmon.timer
[Unit]
Description=Run SMART Disk Exporter for Prometheus every 10 minutes
[Timer]
OnBootSec=1min
OnUnitActiveSec=10min
[Install]
WantedBy=multi-user.target
EOF
# Reload the systemd daemon to load the new timer unit file
systemctl daemon-reload
# Enable the timer to start automatically at boot
systemctl enable smartmon.timer
# Start the timer immediately
systemctl start smartmon.timer
}
# Parse command-line options
parse_args () {
while [[ $# -gt 0 ]]; do
case "$1" in
--seagate_special)
SEAGATE_SPECIAL=true
shift # Move to next argument
;;
*)
echo "❌ Unknown option: $1"
exit 1
;;
esac
done
}
# Execute the individual steps
parse_args $@
check_node_exporter
ensure_textfile_directory
prepare_script
create_systemd_service
create_systemd_timer