Wednesday, October 16, 2013

Traffic Meter Geeklet




Traffic Meter



Difficulty: Advanced Users only



The Traffic Meter is to show your current In and Out Bandwidth for Internet Coeenctions

This Looks nice but ist also a bit tricky to setup while for the full Resolut as on the Screenshot you nee several Geeklets that overlay them self to achieve all the Effets

This Geekelt is static in graphical animation

So what do you need:

First you nee to download this ZIP File, it. contains 2 Images inside, the default Background for this and the Overlay with the Stripes.

Download Me

This makes right now 2 Image Geeklets, just drop them on the Screen, setup the URL of the Images and tweak the Size of them, thy need to be equal in size, a tip:

Use the Size Fields in the Geeklet Settings to add the Sizes Manually, alos, the y need to be placed at the same Position, but this comes later

Place the Background Image somewhere you want to keep your Geeklet visible later, fit it into Size and move on to Step 2.
Now drag a Script Geeklet on the Desktop, set its Update Interval to 5 seconds, a timeout after 4 seconds so Geektool does not blow up your CPU if a value arrives too late.
Open the EDIT Window, while OS X comes with native Ruby Language Support, we can use this right from here without further Installs

ruby

def kboutData 
value = %x(
myvar3=`netstat -ib | grep -e "en0" -m 1 | awk '{print $10}'` # bytes out
sleep 1
myvar4=`netstat -ib | grep -e "en0" -m 1 | awk '{print $10}'` # bytes out again
subout=$\(\($myvar4 - $myvar3\)\)
kbout=`echo "scale=2; $subout/1024;" | bc`
echo $kbout
)

return value 
end

def kbinData
value = %x(
myvar1=`netstat -ib | grep -e "en0" -m 1 | awk '{print $7}'` #  bytes in
sleep 1
myvar2=`netstat -ib | grep -e "en0" -m 1 | awk '{print $7}'` # bytes in again
subin=$\(\($myvar2 - $myvar1\)\)
kbin=`echo "scale=2; $subin/1024;" | bc`
echo $kbin
)
return value 
end

#Change to the Max Downspeed of your Network
maxIn = 4000
#Max Progress Bar Width in Chars
NewWMax = 22

#Max Upstream Speed of your Network
maxOut = 125
#Max Progress Bar, same Value as Down Stream should be here
NewMax2 = 22

class String
  # colorization
  def colorize(color_code)
    "\e[#{color_code}m#{self}\e[0m"
  end

  def red
    colorize(31)
  end

  def green
    colorize(32)
  end

  def yellow
    colorize(33)
  end

  def pink
    colorize(35)
  end
end

class Numeric
  def percent_of(n)
    self.to_f / n.to_f * 100.0
  end
end

kbin = kbinData
kbout = kboutData
#kbin = "4780"
#kbout = "130"
kbinTMP = kbin.to_i
if kbinTMP > maxIn
 kbinTMP = maxIn
end

kboutTMP = kbout.to_i 
if kboutTMP > maxOut
 kboutTMP = maxOut
end

kbinTMP2 = (kbinTMP).percent_of(maxIn)
tmp = (NewWMax.to_f/100) * kbinTMP2

kboutTMP3 = (kboutTMP).percent_of(maxOut)
tmp2 = (NewMax2.to_f/100) * kboutTMP3


char = ["2584".hex].pack("U")

if tmp <= 5
 puts (char * tmp).green
elsif tmp <= 10
 puts (char * tmp).yellow 
elsif tmp <= 15
 puts (char * tmp).pink
elsif tmp > 15
 puts (char * tmp).red
end



if tmp2 <= 5
 puts (char * tmp2).green
elsif tmp2 <= 10
 puts (char * tmp2).yellow 
elsif tmp2 <= 15
 puts (char * tmp2).pink
elsif tmp2 > 15
 puts (char * tmp2).red
end


What happens here:

First of all, change the LINE :

maxIn = 4000

This is the MAX Download Speed you can have, its important for Percentage Calculation
Once done, change this one  where you have to play with:

NewWMax = 22

This Line shows how many Boxes may be drawn, or better how many Chars are allowed for a full 100%  Download Speed, if your Bar gets too long later, you need to decrease this Setting, this Geeklet was made on a 27” iMac  and the Geeklet has aca.200p0x Width here, so it relies on Font Size, Resolution and Width of Geeklet, you will need to find your own value, try to match 100% tge Background Image Covering...

Once changed, there are 2 more Lines to change, the TotalOut Values are the next 2 Lines below, same procedure


The Script draws a Progress Bar in 4 Colors related to the Speed, so in this case 4000, so each 1000 it changes the color from green to red

Once done drag this Geeklet into position on top of ypur Background image so the bars will cover the blacked bars on the main Geeklet

Drag another Script Geeklet onto Desktop, open the Edit Window and add:


ruby

def kboutData 
value = %x(
myvar3=`netstat -ib | grep -e "en0" -m 1 | awk '{print $10}'` # bytes out
sleep 1
myvar4=`netstat -ib | grep -e "en0" -m 1 | awk '{print $10}'` # bytes out again
subout=$\(\($myvar4 - $myvar3\)\)
kbout=`echo "scale=2; $subout/1024;" | bc`
echo $kbout
)

return value 
end

def kbinData
value = %x(
myvar1=`netstat -ib | grep -e "en0" -m 1 | awk '{print $7}'` #  bytes in
sleep 1
myvar2=`netstat -ib | grep -e "en0" -m 1 | awk '{print $7}'` # bytes in again
subin=$\(\($myvar2 - $myvar1\)\)
kbin=`echo "scale=2; $subin/1024;" | bc`
echo $kbin
)
return value 
end


maxIn = 4000
NewWMax = 20

maxOut = 125
NewMax2 = 20

class String
  # colorization
  def colorize(color_code)
    "\e[#{color_code}m#{self}\e[0m"
  end

  def red
    colorize(31)
  end

  def green
    colorize(32)
  end

  def yellow
    colorize(33)
  end

  def pink
    colorize(35)
  end
end

class Numeric
  def percent_of(n)
    self.to_f / n.to_f * 100.0
  end
end

kbin = kbinData
kbout = kboutData
#kbin = "4780"
#kbout = "130"
kbinTMP = kbin.to_i
if kbinTMP > maxIn
 kbinTMP = maxIn
end

kboutTMP = kbout.to_i 
if kboutTMP > maxOut
 kboutTMP = maxOut
end

IN =  kbin.to_i
if IN > 1024  
 tmp123 = (IN.to_f / 1024)
 tmp456 = sprintf('%.2f', tmp123)
 INdse = tmp456.to_s + " Mb  "
else
 INdse = IN.round.to_s + " kb  "
end

puts INdse

puts kboutTMP.to_s + " kb"



This one is similar to the previous one but it doesnt draw any bars, it just outputs the Numbers in kb/s or mb/s if needed
Place this one onto the background image at the right side so the values may be above the thin white lines, Set the Update or refresh Interval to 5 seconds, add a timeout at 4 seconds to reduce CPU Load once a value was too hard to get so Geektool cancels a call

Finally drag the Overlay with the Stripes onto all the previous Geeklets and make it front most to be on top of all with “rightclick and set frontmost”

The tricky part is to place all the Geeklets at the right position, use the X and Y Fields in Settings to set position manually, its easier than with Mouse Drags...

And once finished and fine tuned everything, you get this or a similar Result, or at least it should look like this:



Final Note:

As mentioned at the TOP, this Geeklet is for advanced Users, it just works but it relies on too many Things like the Sizing on Screen and related to this the Char Count and and and...

Its just too tricky to make this a Click and Install version, you dont need to learn or understand Ruby, well the Code just works but the Appearance requires a bit more Attention, thats why such Geeklets are rarely posted, while they are too complex to explain right or hard to follow for Beginners

No comments:

Post a Comment