Menu

Pinger

David Lurth

+--------+
| pinger |
+--------+

Ubuntu 14.04.1 Server amd64

Install OpenSSH server
# apt-get install openssh-server

SSH into the Pinger server using Putty (or another SSH client)

Become root
# sudo su

Create necessary files and folders
# mkdir /pinger
# cd /pinger
# mkdir logs
# mkdir old_logs
# mkdir unreachable
# touch nodes
# nano pinger.sh
Copy and Paste the following:

#!/bin/bash
to_address='email@domain.com'
timestamp=date +%m-%d_%H:%M:%S
while read node; do
response=$(ping -c1 $node |head -2 |tail -1)
if [ -z "$response" ]; then
response="Unreachable"
echo $timestamp > /pinger/unreachable/$node
mail -s "unreachable - $node" $to_address <<< $timestamp
elif [ -e "/pinger/unreachable/$node" ]; then
if [[ $response =~ "Unreachable" ]]; then
response="Unreachable"
else
rm /pinger/unreachable/$node
response=$(echo "$response" |awk -F "time=" '{ print $2 }')
mail -s "reachable again - $node" $to_address <<< $timestamp
fi
else
response=$(echo "$response" |awk -F "time=" '{ print $2 }')
fi
log="$timestamp - $response"
echo $log >> /pinger/logs/$node
done </pinger/nodes

  Ctrl-X
  Y
  Enter

# nano rotatelogs.sh
#!/bin/bash
new_dir=date +%m-%d-%y
mkdir /pinger/old_logs/$new_dir
mv /pinger/logs/* /pinger/old_logs/$new_dir

Make pinger.sh and rotatelogs.sh executable
# chmod u+x pinger.sh
# chmod u+x rotatelogs.sh

Add a node to be pinged
# echo "dnsname" >> nodes
# echo "ipaddress" >> nodes

Install msmtp
# apt-get install msmtp-mta
# nano ~/.msmtprc
defaults
logfile ~/msmtp.log
account gmail
auth on
host smtp.gmail.com
from your_address@gmail.com
auth on
tls on
tls_trust_file /usr/share/ca-certificates/mozilla/Equifax_Secure_CA.crt
user your_address@gmail.com
password your_gmail_password
port 587
account default : gmail
# chmod 600 ~/.msmtprc

Install mailx
# apt-get install heirloom-mailx
# nano ~/.mailrc
set sendmail="/usr/bin/msmtp"
set message-sendmail-extra-arguments="-a gmail"

Create Cron to run pinger.sh (every minute) and rotatelogs.sh (every week)
# crontab -e
* * * /pinger/pinger.sh
0 0 * * 0 /pinger/rotatelogs.sh

==========
Optional
==========

Install Apache
# apt-get install apache2
# echo "ServerName localhost" | sudo tee /etc/apache2/conf-available/fqdn.conf && sudo a2enconf fqdn

Install PHP5
# libapache2-mod-php5

Restart Apache
# service apache2 restart

Create necessary HTML and PHP files
# cd /var/www/html
# rm index.html
# nano index.html


<title>pinger</title>
<script><br> function init(){<br> getData("nodes");<br> }<br> function getData(type,node){<br> var ajax = new XMLHttpRequest();<br> var url = "getData.php?type=" + type;<br> if (node)<br> url += "&amp;node=" + node;<br> ajax.open("GET", url, false);<br> ajax.send();<br> var text = ajax.responseText;<br> if (type == "nodes"){<br> var json = JSON.parse(text);<br> var size = json.nodes.length;<br> var node = "";<br> text = "";<br> for (i=0;i&lt;size;i++){<br> node = json.nodes<a class="alink notfound" href="i">[i]</a>.name;<br> text += "<a id=\"" + node + "\" href=\"javascript:getData('logs','" + node + "')\" onmouseover=\"getData('logs','" + node + "')\" onmouseout=\"clearLogs()\">" + node + "</a></br>";<br> }<br> getID(type).innerHTML = text;<br> if (json.unreachable){<br> size = json.unreachable.length;<br> for (i=0;i&lt;size;i++)<br> getID(json.unreachable<a class="alink notfound" href="i">[i]</a>.name).style.color = "red";<br> }<br> setTimeout("getData(\"nodes\")",30000);<br> }<br> else<br> getID(type).innerHTML = text;<br> }<br> function clearLogs(){getID("logs").innerHTML = "";}<br> function getID(id){return document.getElementById(id);}<br> </script>
<style><br> body {background-color:black;color:#009bd5;font-family:arial;font-size:14}<br> a {color:#009bd5;text-decoration:none}<br> a:hover {color:white}<br> #logs {position:fixed;top:8;left:250;color:white}<br> </style>





# nano getData.php
<?php
if ($_GET["type"] == "nodes"){
$nodes = file('/pinger/nodes', FILE_IGNORE_NEW_LINES);
sort($nodes);
$json = "{\"nodes\":[";]
foreach ($nodes as $node)
$json .= "{\"name\":\"" . $node . "\"},";
$json = rtrim($json, ",");
$unreachable = array_slice(scandir('/pinger/unreachable'), 2);
if (count($unreachable)){
$json .= "
,\"unreachable\":[";]
foreach ($unreachable as $node)
$json .= "{\"name\":\"" . $node . "\"},";
$json = rtrim($json, ",");
}
$json .= "
}";
echo $json;
}
else {
$logs = file("/pinger/logs/" . $_GET["node"], FILE_IGNORE_NEW_LINES);
if (count($logs) > 30)
$logs = array_slice($logs, -30);
foreach ($logs as $log)
echo substr($log, 6) . "
";
}
?>

Go to IP Address of "pinger" server in a browser
Reachable nodes are "blue"
Unreachable nodes are "red"
Hover over node name to see last 30 minutes of response times
Page refreshes every 30 seconds automatically


Want the latest updates on software, tech news, and AI?
Get latest updates about software, tech news, and AI from SourceForge directly in your inbox once a month.