2016年4月29日 星期五

MySQL備忘 ... CREATE TEMPORARY TABLE


    Temporary Table語法 
CREATE TEMPORARY TABLE tmp_name (
        id int(10) NOT NULL ,
        name VARCHAR(10) NOT NULL
ENGINE=InnoDB DEFAULT CHARSET=utf8;

建立Temppry Table:
        也可使用ENGINE = MEMBORY來做儲存.

    使用說明 
    建立Temporary Table僅有該連線可使用,其它連線無法使用(table不存在,table內資料也查詢不到)
    不同的連線,可建立相同名稱的temporary Table!
    Show Tables看不到Temporary table出現
    可使用index / PK / Unique

    應用 
    Session如果是以資料庫儲存的,可用temporary table來作儲存,登出後此table就drop。
    購物車的暫存機置,如無需保存到下次登入,可用Temporary table。
    大量資料的子集處理
在大的資料量下讓資料庫不斷的排序搜尋,非常影響資料庫效能的,這時可以建立小的temporary table
用複合語法來建立的資 料,例如將:
insert into tmp_data (data_id , data_name)
select data_id , data_name from data where data_id >1000 ;

    跨Table的合併資料查詢處理
以往對於跨table的索引方式,例如有CUSTOMER_A和CUSTOMER_B兩個table都有NAME和TIME欄位,如果要查詢NAME並用TIME排序也可用! 

2016年4月26日 星期二

Fail2ban 安裝與設定

apt-get update
apt-get install fail2ban
cp /etc/fail2ban/jail.conf /etc/fail2ban/jail.local
vim /etc/fail2ban/jail.conf

[asterisk-iptables]
# if more than 4 attempts are made within 6 hours, ban for 24 hours
enabled  = true
filter   = asterisk
action   = iptables-allports[name=ASTERISK, protocol=all]
              sendmail[name=ASTERISK, dest=dest@email.here, sender=fail2ban@address.here]
logpath  = /var/log/asterisk/security
maxretry = 4
findtime = 21600
bantime = 86400


vim /etc/fail2ban/filter.d/asterisk.conf
# Fail2Ban configuration file
#
# $Revision: 251 $
#

[INCLUDES]

# Read common prefixes. If any customizations available -- read them from
# common.local
before = common.conf


[Definition]

#_daemon = asterisk

# Option:  failregex
# Notes.:  regex to match the password failures messages in the logfile. The
#          host must be matched by a group named "host". The tag "<HOST>" can
#          be used for standard IP/hostname matching and is only an alias for
#          (?:::f{4,6}:)?(?P<host>\S+)
# Values:  TEXT
#
# Asterisk 1.8 uses Host:Port format which is reflected here

failregex = NOTICE.* .*: Registration from '.*' failed for '<HOST>:.*' - Wrong password
            NOTICE.* .*: Registration from '.*' failed for '<HOST>:.*' - No matching peer found
            NOTICE.* .*: Registration from '.*' failed for '<HOST>:.*' - No matching peer found
            NOTICE.* .*: Registration from '.*' failed for '<HOST>:.*' - Username/auth name mismatch
            NOTICE.* .*: Registration from '.*' failed for '<HOST>:.*' - Device does not match ACL
            NOTICE.* .*: Registration from '.*' failed for '<HOST>:.*' - Peer is not supposed to register
            NOTICE.* .*: Registration from '.*' failed for '<HOST>:.*' - ACL error (permit/deny)
            NOTICE.* .*: Registration from '.*' failed for '<HOST>:.*' - Device does not match ACL
            NOTICE.* .*: Registration from '\".*\".*' failed for '<HOST>:.*' - No matching peer found
            NOTICE.* .*: Registration from '\".*\".*' failed for '<HOST>:.*' - Wrong password
            NOTICE.* <HOST> failed to authenticate as '.*'$
            NOTICE.* .*: No registration for peer '.*' \(from <HOST>\)
            NOTICE.* .*: Host <HOST> failed MD5 authentication for '.*' (.*)
            NOTICE.* .*: Failed to authenticate user .*@<HOST>.*
            NOTICE.* .*: <HOST> failed to authenticate as '.*'
            NOTICE.* .*: <HOST> tried  to authenticate with nonexistent user '.*'
            VERBOSE.*SIP/<HOST>-.*Received incoming SIP connection from unknown peer
  


service fail2ban start

FreePBX 12 + Asterisk 13 安裝備忘

sudo -i
apt-get update && apt-get upgrade -y

apt-get install -y build-essential linux-headers-`uname -r` openssh-server apache2 mysql-server mysql-client bison flex php5 php5-curl php5-cli php5-mysql php-pear php5-gd curl sox libncurses5-dev libssl-dev libmysqlclient-dev mpg123 libxml2-dev libnewt-dev sqlite3 libsqlite3-dev pkg-config automake libtool autoconf git unixodbc-dev uuid uuid-dev libasound2-dev libogg-dev libvorbis-dev libcurl4-openssl-dev libical-dev libneon27-dev libsrtp0-dev libspandsp-dev libmyodbc

pear install Console_Getopt

A.         Install iksemel



cd /usr/src
wget https://iksemel.googlecode.com/files/iksemel-1.4.tar.gz
tar xf iksemel-1.4.tar.gz
rm iksemel-1.4.tar.gz
cd iksemel-*
./configure
或使用
./configure --with-libgnutls-prefix=/usr
make
make install



B.          Download Asterisk source files.



cd /usr/src
wget http://downloads.asterisk.org/pub/telephony/libpri/libpri-1.4-current.tar.gz
wget http://downloads.asterisk.org/pub/telephony/asterisk/asterisk-13-current.tar.gz
wget -O jansson.tar.gz https://github.com/akheron/jansson/archive/v2.7.tar.gz
wget http://www.pjsip.org/release/2.4/pjproject-2.4.tar.bz2



Compile and install pjproject


cd /usr/src
tar -xjvf pjproject-2.4.tar.bz2
rm -f pjproject-2.4.tar.bz2
cd pjproject-2.4
CFLAGS='-DPJ_HAS_IPV6=1'./configure --enable-shared --disable-sound --disable-resample --disable-video --disable-opencore-amr
make dep
make
make install



Compile and install libsrtp


cd /usr/src/
wget http://srtp.sourceforge.net/srtp-1.4.2.tgz
tar zxvf srtp-1.4.2.tgz
cd
srtp
autoconf
./configure
make
make
install
cp
/usr/local/lib/libsrtp.a /lib



C.         Compile and Install jansson



cd /usr/src
tar vxfz jansson.tar.gz
rm -f jansson.tar.gz
cd jansson-*
autoreconf -i
./configure
make
make install



D.        Compile and install Asterisk



cd /usr/src
tar xvfz asterisk-13-current.tar.gz
rm -f asterisk-13-current.tar.gz
cd asterisk-*
contrib/scripts/install_prereq install
./configure -with-crypto -with-ssl -with-srtp=/usr/local/lib
contrib/scripts/get_mp3_source.sh
make menuselect
make
make install
make config
ldconfig
update-rc.d -f asterisk remove
chkconfig asterisk on
apt-get install sysv-rc-conf
sysv-rc-conf asterisk on



# mkdir /etc/asterisk/keys
# cd /usr/src/asterisk-13.7.2/contrib/scripts
# ./ast_tls_cert -C sipim.tibtrade.net -O "tibtrade" -d /etc/asterisk/keys

Next, we generate a client certificate for our SIP device.
# ./ast_tls_cert -m client -c /etc/asterisk/keys/ca.crt -k /etc/asterisk/keys/ca.key -C sipim.tibtrade.net -O "tibtrade" -d /etc/asterisk/keys -o malcolm

Install Asterisk Soundfiles.
The 'make install' above installs a standard low-quality base sound file by default. This is suitable if you are on a small, underpowered system (such as a Rasberry Pi), but on a larger system you should install higher quality soundfiles.  Note that this installs the (8khz) 'wav' soundfiles and G722 (High Definition 'Wideband') audio.


cd /var/lib/asterisk/sounds
wget http://downloads.asterisk.org/pub/telephony/sounds/asterisk-core-sounds-en-wav-current.tar.gz
wget http://downloads.asterisk.org/pub/telephony/sounds/asterisk-extra-sounds-en-wav-current.tar.gz
tar xvf asterisk-core-sounds-en-wav-current.tar.gz
rm -f asterisk-core-sounds-en-wav-current.tar.gz
tar xfz asterisk-extra-sounds-en-wav-current.tar.gz
rm -f asterisk-extra-sounds-en-wav-current.tar.gz
# Wideband Audio download
wget http://downloads.asterisk.org/pub/telephony/sounds/asterisk-core-sounds-en-g722-current.tar.gz
wget http://downloads.asterisk.org/pub/telephony/sounds/asterisk-extra-sounds-en-g722-current.tar.gz
tar xfz asterisk-extra-sounds-en-g722-current.tar.gz
rm -f asterisk-extra-sounds-en-g722-current.tar.gz
tar xfz asterisk-core-sounds-en-g722-current.tar.gz
rm -f asterisk-core-sounds-en-g722-current.tar.gz



Install and Configure FreePBX

A.       Create the Asterisk user and set base file permissions.


useradd -m asterisk
chown asterisk. /var/run/asterisk
chown -R asterisk. /etc/asterisk
chown -R asterisk. /var/{lib,log,spool}/asterisk
chown -R asterisk. /usr/lib/asterisk
rm -rf /var/www/html



B.         A few small modifications to Apache.



sed -i 's/\(^upload_max_filesize = \).*/\120M/' /etc/php5/apache2/php.ini
cp /etc/apache2/apache2.conf /etc/apache2/apache2.conf_orig
sed -i 's/^\(User\|Group\).*/\1 asterisk/' /etc/apache2/apache2.conf
sed -i 's/AllowOverride None/AllowOverride All/' /etc/apache2/apache2.conf
service apache2 restart



C.         Configure ODBC

Edit /etc/odbcinst.ini and add the following. Note that this command assumes you are installing to a new machine, and that the file is empty. If this is not a freshly installed machine, please manually verify the contents of the file, rather than just copying and pasting the lines below. The 'EOF' does no go in the file, it simply signals to the 'cat' command that you have finished pasting.


cat >> /etc/odbcinst.ini << EOF
[MySQL]
Description = ODBC for MySQL
Driver = /usr/lib/x86_64-linux-gnu/odbc/libmyodbc.so
Setup = /usr/lib/x86_64-linux-gnu/odbc/libodbcmyS.so
FileUsage = 1
EOF


You may need to verify these paths, if you're not on a x86_64 machine. You can use the command `find / -name libmyodbc.so` to verify the location
Edit or create /etc/odbc.ini and add the following section. Note that, again, this command assumes you are installing to a new machine, and the file is empty. Please manually verify the contents of the files if this is not the case.


cat >> /etc/odbc.ini << EOF
[MySQL-asteriskcdrdb]
Description=MySQL connection to 'asteriskcdrdb' database
driver=MySQL
server=localhost
database=asteriskcdrdb
Port=3306
Socket=/var/run/mysqld/mysqld.sock
option=3 
EOF


D.        Download and install FreePBX.



cd /usr/src
wget http://mirror.freepbx.org/modules/packages/freepbx/freepbx-13.0-latest.tgz
tar vxfz freepbx-13.0-latest.tgz
rm -f freepbx-13.0-latest.tgz
cd freepbx
chmod +x /usr/sbin/fwconsole
./start_asterisk start
./install -n

2016年4月14日 星期四

Redmine 啟動背景執行 Shell Script

最近VirtualPC再Windows重啟後透過排程設定背景執行,所以往常Redmine手動啟動變得沒有途徑可以輸入,寫入vc.local啟動命令列方式看來也殘念了,找到以下這腳本目前運作OK~~

#!/bin/bash # Modify it to your configuration DIR=/var/www/html/redmine/ # Start Redmine in daemon mode. start(){ cd $DIR ruby script/rails server webrick -e production -d > log/redmine.log } # Stop Redmine daemon stop(){ RUBYPID=`ps aux | grep "rails" | grep -v grep | awk '{print $2}'` if [ "x$RUBYPID" != "x" ]; then kill -2 $RUBYPID fi } # Check if Redmine is running status(){ RUBYPID=`ps aux | grep "rails" | grep -v grep | awk '{print $2}'` if [ "x$RUBYPID" = "x" ]; then echo "* Redmine is not running" else echo "* Redmine is running" fi
}

case "$1" in
  start)
    start
    status
    ;;
  stop)
    stop
    sleep 2
    status
    ;;
  status)
    status
    ;;
  restart|force-reload)
    stop
    start
    ;;
  *)
    echo "Usage: $0 {start|stop|restart|force-reload|status}"
    exit 1
esac

2016年4月12日 星期二

RSYNC 同步資料備忘...

雙邊伺服器將SSL認證互相設定後,就不用進行輸入密碼驗證之程序。

指令 rsync -avz --rsh='ssh -p 1022' /來源目錄 Account@Host:/目的路徑
將"來源目錄"複製到"目的路徑"下

指令 rsync -avz --rsh='ssh -p 1022' /來源目錄/ Account@Host:/目的路徑
將"來源目錄/*.*"複製到"目的路徑"下

選項與參數:
-v :觀察模式,可以列出更多的資訊,包括鏡像時的檔案檔名等;
-q :與 -v  相反,安靜模式,略過正常資訊,僅顯示錯誤訊息;
-r :遞迴複製!可以針對『目錄』來處理!很重要!
-u :僅更新 (update),若目標檔案較新,則保留新檔案不會覆蓋;
-l :複製連結檔的屬性,而非連結的目標原始檔案內容;
-p :複製時,連同屬性 (permission) 也保存不變!
-g :保存原始檔案的擁有群組;
-o :保存原始檔案的擁有人;
-D :保存原始檔案的裝置屬性 (device)
-t :保存原始檔案的時間參數;
-I :忽略更新時間 (mtime) 的屬性,檔案比對上會比較快速;
-z :在資料傳輸時,加上壓縮的參數!
-e :使用的通道協定,例如使用 ssh 通道,則 -e ssh
-a :相當於 -rlptgoD ,所以這個 -a 是最常用的參數了!

2016年4月8日 星期五

備忘~JavaScript export Table to CSV File

中文處理上透過encodeURIComponent()編碼成UTF 8,直接用EXCEL開啟時若中文是亂碼那先檢查(可用NotePad++或UltraEdit之類編輯器檢視)檔案格式是否為 UTF-8 ( 有 BOM )

用PHPExcel()來匯出效果更好寫,不過在部分情況下不允許就將就著用。

1. JavaScript區塊
<script type="text/javascript">
function exportTableToCSV($table, filename) {
var $headers = $table.find('tr:has(th)')
,$rows = $table.find('tr:has(td)')

// Temporary delimiter characters unlikely to be typed by keyboard
// This is to avoid accidentally splitting the actual contents
,tmpColDelim = String.fromCharCode(11) // vertical tab character
,tmpRowDelim = String.fromCharCode(0) // null character

// actual delimiter characters for CSV format
,colDelim = '","'
,rowDelim = '"\r\n"';

// Grab text from table into CSV formatted string
var csv = '"';
csv += formatRows($headers.map(grabRow));
csv += rowDelim;
csv += formatRows($rows.map(grabRow)) + '"';

// 若欲輸出的檔案為 UTF-8 ( 無 BOM )
var csvData = 'data:application/csv;charset=utf-8,' + encodeURIComponent(csv);

// 若欲輸出的檔案為 UTF-8 ( 有 BOM )
var csvData = 'data:application/csv;charset=utf-8,' + encodeURIComponent('\uFEFF' + csv);

$(this)
.attr({
'download': filename
,'href': csvData
//,'target' : '_blank' //if you want it to open in a new window
});

//------------------------------------------------------------
// Helper Functions
//------------------------------------------------------------
// Format the output so it has the appropriate delimiters
function formatRows(rows){
return rows.get().join(tmpRowDelim)
.split(tmpRowDelim).join(rowDelim)
.split(tmpColDelim).join(colDelim);
}
// Grab and format a row from the table
function grabRow(i,row){

var $row = $(row);
//for some reason $cols = $row.find('td') || $row.find('th') won't work...
var $cols = $row.find('td');
if(!$cols.length) $cols = $row.find('th');

return $cols.map(grabCol)
.get().join(tmpColDelim);
}
// Grab and format a column from the table
function grabCol(j,col){
var $col = $(col),
$text = $col.text();

return $text.replace('"', '""'); // escape double quotes

}
}
 
//$('#export').click(function() {
$(".export").on('click', function (event) {
var outputFile = window.prompt("請輸入匯出之檔名") || 'export';
outputFile = outputFile.replace('.csv','') + '.csv'

// CSV
exportTableToCSV.apply(this, [$('#dvData>table'), outputFile]);
});
</script>

2. HTML區塊
<a href='#' class='export'>匯出</a>

<div style="display:none" id="dvData">
  <table>
      <tr><th>標題A</th><th>標題B</th></tr>
      <tr><td>資料A1</td><td>資料B1</td></tr>
      <tr><td>資料A2</td><td>資料B2</td></tr>
  </table>
</div>

2016年4月6日 星期三

FaceBook oAuth for WebSite

Main Url : https://developers.facebook.com/apps 

1. 先進行註冊後即可於個人FB左下角看到開發人員項目,點選"管理應用程式"

2. 建立新的FaceBook APP取得App ID


3. APP Dashboard 

   a.來取得 App Secret,程式呼叫時需要帶入
   b. 在Settings頁設定該APP相關資訊 
   c. 在App Review設定Public為Yes <- 請先檢查相關設定均已完成。

<?php
  $tokenurl='https://graph.facebook.com/oauth/access_token';
  $client_id='170145......';
  $redirect_uri='http://www.xxx.net/zz.php';
  $client_secret="6f5cbdea2bc......";

  //取得Login的Access_token
  $tokenurl=$tokenurl."?client_id=".$client_id."&redirect_uri=".$redirect_uri."&client_secret=".$client_secret."&code=".$_GET["code"]; 
  $xml = file_get_contents($tokenurl);

 $NewString = preg_split("/[&,]+/", $xml);
 $appsecret_proof = hash_hmac('sha256', $NewString[0], $client_secret);

  //取得Login_id 
  $GetUserUrl='https://graph.facebook.com/me?access_token='.$access_token.'&appsecret_proof='.$appsecret_proof;  

  $res = file_get_contents($GetUserUrl);  
  $arr = json_decode($res);

  echo $arr->id. $arr->name; 

?>

Asuswrt-Merlin SIP Passthrough setting

ASUS原廠的畫面無法設定,所以改用第三方的韌體。未設定的話會造成NAT穿透失敗致使一方聽不到聲音的情況,原先使用DD-WRT韌體處理,不過因實際套用的環境觀察其效能有不可預期之狀況故放棄之 ...

firmware download page : https://www.mediafire.com/folder/bkfq2a6aebq68/Asuswrt-Merlin

1.       Telnet進終端
l   nvram set nf_sip=0
l   nvram commit
l   reboot

2.      進階設定->Firewall
l   WAN pings (or Respond Ping Request from WAN) -> Enable -> Click Apply
l   Logged packets type -> Set to Both -> Click Apply

3.      進階設定->WAN
l   NAT Passthrough -> SIP Passthrough -> Disable -> Click Apply


2016年4月2日 星期六

MySQL error code 1286 Unknown storage engine 'InnoDB'" 錯誤處理紀錄

Azure上的DB因為LOG FILE因素Crash 重啟後出現 1286錯誤,在Mysql中innodb_plugin.so 載入是NO,INNODB引擎載入失敗所以DB出現未知錯誤。

經查LOG,buffer pool size=3G?? 這是甚麼神設定~這是後話...當初Google大神給的錯答案吧。

InnoDB: Initializing buffer pool, size = 3.0G
InnoDB: Error: cannot allocate 3221241856 bytes o
InnoDB: memory with malloc! Total allocated memory
InnoDB: by InnoDB 48401248 bytes. Operating system errno: 12
InnoDB: Check if you should increase the swap file or
InnoDB: ulimits of your operating system.
InnoDB: On FreeBSD check you have compiled the OS with
InnoDB: a big enough maximum process size.
InnoDB: Note that in most 32-bit computers the process
InnoDB: memory space is limited to 2 GB or 4 GB.

當時~感覺上就是記憶體空間不足,因為這是64位元電腦記憶體並沒有4GB限制,然後SWAP確實不足明顯Free Memory偏少,看來是Azure A1規模給的1.75GB RAM不夠用了造成,更改成A2規模有3.5GB的RAM後再重啟出現此錯誤。

Error: page 11704 log sequence number 0 1235697021
is in the future! Current system log sequence number 0 1226442301.
Your database may be corrupt or you may have copied the InnoDB tablespace but not the InnoDB log files. See http://dev.mysql.com/doc/refman/5.1/en/forcing-innodb-recovery.html for more information.

官網上就是說在my.cnf加上innodb_force_recovery = 1解釋就很清楚了但是Restart後依舊是
認得出InnoDB的Database,但是內容如Table, View ... 都消失了。看來是Log File搞的鬼...依照下面的步驟執行後就正常了。

  1. Revert any config changes you've made to the log file size and start MySQL again.
  2. In your running MySQL: SET GLOBAL innodb_fast_shutdown=0;
  3. Stop MySQL
  4. Make the configuration change to the log file size.
  5. Delete both log files. 不要刪掉ibData1這檔案
  6. Start MySQL. It will complain about the lack of log files, but it'll create them and all will be well.
PS. 記得把my.cnf裡面innodb_force_recovery設回0,不然DB是無法寫入的。

4/7 補充: 又crash了,更改設定值如下

innodb_flush_log_at_trx_commit  = 2   //Default = 1, 0:fast but unsafe
innodb_buffer_pool_size               = 256M
innodb_additional_mem_pool_size = 20M
innodb_lock_wait_timeout             = 50
innodb_log_file_size                      = 64M
innodb_log_buffer_size                  = 8M