2015年12月29日 星期二

[Parser] - PHP 抓取網頁內容變成 DOM object

官方網站:http://simplehtmldom.sourceforge.net/
使用說明:http://simplehtmldom.sourceforge.net/manual.htm

抓取  simple_html_dom.php
# wget  http://sourceforge.net/projects/simplehtmldom/files/simple_html_dom.php/download

編輯程式
# vim getdata.php

<?PHP

include_once('simple_html_dom.php');
header("Content-Type:text/html; charset=utf-8");

$url = 'https://xxxx';
$html = file_get_html($url);
$ret = $html->find('a[class=list-button]');

// Find all links

foreach( $ret as $element)
$list[] =  $element->href ;
echo  '<pre>' ; print_r($t) ; echo '</pre>';

// 可把 array list 寫到 DB 或 變成 json 格式

?>

2015年12月28日 星期一

[Watch] - 偵測某各目錄下檔案變動 ifttt

環境 Debian 8
使用工具 :  inotify-tools

# apt-get install inotify-tools

使用  inotifywait 撰寫 shell

# vim watch.sh

#!/bin/sh
inotifywait -m 預監控的目錄 -e create -e moved_to |
while read path action file; do
echo "The file '$file' appeared in directory '$path' via '$action'"
cat content.txt | mail -s "[xxx] xx 通知  - $file"  fifi@xxxx -- -f fifi@xxx

# do something with the file
done


# chmod + x watch.sh
# runlevel

2015年12月23日 星期三

[Monitor] Zabbix 2.X



測試環境 : Debian8 64bit ( Vmware EXSI 後續可以用 Docker  ^^ )

G1 : 192.168.89.100 ( Zabbix Server   )
L1 : 192.168.89.211 ( Agent 1 )
L2 : 192.168.89.212 ( Agent 2 )
D1 : 192.168.89.111 ( Agent 3  )
D2 : 192.168.89.112 ( Agent 4  )

1. 安裝套件

# sudo apt-get install zabbix-server-mysql
# sudo apt-get install zabbix-frontend-php
# sudo apt-get install php5-mysql
# sudo apt-get autoremove php5-pgsql


2. 修改後台所需參數

# sudo vim /etc/php5/apache2/php.ini


php_value max_execution_time 300
php_value memory_limit 128M
php_value post_max_size 16M
php_value upload_max_filesize 2M
php_value max_input_time 300
php_value date.timezone ASIA/Taipei

3. 建立 DB 權限  User /  匯入資料庫

# mysql -u root -p
mysql> create database zabbix character set utf8 collate utf8_bin;
mysql> grant all privileges on zabbix.* to zabbix@localhost identified by 'xxxxxx';
mysql> flush privileges;

# zcat /usr/share/zabbix-server-mysql/{schema,images,data}.sql.gz | mysql -uzabbix -p

4. 設定 Zabbix Server 參數
# sudo vim /etc/zabbix/zabbix_server.conf

DBName=zabbix
DBUser=zabbix
DBPassword=xxxxxx
DBSocket=/var/run/mysqld/mysqld.sock

5. 設定後台網頁

#sudo vim /etc/zabbix/zabbix.conf.php
<?php
// Zabbix GUI configuration file
global $DB;

$DB['TYPE']     = 'MYSQL';
$DB['SERVER']   = 'localhost';
$DB['PORT']     = '3306';
$DB['DATABASE'] = 'zabbix';
$DB['USER']     = 'zabbix';
$DB['PASSWORD'] = 'xxxxxx';

// SCHEMA is relevant only for IBM_DB2 database
$DB['SCHEMA'] = '';

$ZBX_SERVER      = 'localhost';
$ZBX_SERVER_PORT = '10051';
$ZBX_SERVER_NAME = 'G1';

$IMAGE_FORMAT_DEFAULT = IMAGE_FORMAT_PNG;
?>

# ln -s /usr/share/zabbix  /var/www/html/

6. 啟動 Zabbix Server 服務

# sudo /etc/init.d/zabbix-server restart

7. 打開 Browser  輸入 http://G1/zabbix/
預設帳號/密碼 : Admin/zabbix

# Other 假設要設定中文打開 array 即可

# sudo vim /usr/share/zabbix/include/locales.inc.php
'zh_CN' => array('name' => _('Chinese (zh_CN)'),        'display' => true),
'zh_TW' => array('name' => _('Chinese (zh_TW)'),        'display' => true),

# 解決圖形亂碼

# copy Windows 某個字型  ex. C:\Windows\Fonts\微軟正黑體
# mv /tmp/msjhbd.ttf /var/www/html/zabbix/fonts/
# vim /var/www/html/zabbix/include/defines.inc.php
define('ZBX_FONT_NAME', 'msjhbd');
define('ZBX_FONT_NAME', 'msjhbd');

8. 安裝 zabbix-agent
#  sudo apt-get install zabbix-agent

修改 agent 參數
# sudo vim /etc/zabbix/zabbix_agentd.conf
Server=G1
ServerActive=G1
Hostname=G1



[HA] - Mariadb + Galera

參考 https://goo.gl/uF18Yg

測試環境 : Debian8 64bit   PS ( InnoDB  Only / Myisam 不支援 , 也不建議開啟 )

D1 : 192.168.89.111 ( Node 1  )
D2 : 192.168.89.112 ( Node 2  )

1. 設定 Node1 / Node2 主機名稱
# vim /etc/hosts

192.168.89.211  L1
192.168.89.212  L2
192.168.89.111  D1
192.168.89.112  D2
192.168.89.100  G1


2 MariaDB repo 加進系統
# sudo apt-get install python-software-properties
# sudo apt-key adv --recv-keys --keyserver keyserver.ubuntu.com 0xcbcb082a1bb943db
# sudo add-apt-repository 'deb http://ftp.yz.yamagata-u.ac.jp/pub/dbms/mariadb/repo/10.0/debian wheezy main'

3 安裝套件
# sudo apt-get update
# sudo apt-get install mariadb-galera-server-10.0


4. Node 1 & Node2 兩台都先進去 Mariadb 設定帳號
# mysql -uroot -p

MariaDB [(none)]> > GRANT USAGE ON *.* TO cluster@'%' IDENTIFIED BY 'xxxxxx';
MariaDB [(none)]> > GRANT ALL PRIVILEGES ON *.* TO cluster@'%';
MariaDB [(none)]> > GRANT USAGE ON *.* TO cluster@'localhost' IDENTIFIED BY 'xxxxxx';
MariaDB [(none)]> > GRANT ALL PRIVILEGES ON *.* TO cluster@'localhost';
MariaDB [(none)]> > FLUSH PRIVILEGES;

5. 先設定 Node 1  192.168.89.111
# vim /etc/mysql/conf.d/wsrep.cnf

[MYSQLD]
wsrep_provider=/usr/lib/galera/libgalera_smm.so
binlog_format="row"
wsrep_cluster_name="MYCLUSTER"
wsrep_node_address="192.168.89.111"
wsrep_node_name="db1"
wsrep_cluster_address="gcomm://"
wsrep_sst_method=rsync
wsrep_sst_auth=cluster:xxxxxx

6. Restart 192.168.89.111  MariaDB
# sudo service mysql restart

7.  設定 Node2
# vim  /etc/mysql.conf.d/wsrep.cnf

[MYSQLD]
wsrep_provider=/usr/lib/galera/libgalera_smm.so
binlog_format="row"
wsrep_cluster_name="MYCLUSTER"
wsrep_node_address="192.168.89.112"
wsrep_node_name="db2"
wsrep_cluster_address="gcomm://192.168.89.111"
wsrep_sst_method=rsync
wsrep_sst_auth=cluster:xxxxxx

# sudo service mysql restart

8. 驗證服務 
# mysql -uroot -p -e "SHOW STATUS LIKE 'wsrep%';"

9. 災難驗證 , 避免 Single point of failure
# 停掉服務 或 主機 ( D1 or D2 )