-
Notifications
You must be signed in to change notification settings - Fork 16
/
monitor.sh
executable file
·193 lines (168 loc) · 5.97 KB
/
monitor.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
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
#/bin/sh
# Shell script to get uptime, disk usage, cpu usage, RAM usage,system load,etc.
# from multiple Linux servers and output the information on a single server
# in html format. Read below for usage/installation info
# *---------------------------------------------------------------------------*
# * dig_remote_linux_server_information.bash,v0.1, last updated on 25-Jul-2005*
# * Copyright (c) 2005 nixCraft project *
# * Comment/bugs: http://cyberciti.biz/fb/ *
# * Ref url: http://cyberciti.biz/nixcraft/forum/viewtopic.php?t=97 *
# * This script is licensed under GNU GPL version 2.0 or above *
# *---------------------------------------------------------------------------*
# * Installation Info *
# ----------------------------------------------------------------------------*
# You need to setup ssh-keys to avoid password prompt, see url how-to setup
# ssh-keys:
# cyberciti.biz/nixcraft/vivek/blogger/2004/05/ssh-public-key-based-authentication.html
#
# [1] You need to setup correct VARIABLES script:
#
# (a) Change Q_HOST to query your host to get information
# Q_HOST="192.168.1.2 127.0.0.1 192.168.1.2"
#
# (b) Setup USR, who is used to connect via ssh and already setup to connect
# via ssh-keys
# USR="nixcraft"
#
# (c)Show warning if server load average is below the limit for last 5 minute.
# setup LOAD_WARN as per your need, default is 5.0
#
# LOAD_WARN=5.0
#
# (d) Setup your network title using MYNETINFO
# MYNETINFO="My Network Info"
#
# (e) Save the file
#
# Please refer to forum topic on this script:
# Also download the .gif files and put them in your output dir
#
# ----------------------------------------------------------------------------
# Execute script as follows (and copy .gif file in this dir) :
# this.script.name > /var/www/html/info.html
# ============================================================================
# This script is part of nixCraft shell script collection (NSSC)
# Visit http://bash.cyberciti.biz/ for more information.
# -------------------------------------------------------------------------
# SSH SERVER HOST IPS, setup me
# Change this to query your host
declare -A HOST_INFO
Q_HOSTS="http mail database onyx gnn"
HOST_INFO[http]="192.168.100.101"
HOST_INFO[http_ip]="192.168.100.101"
HOST_INFO[http_username]="username"
HOST_INFO[http_pass]="password"
HOST_INFO[mail]="192.168.100.102"
HOST_INFO[mail_ip]="192.168.100.102"
HOST_INFO[mail_username]="username"
HOST_INFO[mail_pass]="password"
HOST_INFO[database]="192.168.100.103"
HOST_INFO[database_ip]="192.168.100.103"
HOST_INFO[database_username]="username"
HOST_INFO[database_pass]="password"
HOST_INFO[onyx]="192.168.100.104"
HOST_INFO[onyx_ip]="192.168.100.104"
HOST_INFO[onyx_username]="username"
HOST_INFO[onyx_pass]="password"
HOST_INFO[gnn]="192.168.100.105"
HOST_INFO[gnn_ip]="192.168.100.105"
HOST_INFO[gnn_username]="username"
HOST_INFO[gnn_pass]="password"
# Show warning if server load average is below the limit for last 5 minute
LOAD_WARN=5
# Your network info
MYNETINFO="My Network Info"
#
# if it is run as cgi we can do reload stuff too :D
PBY='Powered by <a href="http://bash.cyberciti.biz/monitoring/get-system-information-in-html-format/">script</a>'
# font colours
GREEN='<font color="#00ff00">'
RED='<font color="#ff0000">'
NOC='</font>'
LSTART='
<ul>
<li>'
LEND='</li>
</ul>
'
# Local path to ssh and other bins
SSH="/usr/bin/ssh"
PING="/bin/ping"
NOW="$(date)"
## functions ##
writeHead(){
echo '<HTML><HEAD><TITLE>Network Status</TITLE></HEAD>
<BODY alink="#0066ff" bgcolor="#000000" link="#0000ff" text="#ccddee" vlink="#0033ff">'
echo '<CENTER><H1>'
echo "$MYNETINFO</H1>"
echo "Generated on $NOW"
echo '</CENTER>'
}
writeFoot(){
echo "<HR><center>$PBY</center>"
echo "</BODY></HTML>"
}
## main ##
writeHead
echo '<TABLE WIDTH=100% BORDER=2 BORDERCOLOR="#000080" CELLPADDING=4 CELLSPACING=4 FRAME=HSIDES RULES=NONE" >'
echo '<TR VALIGN=TOP>'
for host in $Q_HOSTS
do
echo '<TD WIDTH=33% BGCOLOR="#0099ff">'
_CMD="sshpass -p ${HOST_INFO[$host'_pass']} ssh ${HOST_INFO[$host'_username']}@${HOST_INFO[$host'_ip']}"
rhostname="$($_CMD hostname)"
ruptime="$($_CMD uptime)"
if $(echo $ruptime | grep -E "min|days" >/dev/null); then
x=$(echo $ruptime | awk '{ print $3 $4}')
else
x=$(echo $ruptime | sed s/,//g| awk '{ print $3 " (hh:mm)"}')
fi
ruptime="$x"
rload="$($_CMD uptime |awk -F'average:' '{ print $2}')"
x="$(echo $rload | sed s/,//g | awk '{ print $2}')"
y="$(echo "$x >= $LOAD_WARN" | bc)"
if [ "$y" == "1" ]
then
rload="$RED $rload (High) $NOC"
else
rload="$GREEN $rload (Ok) $NOC"
fi
rclock="$($_CMD date +"%r")"
rtotalprocess="$($_CMD ps axue | grep -vE "^USER|grep|ps" | wc -l)"
rfs="$($_CMD df -hT | grep -vE "^Filesystem|shm"| awk 'BEGIN{print "<ul>"}{w=sprintf("%d",$6);print "<li>" $7 " " $6 "(" $2 ")<BR> <img src=\"indicator.gif\" height=\"4\" width=\"" w "\"> <BR><img src=\"graph.gif\"> <BR>" $4"/"$3 "</li>"}END{ print "</ul>"}')"
rusedram="$($_CMD free -mto | grep Mem: | awk '{ print $3 " MB" }')"
rfreeram="$($_CMD free -mto | grep Mem: | awk '{ print $4 " MB" }')"
rtotalram="$($_CMD free -mto | grep Mem: | awk '{ print $2 " MB" }')"
$PING -c1 "${HOST_INFO[$host'_ip']}">/dev/null
if [ "$?" != "0" ] ; then
rping="$RED Failed $NOC"
else
rping="$GREEN Ok $NOC"
echo "<b><u>$rhostname</u></b><BR>"
echo "Ping status: $rping<BR>"
echo "Time: $rclock<BR>"
echo "Uptime: $ruptime <BR>"
echo "Load avarage: $LSTART $rload $LEND"
echo "Total running process: $LSTART $rtotalprocess $LEND"
echo "Disk status:"
echo "$rfs"
echo "Ram/swap status:
<ul>"
echo "
<li>Used RAM: $rusedram</li>
"
echo "
<li>Free RAM: $rfreeram</li>
"
echo "
<li>Total RAM: $rtotalram </li>
</ul>
"
fi
echo "</td>
"
done
echo "</tr>
</table>
"
writeFoot