Howto: RESTful URI’s, Resources and Actions

Few insights, after working on a few projects where we decided to use REST style semantics for our HTTP end points.

Myths:

  1. RESTful is not meant for complex services that take multi faceted inputs from various resources and generate output not conforming to a single resource.
  2. RESTful URL conventions are limited, as they have only a few verbs and do not realize plethora of things that can be done with the command pattern.
  3. RESTful style of passing an ID in the URL leads to security breaches, as the ID is logged in weblogs/proxies ever over HTTPS, hence the style is flawed.
  4. RESTful style encourages avoidance of state maintenance.

Will elaborate on this later …


Howto: Shared remote desktop on a Linux box running X/Gnome.

On remote PC

  • Install ‘x11vnc’ – this merges and serves the current X session.
  • Start x11vnc:
    > x11vnc -display :0 -forever -shared -clear_all -threads -gui -scale 1280x720
    • – forever: keeps the server running forever {see -loop}
    • – shared: multiple clients can connect
    • -clear_all: reset pressed buttons {CAPS_LOCK}
    • -threads: thread / client
    • -gui: control tk based UI
    • -scale: target client screen size {see – xrandr}

On client PC

  • Install ‘xtightvncviewer’, any other client will also do.
  • Connect:
    > xtightvncviewer -compresslevel 2 -quality 3 $REMOTE_IP

Howto: Access Samba/fileshare on Linux correctly and reliably

Linux Distros (Ubuntu/Gnome) support browsing samba network shares out of the box, assuming Gnome/Nautilus.

Except, somethings that don’t work correctly always.

  • Preview of images.
  • Playback of media – music/movies (tried mplayer and totem)
  • The problem I imagine is Gnome/Nautilus is not mounting on/or not using Linux VFS.

Hence to get media preview / playback just mount it the standard way.

> mount -t cifs //$host/$i /mnt/smbfs_$host/$i -o username=$user,password=$pass

Good to always protect share with a password.

NOTE:

As of writing this I’m on Debian 8 Jessy. 

And as to yhy not NFS: I usually have dumped media files over a fileserver and find it convenient to share it over samba, since I have Windows users over my subnet.


Howto: Monitor [Linux] processes remotely using ps, ruby and gnulpot

Had been thinking a lot and (not doing anything) researching on the web to find methods to monitor specific application performances, i.e. %CPU, %RAM utilization, etc.

Having had a Lenovo G400 for about 8 years now with 1 GB RAM, and still making passes at compilers. Recent applications are all about user experience and all the unwanted glitter. And I had to be informed about ones that are a hog, to be blown out.

Finally got myself to script a monitoring.

In a nutshell, I launch the script to monitor resources using top and log to a file. Use the app, launch, do stuff, exit.Stop monitoring and generate the application performance profile.

> ./ptool.rb mtop
> ./ptool.rb mtop_kill
> ./ptool.rb mtop_report > mtop.csv

And a small script to generate the graph using gnuplot.
> ./mtop.plot

google-chrome-stable/Version 37.0.2062.120/Arch Linux 32bit
google-chrome

chromium/Version 37.0.2062.120 (281580)/Arch Linux 32bit
chromium

Below is the code listing:

#!/usr/bin/ruby
# name: ptool.rb
# author: bucket.size@gmail.com

require 'pp'

USER='jb'
PUSER='jb'
PNAME='chrom'

TI = { cpu: 8, mem:9 }

def mtop tag='sys', remote='localhost'
  cmd = "top -u #{PUSER} -d 2 -b > /tmp/top_#{tag}-#{remote}.log &"
  if remote != 'localhost'
    cmd = "rsh #{USER}@#{remote} "+cmd
  end
  puts cmd
  system(cmd)
end

def mtop_kill tag='sys', remote='localhost'
  cmd = "pkill -f \"top -u #{PUSER} -d 2 -b\" &"
  if remote != 'localhost'
    cmd = "rsh #{USER}@#{remote} "+cmd
  end
  system(cmd)
end

def mtop_report tag='sys', remote='localhost'
  r, t, acpu, amem =[], 0.0, 0.0, 0.0
  File.open("/tmp/top_#{tag}-#{remote}.log") do |f|
    acc=[]
    f.each_line do |line|
      # accum
      if (line =~ /#{PNAME}/)
        # puts line
        cpu, mem = line.split(" ")[TI[:cpu]].to_f, line.split(" ")[TI[:mem]].to_f
        acc << [cpu, mem]
      end

      # reduce/iter
      if (line =~ /^top/)
        #pp acc
        #puts '-------------'
        ccpu, cmem = 0.0, 0.0
        acc.each do |e|
          ccpu, cmem = ccpu+e[0], cmem+e[1]
        end
        acc=[]
        acpu, amem = acpu+ccpu, amem+cmem
        r << [t += 2, ccpu, cmem]
      end
    end
  end
  # average
  r << [acpu/r.size, amem/r.size]
  cvs_out r
  r
end

def cvs_out rlist
  av = rlist.pop
  puts "time,cpu,mem,acpu,amem"
  r = rlist.each do |e|
    puts "#{e[0]},#{e[1]},#{e[2]},#{av[0]},#{av[1]}"
  end
end

self.send ARGV[0]
#!/usr/bin/gnuplot
# adapted from (http://gnuplot.sourceforge.net/demo/histograms.1.gnu)
# name: mtop.plot
# author: bucket.size@gmail.com

#set terminal pngcairo  transparent enhanced font "arial,10" fontscale 1.0 size 800, 300 
set terminal pngcairo  enhanced font "arial,10" fontscale 1.0 size 800, 300
set output 'mtop.png'
set key inside right top vertical Right noreverse noenhanced autotitles nobox
set datafile missing '-'
#set style data linespoints
set style data lines
set xtics border in scale 1,0.5 nomirror rotate by -45  offset character 0, 0, 0 autojustify
set xtics  norangelimit font ",8"
set xtics   ()
set title "app. perf. data"
set datafile separator ","
plot 'mtop.csv' using 1:2 title '%cpu',\
      '' using 1:3 title '%mem',\
      '' using 1:4 title 'a-cpu',\
      '' using 1:5 title 'a-mem'


Howto: Setup Samba/fileshare on linux and access files remotely

Partitions can be automounted either by linux or nautilus on access.

set up partition mounts points correctly

Partitions can be automounted either by linux or nautilus on access.

  1. partitions automounted on access from nautilus don’t work well with samba

    this is something todo with ‘fuseblk’ driver or access parameter

    line from mtab that quirks with samba

    /dev/sda6 /media/swetha/sg fuseblk rw,nosuid,nodev,allow_other,default_permissions,blksize=4096 0 0

  2. edit /etc/fstab to manually configue mounting.

    below config works and is accessible for all users.

    /dev/sda6 /media/sg ntfs-3g defaults,windows_names,locale=en_US.utf8 0 0

  3. remount

    sudo umount /dev/sda6 sudo mount -a

set up samba share correctly

Nautilus provides a way to create temporary network shares.

  1. don’t create shares from nautilus, disable all shares managed by nautilus.
  2. install smbd, samba
  3. install system-config-samba.

    apt-get install system-config-samba

  4. create shares using system-config-samba run as root with sudo.

    sudo system-config-samba

    select the folder to which to partition is mounted

    set visible

    set access to users

  5. restart samba

    sudo service smbd restart

Given your ubuntu box IP address is 192.168.1.10.

You should now be able to access all the linux shares at \\192.168.1.10 or from another linux box at smb://192.168.1.10.


Filing my first RTI

Noted a discrepancy in claim settling with a bank. Decided to get the records of the recurrent deposits made.

RTI Act, 2005 is a Law that provides a machinery for a Citizen to exercise and realize his basic right – Right To Information.

Its actually quite simple to seek information under this Act and it is very legally binding.

I’ll elaborate more on that later. For starters, it’s just best to read up the Act publication. It’s about 27 pages in total, the first 14 being relevant and adequate for a first time applicant.

http://rti.gov.in/rti-act.pdf

The documentation does not provide for a requisition form template, hence created one for the above purpose – template.


Dawn unto the dinosaur

Been out of blogging for a while and now just out of curiosity, just logged in. Glad I started it a couple of years back. But it’s just that if feel the world or my thoughts have regressed. I’ve just become or am becoming like everybody else, in a way of saying that I’m becoming a nobody.

Well, actually I don’t really fret not blogging this whole duration of blog-a-llectual slumber. Its just that I had to switch between a couple of jobs to better my financial stand in order to find a bride and get married (the way it is in this geo-social-space ). Now having done that, feel a bit relieved.

Nevertheless the sense of accomplishment that was always a part of for having done nothing that add path to anything worth having, is being efferently evasive. That again to say that I have it but I don’t quite have it altogether as opposed to the saying earlier that I have it, but not quite yet.

I need to definitely clean up stuff, start from scratch, root & reboot.