Chamnap Chhorn

Ruby, Rails, and JavaScript Developer

How Useful Is Monit?

| Comments

From here assuming that you know what monit is, but if you don’t, just go to see Monit. It’s basically a monitoring utility in unix system. You could monitor almost anything in your server. It’ll automatically restart the service if it stops and send you an email.

Generally, some people don’t like it very much because it send too many emails when your server is in trouble, and those emails are annoying. A part of my job is the servers’ maintainer and deployer. My job is to keep the servers fast and stable. By now, we have around 10 servers using Amazon Web Service. There are many services need to be run properly, otherwise my work’s inbox will contain some new emails. There are some ruby scripts as well need to run on the production server. The problem is those scripts are a bit heavy, it took more than 50% of the cpu to process, and if we more than 2 simultaneously, we’ll see some complaints from customers saying that “our websites are performing slowly. Why?”

Generally, we often lose our server because something is wrong on the server. We are really don’t know why. It happens on one server after another. Until I decide to use monit to monitor many services as possible. It seems when the ubuntu server is heavily processing, the sshd service is down. We never know it was down. We just can’t remote our server. The solution is to reboot, but next few days it happens again. Usually, when something weird happens during the startup, the sshd service is never up. How can we remote to those servers? Clearly, there are no way if that service is down.

I see the answer when I add monit on all servers, and I run some scripts on the server as usual. The next morning came up, two emails from monit saying “sshd is down” and “sshd is up”. (I didn’t notice about it until the next few days.) That’s good, isn’t it? I don’t have to reboot the server. When monit detects there is no such service, it simply restart that service. By now, I don’t really understand why sshd is always down when the server is heavily busy. I tried to upgrade to the latest AWS ami already, but the problem still exists. Maybe the server is too overloaded. The solution is to expand more servers or refactor those ruby processes.

Be sure to monit sshd service to all your server.

1
2
3
4
5
6
check process sshd with pidfile /var/run/sshd.pid
    group system
    start program  "/etc/init.d/ssh start"
    stop program  "/etc/init.d/ssh stop"
    if failed port 1234 protocol ssh then restart
    if 5 restarts within 5 cycles then timeout

You can checkout God as well if you want nicer syntax and more readable code.

Add Ssh Key to Your Ubuntu Server

| Comments

Add ssh-key to your ubuntu server is a common task for most developers to be able to remote your dedicated server via SSH. Normally, I do this with my local virtual box server. It’s fairly easy to do so. Here, I just copied the steps from Yoolk Wiki, written by my boss, Chris.

Generally, it works well on Ubuntu 9.04, but there is problem with Ubuntu 10.04. I’ll show you here:

  1. server: sudo apt-get update
  2. server: sudo apt-get install openssh-server
  3. server: mkdir .ssh
  4. client: ssh-keygen (don’t enter any values, press return three times, yes passwords should be blank)
  5. client: cat .ssh/id_rsa.pub - copy the output to the clipboard (very carefully, no pre/trailing white space)
  6. server: touch .ssh/authorized_keys
  7. server: sudo vim .ssh/authorized_keys - paste clipboard contents (in order to paste from clipboard, you must remote to your server by login through terminal console)

For Ubuntu 10.04, you must run this command ssh-add. If it adds duplicate keys, run ssh-add -D and run ssh-add again. Hope it could help.

High Performance Website at Hackerspacepp

| Comments

I’m a little bit disappointed in myself. What I intended to do presentation is about Web Scaling, but I ended up with go through the front end side. This presentation will focus on how to make your website faster on the front end side. I did focus on some aspects on HTTP concept as well. Those are rules from Steve Souders, but I modify and adjust to make it clear to the audience.

REST Presenation on DevCamp

| Comments

Last sunday, I talked to some people, around 10 software developers, about how to build web services using REST architecture. However, I didn’t talk on my previous slide presentation. I took on Doing REST Right by Kerry Buckley. I decided to choose this slide because I feel I learnt something new and it’s recommended by my boss, Chris. Actually, I were not well-prepared, but the presentation went well. I missed some slides before I went to do this presentation.

Meta-programming in Ruby and JavaScript

| Comments

Recently, I have been working with writing a ruby gem, Yoolk API Gem. What is really interesting for me is I do some meta programming and object-oriented programming in Ruby which I have never experienced before. A few month later, there is a requirement that my team needs to write in JavaScript, but I don’t want to touch JavaScript really much. Therefore, my team member took over this task. Whenever I write code in Ruby, I just try to think how to do it in JavaScript as well. Several things that came up to my mind with some from my team member:

  • Defer class from a variable
1
2
3
    var klass = "Person";
    p = new window[klass]; //class without namespace
    p = new yoolk[klass]; //class with namespace
1
2
3
    klass = "Person"
    p = Object.const_get(klass).new #class without namespace
    p = Yoolk.const_get(klass).new #class with namespace
  • Access class method from instance object
1
2
    var p = new Person();
    p.constructor.getCount();
1
2
    p = Person.new
    p.class.count
  • Define method of an object
1
2
3
4
    var p = new Person();
    p.hello = function() {
      alert('hello');
    };
1
2
3
4
    p = Person.new
    def p.hello
      puts "hello"
    end
  • Define class methods
1
2
3
    Person.hello = function() {
      alert('hello');
    };
1
2
3
    def Person.hello
      puts "hello"
    end

My Presentation at Barcamp Phnom Penh 2009

| Comments

I did two presentations at Barcampp in last week. One is for people who starts learning about the web. I talked on many basic concepts, the history, and how the web works. Some of friends complained me that my slide is too much. Anyway, I think I missed several main points. I just feel it is a prime concept of the web, and it drives me really well since I started learning it. The other is about JavaScript in OOP way. It is the same topic but very compact as I used to do presentation in my office. I did fairly good with borey since some of the audiences interest my presentation.

Maintaining Javascript Pop-Up Window Communication Across Window Opener Page Loads

| Comments

I came across a blog post that talks how to maintain a reference to javascript popup window while the parent window has been navigated away. This scenario doesn’t want to reload the child popup window. I just quoted out from 1 Pixel Out. There is a really nick trick.

In the main window:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
var popupWin = null;

function openPopup() {
   var url = "popup.htm";
   popupWin = open( "", "popupWin", "width=500,height=400" );
   if( !popupWin || popupWin.closed || !popupWin.doSomething ) {
      popupWin = window.open( url, "popupWin", "width=500,height=400" );
   } else {
      popupWin.focus();
   }
}

function doSomething() {
   openPopup();
   popupWin.doSomething();
}

In the popup:

1
2
3
4
5
self.focus();

function doSomething() {
   alert("I'm doing something");
}

http://www.bennadel.com/blog/89-Maintaining-Javascript-Pop-Up-Window-Communication-Across-Window-Opener-Page-Loads.htm, http://www.1pixelout.net/2005/04/19/cross-window-javascript-communication/, http://www.1pixelout.net/2006/12/15/cross-window-javascript-communication-20/, http://www.1pixelout.net/wp-content/downloads/popups20.zip