2014年8月21日 星期四

[Linux] 安裝 nicstat @ Ubuntu 12.04

在 Ubuntu 14.04 只需用 atp-get install nicstat 即可,但在 12.04 就得自己編了:

$ sudo apt-get install gcc gcc-multilib
$ cd /tmp
$ wget -qO- http://nchc.dl.sourceforge.net/project/nicstat/nicstat-1.92.tar.gz | tar -xvzf -
$ cp /tmp/nicstat-1.92/Makefile.Linux /tmp/nicstat-1.92/Makefile
$ cd /tmp/nicstat-1.92 && make && sudo make install

[Linux] 使用常見的指令進行系統監控 @ Ubuntu 14.04

取得 Server 代號 - 使用 hostname 指令:

$ hostname

取得 Server OS 資訊 - 使用 lsb_release 指令:

$ lsb_release -a

取得 CPU 使用率 - 使用 vmstat、tail 和 awk 指令:

$ echo $((100-$(vmstat|tail -1|awk '{print $15}')))

取得 System Loading 資訊 - 使用 uptime 和 awk 指令:

$ uptime | egrep -o 'load average[s]*: [0-9,\. ]+' | awk -F',' '{print $1$2$3}' | awk -F' ' '{print $3}'
$ uptime | egrep -o 'load average[s]*: [0-9,\. ]+' | awk -F',' '{print $1$2$3}' | awk -F' ' '{print $4}'
$ uptime | egrep -o 'load average[s]*: [0-9,\. ]+' | awk -F',' '{print $1$2$3}' | awk -F' ' '{print $5}'


取得 Apache Web Server Processes 資訊 - 使用 pgrep 跟 wc 指令:

$ pgrep apache2 | wc -l

取得 Memory 使用率 - 使用 free、grep 跟 awk 指令:

$ free -m | grep Mem | awk '{print $3/$2 * 100}'

取得 Network 即時流量 - 使用 nicstat、grep 跟 awk,假定網路卡是 eth 開頭:

In:
$ nicstat | grep eth |  awk '{print $3}'

Out:
$ nicstat | grep eth |  awk '{print $4}'


最後,檢查上述指令是否都存在:

#!/bin/sh
CMD_USAGE=$(echo 'hostname curl pgrep wc awk tail uptime vmstat free nicstat' | tr ";" "\n")
for cmd in $CMD_USAGE
do
        path=`which $cmd`
        if [ -z $path ] || [ ! -x $path ] ; then
                echo "$cmd not found"
                exit
        fi
done


其他資源:

[Linux] 找尋 PHP 檔案內,用到 mysql_* 函數的檔案清單 @ Ubuntu 14.04

三個月前用過又忘了 Orz  還是筆記一下:

$ find /path/target -name "*.php" -exec sh -c 'cnt=`grep -c "mysql_" {}` && test $cnt -gt 0 && echo {}' \;

2014年8月19日 星期二

iOS 開發筆記 - 使用 NSURLConnection 取得 HTTP Content-type / MIME type Only

原先 NSURLConnection 本身就可以在 connection: didReceiveResponse: 時,從 NSURLResponse 中取得 MIMEType 了,但如果就只是需要 MIMEType 的話,不仿對 NSURLRequest 中,多設定 [req setHTTPMethod:@"HEAD"]; 資訊,如此一來,就只抓 Header 資訊而已。

- (void)checkMIMEType:(NSURL *)url
{
NSMutableURLRequest *req  = [NSMutableURLRequest requestWithURL:url];
[req setHTTPMethod:@"HEAD"];
[[NSURLConnection connectionWithRequest:req delegate:self] start];
}

- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response{
NSLog(@"didReceiveResponse: %@, URL: %@", [response MIMEType], connection.currentRequest.URL);
}

- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data {
NSLog(@"didReceiveData: %lu", [data length]);
}

- (void)connectionDidFinishLoading:(NSURLConnection *)connection {
NSLog(@"didReceiveLoading");
}


以上述的例子中,當 [req setHTTPMethod:@"HEAD"]; 時,不會跑進 connection: didReceiveData: 裡。

[Linux] 安裝與使用 influxDB @ Ubuntu 14.04

最近想搞系統監控圖表,雖然已經有一些常見做法,不過,還是先試一下新東西吧,用用最近很夯的 Grafana 圖表神器,但還是得搭配一套儲存系統,就先試試 influxDB 了。

首先,先裝 influxDB,用它記錄時間屬性的資料流:

$ wget http://s3.amazonaws.com/influxdb/influxdb_latest_amd64.deb
$ sudo dpkg -i influxdb_latest_amd64.deb
$ sudo service influxdb start
Starting the process influxdb [ OK ]
influxdb process was started [ OK ]


安裝後,就可以從 http://localhost:8083 登入,預設帳密 root/root。登入後,記得更改一下帳密,以下仍以 root/root 為例。



先建立一個 Database : server



若要新增資料,可以透過 POST API 進行,且 API 預設採用 8086 port:

例如建立一個 table 名為 http ,並有 id, val 兩個欄位,依序傳入 3 筆資料 ["server1", 23] , ["server1", 80], ["server1", 36], ["server2",70]

$ curl -X POST -d '[{"name":"http","columns":["id","val"],"points":[["server1",23]]}]' 'http://localhost:8086/db/server/series?u=root&p=root'

...


瀏覽時,使用 SELECT * FROM http WHERE id = 'server1' 即可:



未來就可以開放 API 提供 Grafana 撈資料出來。