2015年1月25日 星期日

Instal Python Eggs

easy_install somepackage.egg

easy_install http://somehost.somedomain.com/somepackage.egg




References :
A small introduction to Python Eggs

2015年1月23日 星期五

MySQL show all grouped results

select t.Letter, t.Value
from MyTable t
inner join (
    select Letter, sum(Value) as ValueSum
    from MyTable
    group by Letter
) ts on t.Letter = ts.Letter
order by ts.ValueSum desc, t.Letter, t.Value desc




References :
sorting - MySQL show all grouped results and sort - Stack Overflow

Python read file don't include '\n'

with open(fname) as f:
    content = f.read().splitlines()




References :
string - Python: read file line by line into array - Stack Overflow

MySQL Error 1153 - Got a packet bigger than 'max_allowed_packet' bytes


因 MySQL 有單筆 SQL Query 長度的限制,在匯入檔案時,一次 INSERT 資料很大會錯誤,需調整單筆 SQL Query 長度

查詢目前 max_allowed_packet 設定值
mysql> show variables like 'max_allowed_packet';

更改爲 256MB (Client 需重新連結才會生效)

mysql> set global max_allowed_packet = 1024 * 1024 * 256;




References :
MySQL Error 1153 - Got a packet bigger than 'max_allowed_packet' bytes - Stack Overflow

2015年1月22日 星期四

MySQL 開啓檔案數


系統允許最大開啓檔案數
$ sysctl -a | grep fs.file-max
fs.file-max = 146925
$ cat /proc/sys/fs/file-max
146925
目前系統所有程式已開啓的檔案數
$ lsof | wc -l
18120
每個 shell 允許開啓檔案數
$ ulimit -n
1024
MySQL 最大允許開啓檔案數 (與 open_files_limit 變數設定 (default: 0) 或 ulimit 有關)
mysql> show variables like 'open_files_limit';
+------------------+-------+
| Variable_name | Value |
+------------------+-------+
| open_files_limit | 1024 |
+------------------+-------+
MySQL 開啓的檔案數
$ pgrep mysqld
1661
1988
$ ls /proc/1988/fdinfo/ | wc -l
248
$ lsof -a -d 1-999 -p 1988
MySQL 目前開啓的檔案數
mysql> show status like '%Open_files%';
+---------------+-------+
| Variable_name | Value |
+---------------+-------+
| Open_files | 221 |
+---------------+-------+

2015年1月19日 星期一

Enable CORS in ElasticSearch


ElasticSearch 1.4.0 版以上預設停用 CORS
$ vi /data/elasticsearch.yml
http.cors.enabled: true

$ docker run -d -p 9200:9200 -p 9300:9300 -v :/data dockerfile/elasticsearch /elasticsearch/bin/elasticsearch -Des.config=/data/elasticsearch.yml




References :
dockerfile/elasticsearch Repository | Docker Hub Registry - Repositories of Docker Images

2015年1月18日 星期日

send mail via Gmail from command line

# apt-get install heirloom-mailx 
 
$ vi ~/.mailrc
account gmail {
set smtp-use-starttls
set smtp=smtp://smtp.gmail.com:587
set smtp-auth=login
set smtp-auth-user=%REPLACE WITH YOUR E-MAIL ACCOUNT%
set smtp-auth-password=%REPLACE WITH YOUR PASSWORD%
set from=%NAME YOUR E-MAIL SOURCE. NO SPACES%
}
 
 
$ echo "Test Email" | mailx -v -A gmail -s "test" whereshouldIsendthisemail@domain.com 




References :
Can’t send e-mail from the Ubuntu command line – mailx: invalid option — ‘A’

Git remove subtree

$ git filter-branch --index-filter 'git rm --cached --ignore-unmatch -rf dir1 dir2 dirN file1 file2 fileN' --prune-empty -f HEAD
 
 
 
 
References :
How to remove previously added git subtree and its history - Stack Overflow

export MySQL database into separate files per table

#!/bin/bash

BACKUP_DIR=/tmp/db_backup                                                                                                                    
HOST='127.0.0.1'
USER='root'
PASSWORD='root'
DATABASE='mydb'

for T in `mysql -u $USER --password="$PASSWORD" -h $HOST -N -B -e "show tables from $DATABASE"`;
do
    echo "Backing up $T"
    mysqldump --skip-comments --compact -u $USER --password="$PASSWORD" -h $HOST $DATABASE $T > $BACKUP_DIR/$T.sql
done;




References :
Export MySQL Database into Separate Files per Table | JamesCoyle.net

2015年1月16日 星期五

Carbon Relay (graphite proxy)

carbon-relay-ng
$ export GOPATH=/some/path/
$ export PATH="$PATH:$GOPATH/bin"
$ go get -d github.com/graphite-ng/carbon-relay-ng
$ go get github.com/jteeuwen/go-bindata/...
$ cd "$GOPATH/src/github.com/graphite-ng/carbon-relay-ng"
$ git checkout v0.5
$ ./make.sh
$ vi carbon-relay-ng.ini
listen_addr # 接收資料的 port (代替 Carbon 的位置 (TCP 2003))
admin_addr # 使用 telnet 管理的 port
http_addr  # Web UI 的 port
$ ./carbon-relay-ng carbon-relay-ng.ini




References :
Graphite Scale Out - Adm.in Berlin
graphite-ng/carbon-relay-ng 

Alternative :
markchadwick/graphite-relay
grobian/carbon-c-relay

Collectd Installation

Install : 
# apt-get install collectd

Configuration :
# vi /etc/collectd/collectd.conf
Hostname "XXX"
BaseDir "/var/lib/collectd"
PIDFile "/var/run/collectd.pid"
PluginDir "/usr/local/lib/collectd"
TypesDB "/usr/local/share/collectd/types.db"
Interval 10
# 停用 rrdtool
# LoadPlugin rrdtool

# 啓用輸出到 graphite

LoadPlugin write_graphite





Host "localhost"
Port "2003"
Protocol "tcp"
LogSendErrors true
Prefix "collectd."
StoreRates true
AlwaysAppendDS false
EscapeCharacter "_"
# /etc/init.d/collectd restart



References :
How To Configure Collectd to Gather System Metrics for Graphite on Ubuntu 14.04 | DigitalOcean

2015年1月13日 星期二

configure.ac:27: error: must install xorg-macros 1.18 or later before running autoconf/autogen

# apt-get install xutils-dev

x11vnc

$ x11vnc -loop -rfbauth ~/.vnc/passwd
-loop (restarting the x11vnc process whenever it terminates)

$ x11vnc -bg -reopen -forever -rfbauth ~/.vnc/passwd
-bg - is running on the background
-reopen - after you will log in and the x11vnc will be terminated this swich renew x11vnc connection
-forever - after you close vnc client the x11vnc server won't close but keep "running"

Authentication
create password file
-storepasswd pass file (default file is ~/.vnc/passwd)
use authentication on RFB protocol
-rfbauth passwd-file




References :
hykang's blog: x11vnc指令教學 
[xubuntu] X11vnc as boot loaded service

CodeIgniter form validation functions

codeigniter/libraries/Form_validation.php

    public function required($str)
    public function regex_match($str, $regex)
    public function matches($str, $field)
    public function is_unique($str, $field)
    public function min_length($str, $val)
    public function max_length($str, $val)
    public function exact_length($str, $val)
    public function valid_email($str)
    public function valid_emails($str)
    public function valid_ip($ip, $which = '')
    public function alpha($str)
    public function alpha_numeric($str)
    public function alpha_dash($str)
    public function numeric($str)
    public function is_numeric($str)
    public function integer($str)
    public function decimal($str)
    public function greater_than($str, $min)
    public function less_than($str, $max)
    public function is_natural($str)
    public function is_natural_no_zero($str)
    public function valid_base64($str)
    public function prep_for_form($data = '')
    public function prep_url($str = '')
    public function strip_image_tags($str)
    public function xss_clean($str)
    public function encode_php_tags($str)

2015年1月10日 星期六

CodeIgniter Disallowed Key Characters error

fix your regex rule
$ vi system/core/input.php
if ( ! preg_match("/^[a-z0-9:_\/-|]+$/i", $str))
{
    exit('Disallowed Key Characters.');
}







References :
Disallowed Key Characters error / Forums / Community / EllisLab


tty1 auto login startx

auto login tty
http://a0726h77.blogspot.tw/2015/01/tty-auto-login.html

auto start X
$ vi ~/.bashrc
if [ -z "$DISPLAY" ] && [ $(tty) == /dev/tty1 ]; then
    startx
fi




References :
Debian User Forums • View topic - auto login and startx without a display manager - lenny

Disable screen blanking in X-Windows

# vi /etc/X11/xinit/xinitrc # or ~/.xinitrc
xset s off # don't activate screensaver
xset -dpms # disable DPMS (Energy Star) features.
xset s noblank # don't blank the video device




References :
Disable screen blanking in X-Windows on Raspbian - Raspberry Pi Stack Exchange

2015年1月9日 星期五

Enabling / Disabling upstart service

control job
# start foo
# stop foo 

disable service 
$ echo manual | sudo tee /etc/init/SERVICE.override
 
enable service  
$ sudo rm /etc/init/mysql.override 




References :
upstart - How to enable or disable services? - Ask Ubuntu

tty auto login

# apt-get install mingetty


Debian :
# vi /etc/inittab
# 1:2345:respawn:/sbin/getty 38400 tty1
1:2345:respawn:/sbin/mingetty --autologin root --noclear tty1

Ubuntu :
# vi /etc/init/tty1.conf
# exec /sbin/getty -8 38400 tty1
exec /sbin/mingetty --autologin root --noclear tty1




References :
Debian User Forums • View topic - auto login and startx without a display manager - lenny
How do I auto-login as root into the TTY upon boot? - Ask Ubuntu

xterm black background

$ xterm -bg black -fg white




References :
[SOLVED] How to make xterm black background by default (Page 1) / Newbie Corner / Arch Linux Forums

don't wrap command line output

$ some_command | less -S




References :
[ubuntu] How to set no wrapping in Terminal?

Systemd

傳統 init script 位置
$ ls /etc/init.d/*

systemd service 描述檔位置
$ ls /etc/systemd/system/*

令 systemd 重新載入設定檔,若有 service 描述檔,則會取代 init script
# systemctl daemon-reload

查看 service 資訊,可看到目前載入爲 service 描述檔
$ systemctl status crond
● cron.service - Regular background program processing daemon
   Loaded: loaded (/lib/systemd/system/cron.service; enabled)
   Active: active (running) since 五 2015-01-09 12:49:34 CST; 10h ago
     Docs: man:cron(8)
 Main PID: 536 (cron)
   CGroup: /system.slice/cron.service
           └─536 /usr/sbin/cron -f

Systemd 採用 System State 的方式,取代以往的 runlevel,當令一個 service 在開機後執行,則會在 /etc/systemd/system/multi-user.target.wants/ 下建立一個軟連結
$ systemctl [enable|disable] foo
$ ls /etc/systemd/system/multi-user.target.wants/

取得目前的 System-State (get current state)
$ systemctl get-default




References :
Systemd - FedoraProject

2015年1月7日 星期三

php array subtraction

$array1 = array('name', 'phone', 'created');
$array2 = array('created');
$result = array_diff($array1, $array2);

print_r($result);




References :
PHP: array_diff - Manual

2015年1月2日 星期五

Google Apps Script fetch external url

var response = UrlFetchApp.fetch("http://example.com/api/id=1");
var json = response.getContentText();
var data = JSON.parse(json);
Logger.log(data);




References :
External APIs - Google Apps Script — Google Developers
Class HTTPResponse - Google Apps Script — Google Developers