monitor

monitor script
git clone git://git.suckless.org/monitor
Log | Files | Refs | LICENSE

commit 0361552b86ea235070c064e34e29c3fe79738769
Author: Anselm R Garbe <garbeam@gmail.com>
Date:   Tue,  4 Jul 2017 17:23:07 +0200

created public repo due to interested people

Diffstat:
ALICENSE | 21+++++++++++++++++++++
Afoot.tpl | 3+++
Ahead.tpl | 16++++++++++++++++
Amonitor.rc | 99+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
4 files changed, 139 insertions(+), 0 deletions(-)

diff --git a/LICENSE b/LICENSE @@ -0,0 +1,21 @@ +MIT/X Consortium License + +© 2009-2017 Anselm R Garbe <anselm@garbe.us> + +Permission is hereby granted, free of charge, to any person obtaining a +copy of this software and associated documentation files (the "Software"), +to deal in the Software without restriction, including without limitation +the rights to use, copy, modify, merge, publish, distribute, sublicense, +and/or sell copies of the Software, and to permit persons to whom the +Software is furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in +all copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL +THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING +FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER +DEALINGS IN THE SOFTWARE. diff --git a/foot.tpl b/foot.tpl @@ -0,0 +1,3 @@ + </table> +</body> +</html> diff --git a/head.tpl b/head.tpl @@ -0,0 +1,16 @@ +<!DOCTYPE html> +<html> +<head> + <title>monitor</title> + <style> + .ok { background-color: #008800; color: #ffffff; } + .fail { background-color: #880000; color: #ffffff; } + .na { background-color: #000088; color: #ffffff; } + td { border: 1px solid #000088; } + body { font-family: Helvetica, Verdana, Arial, 'Liberation Sans', FreeSans, sans-serif; font-size: 10pt; } + </style> + <meta charset="utf-8"> +</head> +<body> + <table> + <tr><th>Last check</th><th>ICMP</th><th>HTTP</th><th>DNS</th><th>SMTP</th><th>Graph</th></tr> diff --git a/monitor.rc b/monitor.rc @@ -0,0 +1,99 @@ +#!/usr/local/plan9/bin/rc +# +# requires +# - 9base +# - ping +# - curl +# - netcat (nc) +# - dig +# +PATH=/usr/local/plan9/bin:$PATH + +hosts=(\ + suckless.org:88.99.169.165:garbeam@gmail.com:0:1:1:0\ + sta.li:88.99.169.165:garbeam@gmail.com:0:1:0.0\ + garbe.us:94.130.56.76:garbeam@gmail.com:0:1:1:0\ +) + +fn ok { + line=$line^' 1' + html=$html^'<td class="ok">OK</td>' +} + +fn fail { + line=$line^' 0' + if(~ $1 '1') { + html=$html^'<td class="fail">FAIL</td>' + failtext=$failtext^'['^$2^': FAIL]' + } + if not + html=$html^'<td class="na">NA</td>' +} + +fn check_host { + ifs=(':') { pair=`{echo -n $1} } + host=$pair(1) + ip=$pair(2) + contact=$pair(3) + icmp_required=$pair(4) + http_required=$pair(5) + dns_required=$pair(6) + smtp_required=$pair(7) + failtext='' + line=`{date -n} + now=`{date -u} + html='<tr><td>'^$"now^'</td>' +# ICMP + if(ping -c 1 $host >/dev/null >[2]/dev/null) + ok + if not { + sleep 5 + if(ping -c 1 $host >/dev/null >[2]/dev/null) + ok + if not + fail $icmp_required 'ICMP' + } +# HTTP + if(curl -I 'http://'^$host --connect-timeout 10 >/dev/null >[2]/dev/null) + ok + if not { + sleep 5 + if(curl -I 'http://'^$host --connect-timeout 10 >/dev/null >[2]/dev/null) + ok + if not + fail $http_required 'HTTP' + } +# DNS + if(dig $host | grep -v '^;' | grep A | grep $ip >/dev/null >[2]/dev/null) + ok + if not { + sleep 5 + if(dig $host | grep -v '^;' | grep A | grep $ip >/dev/null >[2]/dev/null) + ok + if not + fail $dns_required 'DNS' + } +# SMTP + if(nc -z -w 5 $host 25 >/dev/null >[2]/dev/null) + ok + if not { + sleep 5 + if(nc -z -w 5 $host 25 >/dev/null >[2]/dev/null) + ok + if not + fail $smtp_required 'SMTP' + } + echo $line >> '/data/'^$host^'.dat' + gnuplot '/data/'^$host^'.gnu' > '/var/www/static/monitor.garbe.us/'^$host^'.png' + html=$html^'<td><img src="'^$host^'.png"/></td></tr>' + echo $html >> /var/www/static/monitor.garbe.us/index.html + if(~ $failtext '') + ; + if not + echo $failtext | mail -s $host $contact +} + +cat /var/www/static/monitor.garbe.us/head.tpl > /var/www/static/monitor.garbe.us/index.html +for(i in $hosts) + check_host $i +cat /var/www/static/monitor.garbe.us/foot.tpl >> /var/www/static/monitor.garbe.us/index.html