馬里奧

編程沒有什麼不好


  • 首頁

  • 標籤

  • 分類

  • 歸檔

Docker Selenium(2) 更改容器變量

發表於 2018-06-08 | 分類於 Selenium |

默認的Docker Selenium 只提供一個Browser Session可以用,每當有超過兩個Browser使用時,總是有一個在等待,這樣對於測試效率大大的降低,而建置時只要設好變量,就可以一次開10個…

加入變量

1
2
3
4
5
# Browser 最大到10個
NODE_MAX_SESSION: 10
NODE_MAX_INSTANCES: 10
# VNC 沒有密碼
VNC_NO_PASSWORD: 1

docker-compose.yml

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
# To execute this docker-compose yml file use docker-compose -f <file_name> up
# Add the "-d" flag at the end for deattached execution
version: '2'
services:
firefox:
image: selenium/node-firefox-debug:3.12.0-boron
volumes:
- /dev/shm:/dev/shm
depends_on:
- hub
environment:
HUB_HOST: hub
NODE_MAX_SESSION: 10
NODE_MAX_INSTANCES: 10
ports:
- "5900:5900"

chrome:
image: selenium/node-chrome-debug:3.12.0-boron
volumes:
- /dev/shm:/dev/shm
depends_on:
- hub
environment:
HUB_HOST: hub
NODE_MAX_SESSION: 10
NODE_MAX_INSTANCES: 10
ports:
- "5901:5900"
hub:
image: selenium/hub:3.12.0-boron
ports:
- "4454:4444"

執行結果

更多變量

透過docker inspect NAME|ID看容器變量。

Selenium hub 變量

selenium/hub:3.12.0-boron

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
"Env": [
"PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin",
"DEBIAN_FRONTEND=noninteractive",
"DEBCONF_NONINTERACTIVE_SEEN=true",
"TZ=UTC",
"GRID_MAX_SESSION=5",
"GRID_NEW_SESSION_WAIT_TIMEOUT=-1",
"GRID_THROW_ON_CAPABILITY_NOT_PRESENT=true",
"GRID_JETTY_MAX_THREADS=-1",
"GRID_CLEAN_UP_CYCLE=5000",
"GRID_BROWSER_TIMEOUT=0",
"GRID_TIMEOUT=30",
"GRID_DEBUG=false",
"GRID_HUB_PORT=4444",
"GRID_HUB_HOST=0.0.0.0"
],

Node 變量

selenium/node-chrome-debug:3.12.0-boron

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
"Env": [
"NODE_MAX_INSTANCES=10",
"NODE_MAX_SESSION=10",
"HUB_HOST=hub",
"VNC_NO_PASSWORD=1",
"PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin",
"DEBIAN_FRONTEND=noninteractive",
"DEBCONF_NONINTERACTIVE_SEEN=true",
"TZ=UTC",
"LANG_WHICH=en",
"LANG_WHERE=US",
"ENCODING=UTF-8",
"LANGUAGE=en_US.UTF-8",
"LANG=en_US.UTF-8",
"SCREEN_WIDTH=1360",
"SCREEN_HEIGHT=1020",
"SCREEN_DEPTH=24",
"DISPLAY=:99.0",
"NODE_HOST=0.0.0.0",
"NODE_PORT=5555",
"NODE_REGISTER_CYCLE=5000",
"NODE_POLLING=5000",
"NODE_UNREGISTER_IF_STILL_DOWN_AFTER=60000",
"NODE_DOWN_POLLING_LIMIT=2",
"NODE_APPLICATION_NAME=",
"DBUS_SESSION_BUS_ADDRESS=/dev/null"
],

zabbix2 + centos7建置筆記

發表於 2018-06-08 | 分類於 Zabbix |

1. LAMP

Linux,Apache,MariaDB,PHP

Linux

安裝centos7。

Apache

1
yum install httpd

跟著回答 “y” 後便會完成安裝, 然後輸入以下指令啟動及設定 Apache 開機自動執行:

1
2
systemctl start httpd
systemctl enable httpd

這時 Apache 已經啟動了, 可以在瀏覽器輸入伺服器的位置試試, 例如 http://localhost

MariaDB

執行以下指令安裝 MariaDB:

1
yum install mariadb-server mariadb

![](https://i.imgur.com/LM7jIy7.png

啟動及設定開機自動執行 MariaDB:

1
2
systemctl start mariadb
systemctl enable mariadb

執行以下指令設定 MariaDB 的 root 密碼, 預設是空密碼, 所以建議盡快修改:

設定 mysql

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
mysql_secure_installation


/usr/bin/mysql_secure_installation: line 379: find_mysql_client: command not found
NOTE: RUNNING ALL PARTS OF THIS SCRIPT IS RECOMMENDED FOR ALL MariaDB
SERVERS IN PRODUCTION USE! PLEASE READ EACH STEP CAREFULLY!
In order to log into MariaDB to secure it, we'll need the current
password for the root user. If you've just installed MariaDB, and
you haven't set the root password yet, the password will be blank,
so you should just press enter here.
Enter current password for root (enter for none):
OK, successfully used password, moving on...
Setting the root password ensures that nobody can log into the MariaDB
root user without the proper authorisation.


是否更新root使用者密碼或者N
Set root password? [Y/n] y ## Enter Y and press Enter
New password: ## Enter new password
Re-enter new password: ## Enter password again
Password updated successfully!
Reloading privilege tables..
... Success!
By default, a MariaDB installation has an anonymous user, allowing anyone
to log into MariaDB without having to have a user account created for
them. This is intended only for testing, and to make the installation
go a bit smoother. You should remove them before moving into a
production environment.

是否要刪除匿名使用者
Remove anonymous users? [Y/n] y ## Enter Y and press Enter
... Success!
Normally, root should only be allowed to connect from 'localhost'. This
ensures that someone cannot guess at the root password from the network.

是否不允許遠端登入root
Disallow root login remotely? [Y/n] y ## Enter Y and press Enter
... Success!
By default, MariaDB comes with a database named 'test' that anyone can
access. This is also intended only for testing, and should be removed
before moving into a production environment.

是否刪除測試資料庫
Remove test database and access to it? [Y/n] y ## Enter Y and press Enter
- Dropping test database...
... Success!
- Removing privileges on test database...
... Success!
Reloading the privilege tables will ensure that all changes made so far
will take effect immediately.

是否重新讀取權限
Reload privilege tables now? [Y/n] y ## Enter Y and press Enter
... Success!
Cleaning up...
All done! If you've completed all of the above steps, your MariaDB
installation should now be secure.
Thanks for using MariaDB!

完成後可以用測試一下 MariaDB 是否已經啟動:

1
mysql -u root -p

PHP

1
yum install php php-mysql php-gd php-pear -y

Test PHP:

testphp.php

1
vi /var/www/html/testphp.php

加入以下幾行

1
2
3
<?php 
phpinfo();
?>

重啟 httpd service:

1
systemctl restart httpd

2. zabbix

安裝zabbix 2.4

安裝EPEL repository

1
yum install epel-release

加入zabbix相關源

1
2
rpm --import http://repo.zabbix.com/RPM-GPG-KEY-ZABBIX
rpm -Uv http://repo.zabbix.com/zabbix/2.4/rhel/7/x86_64/zabbix-release-2.4-1.el7.noarch.rpm

安裝zabbix-server

1
yum install zabbix-server-mysql zabbix-web-mysql zabbix-agent zabbix-java-gateway

更改zabbix server時區

編輯 /etc/httpd/conf.d/zabbix:

1
vi /etc/httpd/conf.d/zabbix.conf

更改時區

php_value date.timezone Asia/Taipei

重啟服務

1
2
Restart the httpd
systemctl restart httpd

創建zabbix群組及使用者

在終端上輸入命令

1
2
groupadd zabbix 
useradd zabbix -g zabbix

創建DB使用者及資料庫

登入 MariaDB

1
mysql -u root -p

建立zabbix資料庫及zabbix使用者

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
Welcome to the MariaDB monitor. Commands end with ; or \g.
Your MariaDB connection id is 10
Server version: 5.5.41-MariaDB MariaDB Server
Copyright (c) 2000, 2014, Oracle, MariaDB Corporation Ab and others.
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

MariaDB [(none)]> create database zabbix character set utf8;
Query OK, 1 row affected (0.05 sec)

MariaDB [(none)]> grant all privileges on zabbix.* to 'zabbix'@'localhost' identified by 'zabbix';
Query OK, 0 rows affected (0.21 sec)

MariaDB [(none)]> flush privileges;
Query OK, 0 rows affected (0.00 sec)

MariaDB [(none)]> exit
Bye

導入zabbix模板資料庫

1
2
3
4
5
mysql -u root -p zabbix < /usr/share/doc/zabbix-server-mysql-2.4.8/create/schema.sql

mysql -u root -p zabbix < /usr/share/doc/zabbix-server-mysql-2.4.8/create/images.sql

mysql -u root -p zabbix < /usr/share/doc/zabbix-server-mysql-2.4.8/create/data.sql

設置zabbix與資料庫連接

編輯 /etc/zabbix/zabbix_server.conf

1
vi /etc/zabbix/zabbix_server.conf

確認剛剛設定好的資料庫名、使用者、密碼

  • DBName
  • DBUser
  • DBPassword
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
LogFile=/var/log/zabbix/zabbix_server.log
LogFileSize=0

PidFile=/var/run/zabbix/zabbix_server.pid

DBHost=localhost
DBName=zabbix
DBUser=zabbix
DBPassword=zabbix
DBSocket=/var/lib/mysql/mysql.sock

StartPollers=20
StartIPMIPollers=0
StartPollersUnreachable=1
StartTrappers=5
StartPingers=1
StartDiscoverers=5
StartHTTPPollers=1

SNMPTrapperFile=/var/log/snmptt/snmptt.log

CacheSize=8M
StartDBSyncers=4
HistoryCacheSize=32M
TrendCacheSize=4M
ValueCacheSize=32M
Timeout=3
TrapperTimeout=300
UnreachablePeriod=45
UnavailableDelay=60
UnreachableDelay=15

AlertScriptsPath=/usr/lib/zabbix/alertscripts
ExternalScripts=/usr/lib/zabbix/externalscripts

編輯php設定檔,進行調優

1
vi /etc/php.ini

修改php.ini內的數值。

1
2
3
4
5
6
max_execution_time = 600
max_input_time = 600
memory_limit = 256M
post_max_size = 32M
upload_max_filesize = 16M
date.timezone = Asia/Taipei

讓防火牆開啟10050及10051

1
2
3
4
Adjust Firewall and SELinux settings
Adjust iptables to allow the zabbix ports 10050 and 10051.
firewall-cmd --permanent --add-port=10050/tcp
firewall-cmd --permanent --add-port=10051/tcp

重新啟動防火牆

1
systemctl restart firewalld

3. 啟動zabbix

用瀏覽器進入http://yourip/zabbix,開始初步設定zabbix。

檢查PHP服務

連線資料庫(輸入user跟passwd)

設定初始名稱

再次確認所有狀態

設置完畢

成功!zabbix主頁

常見問題

[問題] zabbix-server沒有啓動成功


登入zabbix後會出現”Zabbix server is not running: the information displayed may not be current.”

如何解決:

查看zabbix-server狀態

1
systemctl status zabbix-server

看到zabbix-server並沒有啓動成功

啓動zabbix-server

1
systemctl start zabbix-server

啓動後 在看一次zabbix-server狀態

看到綠色文字的地方已經顯示成功啓動

在刷新一次網頁

這樣就沒有提示錯誤,這是比較簡單的除錯方法,如果還不能解決有可能是端口沒有開放導致,可以先netstat查看端口。

查看zabbix_server端口

1
netstat -tulpn | grep zabbix_server

zabbix-agent Error

如果agent端出錯報紅,可以先參考上方zabbix-server解決方式進行排除。

可查看zabbix_server及zabbix_agentd的日誌
/var/log/zabbix/zabbix_server.log(是否有啟動錯誤)
/var/log/zabbix/zabbix_agentd.log(是否正常連線)

如假設都資料庫及zabbix_server皆已正常,建議查看Zabbix_server.conf的配置

檔案參考連結(Zabbix_server.conf) 帶新增

[問題] 選單中沒有繁體中文

修改zabbix源文件

1
vim /usr/share/zabbix/include/locales.inc.php

找出’zh_TW’

大概在56行的位置將display改成true,如下圖。

1
'zh_TW' => array('name' => _('Chinese (zh_TW)'),        'display' => true),

修改後就可以設定繁體中文了

[問題] 圖形 中文亂碼


修改zabbix 介面無法顯示中文字體的錯誤

如何解決:

下載微軟雅黑字型檔

1
2
3
wget https://github.com/StellarCN/scp_zh/raw/master/fonts/SimHei.ttf
chmod 755 SimHei.ttf
sudo mv SimHei.ttf /usr/share/zabbix/fonts/SIMHEI.ttf

修改配置文件

1
sudo vim /usr/share/zabbix/include/defines.inc.php

搜尋兩個名稱並修改對應字型

ZBX_GRAPH_FONT_NAME

1
2
3
define('ZBX_GRAPH_FONT_NAME',           'graphfont');
改爲
define('ZBX_GRAPH_FONT_NAME', 'SIMHEI');

ZBX_FONT_NAME

1
2
3
define('ZBX_FONT_NAME', 'graphfont');
改爲
define('ZBX_FONT_NAME', 'SIMHEI');

或也可以直接將原字型graphfont取代成SIMHEI

1
2
# 在VIM內輸入
:1,$s/graphfont/SIMHEI/g

在刷新一次圖形頁面

Docker Selenium(1) 搭建服務

發表於 2018-06-08 | 分類於 Selenium |

Docker Selenium能讓Selenium在Docker中運行,可加速建置時間及獨立出各瀏覽器的版本
,保持了一定的隔離性,是非常好的測試環境。

docker-selenium 官方文檔

鏡像介紹

selenium/hub: Grid Hub,相當於一個空白的Seleniun Server,
selenium/node-chrome: Chrome節點,需加入Grid Hub才能使用。
selenium/node-firefox: Firefox節點,需加入Grid Hub才能使用。
selenium/node-chrome-debug: Chrome節點,同時包含VNC Server,需加入Grid Hub才能使用。
selenium/node-firefox-debug: Firefox節點,同時包含VNC Server,需加入Grid Hub才能使用

selenium/standalone-chrome: 包含Chrome的Seleniun Server,可直接調用。
selenium/standalone-firefox: 包含Chrome的Seleniun Server,可直接調用。
selenium/standalone-chrome-debug: 包含Chrome的Seleniun Server,同時包含VNC Server,可直接調用。
selenium/standalone-firefox-debug: 包含Chrome的Seleniun Server,同時包含VNC Server,可直接調用。

使用

docker conpose方法

selenium 官方提供了docker compose方法,直接使用就可以輕鬆搭建了。

官方連結

  1. docker-compose.yaml
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
# To execute this docker-compose yml file use docker-compose -f <file_name> up
# Add the "-d" flag at the end for deattached execution
version: "3"
services:
selenium-hub:
image: selenium/hub:3.12.0-boron
container_name: selenium-hub
ports:
- "4448:4444"
chrome:
image: selenium/node-chrome:3.12.0-boron
depends_on:
- selenium-hub
environment:
- HUB_HOST=selenium-hub
- HUB_PORT=4444
firefox:
image: selenium/node-firefox:3.12.0-boron
depends_on:
- selenium-hub
environment:
- HUB_HOST=selenium-hub
- HUB_PORT=4444
  1. docker-compose up --build

  1. 搭建成功

在docker-compose.yaml中我指定公開的是4448端口,打開頁面看到可以成功訪問,能調用的瀏覽器有Chrome及Firefox.

Python 調用

調用方法與本地搭建selenium server相同,以下以chrome為例。

  1. 創建一個Chrome對象
1
2
3
4
5
6
7
from selenium import webdriver
from selenium.webdriver.common.desired_capabilities import DesiredCapabilities

browser = webdriver.Remote(
command_executor='http://192.168.8.22:4448/wd/hub',
desired_capabilities=DesiredCapabilities.CHROME,
)

如果成功調用,在slenium頁面也會看到一個chrome正在被使用。

  1. 進入Google首頁,試著打印網頁標題
1
2
3
browser.get('http://www.google.com')
print(browser.title)
# Google
  1. 測試截取當前頁面
1
browser.save_screenshot("chrome.png")

  1. 釋放瀏覽器
1
browser.quit()

結束調用,slenium頁面上的chrome已經變回彩色的。

另外一種模式 : Debug Mode

Docker Selenium 還有一種 Debug Mode,主要提供了有畫面的selenium,能直接看到瀏覽器在做什麼,Debug Mode使用了VNC服務器並佔用了5900端口,但要注意的是每個端口只能運行一個節點(Chrome、Firefox等),如果希望包含兩個節點或更多,那麼就必須公開不同的端口,如5900:5900、5901:5900…

Docker Compose 方法:

  1. docker-compose up --build
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
# To execute this docker-compose yml file use docker-compose -f <file_name> up
# Add the "-d" flag at the end for deattached execution
version: '2'
services:
firefox:
image: selenium/node-firefox-debug:3.12.0-boron
volumes:
- /dev/shm:/dev/shm
depends_on:
- hub
environment:
HUB_HOST: hub
ports:
- "5900:5900"

chrome:
image: selenium/node-chrome-debug:3.12.0-boron
volumes:
- /dev/shm:/dev/shm
depends_on:
- hub
environment:
HUB_HOST: hub
ports:
- "5901:5900"

hub:
image: selenium/hub:3.12.0-boron
ports:
- "4450:4444"
  1. 訪問selenium頁面

這個頁面顯示了Selenium hub的狀態,如果想看到VNC的情況,需要進入Docker 容器才看的到。

用VNC 看瀏覽器在做什麼

  1. 安裝VNC軟體

推薦工具 免費VNC : ultravnc

ultravnc下載地址

  1. 打開UltraVNC Viewer

下載並安裝完成後,連進Docker內的VNC Server

  1. 點擊連接

  1. 輸入密碼

默認密碼 : secret

預設是有密碼的,若不希望輸入密碼可在容器變量中加入VNC_NO_PASSWORD: 1。

  1. 連接成功

看到這個畫面代表成功連進了VNC

Python 調用

  1. 創建一個腳本,測試selnium運行的情況。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
from selenium import webdriver
from selenium.webdriver.common.desired_capabilities import DesiredCapabilities
from selenium.webdriver.common.keys import Keys

browser = webdriver.Remote(
command_executor='http://192.168.8.22:4450/wd/hub',
desired_capabilities=DesiredCapabilities.CHROME,
)

browser.get("http://www.google.com")
browser.find_element_by_id("lst-ib").send_keys("python")
browser.find_element_by_name("btnK").send_keys(Keys.ENTER)
titles = browser.find_elements_by_xpath(".//h3[@class='r']//a")
print([i.text for i in titles])
browser.quit()
  1. 有畫面的selenium出現了

小結

docker selenium透過Docker建置的關係,構建非常方便快速,這邊主要介紹了兩種模式創建容器,一種是可以看見瀏覽器的,一種是默默執行的,測試運行速度也非常的快,感覺起來似乎比實體主機還要順上很多,但以長期的測試情況,就不知道會不會有其他問題了。

Docker 管理工具 (1) Rancher Server主機調度

發表於 2018-06-08 | 分類於 Docker |

主機調度用於當Rancher Server管理多台Rancher Agent時,若新增應用要在指定的Rancher Agent中創建就需要定義主機標籤。

  1. 設置主機標籤

name=ubuntuDocker

確認主機已設置好標籤

  1. 指定主機創建應用

使用方法:

1
2
3
labels:
io.rancher.scheduler.global: 'true'
io.rancher.scheduler.affinity:host_label: name=DockerHost

Example:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
version: '2'
services:
ipython2215:
image: ipython/notebook
environment:
PASSWORD: '123456'
USE_HTTP: '1'
stdin_open: true
tty: true
ports:
- 2215:8888/tcp
labels:
io.rancher.scheduler.affinity:host_label: name=DockerHost
io.rancher.scheduler.global: 'true'
  1. 構建完成

Zabbix2歷史紀錄遷移到Zabbix3

發表於 2018-06-08 | 分類於 Zabbix |

Zabbix的歷史數據都存於mysql中,而資料主要儲存於 history、history_uint、trends_uint、trends這4張表中,並且分
爲數值的歷史數據與圖形的歷史數據。

數值數據 :history、history_uint
圖形數據 :trends_uint、trends

嘗試直接使用資料庫表對表的方式倒回,結果是失敗的,原因在於Zabbix2跟3的資料庫設計不太相同,無法直接互拉資料,需要先查詢在針對項目ID在進行增刪改查。

以下示例操作history資料表,以查詢的方式找出item與host,zabbix2和3進行比對之後,最後再將Zabbix2的數值與zabbix的主機項目進行合併,進而取回先前zabbix2的所有資料,若要拉回所有資料還需要將其他三張表(history_uint、trends_uint、trends)導回才行。

查詢比對

zabbix2 舊表查詢

舊表 192.168.8.30 查出history中對應的item與host

1
2
3
4
5
6
7
8
9
10
11
12
‐‐ 查詢history的項目數
SELECT
history.itemid,
items.`name` ,
`hosts`.`host`
FROM
items
INNER JOIN `hosts` ON items.hostid = `hosts`.hostid
INNER JOIN history ON history.itemid = items.itemid
‐‐ 透過itemid去除重複
GROUP BY itemid
HAVING count(*)>1

查詢結果

表關聯

已經得到所有history對應出item,host的結果,這邊我只篩選出主機”FGT-100D”, “NEWPC”,”IPCam”,因爲只需要這些主機的資料。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
SELECT *
FROM (
SELECT
history.itemid,
items.`name` ,
`hosts`.`host`
FROM
items
INNER JOIN `hosts` ON items.hostid = `hosts`.hostid
INNER JOIN history ON history.itemid = items.itemid
GROUP BY itemid
HAVING count(*)>1 ) AS new
-- 篩選主機"FGT-100D", "NEWPC","IPCam"
WHERE new.`host` IN (
"FGT-100D",
"NEWPC",
"IPCam"
);

查詢結果 共67筆

192.168.8.30 中的itemID已經出來

zabbix3 新表查詢

換新表 192.168.8.28 zabbix3版本,操作方式也是相同的。

透過查詢會發現一樣的項目但itemID卻不一樣,所以直接倒回是沒有用的,所以需要進行ID匹配在倒回數據。

查詢對比

新舊表合併查詢

這邊我將剛剛查詢zabbix2跟3的結果拉出來,再次進行查詢找出同主機同項目對應的itemid。

1
2
3
4
5
6
7
8
9
10
11
-- history新舊表合併查詢
SELECT
zabbix3_history.itemid as zabbix3_itemid,
zabbix3_history.`name` as zabbix3_name,
zabbix3_history.`host` as zabbix3_host,
zabbix2_history.itemid as zabbix2_itemid,
zabbix2_history.`name` as zabbix2_name,
zabbix2_history.`host` as zabbix2_host
FROM
zabbix3_history
INNER JOIN zabbix2_history ON zabbix3_history.`name` = zabbix2_history.`name`

倒回資料

新建一張表 new_history

將”zabbix2”匹配的itemID搜索出來,新建一張表。

更新itemid

資料是要由zabbix2倒回zabbix3的,剛剛已經建立了一張zabbix2的表,現在同樣把zabbix3匹配的item找出來直接取代更換掉zabbix2的ID,這樣的方式目的在於只更換項目而數據是保留的。

使用試算表協助建立更新語法

在試算表中可以看到zabbix2跟3的對應,實際上就是把zabbix2的itemid取代成zabbix2的。

執行

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
UPDATE new_history SET new_history.itemid = 25541 WHERE new_history.itemid = 25089;
UPDATE new_history SET new_history.itemid = 25542 WHERE new_history.itemid = 25091;
UPDATE new_history SET new_history.itemid = 25554 WHERE new_history.itemid = 26710;
UPDATE new_history SET new_history.itemid = 25554 WHERE new_history.itemid = 26814;
UPDATE new_history SET new_history.itemid = 25555 WHERE new_history.itemid = 26711;
UPDATE new_history SET new_history.itemid = 25555 WHERE new_history.itemid = 26815;
UPDATE new_history SET new_history.itemid = 25557 WHERE new_history.itemid = 26710;
UPDATE new_history SET new_history.itemid = 25557 WHERE new_history.itemid = 26814;
UPDATE new_history SET new_history.itemid = 25558 WHERE new_history.itemid = 26711;
UPDATE new_history SET new_history.itemid = 25558 WHERE new_history.itemid = 26815;
UPDATE new_history SET new_history.itemid = 25648 WHERE new_history.itemid = 26819;
UPDATE new_history SET new_history.itemid = 25649 WHERE new_history.itemid = 26875;
UPDATE new_history SET new_history.itemid = 25650 WHERE new_history.itemid = 26823;
UPDATE new_history SET new_history.itemid = 25651 WHERE new_history.itemid = 26820;
UPDATE new_history SET new_history.itemid = 25652 WHERE new_history.itemid = 26821;
UPDATE new_history SET new_history.itemid = 25654 WHERE new_history.itemid = 26832;
UPDATE new_history SET new_history.itemid = 25656 WHERE new_history.itemid = 26833;
UPDATE new_history SET new_history.itemid = 25657 WHERE new_history.itemid = 26818;
...

取代對應ID

等了很久,總算更新完了…

匯出Sql

將new_history匯出 SQL檔

匯出後,打開Sql檔取代下列文字,若檔案較大建議使用支持大文件的編輯器(例如:emeditor)

1
2
3
INSERT INTO `new_history`
改爲
insert ignore into `history`

用insert是因為並不是要取代整個”history”表,只是要追加項目,而用insert ignore是要避免重複插入。

開始倒回數據

將取代完的sql,重新匯入。

倒回成功!!

IE11企業模式

發表於 2018-06-08 | 分類於 Windows |

當初想暸解企業模式,主要是因為IE11並不支持goole線上文件,必須使用開發者切換到IE10才可以正常使用,再後來了解到IE企業模式可以透過XML針對不同domain來調整默認IE版本開啟,但我電腦的版本是windows家用版,在登錄檔中根本找不到這個功能,最後是先在windows專業版上面設定好reg再放到家用版使用。

建立XML

讓IE知道要哪個domain要用哪個版本開啟,這邊是在C盤上建立,而要對應的版本在docMode後面輸入就好。

IE10 - docMode=”10”
IE11 - docMode=”edge”

C:\sites.xml

1
2
3
4
5
6
7
<rules version="2">
<docMode>
<domain docMode="10">google.com</domain>
<domain docMode="edge">www.baidu.com</domain>
</docMode>
<emie />
</rules>

建立登錄檔

正式開啟IE企業模式,根據剛剛建立的xml進行調整。

iemode.reg

1
2
3
4
Windows Registry Editor Version 5.00
[HKEY_LOCAL_MACHINE\SOFTWARE\Policies\Microsoft\Internet Explorer\Main\EnterpriseMode]
"Enable"=""
"SiteList"="C:\\\sites.xml"

對於愛用IE的用戶來說,這是一個非常好用的功能,到目前發現仍有不少網站無法完整支持IE11。

Zabbix學習筆記(6) 用Python製作簡易Zabbix微監控

發表於 2018-06-08 | 分類於 Zabbix |

概念說明

Zabbix前端報警及郵件發信等已可立即告警,但如果能整合到內部系統豈不甚好?

既然如此,後面將會以下圖說明由Redis Server做為中間接收的媒介,讓Zabbix與內部系統整合告警。

1. Zabbix Server

Zabbix告警動作可以選擇腳本,這點非常強大,只要設好監控項到動作告警,剩下都可以交由腳本做進一步的呈現。

觸發器

創建觸發器。

報警媒介

創建腳本RedisAlarm.py,當發報時會將觸發器的狀態寫入到Redis中。

名稱:RedisAlarm
類型:“腳本”
腳本名稱:RedisAlarm.py
(
路經:/usr/local/share/zabbix/alertscripts,
權限:777
)
腳本參數:
{ALERT.SENDTO} 收件人地址
{ALERT.SUBJECT} 主題
{ALERT.MESSAGE} 詳細內容

  • 創建腳本

RedisAlarm.py

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
#!/home/zabbix/anaconda2/bin/python
# -*- coding: utf-8 -*-

'''
# RedisAlarm.py
'''
import sys
import redis

if __name__ == '__main__':
# {ALERT.SENDTO}
rc = redis.StrictRedis(sys.argv[1].decode('utf8'))
# {ALERT.SUBJECT}
status = sys.argv[2].decode('utf8')
# {ALERT.MESSAGE}
name = sys.argv[3].decode('utf8')

if "OK" in status:
rc.set(name,status)
else:
rc.set(name,status)

用戶報警媒介”收件人”

設定收件人為RedisServerIP,設定完記得按更新。

動作

  • 選擇觸發器

    一個動作可包含多個觸發器。

  • 操作步驟

    默认接收人:{TRIGGER.STATUS} 觸發器狀態
    默认信息:{TRIGGER.NAME} 觸發器名稱

  • 恢复操作步驟

    默认接收人:{TRIGGER.STATUS} 觸發器狀態
    默认信息:{TRIGGER.NAME} 觸發器名稱

2. Redis Server

Redis Server是一個Key,Velue的數據庫,默認數據是存於內存的,擁有極快的存取速度,通常用於緩存資料使用。

建立初始資料

先確保Redis可否連接,在新增觸發器名稱作為Redis Key,這邊用一個簡單的腳本新增。

1
2
3
4
5
6
7
8
9
10
11
12
13
import redis
r = redis.StrictRedis("127.0.0.1")

# Check redis server
try:
r.ping()
except Exception as e:
print "RedisServer Error"

# Add redis keys
items = ["Google","Modem","Negix1","Negix2"]
[r.set(i,"OK") for i in items]
print r.keys()

先全部新增為”OK”,不然後面Get不到值會出錯。

3. Zabbix Alarm Client

利用Wxpython製作一個實時監測Zabbix監控項的小程序。

完成圖

wxpython

Docker 管理工具 (1) Rancher Server建置

發表於 2018-06-08 | 分類於 Docker |
  1. 啟動 Rancher Server
1
docker run -d --restart=always -p 8080:8080 rancher/server

  1. 訪問Rancher Server頁面

  1. 設定語言

  1. 添加 Rancher Agent

    如果沒有特別的域名,直接點擊保存即可

    將方框中的腳本,複製到Agent端執行

    例如 我的rencher Server及agent是同一臺主機,那麼直接貼上執行即可。

    執行完畢後,可以在主機列表中看到剛剛新增的主機。

  1. 自定義應用

測試在Rencher的第一個應用,這邊以Tomcat爲例。

tomcat:latest
添加應用

輸入”應用名稱:myapp”後點擊創建。

點擊”添加服務”

服務中”名稱”可以自定義,”選擇鏡像:tomcat:lastet”(Rencher將會自動拉取鏡像),端口映射部分由於Rencher已經定義了8080公開端口,因此新建的服務必須使用其他端口對應容器內的8080端口設定,這邊使用8081測試。

拉到頁面下方看到這些設定可以不用管他,直接點擊創建。

看到”Active”就代表正常啓動成功。

進入http:///8081網址,就可以看到Tomcat的頁面。

  1. 創建compose應用

建立一個新的應用包含”MongoDB”的服務。

docker-compose.yml

1
2
3
4
5
6
7
8
9
10
version: '2'

services:
mongo:
image: 'mongo:latest'
ports:
- '27017:27017'
volumes:
- '/home/docker/workspace/mongo/data:/data/db'
restart: always

Test TCP

1
nc -vz 10.211.55.12 27017

測試TCP端口27017可不可連

Test GUI

瀏覽器自動化(6) Selenium Server

發表於 2018-06-08 | 分類於 Selenium |

selenium-server可支持遠程操作,而cilent端要使用selenium,直接遠程調用selenium-server,支持多系統平台或多版本瀏覽器同時測試,因為是由clinet進行控制,selenium服務器也不用拉取腳本,非常方便管理。

基本環境

  1. java

必須先安裝java,選擇jre即可。

官網下載

  1. webdriver

下載webdriver加到環境變數,一定要加入否則會無法驅動。

ChromeDriver地址

  1. selenium-server-standalone

至官網下載最新版本selenium-server,需透過java啟動server。

官網下載

  1. python module selenium
1
pip install selenium

啟動

  • 啟動 selenium-server
1
java -jar selenium-server-standalone-3.12.0.jar

啟動 默認是4444端口

網頁訪問

進入console可以看到目前開啟了那些Sessions,點擊Take Screenshot會顯示當前畫面截圖。

  • client 腳本控制

server與client可以是同一台,也可以分開,由於是瀏覽器自動化,分開的話會更好調試,也比較不會有搶屏的問題。

官方文檔有說明驅動方式:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
Usage Example::

from selenium import webdriver

selenium_grid_url = "http://198.0.0.1:4444/wd/hub"

# Create a desired capabilities object as a starting point.
capabilities = DesiredCapabilities.FIREFOX.copy()
capabilities['platform'] = "WINDOWS"
capabilities['version'] = "10"

# Instantiate an instance of Remote WebDriver with the desired capabilities.
driver = webdriver.Remote(desired_capabilities=capabilities,
command_executor=selenium_grid_url)

Note: Always use '.copy()' on the DesiredCapabilities object to avoid the side
effects of altering the Global class instance.

remote_script1.py

1
2
3
4
5
6
7
8
9
10
11
12
from selenium.webdriver.common.desired_capabilities import DesiredCapabilities
from selenium import webdriver


browser = webdriver.Remote(
command_executor='http://127.0.0.1:4444/wd/hub',
desired_capabilities=DesiredCapabilities.CHROME,
)

browser.get("http://www.google.com")

browser.quit()

DesiredCapabilities為瀏覽器的一些自定義,以下的說明文檔查看更多的配置。

說明文檔

1
2
DesiredCapabilities.CHROME # 默認的設置,返回的是字典類型
>>> {'browserName': 'chrome', 'version': '', 'platform': 'ANY'}

Jenkins學習(6) 遠程構建

發表於 2018-06-08 | 分類於 Jenkins |

Jenkinsm遠程構建可不透過web介面,直接使用request請求構建項目,於整合項目或直接調試時非常方便。

Token

取得User API Token

在user中找到當前用戶的API Token,如:af6d231944260d58ab86f2c8d587b34e

創建項目Token

在項目中勾選触发远程构建,在輸入身份验证令牌字段,如:Maliao

遠程構建

Curl build

說明

1
curl -X POST http://<user>:<User API Token>@<project url>/build?token=<project token>
  • 執行構建
1
curl -X POST http://jenkins:af6d231944260d58ab86f2c8d587b34e@192.168.8.22:8085/jenkins/job/ptt_1/build?token=Maliao
  • 构建队列

此時,Jenkins會將項目加到對列並完成構建

123…5

Maliao Guo

41 文章
8 分類
18 標籤
GitHub
© 2018 Maliao Guo
由 Hexo 強力驅動
|
主題 — NexT.Pisces v5.1.4