RubyPing – custom ping and it’s future

I ride the train almost everyday for an hour each way and work using my laptop. It was my hope that the wi-fi on the train worked sufficiently that I’d have a constant Internet connection but this is not the case. The wi-fi drops out often and make working miserable.

To correct this I purchased a mobile hotspot from T-Mobile, the MF-61, and it works a bit better. There are less drop outs so I can work more. There is a dilemma however. When I browse the web and there’s not connection I can sit an stare at a spinning cursor for sometime before I realize what’s happened. To give me more information I open a command window (Win7) and run PING endlessly. This window is placed far to one size of the screen and my work covers almost all of it so that I an peek at the scrolling endless line of data to quickly access my connection.

The dilemma is that PING in Windows and in Cygwin engages every second or so which is not what I want. I pay for my connection through the hotspot so every packet counts more or less. It’s not a lot of data but it is the principle that counts to me. I just want to know every so often what the connection status is, am I connect or not. Hence, I wrote RubyPing in, well, ruby.

 

# Customized PING
#
# TODO
# 1 - add logging. I'd like to record my train rides for later analysis. What
# might be really good is if I could get the geolocation at the same time from my
# phone and add it to the log. Hmmmm.
#
# If the laptop and the phone where sharing a network connection, in mu case
# my mobile hotspot, the laptop could query the phone for it's geo location.
# This would likely require a little app running on the phone serving this information
# when it got a request. Is a tiny webserver available for the Android?
#
# What's the absolute easiest way to do this?
# One way is a GPS Logging app on the phone. Log the timestamped data
# and send/grab that file. Merge that file with the one RubyPing produces
# creating data that can be mapped.
#
# https://github.com/defunkt/choice
require 'net/ping'
require 'resolv'
require 'choice'
def InMs(rawNumber)
 return (rawNumber * 1000).truncate()
end
Choice.options do
 option :host do
 short '-h'
 long '--host=HOST'
 desc 'The hostname or ip of the host to bind to (default 127.0.0.1)'
 default '127.0.0.1'
 end
option :delay do
 short '-d'
 long '--delay=delayInSec'
 desc 'The seconds to delay until the next PING (default 5)'
 default 5
 end
option :repeatCount do
 short '-r'
 long '--repeatCount=Number'
 desc 'The number of times to repeat (default 5)'
 default 5
 end
option :help do
 long '--help'
 desc 'Show this message'
 end
end
@executeDir = File.expand_path File.dirname(__FILE__)
@logFilePath = File.join(@executeDir, "pinglog.txt")
@ip = Resolv.getaddress Choice[:host]
@delay = Choice[:delay]
@icmp = Net::Ping::ICMP.new(host=@ip)
rtary = []
pingfails = 0
repeat = Choice[:repeatCount].to_i
puts 'starting to ping ' + Choice[:host] + ' at ' + @ip
(1..repeat).each do
 t = DateTime.now
 timestamp = t.strftime("%m/%d/%Y %H:%M:%S") # 07/24/2006 09:09:03
 @msg = '';
 begin
   if @icmp.ping
     rtary << @icmp.duration
     #puts "host replied in #{@icmp.duration}"
     @msg = '%4.0d ms host replied at %s' % [InMs(@icmp.duration), timestamp]
   else
     pingfails += 1
     @msg =" !!! timeout at %s" % timestamp
   end
   puts @msg
   File.open(@logFilePath, 'a') { |file| file.write(@msg + "\n") }
   rescue Exception => e
   puts e.message
   puts.e.backtrace.inspect
 end
 sleep @delay.to_i
end
avg = rtary.inject(0) {|sum, i| sum + i}/(repeat - pingfails)
#puts "Average round-trip is #{avg}\n"
puts 'Average round-trip is %d ms' % InMs(avg)
puts "#{pingfails} packets were dropped"

The screen output looks like this:

41 ms host replied at 12/19/2013 07:05:25
41 ms host replied at 12/19/2013 07:05:30
51 ms host replied at 12/19/2013 07:05:35
!!! timeout at 12/19/2013 07:05:40
24 ms host replied at 12/19/2013 07:05:50
32 ms host replied at 12/19/2013 07:05:55
51 ms host replied at 12/19/2013 07:06:00

Both resolved pings and timeouts are handled in the code as evident from above.

The log file format looks like this:

41 ms host replied at 12/19/2013 07:05:25
41 ms host replied at 12/19/2013 07:05:30
51 ms host replied at 12/19/2013 07:05:35
!!! timeout at 12/19/2013 07:05:40
24 ms host replied at 12/19/2013 07:05:50
32 ms host replied at 12/19/2013 07:05:55
51 ms host replied at 12/19/2013 07:06:00

Exactly the same (no surprise).

Launching if from the command line it accepts three parameters, the host to ping by name or ip, the delay between pings and the number of repetitions.

Typical launch:

ruby rubyping.rb -h google.com -d 5 -r 999

Ping google.com every 5 seconds for 999 times.  Given the latency of the ping, the actual delay is more like 12 second BUT in principle it works as desired.

AND, the way it’s written, it logs the pings to a log file. I did this for a follow on project I have in mind. To gather location data as I am traveling, merge that data with the ping log and come up with active/dead spots along my trip.

A few days ago I found an app for my Android phone called GPS Logger that logs the location of the phone via GPS to a file in a variety of formats. This data along with my ping log can now create the data I am after.

This led me to find open-source server software, OpenGTS project, that consumes GPS logs and report on it including visual mapping a la GIS. There’s an enterprise version of the software too if need be.

The next step is to merge the ping log with the GPS log and come up with a data source that can be used by other apps like OpenGTS.

 

Need Mcrypt for PHP on MAC – this way does work

Trying the PHP framework Laravel I found that the framework would not start without Mcrypt installed, the encryption library. After some research I realized that I’d have to build the LIB and this page gave very good instructions.

http://www.leonardaustin.com/technical/how-to-install-mcrypt-on-mac-osx10-7-and-osx10-6

A few things need to be understood:

  • Your MAC PHP is likely at one version and the available LIB code is later; find the right archive.
  • When you build the LIB there will likely be non-fatal errors; it’s ok.
  • You only need the .SO file and nothing more for PHP.

My MAC’s php version is 5.3.15 BUT all the releases are not obvious on the PHP repository so you go to the archive on this site and find you major.minor version which for me is 5.3 at the snapshot page.

I created the mcrypt directory and put the libmcrypt-2.5.8.tar.gz and php5.3-201301231830.tar.gz files in the directory and untarred them as directed.

Then, using the build instructions for the lib and php, I built them.

In the end I had on disk the .SO which is what I was after located at /usr/lib/php/extensions/no-debug-non-zts-20090626/mcrypt.so

I added to the end of the /private/etc/php.ini file the lineextension = /usr/lib/php/extensions/no-debug-non-zts-20090626/mcrypt.so

Then I restarted Apache and ran phpinfo() in a skeletal file and found that mcrypt was now installed.

I checked the default page for Laravel and there it was.

Excellent instructions.

Shutting down Apache on Mac Mountain Lion is not easy

Mountain Lion (OS 10.8) is at this time the newest version of the Mac OS and the changes to the Apache web server that ships with it lead many bloggers to misinform readers about controlling the server. Let’s set the record straight.

You want to shut Apache down so that it releases it’s locks on files and so that you can launch Apache in single process mode for easier debugging. While Apache is running you may not be able to edit your web site/app files if the server has locked them. Shutting the server down frees the locks letting you edit or replace files. Then, for easier debugging, you may want to start up Apache consuming one process. To start it up this way you first have to shut it down. These are both good reasons and this article shows you how to shut it down.

Blog after blog say to shut it down use apachectl

sudo apachectl stop

Well, this doesn’t work on Mountain Lion.

As it turns out there is a launch daemon for apache that restarts the stopped apache. To stop apache you have to unload the launch daemon.

sudo /System/Library/LaunchDaemons/launchctl unload org.apache.httpd.plist

To verify that you’ve stopped apache, use ps and grep

ps -ax | grep -i httpd

You should see the console session returned and no other lines with httpd in it. If you issue this several times in a row and the only thing returned is one line for the console sessions, you have successfully stopped the daemon.

Then to start a single process Apache

sudo /usr/sbin/httpd -k start -X -f /etc/apache2/httpd.conf

Once again use the ps and grep command line and you’ll see

/System/Library/LaunchDaemons: ps -ax | grep -i httpd
 6098 ttys002 0:00.00 grep -i httpd
 6094 ttys003 0:00.02 sudo /usr/sbin/httpd -k start -X -f /etc/apache2/httpd.conf
 6095 ttys003 0:00.17 /usr/sbin/httpd -k start -X -f /etc/apache2/httpd.conf
/System/Library/LaunchDaemons:

To find out how to do this took considerable time – many blogs say it’s just apachectl and it’s not. Hope this helps.

 

Ubuntu 11.04 boot hung on the splash screen, here’s the fix

Last night all was well with my Ubuntu laptop and I shut it down as normal. This morning it hung on boot at the splash screen with 5 dots lit up. Reading posts here and there suggested I should just let the machine cook for 15 minutes or so (something like Windows and it’s pre-boot installations of updates). So I left it alone for an hour and no improvement. The disk activity indicator almost from the point of the splash screen slowed to once or so a minute for the entire hour. Nothing was happening and waiting any longer would be pointless.

I read up on the grub recovery mode and jumped into the grub menu (hold the Shift key down while booting) and selected Recover Mode and in the console hit Control-D to get to the login prompt.

There, I tried a few things always suspecting it was the monitor/xserver/display. Something about the display kept gnawing at me but I cannot say what it was.

I hit upon a writeup that showed this:

Booted into recovery mode and logged in on command line.

Checked everything was up to date, reinstalled gdm and xorg:
$ sudo apt-get update && sudo apt-get upgrade
$ sudo apt-get install --reinstall gdm xorg

Removed xorg.conf so that a new one was created upon boot:
$ sudo mv /etc/X11/xorg.conf /etc/X11/xorg.conf.faulty
$ sudo reboot

This did work and after rebooting the only issue was I didn’t have the right display (NVidia) driver. It just so happened that a system update brought in the latest kernel changes and coincidentally gave me the right driver. I had to seek no further.

This took over 4 hours of my time to fix.

Remember what you did a few days ago on your Linux computer

I am busy like most people throughout the day and switch tasks often. Taking good notes during my work is tedious and there are times when I forget or cannot and I lose knowledge this way. I lose the  steps leading to solutions, I forget to record some item for billing purposes or I need to verify the length of time I spent doing something. What I want is someway to automatically take a snapshot of my monitor screen(s) producing a time stamped image. There are solutions in Windows and for Mac but none that I found for  Linux.

Shots folder

First, there’s a top-level folder I call shots as in snapshot that

Folder holding daily folders of images

holds all the folders for each days worth of images. The location and name of this folder can be changed in the script. I keep it in my home folder for convenience.

Daily folders
Each day the script checks for a folder to hold the images and if it doesn’t exist it creates it. The name of the daily folder is the date. Over time these folders will collect and build up taking valuable disk space. You should delete the oldest of these folders as a matter of good disk hygiene. Then again the script could be modified to search for folders that are older than a configurable number of days and remove the image contents and the folders.

Folders created every day containing images

Images
Desktop images are snapped in the current script every 300 seconds or 5 minutes and dropped into the daily folder in the shots folder. Scrot is configurable through the command line giving you the ability to reduce resolution of images, for instance, thereby reducing the size and clarity of the images if that’s a concern.

Inside a daily folder

Scrot has other configurable parameters so you should read the man page at least.

Here’s how you solve this in Linux. The recipe is simple:

  • 1 – screen capture utility
  • 1 – shell script

Fulfilling the first item, what we need is as simple screen capture utility to take a snapshot every so often and place the image in a known location. So there’s a single folder containing date stamped folders and each date stamped folder contains date and time stamped images. How do we do this? There are several utilities and applications in the Linux world that can take screen captures. The simple one that free that does the job more than adequately is SCROT. For Ubuntu,

sudo apt-get install scrot

Using scrot, it takes a screen capture, labels it with the date and time and move it into a known directory. It’s accomplished this way:

scrot ‘%Y-%m-%d_%H:%M:%S_$wx$h.png’ -e ‘mv $f ‘”$DIRECTORY”

Where $DIRECTORY is the path to a directory of your choosing.

Executing this command line yields a file named:

2012-07-24_18:56:14_1600x900.png

if executed on 7/24/2012 at 6:54. The photo is a 1600×900 pixel PNG file.

This works well. Then next and final thing to do in our recipe is create a shell script that periodically executes this command and each day change the date stamp. Here the bash shell script:


#!/bin/bash
# TODAY - today's date.
# DIRECTORY - directory to store the image in; ours is 'shots'.
TODAY=$(date +"%Y-%m-%d")
DIRECTORY="$HOME/shots/$TODAY"
# Repeat forever.
while true;
do
# If the directory does not exist, create it.
if [ ! -d "$DIRECTORY" ]; then
mkdir $DIRECTORY
fi
# take a shot and name it with a timestamp and move the shot
# to the 'shots' folder.
scrot '%Y-%m-%d_%H:%M:%S_$wx$h.png' -e 'mv $f '"$DIRECTORY"
# do again
sleep 5m
done

This script is setup to automatically run when you log in or is run manually, your choice.

 

Keep your camera updated

I get a new feature with my digital camera by updating it’s firmware, who knew you could update the camera?

It never dawned on me that me DLSR camera from Samsung would or even could be updated like my fancy mobile phone or my laptop computer. It never dawned on me but as I found out it can be updated and it was a pleasant surprise to boot.

I’ve had a Samsung NX10 for almost year and a half and have enjoyed it immensely. I have no real complaints except for the absense of a way shoot panoramic scenes. It’s not a big deal, I have a way to create panoramas from discrete photos by way of a Windows program called Hugin which works great. Still, there are times when I’d just like to take thise shoots and be done with it.

I went on the Samsung site and drilled down from the NX10 page to the downloads page to the firmware page. Found the latest firmware and the installation guide. The guide was simple as I expected it to be, obtained the firmware and had the camera use the firmware. After a minute the camera was updated and turning the camera mode to SCENE and I saw panarama mode with instructions on the screen. The instructions said basically shoot and move the camera, I clicked the shutter button and moved the camera slowly left to right surveying the room. The camera took perhaps 8 shots end when I clicked the bitton again. Hitting the review button I saw the stitched together long photo and felt the moment of satisfaction now knowing that I now had another tool in my little toolshed. Thr picture was really quite impressive and so is upgrading the firmware of the camera.

Have a digital camera, see if it can updated, you’ll appreciate it.

Rooting my Asus Transformer (TF101) and one of those scarry moments

Rooting a new tablet can be scarry (what if I brick it?), so finding a set of instructions that worked the first time is key and that’s what I present for the Asus Transformer TF101.

I’ve put off getting a tablet until now and for someone who works on being up to date with technology this was difficult to say the least. The Asus Transformer was the choice tablet to get given performance and price. I’ll leave others to argue the pros and cons on this, for me this was a good choice.

One thing though, root access is required for some of the functions that my tablet would undertake. An example is this – running a video player on the tablet pulling a ripped DVD from a remote server on my lan. The remote server has a disk carrying my ripped DVDs (I rip my own DVDs so that I can do just this, watch them from my pc or tablet remotely). The server needs to be a device that Linux (Android if you will) can access and to mount devices that are not in the existing /DEV and /MNT directories requires root access.

Googling around a bit I found the XDA-Developers forums which had a number of articles/videos explaining how to root the tablet.

These are the resources I used:

Video instructions
A list of resources from XDA-DEVELOPER
The Revolution HD Rom you could use to replace the stock Rom.

I followed the video instructions and reviewed the other resources I’ve just pointed you too and found that it worked wonderfully well.

I didn’t try the Rom and include it here for reference and as a possible future assignment for myself.

Bottom line – this really did work and worked well. I now have root access and it’s allowed me to do, just as I pointed out, setup a mounting point to the remote drive and now play dvds over my wireless right to my tablet.

Fix missing icons after upgrading Ubuntu 11.04 to 11.10

Upgrading Ubuntu 11.04 to 11.10 had a problem, the icons of some apps were gone. Here’s how I fixed it – easily.

Ubuntu 11.04 offered an upgrade to 11.10 which was easy enough to make just click the button and poof, the upgrade happens. But after the upgrade Unity showed a number of icons missing and the ubiquitous “no icon” document icon was evident for a number of apps.

Some googling proved worthless so I tried something myself – using the CompizConfig Setting Manager (ccsm) I changed the profile from Unity to Default and found Unity was gone and so was virtually any other control of
Ubuntu. So I went back into CCSM and changed the profile from Default to Unity. When complete the icon were back too!

I don’t know what happened in the upgrade and I don’t know why this fixed the missing icons. It worked however.

Stopping Skype’s HOME window from opening

So you log into Skype every time your PC starts and that darn HOME window appears too. All you want is your contacts list but there’s that window. I was wondering how to stop it and the options Skype makes available doesn’t seem to have a way to turn it off. Well the guy in this this blog entry described just how to do and it works well. I am repeating it here only for simplicy sake:

1. Close Skype
2. Go to c:\users\YOUR USER NAME\AppData\roaming\skype\shared_dynco
3. Open file dc.db in notepad,delete its content, save and close
4. In dc.db file properties set it up to read-only and confirm
5. Return a folder back and go to shared_httpfe
6. Repeat steps 3 and 4 in file queue.db

Don’t forget, make these empty files read-only.

On the way to fixing my Gem version problem (wrong rake version) I discover gedit

Gedit is a simple wysiwyg test editor and people want it to work like OS X’s TextMate too, here how.

So I’ve been using VIM for a while since it’s a little easier than VI and this is not saying too much really. I am sure it’s got it’s following but I prefer a GUI style editor, the command set is simpler and more consistent (cut, copy and paste for instance).

However, gedit is just a bit too primitive out of the box, I’d like some of the programmer support textmate offers. Here’s the steps to improve the experience originally found at SudoBits:

#1 Install gedit plugins

sudo apt-get install gedit-plugins

#2 Install TextMate Fonts

Download the font – Monaco, the TextMate default font  Then copy it to home->username ->.fonts (hit CTRL+H to see hidden files).

#3 Install Syntax color Scheme

Download the DarkMate theme coloring scheme and install it from gedit. To install open gedit and click on Edit -> Preferences -> Font & Colors. Then click on Add button and locate the download file(darkmate.xml).

#4 Install File Browser plugin

Download the class browser plugin ; Extract it to FileSystem-> usr-> share->gedit2->plugins. Then activate it by select the check box from gedit(Edit->Preferences->Plugins).

Now, Gedit is a gui-type editor for programming work on Linux’s with TextMate and you used it side-by-side other editors like Eclipse.