-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathreport_healthcheck.sh
executable file
·144 lines (119 loc) · 3.4 KB
/
report_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
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
144
#!/bin/bash
##############################################################
# Copyright 2019 Daniel Grant
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
##############################################################
##############################################################
# report_health.sh
#
# Script that makes a number of calls to gather information
# about the servers health, and reports this information via
# email.
#
# Author: Daniel Grant
# Version: 1.0.1
##############################################################
# Configuration
TO_EMAIL=
CMD_SENDMAIL=/usr/sbin/sendmail
log() {
if [ "$1" = "ERROR" ]; then
echo "[$(date +"%Y-%m-%d %H:%M:%S")] [ERROR] $2"
else
echo "[$(date +"%Y-%m-%d %H:%M:%S")] [INFO] $2"
fi
}
log_info() {
log "INFO" "$1"
}
log_error() {
log "ERROR" "$1"
}
check_var() {
if [ -z "$2" ]; then
log_error "$1 is not set"
exit 1
else
log_info "Got $1 = $2"
fi
}
check_cmd() {
if [ -x "$2" ]; then
log_info "Got $1 = $2"
else
log_error "$2 does not exist"
exit 1
fi
}
check_file() {
if [ -w "$2" ]; then
log_info "Got $1 = $2"
else
log_error "$2 does not exist or is not writeable"
exit 1
fi
}
insert_break() {
echo "---------------------------------" >> "$1"
}
# Verify and log the configuration
check_var "TO_EMAIL" "$TO_EMAIL"
check_cmd "CMD_SENDMAIL" "$CMD_SENDMAIL"
# Create, verify and log the temporary file
OUTFILE=$(mktemp)
check_file "temporary file" "$OUTFILE"
# Report hostname and server uptime
echo "$(hostname -f): $(uptime --pretty)" >> "$OUTFILE"
insert_break "$OUTFILE"
# Report CPU percentage usage
top -b -n 1 | grep ^%Cpu >> "$OUTFILE"
insert_break "$OUTFILE"
# Report system load averages
echo "Load Average: $(cat /proc/loadavg)" >> "$OUTFILE"
insert_break "$OUTFILE"
# Report memory consumption
free -h >> "$OUTFILE"
insert_break "$OUTFILE"
# Report disk usage
df -h >> "$OUTFILE"
insert_break "$OUTFILE"
# Report IO stats
iostat -dmx | tail -n +3 >> "$OUTFILE"
insert_break "$OUTFILE"
# Report currently logged in users
echo -e "Currently logged in users:\n$(who)" >> "$OUTFILE"
insert_break "$OUTFILE"
# Report last 10 logins
echo -e "Last 10 logins:\n$(last | head -10)" >> "$OUTFILE"
insert_break "$OUTFILE"
# Report top 10 processes by CPU
echo -e "Top 10 processes by CPU:\n$(ps -eo pcpu,pid,user,args | sort -k 1 -r | head -11)" >> "$OUTFILE"
insert_break "$OUTFILE"
# Report top 10 processes by memory
echo -e "Top 10 processes by memory:\n$(ps -eo pmem,pid,user,args | sort -k 1 -r | head -11)" >> "$OUTFILE"
# Dispatch the email
if [ -s "$OUTFILE" ]; then
log_info "Emailing report to: $TO_EMAIL"
(
echo "Subject: [healthcheck report] $(hostname -f)"
echo "To: $TO_EMAIL"
echo ""
cat "$OUTFILE"
) | $CMD_SENDMAIL $TO_EMAIL
else
log_error "Temporary file '$OUTFILE' does not exist or is empty"
fi
# Delete the temporary file
log_info "Deleting temporary file '$OUTFILE'"
rm -f "$OUTFILE"
exit 0