2

My mac runs as a server with an dyndns ip. To work properly, it must be connected to the internet all the time. but unfortunately it seems that it is not connected a lot of times during the day. So I need a tool that checks i.e. once per minute if the mac is connected to the internet (i.e. by trying to reach google), and writes it to a log file.

Do you know something that can help out here? I need to check it on the machine, if itself has got internet. Mac OS 10.5. Thanks guys!!

IRTFM
  • 21
  • 1
  • 2

4 Answers4

3

Nagios will run on a Mac and it's open source / free!

http://www.macworld.com/article/134079/2008/06/nagios.html

Installing Nagios allows you to turn any Mac running Mac OS X Server into a network monitoring station able to notify you of both hardware and software problems on your server, usually before anyone else notices you. Nagios can use both e-mail and SMS to notify you of both problems and recovery from problems. (The latter is important, since it can often save you from a late-night remote access session.) Since Nagios requires Perl, Apache, SNMP and a few other things that Mac OS X Server either ships with, or that can be installed with ease, it’s a great way for any network administrator to better monitor their network.

Chealion
  • 5,733
  • 28
  • 29
TrevJen
  • 264
  • 1
  • 7
  • 23
  • thank you, that's nice. but i have just normal mac os x, no server. its a mac book that's always on ;) – IRTFM Jul 28 '09 at 17:53
  • @IRTFM - You don't need OS X Server you can do it with OS X Client as well - you just may need to install an extra package. (Not having installed on client myself I don't know what package(s)) – Chealion Jul 28 '09 at 22:37
  • Yes, it definitely does work fine on a MacBook. I set up my test installation that way before moving it to a server. – John Gardeniers Jul 28 '09 at 23:07
3

You can install nagios on a normal mac, they are Unix machines too :)

Would a script like this do ?

 #!/bin/bash
 LOG=/tmp/LOG
 rm -f $LOG
 while ( true );
 do 
  date >> $LOG;
  curl http://www.google.com/ > /dev/null  2>> $LOG
  sleep 60 ;
 done
James
  • 2,232
  • 1
  • 13
  • 19
2

Nagios is overkill for this, IMHO. Also, one likely cause for this extened offline phases is missing network traffic, so your router goes offline.

This will be prevented by James' script above, which I would just slightly modify to avoid downloading the google page every 60 seconds.

Just replace the curl google.com line witht the following

ping -c1 -t 5 www.google.com > /dev/null
if [ "$?" = "0" ]; then
    echo "I am online" > $LOG
else 
    echo "Boo, the internet is broken, google doesn't answer" > $LOG
fi

or, if you only want to log offline phases, modify the complete script like so:

#!/bin/bash
LOG=/tmp/internetlog
rm -f $LOG
while ( true );
do 
   ping -c1 -t 5 www.google.com > /dev/null
   if [ "$?" -gt "0" ]; then
       echo `date`  " I am offline" >> $LOG
   fi

 sleep 60 ;
done

This replace the curl call, which downloads the complete page from google with a single ping with a 5 second timeout, which is normally enough.

Sven
  • 98,649
  • 14
  • 180
  • 226
  • Thanks, many years into the future you saved me some effort in creating this myself. Cheers, have a virtual beer on me. – Rocket Garden Aug 08 '20 at 09:59
0

it may be a bit overkill but i'd recommend you to use nagios's plugins link text, using them you can then write a simple shell script to do what you need, or i guess the other alternative you can use simple ping also with shell script, it can be accomplished a lot easier with nagios plugins though.

alexus
  • 13,112
  • 32
  • 117
  • 174