2014年5月19日 星期一

[Linux] 透過 AWK 和 HOSTNAME 資訊執行 Sleep 指令 @ Ubuntu 14.04

最近開始埋一些 crontab 任務到各台機器,但是,有些指令是對 DB 動作的,而通常擺在 crontab 的 DB 指令是為了建出一些 snapshot 且多為 slow query 的,那如果多台機器全部都同時發送 DB query 時,反而又累到了 DB 系統。

這時候想到的方式就是搭配 sleep 的方式,並透過每檯機器的資訊避開一同發送要求,用法就想到 hostname 了:

$ awk '{s=0;for(i=1;i<=length($0);i++){c=sprintf("%c",substr($0,i,1));s+=c ;}; system( "sleep " s); }' /etc/hostname 

最後,在 crontab 上,就可以在待執行指令前面,先安插上述的 script 來跑,如:
  # m h dom mon dow command
*/10 * * * * sh /path/sleep.sh ; /path/job_exec

2014年5月17日 星期六

微型創業(Micro Startup? Personal Startup?) 費用規劃

中午吃飯時,想起前幾天跟學弟閒聊 startup 的趣事,有些人 startup 是為了工作機會,有些人是為了錢,有些人是為了興趣,不論是哪種想法,都是人生的一種規劃與執行。

這幾個月摸了 AWS 、Linode 和 Aliyun 後,深深覺得資金方面的規劃可以透過這些 VPS 服務來"限制"管理,反而人力、人才、成果進度才是最不穩定且需要花心力的地方 :P

就先來聊聊 Micro Startup 的可能的固定花費:
第一年一整年的費用約 (99+25+5) + (20+25*2)*12 + (12+10) = 991 美金/年,也等於一個月約 83 美金(做起來後再煩惱隔年的花費吧)。其它未計算的包含開發用的設備(如 OSX 系列的裝置等)

以 1:30 來算,算起來一個月約 2500 台幣的花費,其實負擔並不會太重 :)

所以,最重的花費其實是"人力時間"與"成果時間" Orz

2014年5月16日 星期五

[PHP] 簡易的 Web Service Unit Test: HTML validator、JSON Format @ Ubuntu 14.04

把 web service 移到 AWS 上後,想到時時刻刻都人工測試感到很 Orz 於是想了一些簡易 Unit Test 方式:
  • 檢驗網頁是否可讀取正常 (strlen)
  • 檢驗網頁 HTML 格式 (tidy 或 xmllint)
  • 檢驗網頁上的 link 個數 (preg_match_all)
  • 檢驗 JSON 格式 (json_decode && json_last_error)
剩下的就...再看看 XD

其他筆記:changyy / web-service-unit-test / phpunit / web-service-unit-test.php 

2014年5月15日 星期四

Linode - Free Upgrade、Restore from Backup、Resize 的時間花費

說真的 Linode 提供的服務對個人而言還滿好用的,只是對於公司營運 service 的不中斷的需求來說,則沒有 AWS 好用 Orz 雖然 Linode 也有 Node Balancer 可以用,但絕大部分的 High Availability 都仍透過 WebOps 負責處理,耗掉的人力並不低,而花在 AWS 的錢,其實就跟請 2~3 位 WebOps 的錢沒兩樣時,但底下的 WebOps 人力可以精簡又做得快樂時,何樂不為?

回到主題,這邊主要是趁 Free Upgrade 模式以及 Backup (Restore to) 機制,測試開一台機器所要耗費的時間,對 Linode 而言,原理都是把機器的硬碟資料 snapshot 起來,再搬到到另一台機器,啟動。所以最耗時的地方都落在資料搬移(複製)。



至於耗時為何需要在意?實在是使用 Linode 等 VPS 後,深深體會不能把這些機器都當做不會掛,某一派 SA 會覺得終於不用管"硬體"了,但這幾個月體驗了數次 Linode Hardware Issue 而直接強迫中斷服務,實在有夠痛 XD 雖然不用自己去修硬體,但服務中斷等同 WebOps 進入加班啊,因此,耗時對初期沒做 HA 的架構來說,就是代表要花多久解決中斷的服務,這時通常是透過 Backups 的 "Restore to" 。

此例分別以 Linode HDD Size 為例:
  • 48GB: 約 16 分鐘
  • 96GB: 約 23 分鐘
  • 384GB: 約 97 分鐘

2014年5月10日 星期六

[Linux] 使用 Nagios 監控 AWS EC2、RDS 服務狀況 @ Ubuntu 12.04

雖然 AWS 已經有不錯的服務監控系統,但原先已經用 Nagios 監控服務,就順手研究一下對於 EC2、RDS 的監控吧。

由於資安角度,預設都不給 PING 的,此外 RDS (此例是 MySQL port 3306) 則只開放特定 TCP Port 而已,對 Nagios 而言,就可以改測試 TCP Port (HTTP,HTTPS,SSH) 等用法,如此一來就可以解決 Host is Down 囉。

define host {
        host_name aws-rdb
        alias aws-rdb-dns
        address aws-rdb-dns
        use rdb-host
        contact_groups db-services
}

define host {
        host_name aws-dev
        alias aws-dev-dns
        address aws-dev-dns
        hostgroups ssh-servers,http-servers
        use ec2-host
        contact_groups pc-services
}

define command {
        command_name check-rds-host-alive
        command_line /usr/lib/nagios/plugins/check_tcp -H $HOSTADDRESS$ -p 3306
}

define command {
        command_name check-ec2-host-alive
        command_line /usr/lib/nagios/plugins/check_tcp -H $HOSTADDRESS$ -p 80
}

define host{
        name                            rdb-host    ; The name of this host template
        notifications_enabled           1       ; Host notifications are enabled
        event_handler_enabled           1       ; Host event handler is enabled
        flap_detection_enabled          1       ; Flap detection is enabled
        failure_prediction_enabled      1       ; Failure prediction is enabled
        process_perf_data               1       ; Process performance data
        retain_status_information       1       ; Retain status information across program restarts
        retain_nonstatus_information    1       ; Retain non-status information across program restarts
                check_command                   check-rds-host-alive
                max_check_attempts              10
                notification_interval           0
                notification_period             24x7
                notification_options            d,u,r
                contact_groups                  admins
        register                        0       ; DONT REGISTER THIS DEFINITION - ITS NOT A REAL HOST, JUST A TEMPLATE!
        }

define host{
        name                            ec2-host    ; The name of this host template
        notifications_enabled           1       ; Host notifications are enabled
        event_handler_enabled           1       ; Host event handler is enabled
        flap_detection_enabled          1       ; Flap detection is enabled
        failure_prediction_enabled      1       ; Failure prediction is enabled
        process_perf_data               1       ; Process performance data
        retain_status_information       1       ; Retain status information across program restarts
        retain_nonstatus_information    1       ; Retain non-status information across program restarts
                check_command                   check-ec2-host-alive
                max_check_attempts              10
                notification_interval           0
                notification_period             24x7
                notification_options            d,u,r
                contact_groups                  admins
        register                        0       ; DONT REGISTER THIS DEFINITION - ITS NOT A REAL HOST, JUST A TEMPLATE!
        }